def test_drop_io_dim(): aff = np.diag([1,2,3,1]) in_dims = list('ijk') out_dims = list('xyz') cm = Affine.from_params(in_dims, out_dims, aff) yield assert_raises(ValueError, drop_io_dim, cm, 'q') for i, d in enumerate(in_dims): cm2 = drop_io_dim(cm, d) yield assert_equal(cm2.ndim, (2,2)) ods = out_dims[:] ids = in_dims[:] ods.pop(i) ids.pop(i) yield assert_equal(ods, cm2.output_coords.coord_names) yield assert_equal(ids, cm2.input_coords.coord_names) a2 = cm2.affine yield assert_equal(a2.shape, (3,3)) ind_a = np.ones((4,4), dtype=np.bool) ind_a[:,i] = False ind_a[i,:] = False aff2 = cm.affine[ind_a].reshape((3,3)) yield assert_array_equal(aff2, a2) # Check non-orth case waff = np.diag([1,2,3,1]) wrong_i = (i+1) % 3 waff[wrong_i,i] = 2 # not OK if in same column as that being removed wcm = Affine.from_params(in_dims, out_dims, waff) yield assert_raises(ValueError, drop_io_dim, wcm, d) raff = np.diag([1,2,3,1]) raff[i,wrong_i] = 2 # is OK if in same row rcm = Affine.from_params(in_dims, out_dims, raff) cm2 = drop_io_dim(rcm, d) # dims out of range give error yield assert_raises(ValueError, drop_io_dim, cm, 'p') # only works for affine case cm = CoordinateMap(lambda x:x, cm.input_coords, cm.output_coords) yield assert_raises(AttributeError, drop_io_dim, cm, 'i') # Check non-square case aff = np.array([[1.,0,0,0], [0,2,0,0], [0,0,3,0], [0,0,0,1], [0,0,0,1]]) cm = Affine.from_params(in_dims, out_dims + ['t'], aff) cm2 = drop_io_dim(cm, 'i') yield assert_array_equal(cm2.affine, [[2,0,0], [0,3,0], [0,0,1], [0,0,1]]) yield assert_raises(ValueError, drop_io_dim, cm, 't')
def test_drop_io_dim(): # test ordinary case of 4d to 3d cm4d = AffineTransform.from_params('ijkl', 'xyzt', np.diag([1,2,3,4,1])) cm3d = drop_io_dim(cm4d, 't') yield assert_array_equal(cm3d.affine, np.diag([1, 2, 3, 1])) # 3d to 2d cm3d = AffineTransform.from_params('ijk', 'xyz', np.diag([1,2,3,1])) cm2d = drop_io_dim(cm3d, 'z') yield assert_array_equal(cm2d.affine, np.diag([1, 2, 1])) # test zero scaling for dropped dimension cm3d = AffineTransform.from_params('ijk', 'xyz', np.diag([1, 2, 0, 1])) cm2d = drop_io_dim(cm3d, 'z') yield assert_array_equal(cm2d.affine, np.diag([1, 2, 1])) # test not diagonal but orthogonal aff = np.array([[1, 0, 0, 0], [0, 0, 2, 0], [0, 3, 0, 0], [0, 0, 0, 1]]) cm3d = AffineTransform.from_params('ijk', 'xyz', aff) cm2d = drop_io_dim(cm3d, 'z') yield assert_array_equal(cm2d.affine, np.diag([1, 2, 1])) cm2d = drop_io_dim(cm3d, 'k') yield assert_array_equal(cm2d.affine, np.diag([1, 3, 1])) # and with zeros scaling for orthogonal dropped dimension aff[2] = 0 cm3d = AffineTransform.from_params('ijk', 'xyz', aff) cm2d = drop_io_dim(cm3d, 'z') yield assert_array_equal(cm2d.affine, np.diag([1, 2, 1]))
def fix_analyze_image(img, fliplr=False): cmap = img.coordmap if fliplr: x_idx = cmap.function_range.index('x') cmap.affine[x_idx] *= -1 if (len(img.shape) < 4) or img.shape[3] != 1: return img try: from nipy.core.reference.coordinate_map import drop_io_dim except: print 'not fixing this image... NIPY API changed' return img # affine is a 5x5: need to get rid of row 3 and col 3 arr = np.asarray(img).reshape(img.shape[:3]) cmap = drop_io_dim(cmap, 't') return ni_api.Image(arr, cmap)
def get_3D_coordmap(img): ''' Gets a 3D CoordinateMap from img. Parameters ---------- img: nib.Nifti1Image or nipy Image Returns ------- nipy.core.reference.coordinate_map.CoordinateMap ''' if isinstance(img, nib.Nifti1Image): img = nifti2nipy(img) if img.ndim == 4: from nipy.core.reference.coordinate_map import drop_io_dim cm = drop_io_dim(img.coordmap, 3) else: cm = img.coordmap return cm