Esempio n. 1
0
def _fetch_hbnssi_participants(data_dir, url, verbose):
    """
    Helper function to fetch_hbnssi.
    This function helps in downloading and loading participants data from .tsv
    uploaded on Open Science Framework(OSF).

    Parameters
    ----------
    data_dir: str
        Path of the data directory. Used to force data storage in a specified
        location. If None is given, data are stored in home directory.
    url: str, optional
        Override download URL. Used for test only(or if you setup a mirror of
        the data). Default: None
    verbose: int
        Defines the level of verbosity of the output.

    Returns
    -------
    participants: numpy.ndarray
        Contains data of each subject age, gender, handedness.
    """
    if url is None:
        url = 'https://osf.io/wtvh3/download'

    files = [('participants.csv', url, {'move': 'participants.csv'})]
    path_to_participants = _fetch_files(data_dir, files, verbose=verbose)[0]

    # Load path to participants
    dtype = [('sid', 'U12'), ('age', '<f8'), ('Gender', 'U4'),
             ('Handedness', 'U4')]
    names = ['sid', 'age', 'gender', 'handedness']
    participants = csv_to_array(path_to_participants, skip_header=True,
                                dtype=dtype, names=names)
    return participants
def _fetch_ibc_surf_masks(data_dir, url, resume, verbose):
    """Helper function to fetch_ibc.

    This function helps in downloading brain and ROI masks for use with IBC
    functional alignment and inter-subject decoding.

    The files are downloaded from Open Science Framework (OSF).

    Parameters
    ----------
    data_dir: str
        Path of the data directory. Used to force data storage in a specified
        location. If None is given, data are stored in home directory.

    url: str, optional
        Override download URL. Used for test only (or if you setup a mirror of
        the data). Default: None

    resume: bool, optional (default True)
        Whether to resume download of a partly-downloaded file.

    verbose: int
        Defines the level of verbosity of the output.

    Returns
    -------
    derivatives_dir: str
        Path on disk to the IBC masks data directory.
    """
    if url is None:
        # Download from the relevant OSF project, using hashes generated
        # from the OSF API. Note the trailing slash. For more info, see:
        # https://gist.github.com/emdupre/3cb4d564511d495ea6bf89c6a577da74
        url = 'https://osf.io/download/{}/'

    # The gzip contains unique download keys per Nifti file and CSV
    # pre-extracted from OSF. Required for downloading files.
    package_directory = os.path.dirname(os.path.abspath(__file__))
    dtype = [('filename', 'U52'), ('uid', 'U24')]
    names = ['filename', 'uid']
    # csv file contains download information
    osf_data = csv_to_array(os.path.join(package_directory,
                                         "ibc_surf_masks.csv"),
                            skip_header=True,
                            dtype=dtype,
                            names=names)

    derivatives_dir = Path(data_dir, 'masks')
    masks = []

    for this_osf_id in osf_data:

        # Download mask
        mask_url = url.format(this_osf_id['uid'])
        mask_target = Path(derivatives_dir, this_osf_id['filename'])
        mask_file = [(mask_target, mask_url, {'move': mask_target})]
        path_to_mask = _fetch_files(data_dir, mask_file, verbose=verbose)[0]
        masks.append(path_to_mask)

    return derivatives_dir
Esempio n. 3
0
def test_csv_to_array():
    # Create a phony CSV file
    with tempfile.NamedTemporaryFile(suffix='.csv', mode='wt') as csv_file:
        csv_file.write('1.,2.,3.,4.,5.\n')
        csv_file.flush()
        assert_true(np.allclose(csv_to_array(csv_file.name),
                    np.asarray([1., 2., 3., 4., 5.])))
        assert_raises(TypeError, csv_to_array, csv_file.name, delimiters='?!')
Esempio n. 4
0
def test_csv_to_array():
    # Create a phony CSV file
    with tempfile.NamedTemporaryFile(suffix='.csv', mode='wt') as csv_file:
        csv_file.write('1.,2.,3.,4.,5.\n')
        csv_file.flush()
        assert_true(
            np.allclose(csv_to_array(csv_file.name),
                        np.asarray([1., 2., 3., 4., 5.])))
        assert_raises(TypeError, csv_to_array, csv_file.name, delimiters='?!')
