def cmap_test(): a = cmap(np.array([5, 5])) # print a[:, :, 0] # print a[:, :, 1] assert a.shape == (5, 5, 2) assert_allclose(a[2, 2, 0], 0) assert_allclose(a[2, 2, 1], 0)
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 cmap_test2(): a = cmap(np.array([4, 4])) assert a.shape == (5, 5, 2)