diff --git a/docs/changes/newsfragments/335.enh b/docs/changes/newsfragments/335.enh new file mode 100644 index 000000000..7c9eae8f1 --- /dev/null +++ b/docs/changes/newsfragments/335.enh @@ -0,0 +1 @@ +Add support for Spearman's correlation in :class:`.JuniferConnectivityMeasure` by `Leonard Sasse`_ diff --git a/junifer/external/nilearn/junifer_connectivity_measure.py b/junifer/external/nilearn/junifer_connectivity_measure.py index 70b4b6d27..82ec8ba78 100644 --- a/junifer/external/nilearn/junifer_connectivity_measure.py +++ b/junifer/external/nilearn/junifer_connectivity_measure.py @@ -13,7 +13,7 @@ from nilearn.connectome import ( prec_to_partial, sym_matrix_to_vec, ) -from scipy import linalg +from scipy import linalg, stats from sklearn.base import clone from sklearn.covariance import EmpiricalCovariance @@ -314,15 +314,18 @@ class JuniferConnectivityMeasure(ConnectivityMeasure): * default ``cov_estimator`` is :class:`sklearn.covariance.EmpiricalCovariance` * default ``kind`` is ``"correlation"`` + * supports Spearman's correlation via ``kind="spearman correlation"`` Parameters ---------- cov_estimator : estimator object, optional The covariance estimator (default ``EmpiricalCovariance(store_precision=False)``). - kind : {"covariance", "correlation", "partial correlation", \ - "tangent", "precision"}, optional - The matrix kind. For the use of ``"tangent"`` see [1]_ + kind : {"covariance", "correlation", "spearman correlation", \ + "partial correlation", "tangent", "precision"}, optional + The matrix kind. The default value uses Pearson's correlation. + If ``"spearman correlation"`` is used, the data will be ranked before + estimating the covariance. For the use of ``"tangent"`` see [1]_ (default "correlation"). vectorize : bool, optional If True, connectivity matrices are reshaped into 1D arrays and only @@ -400,17 +403,22 @@ class JuniferConnectivityMeasure(ConnectivityMeasure): self.cov_estimator_ = clone(self.cov_estimator) # Compute all the matrices, stored in "connectivities" - if self.kind == "correlation": - covariances_std = [ - self.cov_estimator_.fit( - signal.standardize_signal( - x, - detrend=False, - standardize=self.standardize, - ) - ).covariance_ - for x in X - ] + if self.kind in ["correlation", "spearman correlation"]: + covariances_std = [] + for x in X: + x = signal.standardize_signal( + x, + detrend=False, + standardize=self.standardize, + ) + + # rank data if spearman correlation + # before calculating covariance + if self.kind == "spearman correlation": + x = stats.rankdata(x, axis=0) + + covariances_std.append(self.cov_estimator_.fit(x).covariance_) + connectivities = [cov_to_corr(cov) for cov in covariances_std] else: covariances = [self.cov_estimator_.fit(x).covariance_ for x in X] diff --git a/junifer/external/nilearn/tests/test_junifer_connectivity_measure.py b/junifer/external/nilearn/tests/test_junifer_connectivity_measure.py index 14dc8a07c..1f03b8d43 100644 --- a/junifer/external/nilearn/tests/test_junifer_connectivity_measure.py +++ b/junifer/external/nilearn/tests/test_junifer_connectivity_measure.py @@ -71,6 +71,7 @@ CONNECTIVITY_KINDS = ( "tangent", "precision", "partial correlation", + "spearman correlation", ) N_FEATURES = 49