def test_csv_to_array():
    # Create a phony CSV file
    filename = tempfile.mktemp(suffix='.csv')
    try:
        with open(filename, mode='wt') as fp:
            fp.write('1.,2.,3.,4.,5.\n')
        assert_true(np.allclose(csv_to_array(filename),
                    np.asarray([1., 2., 3., 4., 5.])))
        assert_raises(TypeError, csv_to_array, filename, delimiters='?!')
    finally:
        os.remove(filename)
Esempio n. 6
0
def test_csv_to_array():
    # Create a phony CSV file
    filename = tempfile.mktemp(suffix='.csv')
    try:
        with open(filename, mode='wt') as fp:
            fp.write('1.,2.,3.,4.,5.\n')
        assert np.allclose(csv_to_array(filename),
                           np.asarray([1., 2., 3., 4., 5.]))
        pytest.raises(TypeError, csv_to_array, filename, delimiters='?!')
    finally:
        os.remove(filename)
Esempio n. 7
0
    def transform(self, imgs, confounds=None):
        """Signal extraction from regions learned on the images.

        Parameters
        ----------
        imgs : Nifti like images, list

        confounds : csv file or array-like, optional
            Contains signals like motion, high variance confounds from
            white matter, csf. This is passed to signal.clean
        """
        self._check_fitted()
        confounds1_ = None
        confounds2_ = None
        SUBJECTS_TIMESERIES = dict()

        # Getting Masker to transform fMRI images in Nifti to timeseries signals
        # based on atlas learning
        if self.masker is None:
            raise ValueError("Could not find masker attribute. Masker is missing")

        if not isinstance(imgs, collections.Iterable) or \
                isinstance(imgs, _basestring):
            imgs = [imgs, ]

        if self.compute_confounds not in ['compcor_5', 'compcor_10', None]:
            warnings.warn("Given invalid input compute_confounds={0}. Given "
                          "input is diverting to compute_confounds=None"
                          .format(self.compute_confounds))
            self.compute_confounds = None

        if self.compute_not_mask_confounds not in ['compcor_5', 'compcor_10', None]:
            warnings.warn("Invalid input type of 'compute_not_mask_confounds'={0}"
                          "is provided. Switching to None"
                          .format(self.compute_not_mask_confounds))
            self.compute_not_mask_confounds = None

        if confounds is None and self.compute_confounds is None and \
                self.compute_not_mask_confounds is None:
            confounds = [None] * len(imgs)

        if self.compute_confounds is None and \
                self.compute_not_mask_confounds is None:
            confounds_ = None

        if self.compute_confounds is not None:
            if self.compute_confounds_mask_img is None:
                raise ValueError("Parameter `compute_confounds` is given as {0} but "
                                 "`compute_confounds_mask_img` is given as None. "
                                 .format(self.compute_confounds))
            if not isinstance(self.compute_confounds_mask_img, collections.Iterable) or \
                    isinstance(self.compute_confounds_mask_img, _basestring):
                mask_imgs1 = [self.compute_confounds_mask_img]

            if self.compute_confounds == 'compcor_5':
                n_confounds = 5
            elif self.compute_confounds == 'compcor_10':
                n_confounds = 10

            confounds1_ = []

            for mask_img1 in mask_imgs1:
                conf1_ = self.masker.memory.cache(compute_confounds)(
                    imgs, mask_img1, n_confounds=n_confounds)
                confounds1_.append(conf1_)

            confounds1_ = np.hstack(confounds1_)

        if self.compute_not_mask_confounds is not None:
            if self.compute_confounds_non_mask_img is None:
                raise ValueError("Parameter `compute_not_mask_confounds` is given as {0}"
                                 " but `compute_confounds_non_mask_img` is given as None."
                                 .format(self.compute_not_mask_confounds))
            if not isinstance(self.compute_confounds_non_mask_img, collections.Iterable) or \
                    isinstance(self.compute_confounds_non_mask_img, _basestring):
                mask_imgs2 = [self.compute_confounds_non_mask_img]

            if self.compute_not_mask_confounds == 'compcor_5':
                n_confounds = 5
            elif self.compute_not_mask_confounds == 'compcor_10':
                n_confounds = 10

            confounds2_ = []

            for mask_img2 in mask_imgs2:
                conf2_ = self.masker.memory.cache(compute_confounds)(
                    imgs, mask_img2, n_confounds=n_confounds, compute_not_mask=True)
                confounds2_.append(conf2_)

        if confounds1_ is not None and confounds2_ is not None:
            confounds_ = np.hstack((confounds1_, confounds2_))

        if confounds1_ is not None and confounds2_ is None:
            confounds_ = confounds1_

        if confounds1_ is None and confounds2_ is not None:
            confounds_ = confounds2_

        if confounds_ is not None:
            if confounds is not None:
                confs_ = []
                if isinstance(confounds, collections.Iterable):
                    for conf_gen, conf_given in zip(confounds_, confounds):
                        if isinstance(conf_given, _basestring):
                            conf_given = csv_to_array(conf_given)
                        stack_confounds = np.hstack((conf_gen, conf_given))
                        confs_.append(stack_confounds)
                confounds = confs_
            else:
                confounds = confounds_

        if confounds is not None and isinstance(confounds, collections.Iterable):
            if len(confounds) != len(imgs):
                raise ValueError("Number of confounds given doesnot match with "
                                 "the given number of subjects. Add missing "
                                 "confound in a list.")

        for model in self.models_:
            subjects_timeseries = []
            if self.verbose > 0:
                print("[Timeseries Extraction] {0} atlas image is selected"
                      .format(model))
            atlas_img = self.rois_[model]
            masker = check_embedded_atlas_masker(self.masker, atlas_type='auto',
                                                 img=atlas_img, t_r=2.53,
                                                 low_pass=0.1, high_pass=0.01)

            subjects_timeseries = Parallel(n_jobs=self.masker.n_jobs)(
                delayed(_atlas_masker_extraction)(img, masker, confound)
                for img, confound in zip(imgs, confounds))

            if subjects_timeseries is not None:
                SUBJECTS_TIMESERIES[model] = subjects_timeseries
            else:
                warnings.warn("Timeseries signals extraction are found empty "
                              "for model:{0}".format(model))

        self.subjects_timeseries_ = SUBJECTS_TIMESERIES

        if self.connectome_convert:
            if self.connectome_measure is not None:
                if isinstance(self.connectome_measure, collections.Iterable):
                    catalog = self.connectome_measure
                else:
                    if isinstance(self.connectome_measure, _basestring):
                        catalog = [self.connectome_measure, ]
            else:
                warnings.warn("Given connectome_convert=True but connectome "
                              "are given as None. Taking connectome measure "
                              "kind='correlation'", stacklevel=2)
                catalog = ['correlation']

            self.connectome_measures_ = catalog

            connectivities = self._connectome_converter(
                catalog=self.connectome_measures_,
                confounds=self.connectome_confounds)

        return self
