def search(original, rotated): most_sim = { 'sp': [0, 0], # format: [(most_sim, (pixel))] 'mm': [0, 0], 'alt': [0, 0] } nWedges, nRings = rotated.size() for i in xrange(nWedges): sp = sim.sumProd(rotated.pix, original.pix) mm = sim.maxMin(rotated.pix, original.pix) a = sim.alt(rotated.pix, original.pix) print('%d\tsp: %f\tmm: %f\ta: %f' % (i, sp, mm, a)) if sp > most_sim['sp'][0]: most_sim['sp'] = [sp, i] if mm > most_sim['mm'][0]: most_sim['mm'] = [mm, i] if a > most_sim['alt'][0]: most_sim['alt'] = [a, i] rotated.rotate() return most_sim
def rotational_search(rot_ret, goal_ret): most_sim = (0, 0) # format: (most_sim, wedge_index) for wedge_index in range(rot_ret.template.nWedges): mm = sim.maxMin(rot_ret.retina, goal_ret.retina) if mm > most_sim[0]: most_sim = (mm, wedge_index) rot_ret.rotate() return most_sim
def rotational_search(rot_ret, goal_ret): most_sim = [0, 0] # format: [most_sim, wedge_index] for wedge_index in xrange(template.nWedges): mm = sim.maxMin(rot_ret.retina, goal_ret.retina) if mm > most_sim[0]: most_sim = [mm, wedge_index] rot_ret.rotate() return most_sim
def secondarySearch(choiceSpace, s, primary_loc, primary_wedge_index): min_angle = (primary_wedge_index - 0.5) / s.template.nWedges * 2 * pi max_angle = (primary_wedge_index + 0.5) / s.template.nWedges * 2 * pi delta_angle = 2 * pi * s.sp_dist / ( 2 * s.template.nWedges ) # delta_angle is small enough to ensure all the closest pixels to the circumference are visited angle = min_angle choiceSpace_locs = [] while angle < max_angle: pixel = closestPixel(primary_loc + rotate(s.sp_difference, angle)) if pixel not in choiceSpace_locs and pixel[0] >= 0 and pixel[1] >= 0: choiceSpace_locs.append(pixel) angle += delta_angle density_deltas = [] for cs_loc in choiceSpace_locs: sx, sy = cs_loc subSpace = choiceSpace[sx:sx + s.size, sy:sy + s.size] x, y = subSpace.shape if x == s.size and y == s.size: density_deltas.append( (s.secondary.ret.ringwiseDensityDelta(subSpace), cs_loc)) if len(density_deltas) == 0: return (0, (0, 0)) density_deltas.sort() cutOff = min(secondary_queue_size, len(choiceSpace_locs)) choiceSpace_locs = [tup[1] for tup in density_deltas[:cutOff]] target_ret = s.secondary.ret.copy() target_ret.rotate(primary_wedge_index) most_sim = (0, (0, 0)) for loc in choiceSpace_locs: sx, sy = loc # creates subspace specified by loc and transforms it to retina-form goal_ret = s.template.createRetina(choiceSpace[sx:sx + s.size, sy:sy + s.size]) mm = sim.maxMin(target_ret.retina, goal_ret.retina) if mm > most_sim[0]: most_sim = (mm, loc) return most_sim
def sliceSearch(choiceSpace, s, sub_loc, sub_wedge_index): min_angle = (sub_wedge_index - 0.5) / s.subtemplate.nWedges * tao max_angle = (sub_wedge_index + 0.5) / s.subtemplate.nWedges * tao radians_per_slice_wedge = tao / s.template.nWedges min_slice_wedge = int(math.floor(min_angle / radians_per_slice_wedge)) max_slice_wedge = int(math.ceil(max_angle / radians_per_slice_wedge)) # print min_slice_wedge, max_slice_wedge most_sim = (0, 0, (0, 0)) target_ret = s.ret.copy() target_ret.rotate(min_slice_wedge) for wedge in range(min_slice_wedge, max_slice_wedge + 1): angle = float(wedge) / s.template.nWedges * tao # gets vector to sublice center in easel slot's coordinates vectorToSlice = sub_loc + (s.subslice_center - s.subslice.start) # gets vector to slice center in easel slot's coordinates vectorToSlice += rotate(s.center_delta, angle) # gets vector to slice upper right corner in easel slot's coordinates vectorToSlice -= s.slice_center sx, sy = closestPixel(vectorToSlice) # print sx, sy, wedge, angle*360 / tao # creates subspace and transforms it to retina-form subspace = choiceSpace[sx:sx + s.size, sy:sy + s.size] if subspace.shape == (s.size, s.size): goal_ret = s.template.createRetina(subspace) # goal_ret.visualize('test_slot_%d.jpg' % wedge) mm = sim.maxMin(target_ret.retina, goal_ret.retina) if mm > most_sim[0]: most_sim = (mm, wedge, (sx, sy)) # ad = sim.absDiff(target_ret.retina,goal_ret.retina,relevant=True) # print ad # if ad > most_sim[0]: # most_sim = (ad,wedge,(sx,sy)) target_ret.rotate() return most_sim
def tertiarySearch(choiceSpace, s, secondary_loc, choice_sp_angle): rotation = choice_sp_angle - s.sp_angle centerPixel = closestPixel(secondary_loc + rotate(s.ts_difference, rotation)) cx, cy = centerPixel target_ret = s.tertiary.ret.copy() most_sim = (0, (0, 0)) for x in range(cx - 1, cx + 2): for y in range(cy - 1, cy + 2): subSpace = choiceSpace[x:x + s.size, y:y + s.size] sx, sy = subSpace.shape if sx == s.size and sy == s.size: # creates subspace specified by loc and transforms it to retina-form goal_ret = s.template.createRetina(subSpace) mm = sim.maxMin(target_ret.retina, goal_ret.retina) if mm > most_sim[0]: most_sim = (mm, (x, y)) return most_sim
def secondarySearch(choiceSpace, s, primary_loc, primary_wedge_index): min_angle = (primary_wedge_index - 0.5) / s.template.nWedges * tao max_angle = (primary_wedge_index + 0.5) / s.template.nWedges * tao secondary_locs = set() # print primary_loc # print primary_wedge_index # print min_angle # print max_angle # print('') # print s.sp_angle # print s.sp_dist min_pixel = closestPixel(primary_loc + rotate(s.sp_difference, min_angle)) # print min_pixel # adds some "wiggle room"; equals angle of sector with arc length = half the diagonal of one pixel (i.e. 0.5*sqrt_2) min_relative = (min_angle + s.sp_angle) % (tao) - 0.5 * sqrt_2 / s.sp_dist # print min_relative max_relative = (max_angle + s.sp_angle) % (tao) + 0.5 * sqrt_2 / s.sp_dist # print max_relative # print('') q = deque() q.append(min_pixel) visited = [] while len(q) > 0: s_loc = q.popleft() visited.append(s_loc) s_loc = np.array(s_loc) # print s_loc # pixel's center is within half the diagonal of one pixel (i.e. 0.5*sqrt_2) of arc isRadiallyClose = abs(r.dist(primary_loc, s_loc) - s.sp_dist) < 0.5 * sqrt_2 # print isRadiallyClose angle = r.angle(primary_loc, s_loc) % tao isAngularlyClose = min_relative < angle < max_relative # print angle # print isAngularlyClose if isRadiallyClose and isAngularlyClose: # print('trying to add %s' % str(tuple(s_loc))) secondary_locs.add(tuple(s_loc)) # print secondary_locs for move in [[1, 0], [0, 1], [-1, 0], [0, -1]]: new_loc = tuple(s_loc + move) if new_loc not in q and new_loc not in visited: q.append(new_loc) ''' user_input = raw_input('enter...') if user_input == 'q': exit() ''' secondary_locs = [ loc for loc in secondary_locs if isValid(loc, choiceSpace.shape) ] density_deltas = [] for s_loc in secondary_locs: sx, sy = s_loc subSpace = choiceSpace[sx:sx + s.size, sy:sy + s.size] x, y = subSpace.shape if x == s.size and y == s.size: density_deltas.append( (s.secondary.ret.ringwiseDensityDelta(subSpace), s_loc)) if len(density_deltas) == 0: return (0, (0, 0)) density_deltas.sort() cutOff = min(secondary_queue_size, len(secondary_locs)) secondary_locs = [tup[1] for tup in density_deltas[:cutOff]] target_ret = s.secondary.ret.copy() target_ret.rotate(primary_wedge_index) most_sim = (0, (0, 0)) for loc in secondary_locs: sx, sy = loc # creates subspace specified by loc and transforms it to retina-form goal_ret = s.template.createRetina(choiceSpace[sx:sx + s.size, sy:sy + s.size]) mm = sim.maxMin(target_ret.retina, goal_ret.retina) if mm > most_sim[0]: most_sim = (mm, loc) return most_sim
def search(searchSpace, target, locations, queueSize): most_sim = { 'sp': [], # format: [(most_sim, (pixel))] 'mm': [], 'alt': [] } tx, ty = target.size() subSpace = np.empty((tx, ty)) ''' ssx, ssy = searchSpace.size() spArr = np.empty((ssx-tx+1,ssy-ty+1)) altArr = np.empty((ssx-tx+1,ssy-ty+1)) ''' nIterations = 0 for box in locations: nIterations += (box[2] - box[0]) * (box[3] - box[1]) print nIterations counter = 0 for box in locations: for sx in xrange(box[0], box[2]): for sy in xrange(box[1], box[3]): subSpace = searchSpace.pix[sx:sx + tx, sy:sy + ty] sp = sim.sumProd(target.pix, subSpace) #sp = 0 mm = sim.maxMin(target.pix, subSpace) #mm = 0 a = sim.alt(target.pix, subSpace) ''' if queueSize == 1: print('\n%d:\tsp %f.3, mm %f.3, alt %f.3 at %s' % (counter,sp,mm,a,str((sx,sy)))) print('before:') if len(most_sim['sp']) == 0: print('sp:\nmm:\nalt:') else: for key in most_sim.keys(): print('%s:\t%s' % (key,str(most_sim[key]))) if not queueSize == 1: spArr[sx,sy] = sp altArr[sx,sy] = a ''' if counter < queueSize: hq.heappush(most_sim['sp'], (sp, (sx, sy))) hq.heappush(most_sim['mm'], (mm, (sx, sy))) hq.heappush(most_sim['alt'], (a, (sx, sy))) else: hq.heappushpop(most_sim['sp'], (sp, (sx, sy))) hq.heappushpop(most_sim['mm'], (mm, (sx, sy))) hq.heappushpop(most_sim['alt'], (a, (sx, sy))) ''' if queueSize == 1: print('\nafter:') for key in most_sim.keys(): #print('%s:\t%f at %s' % (key,round(most_sim[key][0],3),str(most_sim[key][1]))) print('%s:\t%s' % (key,str(most_sim[key]))) ''' if counter % 10000 == 0: print( str(round(100 * (float(counter) / nIterations), 2)) + r'% done') #print(str(counter) + ': ' + str(most_sim['sp'])) counter += 1 for key in target.most_sim.keys(): #print(most_sim[key]) target.most_sim[key] = [tup[1] for tup in most_sim[key]] ''' if not queueSize == 1: debug(spArr,'sp'+target.fname.split('.')[0]) debug(altArr,'alt'+target.fname.split('.')[0]) ''' return most_sim if queueSize == 1 else None