Search code examples
pythonperformanceminimization

efficiency of error function - minimization problem


I wrote a function to calculate an error per pixel, based on coordinates and color of an image (np-array).

basically it looks like this:

def checkPoints(i, j, x_left, y_top):
  errormap = {}
    for row in possibleMap[i][j]:
      for target in row:
        x,y = target
        dE = np.sqrt((x-i)**2 + (y-j)**2)*dE_fac #distance
        sE = ((skalarError((i>0)*(x - x_mapping_left)) + skalarError((j>0)*(y - y_mapping_top)))*sE_fac)**sE_exp #direction with respect to neighbouring pixels
        cE = abs(color - pic_tomap[x,y])*cE_fac # color difference
        errormap[dE + sE + cE + mE] = [x,y] # a dictionary... keys: ErrorValue, values: coordinates
  return errormap

possibleMap is an npArray, which contains tuples of coordinates
*_fac, *_exp are just constants
x_mapping_left is i-1 except when i=0, then it's 0 (y_mapping_left accordingly with j)

Now I am wondering a few things, because I want the maximum efficiency I can get.

  1. can I use the map-function to go through possibleMap, or does the map-function only work for 1dimensional loops?

  2. As I don't need errormap itself, but just the least ErrorValue with the associated coordinates, is there a better performing way than above code together with:

errormap[min(errormap.keys())]
  1. I am kindly asking for any ideas to get a better performance

Solution

  • I recently found out, that this bit:

    sE = ((skalarError((i>0)*(x - x_mapping_left)) + skalarError((j>0)*(y - y_mapping_top)))*sE_fac)**sE_exp
    

    is devastating in times of performance, because these:

    (i>0)
    (j>0)
    

    are consuming a lot of time.. I don't know why, but when I exchanged them with if then else statements, it was nearly 2 times quicker (: