Beispiel #1
0
    def __init__(self, fmri_data, design_matrices, mask='compute',
                 m=0.2, M=0.9, threshold=.5):
        """Load the data

        Parameters
        ----------
        fmri_data : Image or str or sequence of Images / str
            fmri images / paths of the (4D) fmri images
        design_matrices : arrays or str or sequence of arrays / str
            design matrix arrays / paths of .npz files
        mask : str or Image or None, optional
            string can be 'compute' or a path to an image
            image is an input (assumed binary) mask image(s),
            if 'compute', the mask is computed
            if None, no masking will be applied
        m, M, threshold: float, optional
            parameters of the masking procedure.  Should be within [0, 1]

        Notes
        -----
        The only computation done here is mask computation (if required)

        Examples
        --------
        We need the example data package for this example

        >>> from nipy.utils import example_data
        >>> from nipy.modalities.fmri.glm import FMRILinearModel
        >>> fmri_files = [example_data.get_filename('fiac', 'fiac0', run)
        ...     for run in ['run1.nii.gz', 'run2.nii.gz']]
        >>> design_files = [example_data.get_filename('fiac', 'fiac0', run)
        ...     for run in ['run1_design.npz', 'run2_design.npz']]
        >>> mask = example_data.get_filename('fiac', 'fiac0', 'mask.nii.gz')
        >>> multi_session_model = FMRILinearModel(fmri_files, design_files, mask)
        >>> multi_session_model.fit()
        >>> z_image, = multi_session_model.contrast([np.eye(13)[1]] * 2)

        The number of voxels with p < 0.001

        >>> np.sum(z_image.get_data() > 3.09)
        671
        """
        # manipulate the arguments
        if isinstance(fmri_data, basestring) or hasattr(fmri_data, 'get_data'):
            fmri_data = [fmri_data]
        if isinstance(design_matrices, (basestring, np.ndarray)):
            design_matrices = [design_matrices]
        if len(fmri_data) != len(design_matrices):
            raise ValueError('Incompatible number of fmri runs and '
                             'design matrices were provided')
        self.fmri_data, self.design_matrices = [], []
        self.glms, self.means = [], []

        # load the fmri data
        for fmri_run in fmri_data:
            if isinstance(fmri_run, basestring):
                self.fmri_data.append(load(fmri_run))
            else:
                self.fmri_data.append(fmri_run)
        # set self.affine as the affine of the first image
        self.affine = self.fmri_data[0].get_affine()

        # load the designs
        for design_matrix in design_matrices:
            if isinstance(design_matrix, basestring):
                loaded = np.load(design_matrix)
                self.design_matrices.append(loaded[loaded.files[0]])
            else:
                self.design_matrices.append(design_matrix)

        # load the mask
        if mask == 'compute':
            mask = compute_mask_sessions(
                fmri_data, m=m, M=M, cc=1, threshold=threshold, opening=0)
            self.mask = Nifti1Image(mask.astype(np.int8), self.affine)
        elif mask == None:
            mask = np.ones(self.fmri_data[0].shape[:3]).astype(np.int8)
            self.mask = Nifti1Image(mask, self.affine)
        else:
            if isinstance(mask, basestring):
                self.mask = load(mask)
            else:
                self.mask = mask
Beispiel #2
0
    def __init__(self,
                 fmri_data,
                 design_matrices,
                 mask='compute',
                 m=0.2,
                 M=0.9,
                 threshold=.5):
        """Load the data

        Parameters
        ----------
        fmri_data : Image or str or sequence of Images / str
            fmri images / paths of the (4D) fmri images
        design_matrices : arrays or str or sequence of arrays / str
            design matrix arrays / paths of .npz files
        mask : str or Image or None, optional
            string can be 'compute' or a path to an image
            image is an input (assumed binary) mask image(s),
            if 'compute', the mask is computed
            if None, no masking will be applied
        m, M, threshold: float, optional
            parameters of the masking procedure.  Should be within [0, 1]

        Notes
        -----
        The only computation done here is mask computation (if required)

        Examples
        --------
        We need the example data package for this example

        >>> from nipy.utils import example_data
        >>> from nipy.modalities.fmri.glm import FMRILinearModel
        >>> fmri_files = [example_data.get_filename('fiac', 'fiac0', run)
        ...     for run in ['run1.nii.gz', 'run2.nii.gz']]
        >>> design_files = [example_data.get_filename('fiac', 'fiac0', run)
        ...     for run in ['run1_design.npz', 'run2_design.npz']]
        >>> mask = example_data.get_filename('fiac', 'fiac0', 'mask.nii.gz')
        >>> multi_session_model = FMRILinearModel(fmri_files, design_files, mask)
        >>> multi_session_model.fit()
        >>> z_image, = multi_session_model.contrast([np.eye(13)[1]] * 2)

        The number of voxels with p < 0.001

        >>> np.sum(z_image.get_data() > 3.09)
        671
        """
        # manipulate the arguments
        if isinstance(fmri_data, basestring) or hasattr(fmri_data, 'get_data'):
            fmri_data = [fmri_data]
        if isinstance(design_matrices, (basestring, np.ndarray)):
            design_matrices = [design_matrices]
        if len(fmri_data) != len(design_matrices):
            raise ValueError('Incompatible number of fmri runs and '
                             'design matrices were provided')
        self.fmri_data, self.design_matrices = [], []
        self.glms, self.means = [], []

        # load the fmri data
        for fmri_run in fmri_data:
            if isinstance(fmri_run, basestring):
                self.fmri_data.append(load(fmri_run))
            else:
                self.fmri_data.append(fmri_run)
        # set self.affine as the affine of the first image
        self.affine = self.fmri_data[0].get_affine()

        # load the designs
        for design_matrix in design_matrices:
            if isinstance(design_matrix, basestring):
                loaded = np.load(design_matrix)
                self.design_matrices.append(loaded[loaded.files[0]])
            else:
                self.design_matrices.append(design_matrix)

        # load the mask
        if mask == 'compute':
            mask = compute_mask_sessions(fmri_data,
                                         m=m,
                                         M=M,
                                         cc=1,
                                         threshold=threshold,
                                         opening=0)
            self.mask = Nifti1Image(mask.astype(np.int8), self.affine)
        elif mask == None:
            mask = np.ones(self.fmri_data[0].shape[:3]).astype(np.int8)
            self.mask = Nifti1Image(mask, self.affine)
        else:
            if isinstance(mask, basestring):
                self.mask = load(mask)
            else:
                self.mask = mask