def _raw_fit(self, data):

        group = self.group
        sub_num = self.sub_num

        if group:
            data = MultiPCA._raw_fit(self, data).T
        else:
            data = data.reshape((3, self.n_components, -1))
            data = data[sub_num - 1]

        # plt.plot(data[:, :3])
        # plt.tight_layout()
        # plt.show()
        self.hvmf_fit(data.T)
        return self
Exemple #2
0
    def _raw_fit(self, data):
        """ Fits the parcellation method on this reduced data.
        Data are coming from a base decomposition estimator which computes
        the mask and reduces the dimensionality of images using
        randomized_svd.
        Parameters
        ----------
        data : ndarray
            Shape (n_samples, n_features)
        Returns
        -------
        labels_ : numpy.ndarray
            Labels to each cluster in the brain.
        connectivity_ : numpy.ndarray
            voxel-to-voxel connectivity matrix computed from a mask.
            Note that, this attribute is returned only for selected methods
            such as 'ward', 'complete', 'average'.
        """
        valid_methods = self.VALID_METHODS
        if self.method is None:
            raise ValueError("Parcellation method is specified as None. "
                             "Please select one of the method in "
                             "{0}".format(valid_methods))
        if self.method is not None and self.method not in valid_methods:
            raise ValueError("The method you have selected is not implemented "
                             "'{0}'. Valid methods are in {1}".format(
                                 self.method, valid_methods))

        # we delay importing Ward or AgglomerativeClustering and same
        # time import plotting module before that.

        # Because sklearn.cluster imports scipy hierarchy and hierarchy imports
        # matplotlib. So, we force import matplotlib first using our
        # plotting to avoid backend display error with matplotlib
        # happening in Travis
        try:
            from nilearn import plotting
        except:
            pass

        components = MultiPCA._raw_fit(self, data)

        mask_img_ = self.masker_.mask_img_
        if self.verbose:
            print("[{0}] computing {1}".format(self.__class__.__name__,
                                               self.method))

        if self.method == 'kmeans':
            from sklearn.cluster import MiniBatchKMeans
            kmeans = MiniBatchKMeans(n_clusters=self.n_parcels,
                                     init='k-means++',
                                     random_state=self.random_state,
                                     verbose=max(0, self.verbose - 1))
            labels = self._cache(_estimator_fit,
                                 func_memory_level=1)(components.T, kmeans)
        else:
            mask_ = _safe_get_data(mask_img_).astype(np.bool)
            shape = mask_.shape
            connectivity = image.grid_to_graph(n_x=shape[0],
                                               n_y=shape[1],
                                               n_z=shape[2],
                                               mask=mask_)

            # from data.new_agglo import NewAgglomerativeClustering as AgglomerativeClustering
            from sklearn.cluster import AgglomerativeClustering

            agglomerative = AgglomerativeClustering(n_clusters=self.n_parcels,
                                                    connectivity=connectivity,
                                                    linkage=self.method,
                                                    memory=self.memory,
                                                    compute_full_tree=True)

            labels = self._cache(_estimator_fit,
                                 func_memory_level=1)(components.T,
                                                      agglomerative)

            self.agglomerative = agglomerative
            self.connectivity_ = connectivity
            # Avoid 0 label
            labels = labels + 1
            self.labels_img_ = self.masker_.inverse_transform(labels)
            return self

        # Avoid 0 label
        labels = labels + 1
        self.labels_img_ = self.masker_.inverse_transform(labels)

        return self