def load(filename): """Load an image from the given filename. Parameters ---------- filename : string Should resolve to a complete filename path. Returns ------- image : An `Image` object If successful, a new `Image` object is returned. See Also -------- save_image : function for saving images fromarray : function for creating images from numpy arrays Examples -------- >>> from nipy.io.api import load_image >>> from nipy.testing import anatfile >>> img = load_image(anatfile) >>> img.shape (33, 41, 25) """ img = formats.load(filename) aff = img.get_affine() shape = img.get_shape() hdr = img.get_header() # Get info from NIFTI header, if present, to tell which axes are # which. This is a NIFTI-specific kludge, that might be abstracted # out into the image backend in a general way. Similarly for # getting zooms # axis_renames is a dictionary: dict([(int, str)]) # that has keys in range(3) # the axes of the Image are renamed from 'ijk' # using these names try: axis_renames = hdr.get_axis_renames() except (TypeError, AttributeError): axis_renames = {} try: zooms = hdr.get_zooms() except AttributeError: zooms = np.ones(len(shape)) # affine_transform is a 3-d transform affine_transform3d, affine_transform = \ affine_transform_from_array(aff, 'ijk', pixdim=zooms[3:]) img = Image(img.get_data(), affine_transform.renamed_domain(axis_renames)) img.header = hdr return img
def load(filename): """Load an image from the given filename. Parameters ---------- filename : string Should resolve to a complete filename path. Returns ------- image : An `Image` object If successful, a new `Image` object is returned. See Also -------- save_image : function for saving images fromarray : function for creating images from numpy arrays Examples -------- >>> from nipy.io.api import load_image >>> from nipy.testing import anatfile >>> img = load_image(anatfile) >>> img.shape (33, 41, 25) """ img = nib.load(filename) aff = img.get_affine() shape = img.get_shape() hdr = img.get_header() # If the header implements it, get a list of names, one per axis, # and put this into the coordinate map. In fact, no image format # implements this at the moment, so in practice, the following code # is not currently called. axis_renames = {} try: axis_names = hdr.axis_names except AttributeError: pass else: # axis_renames is a dictionary: dict([(int, str)]) that has keys # in range(3). The axes of the Image are renamed from 'ijk' using # these names for i in range(min([len(axis_names), 3])): name = axis_names[i] if not (name is None or name == ''): axis_renames[i] = name zooms = hdr.get_zooms() # affine_transform is a 3-d transform affine_transform3d, affine_transform = \ affine_transform_from_array(aff, 'ijk', pixdim=zooms[3:]) img = Image(img.get_data(), affine_transform.renamed_domain(axis_renames)) img.header = hdr return img
def load(filename): """Load an image from the given filename. Parameters ---------- filename : string Should resolve to a complete filename path. Returns ------- image : An `Image` object If successful, a new `Image` object is returned. See Also -------- save_image : function for saving images fromarray : function for creating images from numpy arrays Examples -------- >>> from nipy.io.api import load_image >>> from nipy.testing import anatfile >>> img = load_image(anatfile) >>> img.shape (33, 41, 25) """ img = formats.load(filename) aff = img.get_affine() shape = img.get_shape() hdr = img.get_header() # Get info from NIFTI header, if present, to tell which axes are # which. This is a NIFTI-specific kludge, that might be abstracted # out into the image backend in a general way. Similarly for # getting zooms try: fps = hdr.get_dim_info() except (TypeError, AttributeError): fps = (None, None, None) ijk = ijk_from_fps(fps) try: zooms = hdr.get_zooms() except AttributeError: zooms = np.ones(len(shape)) aff = _match_affine(aff, len(shape), zooms) coordmap = coordmap_from_affine(aff, ijk) img = Image(img.get_data(), coordmap) img.header = hdr return img