def init_structures(self, y_shape): self.orig_shape = (y_shape[0], y_shape[1]) if self.max_displ is None: msg = 'Need to call set_max_displ() before update().' raise Exception(msg) self.info('max_displ: %s' % self.max_displ) search_grid = (self.g, self.g) self.phases = get_phase_sequence(self.orig_shape, self.desired_resolution_factor, search_grid=search_grid, gamma=self.gamma, max_displ=self.max_displ, min_shape=self.min_shape) for p in self.phases: self.info('- %s' % p) self.estimators = [] self.current_phase = 0 phase = self.phases[self.current_phase] estimator = self._get_phase_estimator(phase.max_displ) estimator.set_guess(diffeo_identity(phase.shape)) self.estimators.append(estimator) self.phase_obs = 0 self.info('now using phase: %s' % phase) self.last_tmp_guess = None self.reached_end = False
def consistency_based_uncertainty2(d1, d2, normalize_distance=True): """ Returns the assigned uncertainty field for the two diffeomorphisms. """ d12 = diffeo_compose(d1, d2) d21 = diffeo_compose(d2, d1) shape = d1.shape[:2] # compute the distance of each point to the identity identity = diffeo_identity(shape) e2 = diffeo_local_differences_L2(d12, identity) e1 = diffeo_local_differences_L2(d21, identity) if normalize_distance: d1n = diffeo_local_differences_L2(d1, identity) d2n = diffeo_local_differences_L2(d2, identity) d1n[d1n == 0] = 1 d2n[d2n == 0] = 1 e2 = e2 / d2n e1 = e1 / d1n # normalize fields in [0, 1] def normalize(e): m = np.max(e) if m > 0: e = e / m v = 1 - e return v v1 = normalize(e1) v2 = normalize(e2) return v1, v2
def get_value(self): ''' Find maximum likelihood estimate for diffeomorphism looking at each pixel singularly. Returns a Diffeomorphism2D. ''' if not self.initialized(): msg = 'Cannot summarize() because not initialized yet.' raise Diffeo2dEstimatorInterface.NotReady(msg) certainty = np.zeros(self.shape, dtype='float32') certainty.fill(np.nan) dd = diffeo_identity(self.shape) dd[:] = -1 for i in range(self.nsensels): if self.inference_method == DiffeomorphismEstimatorFaster.Order: eord_score = self.neig_eord_score[i, :] best = np.argmin(eord_score) if self.inference_method == DiffeomorphismEstimatorFaster.Similarity: esim_score = self.neig_esim_score[i, :] best = np.argmin(esim_score) jc = self.flat_structure.neighbor_cell(i, best) ic = self.flat_structure.flattening.index2cell[i] if self.inference_method == DiffeomorphismEstimatorFaster.Order: certain = -np.min(eord_score) / np.mean(eord_score) if self.inference_method == DiffeomorphismEstimatorFaster.Similarity: first = np.sort(esim_score)[:10] certain = -(first[0] - np.mean(first[1:])) # certain = -np.min(esim_score) / np.mean(esim_score) # certain = np.min(esim_score) / self.num_samples # certain = -np.mean(esim_score) / np.min(esim_score) dd[ic[0], ic[1], 0] = jc[0] dd[ic[0], ic[1], 1] = jc[1] certainty[ic[0], ic[1]] = certain certainty = certainty - certainty.min() vmax = certainty.max() if vmax > 0: certainty *= (1.0 / vmax) return Diffeomorphism2D(dd, certainty)
def __init__(self, shape, neighborarea, centers=None, topology='plane'): ''' :param shape: :param neighborarea: :param centers: if not None, area around centers Suppose shape = (H, W) nighborarea = (X, Y) A = X * Y N = H * W Then self.neighbor_indices_flat is a K x N array. ''' neighborarea = np.array(neighborarea) if not np.all(neighborarea <= shape): neighborarea = np.minimum(neighborarea, shape) assert neighborarea[0] <= shape[0] assert neighborarea[1] <= shape[1] # for each sensel, create an area cmg = cmap(np.array(neighborarea), max_shape=shape) # if the area size is even, it is bumbed to the next integer assert cmg.shape[0] >= neighborarea[0] assert cmg.shape[1] >= neighborarea[1] assert cmg.shape[0] <= neighborarea[0] + 1 assert cmg.shape[1] <= neighborarea[1] + 1 # but we cannot be bigger than the area anyway assert cmg.shape[0] <= shape[0] assert cmg.shape[1] <= shape[1] self.area_shape = cmg.shape[0], cmg.shape[1] self.shape = shape self.H, self.W = shape self.X = self.area_shape[0] self.Y = self.area_shape[1] self.N = self.H * self.W self.A = self.X * self.Y self.topology = topology check_is_in(topology, self.topology, ['torus', 'plane']) # this is the important state self.neighbor_indices_flat = np.zeros((self.N, self.A), 'int32') # logger.info('Creating Flattening..') self.flattening = Flattening.by_rows(tuple(shape)) # logger.info('..done') if centers is None: self.centers = diffeo_identity(shape) else: if centers.dtype == 'float': # continuous diffeo centers = np.round(centers).astype('int') self.centers = centers for coord in coords_iterate(shape): k = self.flattening.cell2index[coord] cm = cmg.copy() center = self.centers[coord] cm[:, :, 0] += center[0] cm[:, :, 1] += center[1] # torus topology here if self.topology == 'torus': cm[:, :, 0] = cm[:, :, 0] % shape[0] cm[:, :, 1] = cm[:, :, 1] % shape[1] elif self.topology == 'plane': for i in range(2): cmin = cm[:, :, i].min() if cmin < 0: cm[:, :, i] -= cmin assert cm[:, :, i].min() >= 0 cmax = cm[:, :, i].max() if cmax >= shape[i]: delta = -(cmax - (shape[i] - 1)) cm[:, :, i] += delta assert cm[:, :, i].max() < shape[i] assert cm[:, :, i].min() >= 0 else: assert False for i in range(2): cmin, cmax = cm[:, :, i].min(), cm[:, :, i].max() assert cmin >= 0 assert cmax < shape[i] indices = np.zeros(self.area_shape, 'int32') for a, b in coords_iterate(self.area_shape): c = tuple(cm[a, b, :]) indices[a, b] = self.flattening.cell2index[c] # XXX using numpy's flattening indices = np.array(indices.flat) # warnings.warn('remove bias due to ordering') # indices = np.random.permutation(indices) self.neighbor_indices_flat[k, :] = indices
def __init__(self, shape, neighborarea, centers=None, topology="plane"): """ :param shape: :param neighborarea: :param centers: if not None, area around centers Suppose shape = (H, W) nighborarea = (X, Y) A = X * Y N = H * W Then self.neighbor_indices_flat is a K x N array. """ neighborarea = np.array(neighborarea) if not np.all(neighborarea <= shape): neighborarea = np.minimum(neighborarea, shape) assert neighborarea[0] <= shape[0] assert neighborarea[1] <= shape[1] # for each sensel, create an area cmg = cmap(np.array(neighborarea), max_shape=shape) # if the area size is even, it is bumbed to the next integer assert cmg.shape[0] >= neighborarea[0] assert cmg.shape[1] >= neighborarea[1] assert cmg.shape[0] <= neighborarea[0] + 1 assert cmg.shape[1] <= neighborarea[1] + 1 # but we cannot be bigger than the area anyway assert cmg.shape[0] <= shape[0] assert cmg.shape[1] <= shape[1] self.area_shape = cmg.shape[0], cmg.shape[1] self.shape = shape self.H, self.W = shape self.X = self.area_shape[0] self.Y = self.area_shape[1] self.N = self.H * self.W self.A = self.X * self.Y self.topology = topology check_is_in(topology, self.topology, ["torus", "plane"]) # this is the important state self.neighbor_indices_flat = np.zeros((self.N, self.A), "int32") # logger.info('Creating Flattening..') self.flattening = Flattening.by_rows(tuple(shape)) # logger.info('..done') if centers is None: self.centers = diffeo_identity(shape) else: if centers.dtype == "float": # continuous diffeo centers = np.round(centers).astype("int") self.centers = centers for coord in coords_iterate(shape): k = self.flattening.cell2index[coord] cm = cmg.copy() center = self.centers[coord] cm[:, :, 0] += center[0] cm[:, :, 1] += center[1] # torus topology here if self.topology == "torus": cm[:, :, 0] = cm[:, :, 0] % shape[0] cm[:, :, 1] = cm[:, :, 1] % shape[1] elif self.topology == "plane": for i in range(2): cmin = cm[:, :, i].min() if cmin < 0: cm[:, :, i] -= cmin assert cm[:, :, i].min() >= 0 cmax = cm[:, :, i].max() if cmax >= shape[i]: delta = -(cmax - (shape[i] - 1)) cm[:, :, i] += delta assert cm[:, :, i].max() < shape[i] assert cm[:, :, i].min() >= 0 else: assert False for i in range(2): cmin, cmax = cm[:, :, i].min(), cm[:, :, i].max() assert cmin >= 0 assert cmax < shape[i] indices = np.zeros(self.area_shape, "int32") for a, b in coords_iterate(self.area_shape): c = tuple(cm[a, b, :]) indices[a, b] = self.flattening.cell2index[c] # XXX using numpy's flattening indices = np.array(indices.flat) # warnings.warn('remove bias due to ordering') # indices = np.random.permutation(indices) self.neighbor_indices_flat[k, :] = indices
def identity(shape): """ Returns an identity diffeomorphism of the given shape. """ from diffeo2d import diffeo_identity return Diffeomorphism2D(diffeo_identity(shape))