Esempio n. 8
0
def _fetch_hbnssi_functional(participants, data_dir, url, resume, verbose):
    """Helper function to fetch_development_fmri.

    This function helps in downloading functional MRI data in Nifti
    and its confound corresponding to each subject.

    The files are downloaded from Open Science Framework (OSF).

    Parameters
    ----------
    participants : numpy.ndarray
        Should contain column participant_id which represents subjects id. The
        number of files are fetched based on ids in this column.

    data_dir: str
        Path of the data directory. Used to force data storage in a specified
        location. If None is given, data are stored in home directory.

    url: str, optional
        Override download URL. Used for test only (or if you setup a mirror of
        the data). Default: None

    resume: bool, optional (default True)
        Whether to resume download of a partly-downloaded file.

    verbose: int
        Defines the level of verbosity of the output.

    Returns
    -------
    func: list of str (Nifti files)
        Paths to functional MRI data (4D) for each subject.
    """
    if url is None:
        # Download from the relevant OSF project, using hashes generated
        # from the OSF API. Note the trailing slash. For more info, see:
        # https://gist.github.com/emdupre/3cb4d564511d495ea6bf89c6a577da74
        url = 'https://osf.io/download/{}/'

    raiders = 'sub-{0}_task-RAIDERS_space-MNI152NLin2009cAsym_desc-postproc_bold.nii.gz'
    flanker = 'sub-{0}_task-FLANKERTASK_space-MNI152NLin2009cAsym_desc-postproc_bold.nii.gz'
    conditions = 'sub-{0}_labels.csv'
    runs = 'sub-{0}_runs.csv'

    # The gzip contains unique download keys per Nifti file and confound
    # pre-extracted from OSF. Required for downloading files.
    package_directory = os.path.dirname(os.path.abspath(__file__))
    dtype = [('sid', 'U12'), ('raiders', 'U24'), ('flanker', 'U24'),
             ('condition', 'U24'), ('run', 'U24')]
    names = ['sid', 'raiders', 'flanker', 'condition', 'run']
    # csv file contains download information related to OpenScience(osf)
    osf_data = csv_to_array(os.path.join(package_directory, "hbnssi.csv"),
                            skip_header=True, dtype=dtype, names=names)

    derivatives_dir = Path(data_dir, 'derivatives')
    align, decode, labels, sessions = [], [], [], []

    for sid in participants['sid']:
        this_osf_id = osf_data[osf_data['sid'] == sid]

        # Download raiders
        raiders_url = url.format(this_osf_id['raiders'][0])
        raiders_target = Path(derivatives_dir, raiders.format(sid))
        raiders_file = [(raiders_target,
                         raiders_url,
                         {'move': raiders_target})]
        path_to_raiders = _fetch_files(data_dir, raiders_file,
                                       verbose=verbose)[0]
        align.append(path_to_raiders)

        # Download flanker
        flanker_url = url.format(this_osf_id['flanker'][0])
        flanker_target = Path(derivatives_dir, flanker.format(sid))
        flanker_file = [(flanker_target,
                         flanker_url,
                         {'move': flanker_target})]
        path_to_flanker = _fetch_files(data_dir, flanker_file,
                                       verbose=verbose)[0]
        decode.append(path_to_flanker)

        # Download condition labels
        label_url = url.format(this_osf_id['condition'][0])
        label_target = Path(derivatives_dir, conditions.format(sid))
        label_file = [(label_target,
                      label_url,
                      {'move': label_target})]
        path_to_labels = _fetch_files(data_dir, label_file,
                                      verbose=verbose)[0]
        labels.append(path_to_labels)

        # Download session run numbers
        session_url = url.format(this_osf_id['run'][0])
        session_target = Path(derivatives_dir, runs.format(sid))
        session_file = [(session_target,
                        session_url,
                        {'move': session_target})]
        path_to_sessions = _fetch_files(data_dir, session_file,
                                        verbose=verbose)[0]
        sessions.append(path_to_sessions)

    return derivatives_dir
