def test_repr_niimgs():
    # Test with file path
    assert_equal(_utils._repr_niimgs("test"), "test")
    assert_equal(_utils._repr_niimgs(["test", "retest"]), "[test, retest]")
    # Create phony Niimg with filename
    affine = np.eye(4)
    shape = (10, 10, 10)
    img1 = Nifti1Image(np.ones(shape), affine)
    assert_equal(
        _utils._repr_niimgs(img1),
        ("%s(\nshape=%s,\naffine=%s\n)" % (img1.__class__.__name__, repr(shape), repr(affine))),
    )
    _, tmpimg1 = tempfile.mkstemp(suffix=".nii")
    nibabel.save(img1, tmpimg1)
    assert_equal(_utils._repr_niimgs(img1), ("%s('%s')" % (img1.__class__.__name__, img1.get_filename())))
def test_repr_niimgs():
    # Test with file path
    assert_equal(_utils._repr_niimgs("test"), "test")
    assert_equal(_utils._repr_niimgs(["test", "retest"]), "[test, retest]")
    # Create phony Niimg with filename
    affine = np.eye(4)
    shape = (10, 10, 10)
    img1 = Nifti1Image(np.ones(shape), affine)
    assert_equal(_utils._repr_niimgs(img1),
                 ("%s(\nshape=%s,\naffine=%s\n)" %
                  (img1.__class__.__name__, repr(shape), repr(affine))))
    _, tmpimg1 = tempfile.mkstemp(suffix='.nii')
    nibabel.save(img1, tmpimg1)
    assert_equal(_utils._repr_niimgs(img1),
                 ("%s('%s')" % (img1.__class__.__name__, img1.get_filename())))
Exemple #3
0
    def fit(self, imgs=None, y=None):
        """Compute the mask corresponding to the data

        Parameters
        ----------
        imgs: list of Niimg-like objects
            See http://nilearn.github.io/manipulating_visualizing/manipulating_images.html#niimg.
            Data on which the mask must be calculated. If this is a list,
            the affine is considered the same for all.
        """
        # y=None is for scikit-learn compatibility (unused here).

        # Load data (if filenames are given, load them)
        if self.verbose > 0:
            print("[%s.fit] Loading data from %s" % (
                self.__class__.__name__,
                _utils._repr_niimgs(imgs)[:200]))

        # Compute the mask if not given by the user
        if self.mask_img is None:
            mask_args = (self.mask_args if self.mask_args is not None
                         else {})
            if self.mask_strategy == 'background':
                compute_mask = masking.compute_background_mask
            elif self.mask_strategy == 'epi':
                compute_mask = masking.compute_epi_mask
            else:
                raise ValueError("Unknown value of mask_strategy '%s'. "
                                 "Acceptable values are 'background' and "
                                 "'epi'." % self.mask_strategy)
            if self.verbose > 0:
                print("[%s.fit] Computing the mask" % self.__class__.__name__)
            self.mask_img_ = self._cache(compute_mask, ignore=['verbose'])(
                imgs, verbose=max(0, self.verbose - 1), **mask_args)
        else:
            self.mask_img_ = _utils.check_niimg_3d(self.mask_img)

        # If resampling is requested, resample also the mask
        # Resampling: allows the user to change the affine, the shape or both
        if self.verbose > 0:
            print("[%s.fit] Resampling mask" % self.__class__.__name__)
        self.mask_img_ = self._cache(image.resample_img)(
            self.mask_img_,
            target_affine=self.target_affine,
            target_shape=self.target_shape,
            copy=False)
        if self.target_affine is not None:
            self.affine_ = self.target_affine
        else:
            self.affine_ = self.mask_img_.get_affine()
        # Load data in memory
        self.mask_img_.get_data()
        if self.verbose > 10:
            print("[%s.fit] Finished fit" % self.__class__.__name__)
        return self
