def load_spatial_image(filename): img = load_image(filename) in_coords = list(img.coordmap.function_domain.coord_names) other_coords = filter(lambda x: x not in 'ijk', in_coords) if other_coords: idata = np.asanyarray(img) cm_3d = copy(img.coordmap) while other_coords: c = other_coords.pop() slicing = [slice(None)] * idata.ndim slicing[in_coords.index(c)] = 0 idata = idata[slicing] cm_3d = ni_api.drop_io_dim(cm_3d, c) img = ni_api.Image(idata.copy(), cm_3d) del idata return img
def slice_timewise(img, t): """Utility function to slice a 4D image timewise. Naturally, the image is assumed to be 4 dimensional, with a 5x5 affine. Parameters ---------- img : NIPY Image t : int time index Returns ------- a 3D image, whose affine has only spatial coordinates in its range """ if len(img.shape) != 4 or img.affine.shape != (5,5): raise ValueError('This is not a 4D image') t_ax = timedim(img) slicing = [ slice(None) ] * 4 slicing[t_ax] = t cm_3d = ni_api.drop_io_dim(img.coordmap, 't') return ni_api.Image(np.asarray(img)[slicing], cm_3d)