def _fetch_rsvp_trial_surf(participants, data_dir, url, resume, verbose):
    """Helper function to fetch_ibc.

    This function helps in downloading functional MRI data in Nifti format
    and its corresponding CSVs each subject for functional alignment and
    decoding.

    The files are downloaded from Open Science Framework (OSF).

    Parameters
    ----------
    participants : numpy.ndarray
        Should contain column participant_id which represents subjects id. The
        number of files are fetched based on ids in this column.

    data_dir: str
        Path of the data directory. Used to force data storage in a specified
        location. If None is given, data are stored in home directory.

    url: str, optional
        Override download URL. Used for test only (or if you setup a mirror of
        the data). Default: None

    resume: bool, optional (default True)
        Whether to resume download of a partly-downloaded file.

    verbose: int
        Defines the level of verbosity of the output.

    Returns
    -------
    func: list of str (Nifti files)
        Paths to functional MRI data (4D) for each subject.
    """
    if url is None:
        # Download from the relevant OSF project, using hashes generated
        # from the OSF API. Note the trailing slash. For more info, see:
        # https://gist.github.com/emdupre/3cb4d564511d495ea6bf89c6a577da74
        url = 'https://osf.io/download/{}/'

    surf = '{0}_{1}.gii'
    highres_surf = '{0}_{1}_fullres.gii'
    conditions = '{0}_{1}_labels.csv'
    runs = '{0}_{1}_runs.csv'

    # The gzip contains unique download keys per Nifti file and CSV
    # pre-extracted from OSF. Required for downloading files.
    package_directory = os.path.dirname(os.path.abspath(__file__))
    dtype = [('sid', 'U12'), ('hemi', 'U12'), ('surf', 'U24'),
             ('highres_surf', 'U24'), ('condition', 'U24'), ('run', 'U24')]
    names = ['sid', 'hemi', 'surf', 'highres_surf', 'condition', 'run']
    # csv file contains download information
    osf_data = csv_to_array(os.path.join(package_directory,
                                         "rsvp_trial_surf.csv"),
                            skip_header=True,
                            dtype=dtype,
                            names=names)

    derivatives_dir = Path(data_dir, 'rsvp_trial', 'surf_derivatives')
    surf_files, highres_surf_files, labels, sessions = [], [], [], []

    for sid in participants['sid']:
        hemi1, hemi2 = osf_data[osf_data['sid'] == sid]
        for hemi in (hemi1, hemi2):

            # Download surface for decoding
            surf_url = url.format(hemi['surf'])
            surf_target = Path(derivatives_dir, surf.format(sid, hemi['hemi']))
            surf_file = [(surf_target, surf_url, {'move': surf_target})]
            path_to_surf = _fetch_files(data_dir, surf_file,
                                        verbose=verbose)[0]
            surf_files.append(path_to_surf)

            # Download surface
            highres_surf_url = url.format(hemi['highres_surf'])
            highres_surf_target = Path(derivatives_dir,
                                       highres_surf.format(sid, hemi['hemi']))
            highres_surf_file = [(highres_surf_target, highres_surf_url, {
                'move': highres_surf_target
            })]
            path_to_highres_surf = _fetch_files(data_dir,
                                                highres_surf_file,
                                                verbose=verbose)[0]
            highres_surf_files.append(path_to_highres_surf)

            # Download condition labels
            label_url = url.format(hemi['condition'])
            label_target = Path(derivatives_dir,
                                conditions.format(sid, hemi['hemi']))
            label_file = [(label_target, label_url, {'move': label_target})]
            path_to_labels = _fetch_files(data_dir,
                                          label_file,
                                          verbose=verbose)[0]
            labels.append(path_to_labels)

            # Download session run numbers
            session_url = url.format(hemi['run'])
            session_target = Path(derivatives_dir,
                                  runs.format(sid, hemi['hemi']))
            session_file = [(session_target, session_url, {
                'move': session_target
            })]
            path_to_sessions = _fetch_files(data_dir,
                                            session_file,
                                            verbose=verbose)[0]
            sessions.append(path_to_sessions)

    # create out_dir
    Path(data_dir, "rsvp_trial", "decoding").mkdir(parents=True, exist_ok=True)

    # create mask_cache
    Path(data_dir, "rsvp_trial", "mask_cache").mkdir(parents=True,
                                                     exist_ok=True)

    return derivatives_dir