def test_repr_niimgs(tmp_path):
    # Tests with file path
    assert _utils._repr_niimgs("test") == "test"
    assert _utils._repr_niimgs("test", shorten=False) == "test"

    # Shortening long names by default
    long_name = 'this-is-a-very-long-name-for-a-nifti-file.nii'
    short_name = 'this-is-a-very-lon...'
    assert _utils._repr_niimgs(long_name) == short_name
    # Explicit shortening of long names
    assert _utils._repr_niimgs(long_name, shorten=True) == short_name
    # Force long display of long names
    assert _utils._repr_niimgs(long_name, shorten=False) == long_name

    # Tests with list of file paths
    assert _utils._repr_niimgs(["test", "retest"]) == "[test, retest]"
    assert _utils._repr_niimgs(["test", "retest"],
                               shorten=False) == "[test, retest]"

    # Lists of long names up to length 3
    list_of_size_3 = [
        'this-is-a-very-long-name-for-a-nifti-file.nii',
        'this-is-another-very-long-name-for-a-nifti-file.nii',
        'this-is-again-another-very-long-name-for-a-nifti-file.nii'
    ]
    # Explicit shortening, all 3 names are displayed, but shortened
    shortened_rep_list_of_size_3 = ("[this-is-a-very-lon...,"
                                    " this-is-another-ve...,"
                                    " this-is-again-anot...]")

    assert (_utils._repr_niimgs(list_of_size_3,
                                shorten=True) == shortened_rep_list_of_size_3)

    # Force display, all 3 names are displayed
    long_rep_list_of_size_3 = (
        "[this-is-a-very-long-name-for-a-nifti-file.nii,"
        " this-is-another-very-long-name-for-a-nifti-file.nii,"
        " this-is-again-another-very-long-name-for-a-nifti-file.nii]")

    assert (_utils._repr_niimgs(list_of_size_3,
                                shorten=False) == long_rep_list_of_size_3)

    # Lists longer than 3
    # Small names - Explicit shortening
    long_list_small_names = ["test", "retest", "reretest", "rereretest"]
    shortened_rep_long_list_small_names = ("[test,\n"
                                           "         ...\n"
                                           " rereretest]")

    assert (_utils._repr_niimgs(
        long_list_small_names,
        shorten=True) == shortened_rep_long_list_small_names)

    # Small names - Force full display
    long_rep_long_list_small_names = ("[test,\n"
                                      " retest,\n"
                                      " reretest,\n"
                                      " rereretest]")

    assert (_utils._repr_niimgs(
        long_list_small_names,
        shorten=False) == long_rep_long_list_small_names)

    # Long names - Explicit shortening
    list_of_size_4 = list_of_size_3 + [
        'this-is-again-another-super-very-long-name-for-a-nifti-file.nii'
    ]
    shortened_rep_long_list_long_names = ("[this-is-a-very-lon...,\n"
                                          "         ...\n"
                                          " this-is-again-anot...]")

    assert (_utils._repr_niimgs(
        list_of_size_4, shorten=True) == shortened_rep_long_list_long_names)

    # Long names - Force full display in pretty print style for readability

    long_rep_long_list_long_names = (
        long_rep_list_of_size_3[:-1].replace(",", ",\n") +
        ",\n this-is-again-another-super-very-long-name-for-a-nifti-file.nii]")

    assert (_utils._repr_niimgs(
        list_of_size_4, shorten=False) == long_rep_long_list_long_names)

    # Tests with pathlib
    # Case with very long path and small filename
    long_path = Path('/this/is/a/fake/long/path/to/file.nii')
    short_path = Path('.../path/to/file.nii')
    assert _utils._repr_niimgs(long_path, shorten=True) == str(short_path)
    assert _utils._repr_niimgs(long_path, shorten=False) == str(long_path)

    # Case with very long path but very long filename
    long_path_long_name = Path(
        '/this/is/a/fake/long/path/to/my_file_with_a_very_long_name.nii')
    short_name = 'my_file_with_a_ver...'
    assert _utils._repr_niimgs(long_path_long_name, shorten=True) == short_name
    assert _utils._repr_niimgs(long_path_long_name,
                               shorten=False) == str(long_path_long_name)

    # Case with lists
    list_of_paths = [
        Path('/this/is/a/fake/long/path/to/file.nii'),
        Path('/this/is/a/fake/long/path/to/another/file2.nii'),
        Path('/again/another/fake/long/path/to/file3.nii'),
        Path('/this/is/a/fake/long/path/to/a-very-long-file-name.nii')
    ]

    shortened_list_of_paths = ("[...{0},\n"
                               "         ...\n"
                               " a-very-long-file-n...]".format(
                                   str(Path("/path/to/file.nii"))))

    assert _utils._repr_niimgs(list_of_paths,
                               shorten=True) == shortened_list_of_paths

    long_list_of_paths = "[%s]" % ',\n '.join(
        _ for _ in [str(_) for _ in list_of_paths])

    assert _utils._repr_niimgs(list_of_paths,
                               shorten=False) == long_list_of_paths

    # Create phony Niimg without filename
    affine = np.eye(4)
    shape = (10, 10, 10)
    img1 = Nifti1Image(np.ones(shape), affine)
    # Shorten has no effect in this case
    for shorten in [True, False]:
        assert (_utils._repr_niimgs(img1, shorten=shorten).replace(
            "10L",
            "10") == ("%s(\nshape=%s,\naffine=%s\n)" %
                      (img1.__class__.__name__, repr(shape), repr(affine))))

    # Add filename long enough to qualify for shortening
    fd, tmpimg1 = tempfile.mkstemp(suffix='_very_long.nii', dir=str(tmp_path))
    os.close(fd)
    nibabel.save(img1, tmpimg1)
    assert (_utils._repr_niimgs(
        img1,
        shorten=False) == ("%s('%s')" %
                           (img1.__class__.__name__, img1.get_filename())))
    assert (_utils._repr_niimgs(img1, shorten=True) == (
        "%s('%s...')" %
        (img1.__class__.__name__, Path(img1.get_filename()).name[:18])))
    def fit(self, imgs, y=None):
        """Fit the data to atlas decomposition and regions extraction

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

        y : None
            Fit for nothing. only for scikit learn compatibility
        """
        models = []
        PARCELLATIONS = dict()
        if imgs is None or len(imgs) == 0:
            raise ValueError("You should provide a list of data e.g. Nifti1Image"
                             " or Nifti1Image filenames. None/Empty is provided")

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

        # Load data
        if self.verbose > 0:
            print("[%s.fit] Loading data from %s" % (
                self.__class__.__name__,
                _utils._repr_niimgs(imgs)[:200]))

        if not isinstance(self.masker, MultiNiftiMasker):
            raise ValueError("An instance of MultiNiftiMasker should be "
                             "provided from nilearn.input_data.MultiNiftiMasker")

        masker = clone(self.masker)

        valid_models = MODELS_CATALOG

        if self.model is not None and isinstance(self.model, _basestring):
            self.model = [self.model]

        if isinstance(self.model, collections.Iterable):
            for model in self.model:
                models.append(model)
                if model not in valid_models:
                    raise ValueError("Invalid model='{0}' is chosen. Please "
                                     "choose one or more among them {1} "
                                     .format(self.model, valid_models))
                if model == 'dictlearn':
                    if self.verbose > 0:
                        print("[Dictionary Learning] Fitting the model")
                    dict_learn = DictLearning(
                        mask=masker, n_components=self.n_comp,
                        random_state=0, n_epochs=1,
                        memory=masker.memory, memory_level=masker.memory_level,
                        n_jobs=masker.n_jobs, verbose=masker.verbose)
                    # Fit Dict Learning model
                    dict_learn_img, masker_ = _model_fit(imgs, dict_learn)
                    # Gather results
                    PARCELLATIONS[model] = dict_learn_img
                    if self.verbose > 0:
                        print("[Dictionary Learning] Done")
                elif model == 'ica':
                    if self.verbose > 0:
                        print("[CanICA] Fitting the model")
                    canica = CanICA(n_components=self.n_comp, mask=masker,
                                    threshold=3., verbose=masker.verbose,
                                    random_state=0, memory=masker.memory,
                                    memory_level=masker.memory_level,
                                    n_jobs=masker.n_jobs)
                    # Fit CanICA model
                    canica_img, masker_ = _model_fit(imgs, canica)
                    # Gather results
                    PARCELLATIONS[model] = canica_img
                    if self.verbose > 0:
                        print("[CanICA Learning] Done")
                elif model == 'kmeans':
                    if self.verbose > 0:
                        print("[MiniBatchKMeans] Fitting the model")
                    kmeans = Parcellations(
                        algorithm='minibatchkmeans', n_parcels=self.n_parcels,
                        mask=masker, init='k-means++', verbose=masker.verbose,
                        memory=masker.memory, memory_level=masker.memory_level,
                        n_jobs=masker.n_jobs, random_state=0)
                    # Fit MiniBatchKmeans model
                    kmeans_img, masker_ = _cluster_model_fit(imgs, kmeans)
                    # Gather results
                    PARCELLATIONS[model] = kmeans_img
                    if self.verbose > 0:
                        print("[MiniBatchKMeans] Learning Done")
                elif model == 'ward':
                    if self.verbose > 0:
                        print("[Feature Agglomeration] Fitting the model")
                    ward = Parcellations(
                        algorithm='featureagglomeration',
                        n_parcels=self.n_parcels, mask=masker, linkage='ward',
                        verbose=masker.verbose, memory=masker.memory,
                        memory_level=masker.memory_level, random_state=0)
                    # Fit Feature Agglomeration ward linkage model
                    ward_img, masker_ = _cluster_model_fit(imgs, ward)
                    # Gather results
                    PARCELLATIONS[model] = ward_img
                    if self.verbose > 0:
                        print("[Feature Agglomeration (Ward)] Learning Done")

        if self.atlases is not None and not isinstance(self.atlases, dict):
            raise ValueError("If 'atlases' are provided, it should be given as "
                             "a dict. Example, atlases={'name': your atlas image}")

        if self.atlases is not None and isinstance(self.atlases, dict):
            if self.model is None:
                masker_ = None
            for key in self.atlases.keys():
                if self.verbose > 0:
                    print("Found Predefined atlases of name:{0}. Added to "
                          "set of models".format(key))
                PARCELLATIONS[key] = self.atlases[key]
                models.append(key)

        self.models_ = models

        # Gather all parcellation results into attribute parcellations_
        self.parcellations_ = PARCELLATIONS
        # If regions need to be extracted
        if self.regions_extract:
            if self.verbose > 0:
                print("[Region Extraction] Preparing images")
            self._regions_extract(masker_)
        else:
            # when analysis should be run on only networks (ica, dictlearn)
            # without region extraction by keeping regions_extract=False
            # Then this else will help
            self.rois_ = PARCELLATIONS

        return self
