Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _get_lens_max(self, start_mic, axis=0):
cur_mic = start_mic
lens_max = 1 # start with 1 to account for upper left centroid
odd = self.estimate_odd(start_mic, axis)
# check if column of upper left is complete
while cur_mic[axis] < self._lower_r[axis] + self._pitch[axis]/2:
# get adjacent MIC
found_center = find_centroid(self._centroids, cur_mic, self._pitch, axis, self._pattern, odd)
odd = not odd
if len(found_center) != 2:
if len(found_center) > 2:
# average of found centroids
found_center = np.mean(found_center.reshape(-1, 2), axis=0)
else:
if cur_mic[axis] > (self._bbox[axis] - self._pitch[axis]/2):
break
else:
odd = self.estimate_odd(start_mic, axis)
start_mic = find_centroid(self._centroids, start_mic, self._pitch, not axis, self._pattern, not odd)
found_center = start_mic
lens_max = 0
lens_max += 1
cur_mic = found_center
def estimate_odd(self, cur_mic, axis):
return find_centroid(self._centroids, cur_mic, self._pitch, axis, 'hex', odd=False).size == 0
def _calc_pattern(self, pitch):
""" This function determines whether the geometric arrangement of micro lenses is rectangular or hexagonal.
:param pitch: scalar of type int or float representing the spacing between micro image centers
:return: pattern, e.g. 'hex' or 'rec'
"""
# pick arbitrary micro image center in middle of centroid list
point = self._centroids[int(len(self._centroids)/2)]
diff_vertical = []
pattern_list = ['rec', 'hex']
for pattern in pattern_list:
k = 1 if pattern == 'rec' else np.sqrt(3) / 2
adj_vertical = find_centroid(self._centroids, point, [k*pitch, pitch], axis=0, pattern=pattern)
if list(adj_vertical):
diff_vertical.append(abs(k*pitch - abs(adj_vertical[0] - point[0])))
else:
diff_vertical.append(float('inf'))
# store detected pattern type
self._pattern = pattern_list[np.array(diff_vertical).argmin()]
return self._pattern
last_neighbor = self._upper_l
self._mic_list = []
self._mic_list.append([last_neighbor[0], last_neighbor[1], 0, 0])
j = 0
odd = self.estimate_odd(self._upper_l, axis=0)
row_len_odd = True
# print status
self.sta.status_msg('Sort micro image centers', self.cfg.params[self.cfg.opt_prnt])
# jump to the next row
for ly in range(self._lens_y_max):
# find all centers in one row
for lx in range(self._lens_x_max-1): # -1 to account for first row center which is already found
# get adjacent MIC
found_center = find_centroid(self._centroids, last_neighbor, self._pitch, 1, 'rec', None)
# retrieve single MIC
if len(found_center) != 2:
if len(found_center) > 2:
# average of found centroids
found_center = np.mean(found_center.reshape(-1, 2), axis=0)
else:
# skip when looking for last MIC with unequal row length
if lx == self._lens_x_max-1 and (row_len_odd or self._pattern == 'rec'):
break
# create missing centroid
found_center = self._mic_list[j][:2]+np.array([0, self._pitch[1]])
j += 1
self._mic_list.append([found_center[0], found_center[1], ly, lx+1])
last_neighbor = found_center
# find most-left center of next row
if ly < self._lens_y_max-1:
if len(found_center) > 2:
# average of found centroids
found_center = np.mean(found_center.reshape(-1, 2), axis=0)
else:
# skip when looking for last MIC with unequal row length
if lx == self._lens_x_max-1 and (row_len_odd or self._pattern == 'rec'):
break
# create missing centroid
found_center = self._mic_list[j][:2]+np.array([0, self._pitch[1]])
j += 1
self._mic_list.append([found_center[0], found_center[1], ly, lx+1])
last_neighbor = found_center
# find most-left center of next row
if ly < self._lens_y_max-1:
# get adjacent MIC
found_center = find_centroid(self._centroids, self._upper_l, self._pitch, 0, self._pattern, odd)
# retrieve single MIC
if len(found_center) != 2:
if len(found_center) > 2:
# average of found centroids
found_center = np.mean(found_center.reshape(-1, 2), axis=0)
else:
# create missing centroid (considering MLA packing)
if self._pattern == 'rec':
found_center = self._upper_l + np.array([self._pitch[0], 0])
elif self._pattern == 'hex':
if odd:
found_center = self._upper_l + np.array([self._pitch[0], +self._pitch[1]/2])
else:
found_center = self._upper_l + np.array([self._pitch[0], -self._pitch[1]/2])
j += 1
self._mic_list.append([found_center[0], found_center[1], ly+1, 0])
# check if column of upper left is complete
while cur_mic[axis] < self._lower_r[axis] + self._pitch[axis]/2:
# get adjacent MIC
found_center = find_centroid(self._centroids, cur_mic, self._pitch, axis, self._pattern, odd)
odd = not odd
if len(found_center) != 2:
if len(found_center) > 2:
# average of found centroids
found_center = np.mean(found_center.reshape(-1, 2), axis=0)
else:
if cur_mic[axis] > (self._bbox[axis] - self._pitch[axis]/2):
break
else:
odd = self.estimate_odd(start_mic, axis)
start_mic = find_centroid(self._centroids, start_mic, self._pitch, not axis, self._pattern, not odd)
found_center = start_mic
lens_max = 0
lens_max += 1
cur_mic = found_center
return lens_max, cur_mic, start_mic