Exemple #1
0
    def get_unscaled_data(self):
        """ Return image data without image scaling applied

        Summary: please use the ``get_data`` method instead of this
        method unless you are sure what you are doing, and that you will
        only be using image formats for which this method exists and
        returns sensible results.
        
        Use this method with care; the modified Analyze-type formats
        such as SPM formats, and nifti1, specify that the image data
        array, as they are expecting to return it, is given by the raw
        data on disk, multiplied by a scalefactor and maybe with the
        addition of a constant.  This method returns the data on the
        disk, without these format-specific scalings applied.  Please
        use this method only if you absolutely need the unscaled data,
        and the magnitude of the data, as given by the scalefactor, is
        not relevant to your application.  The Analyze-type formats have
        a single scalefactor +/- offset per image on disk. If you do not
        care about the absolute values, and will be removing the mean
        from the data, then the unscaled values will have preserved
        intensity ratios compared to the mean-centered scaled data.
        However, this is not necessarily true of other formats with more
        complicated scaling - such as MINC.

        Note that - unlike the scaled ``get_data`` method, we do not
        cache the array, to minimize the memory taken by the object.
        """
        if not self._files:
            return None
        try:
            fname = self._files['image']
        except KeyError:
            return None
        return read_unscaled_data(self._header, allopen(fname))
Exemple #2
0
def get_unscaled_img(fname):
    ''' Function to get image, data without scalefactor applied

    If the image is of Analyze type, and is integer format, and has
    single scalefactor that is usually applied, then read the raw
    integer data from disk, rather than using the higher-level get_data
    method, that would apply the scalefactor.  We do this because there
    seemed to be images for which the integer binning in the raw file
    data was needed for the histogram-like mask calculation in
    ``compute_mask_files``.

    By loading the image in this function we can guarantee that the
    image as loaded from disk is the source of the current image data.

    Parameters
    ----------
    fname : str
       filename of image

    Returns
    -------
    img : imageformats Image object
    arr : ndarray
    '''
    img = load(fname)
    if isinstance(img, AnalyzeImage):
        dt = img.get_data_dtype()
        if dt.kind in ('i', 'u'):
            from nipy.io.imageformats.header_ufuncs import read_unscaled_data
            from nipy.io.imageformats.volumeutils import allopen
            # get where the image data is, given input filename
            ft = img.filespec_to_files(fname)
            hdr = img.get_header()
            # read unscaled data from disk
            return img, read_unscaled_data(hdr, allopen(ft['image']))
    return img, img.get_data()