def test_product(): cm1 = Affine.from_params('i', 'x', np.diag([2, 1])) cm2 = Affine.from_params('j', 'y', np.diag([3, 1])) cm = product(cm1, cm2) yield assert_equal, cm.input_coords.coord_names, ('i', 'j') yield assert_equal, cm.output_coords.coord_names, ('x', 'y') yield assert_equal, cm.affine, np.diag([2, 3, 1])
def test_product(): affine1 = AffineTransform.from_params("i", "x", np.diag([2, 1])) affine2 = AffineTransform.from_params("j", "y", np.diag([3, 1])) affine = product(affine1, affine2) cm1 = CoordinateMap(CoordinateSystem("i"), CoordinateSystem("x"), np.log) cm2 = CoordinateMap(CoordinateSystem("j"), CoordinateSystem("y"), np.log) cm = product(cm1, cm2) yield assert_equal, affine.function_domain.coord_names, ("i", "j") yield assert_equal, affine.function_range.coord_names, ("x", "y") yield assert_almost_equal, cm([3, 4]), np.log([3, 4]) yield assert_almost_equal, cm.function([[3, 4], [5, 6]]), np.log([[3, 4], [5, 6]]) yield assert_equal, affine.function_domain.coord_names, ("i", "j") yield assert_equal, affine.function_range.coord_names, ("x", "y") yield assert_equal, affine.affine, np.diag([2, 3, 1])
def smooth(self, inimage, clean=False, is_fft=False): """ Apply smoothing to `inimage` Parameters ---------- inimage : ``Image`` The image to be smoothed. Should be 3D. clean : bool, optional Should we call ``nan_to_num`` on the data before smoothing? is_fft : bool, optional Has the data already been fft'd? Returns ------- s_image : `Image` New image, with smoothing applied """ if inimage.ndim == 4: # we need to generalize which axis to iterate over. By # default it should probably be the last. raise NotImplementedError('Smoothing volumes in a 4D series ' 'is broken, pending a rethink') _out = np.zeros(inimage.shape) # iterate over the first (0) axis - this is confusing - see # above nslice = inimage.shape[0] elif inimage.ndim == 3: nslice = 1 else: raise NotImplementedError('expecting either 3 or 4-d image') in_data = inimage.get_data() for _slice in range(nslice): if in_data.ndim == 4: data = in_data[_slice] elif in_data.ndim == 3: data = in_data[:] if clean: data = np.nan_to_num(data) if not is_fft: data = self._presmooth(data) data *= self.fkernel data = fft.irfftn(data) / self.norms[self.normalization] gc.collect() _dslice = [slice(0, self.bshape[i], 1) for i in range(3)] if self.scale != 1: data = self.scale * data[_dslice] if self.location != 0.0: data += self.location gc.collect() # Write out data if in_data.ndim == 4: _out[_slice] = data else: _out = data _slice += 1 gc.collect() _out = _out[[ slice(self._kernel.shape[i] / 2, self.bshape[i] + self._kernel.shape[i] / 2) for i in range(len(self.bshape)) ]] if inimage.ndim == 3: return Image(_out, coordmap=self.coordmap) else: # This does not work as written. See above concat_affine = AffineTransform.identity('concat') return Image(_out, coordmap=product(self.coordmap, concat_affine))
def smooth(self, inimage, clean=False, is_fft=False): """ Apply smoothing to `inimage` Parameters ---------- inimage : ``Image`` The image to be smoothed. Should be 3D. clean : bool, optional Should we call ``nan_to_num`` on the data before smoothing? is_fft : bool, optional Has the data already been fft'd? Returns ------- s_image : `Image` New image, with smoothing applied """ if inimage.ndim == 4: # we need to generalize which axis to iterate over. By # default it should probably be the last. raise NotImplementedError('Smoothing volumes in a 4D series ' 'is broken, pending a rethink') _out = np.zeros(inimage.shape) # iterate over the first (0) axis - this is confusing - see # above nslice = inimage.shape[0] elif inimage.ndim == 3: nslice = 1 else: raise NotImplementedError('expecting either 3 or 4-d image') in_data = inimage.get_data() for _slice in range(nslice): if in_data.ndim == 4: data = in_data[_slice] elif in_data.ndim == 3: data = in_data[:] if clean: data = np.nan_to_num(data) if not is_fft: data = self._presmooth(data) data *= self.fkernel data = fft.irfftn(data) / self.norms[self.normalization] gc.collect() _dslice = [slice(0, self.bshape[i], 1) for i in range(3)] if self.scale != 1: data = self.scale * data[_dslice] if self.location != 0.0: data += self.location gc.collect() # Write out data if in_data.ndim == 4: _out[_slice] = data else: _out = data _slice += 1 gc.collect() _out = _out[[slice(self._kernel.shape[i]/2, self.bshape[i] + self._kernel.shape[i]/2) for i in range(len(self.bshape))]] if inimage.ndim == 3: return Image(_out, coordmap=self.coordmap) else: # This does not work as written. See above concat_affine = AffineTransform.identity('concat') return Image(_out, coordmap=product(self.coordmap, concat_affine))
def smooth(self, inimage, clean=False, is_fft=False): """ :Parameters: inimage : `core.api.Image` The image to be smoothed clean : ``bool`` Should we call ``nan_to_num`` on the data before smoothing? is_fft : ``bool`` Has the data already been fft'd? :Returns: `Image` """ if inimage.ndim == 4: _out = np.zeros(inimage.shape) nslice = inimage.shape[0] elif inimage.ndim == 3: nslice = 1 else: raise NotImplementedError, 'expecting either 3 or 4-d image.' for _slice in range(nslice): if inimage.ndim == 4: data = inimage[_slice] elif inimage.ndim == 3: data = inimage[:] if clean: data = np.nan_to_num(data) if not is_fft: data = self._presmooth(data) data *= self.fkernel else: data *= self.fkernel data = fft.irfftn(data) / self.norms[self.normalization] gc.collect() _dslice = [slice(0, self.bshape[i], 1) for i in range(3)] if self.scale != 1: data = self.scale * data[_dslice] if self.location != 0.0: data += self.location gc.collect() # Write out data if inimage.ndim == 4: _out[_slice] = data else: _out = data _slice += 1 gc.collect() _out = _out[[slice(self._kernel.shape[i]/2, self.bshape[i] + self._kernel.shape[i]/2) for i in range(len(self.bshape))]] if inimage.ndim == 3: return Image(_out, coordmap=self.coordmap) else: concat_affine = AffineTransform.identity('concat') return Image(_out, coordmap=product(self.coordmap, concat_affine))