Exemple #6
0
    def fit(self, imgs=None, y=None):
        """Compute the mask corresponding to the data

        Parameters
        ----------
        imgs: list of Niimg-like objects
            See http://nilearn.github.io/manipulating_visualizing/manipulating_images.html#niimg.
            Data on which the mask must be calculated. If this is a list,
            the affine is considered the same for all.
        """

        # Load data (if filenames are given, load them)
        if self.verbose > 0:
            print("[%s.fit] Loading data from %s" % (
                self.__class__.__name__,
                _utils._repr_niimgs(imgs)[:200]))
        # Compute the mask if not given by the user
        if self.mask_img is None:
            if self.verbose > 0:
                print("[%s.fit] Computing mask" % self.__class__.__name__)
            if not isinstance(imgs, collections.Iterable) \
                    or isinstance(imgs, _basestring):
                raise ValueError("[%s.fit] For multiple processing, you should"
                                 " provide a list of data "
                                 "(e.g. Nifti1Image objects or filenames)."
                                 "%r is an invalid input"
                                 % (self.__class__.__name__, imgs))

            mask_args = (self.mask_args if self.mask_args is not None
                         else {})
            if self.mask_strategy == 'background':
                compute_mask = masking.compute_multi_background_mask
            elif self.mask_strategy == 'epi':
                compute_mask = masking.compute_multi_epi_mask
            else:
                raise ValueError("Unknown value of mask_strategy '%s'. "
                                 "Acceptable values are 'background' and 'epi'.")

            self.mask_img_ = self._cache(compute_mask,
                                         ignore=['n_jobs', 'verbose',
                                                 'memory'])(
                imgs,
                target_affine=self.target_affine,
                target_shape=self.target_shape,
                n_jobs=self.n_jobs,
                memory=self.memory,
                verbose=max(0, self.verbose - 1),
                **mask_args)
        else:
            if imgs is not None:
                warnings.warn('[%s.fit] Generation of a mask has been'
                              ' requested (imgs != None) while a mask has'
                              ' been provided at masker creation. Given mask'
                              ' will be used.' % self.__class__.__name__)
            self.mask_img_ = _utils.check_niimg_3d(self.mask_img)

        # If resampling is requested, resample the mask as well.
        # Resampling: allows the user to change the affine, the shape or both.
        if self.verbose > 0:
            print("[%s.transform] Resampling mask" % self.__class__.__name__)
        self.mask_img_ = self._cache(image.resample_img)(
            self.mask_img_,
            target_affine=self.target_affine,
            target_shape=self.target_shape,
            interpolation='nearest', copy=False)
        if self.target_affine is not None:
            self.affine_ = self.target_affine
        else:
            self.affine_ = self.mask_img_.get_affine()
        # Load data in memory
        self.mask_img_.get_data()
        return self