def _fetch_ibc_alignment(participants, data_dir, url, resume, verbose):
    """Helper function to fetch_ibc.

    This function helps in downloading IBC functional MRI data in Nifti format
    and its corresponding CSVs each subject for functional alignment and
    decoding.

    The files are downloaded from Open Science Framework (OSF).

    Parameters
    ----------
    participants : numpy.ndarray
        Should contain column participant_id which represents subjects id. The
        number of files are fetched based on ids in this column.

    data_dir: str
        Path of the data directory. Used to force data storage in a specified
        location. If None is given, data are stored in home directory.

    url: str, optional
        Override download URL. Used for test only (or if you setup a mirror of
        the data). Default: None

    resume: bool, optional (default True)
        Whether to resume download of a partly-downloaded file.

    verbose: int
        Defines the level of verbosity of the output.

    Returns
    -------
    derivatives_dir: str
        Path on disk to the IBC alignment data directory.
    """
    if url is None:
        # Download from the relevant OSF project, using hashes generated
        # from the OSF API. Note the trailing slash. For more info, see:
        # https://gist.github.com/emdupre/3cb4d564511d495ea6bf89c6a577da74
        url = 'https://osf.io/download/{}/'

    alignment = '{0}_53_contrasts.nii.gz'

    # The gzip contains unique download keys per Nifti file and CSV
    # pre-extracted from OSF. Required for downloading files.
    package_directory = os.path.dirname(os.path.abspath(__file__))
    dtype = [('sid', 'U12'), ('alignment', 'U24')]
    names = ['sid', 'alignment']
    # csv file contains download information
    osf_data = csv_to_array(os.path.join(package_directory,
                                         "ibc_alignment.csv"),
                            skip_header=True,
                            dtype=dtype,
                            names=names)

    derivatives_dir = Path(data_dir, 'alignment')
    align = []

    for sid in participants['sid']:
        this_osf_id = osf_data[osf_data['sid'] == sid]

        # Download alignment
        alignment_url = url.format(this_osf_id['alignment'][0])
        alignment_target = Path(derivatives_dir, alignment.format(sid))
        alignment_file = [(alignment_target, alignment_url, {
            'move': alignment_target
        })]
        path_to_alignment = _fetch_files(data_dir,
                                         alignment_file,
                                         verbose=verbose)[0]
        align.append(path_to_alignment)

    return derivatives_dir