Exemple #7
0
    def fit(self, imgs=None, y=None):
        """Compute the mask corresponding to the data

        Parameters
        ----------
        imgs: list of Niimg-like objects
            See http://nilearn.github.io/manipulating_visualizing/manipulating_images.html#niimg.
            Data on which the mask must be calculated. If this is a list,
            the affine is considered the same for all.
        """

        # Load data (if filenames are given, load them)
        if self.verbose > 0:
            print("[%s.fit] Loading data from %s" %
                  (self.__class__.__name__, _utils._repr_niimgs(imgs)[:200]))
        # Compute the mask if not given by the user
        if self.mask_img is None:
            if self.verbose > 0:
                print("[%s.fit] Computing mask" % self.__class__.__name__)
            if not isinstance(imgs, collections.Iterable) \
                    or isinstance(imgs, _basestring):
                raise ValueError("[%s.fit] For multiple processing, you should"
                                 " provide a list of data "
                                 "(e.g. Nifti1Image objects or filenames)."
                                 "%r is an invalid input" %
                                 (self.__class__.__name__, imgs))

            mask_args = (self.mask_args if self.mask_args is not None else {})
            if self.mask_strategy == 'background':
                compute_mask = masking.compute_multi_background_mask
            elif self.mask_strategy == 'epi':
                compute_mask = masking.compute_multi_epi_mask
            else:
                raise ValueError(
                    "Unknown value of mask_strategy '%s'. "
                    "Acceptable values are 'background' and 'epi'.")

            self.mask_img_ = self._cache(compute_mask,
                                         ignore=[
                                             'n_jobs', 'verbose', 'memory'
                                         ])(imgs,
                                            target_affine=self.target_affine,
                                            target_shape=self.target_shape,
                                            n_jobs=self.n_jobs,
                                            memory=self.memory,
                                            verbose=max(0, self.verbose - 1),
                                            **mask_args)
        else:
            if imgs is not None:
                warnings.warn('[%s.fit] Generation of a mask has been'
                              ' requested (imgs != None) while a mask has'
                              ' been provided at masker creation. Given mask'
                              ' will be used.' % self.__class__.__name__)
            self.mask_img_ = _utils.check_niimg_3d(self.mask_img)

        # If resampling is requested, resample the mask as well.
        # Resampling: allows the user to change the affine, the shape or both.
        if self.verbose > 0:
            print("[%s.transform] Resampling mask" % self.__class__.__name__)
        self.mask_img_ = self._cache(image.resample_img)(
            self.mask_img_,
            target_affine=self.target_affine,
            target_shape=self.target_shape,
            interpolation='nearest',
            copy=False)
        if self.target_affine is not None:
            self.affine_ = self.target_affine
        else:
            self.affine_ = self.mask_img_.get_affine()
        # Load data in memory
        self.mask_img_.get_data()
        return self