From 70ac978224f8e985a4a3d68f394275b49856f2cc Mon Sep 17 00:00:00 2001 From: LeSasse Date: Tue, 6 Aug 2024 12:03:04 +0200 Subject: [PATCH 1/4] add basic spearman correlation implementation --- .../nilearn/junifer_connectivity_measure.py | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/junifer/external/nilearn/junifer_connectivity_measure.py b/junifer/external/nilearn/junifer_connectivity_measure.py index 70b4b6d27..f95d3f7cf 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 @@ -320,10 +320,12 @@ class JuniferConnectivityMeasure(ConnectivityMeasure): cov_estimator : estimator object, optional The covariance estimator (default ``EmpiricalCovariance(store_precision=False)``). - kind : {"covariance", "correlation", "partial correlation", \ - "tangent", "precision"}, optional + kind : {"covariance", "correlation", "spearman correlation", \ + "partial correlation", "tangent", "precision"}, optional The matrix kind. For the use of ``"tangent"`` see [1]_ - (default "correlation"). + (default "correlation", which will use Pearson's correlation). + If "spearman correlation" is used, the data will be ranked before + estimating the covariance. vectorize : bool, optional If True, connectivity matrices are reshaped into 1D arrays and only their flattened lower triangular parts are returned (default False). @@ -400,17 +402,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] -- 2.52.0 From 42d13c125e9f35d53e55da09618efecfce6a9986 Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Tue, 6 Aug 2024 12:44:01 +0200 Subject: [PATCH 2/4] chore: improve docstring for JuniferConnectivityMeasure --- junifer/external/nilearn/junifer_connectivity_measure.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/junifer/external/nilearn/junifer_connectivity_measure.py b/junifer/external/nilearn/junifer_connectivity_measure.py index f95d3f7cf..82ec8ba78 100644 --- a/junifer/external/nilearn/junifer_connectivity_measure.py +++ b/junifer/external/nilearn/junifer_connectivity_measure.py @@ -314,6 +314,7 @@ class JuniferConnectivityMeasure(ConnectivityMeasure): * default ``cov_estimator`` is :class:`sklearn.covariance.EmpiricalCovariance` * default ``kind`` is ``"correlation"`` + * supports Spearman's correlation via ``kind="spearman correlation"`` Parameters ---------- @@ -322,10 +323,10 @@ class JuniferConnectivityMeasure(ConnectivityMeasure): (default ``EmpiricalCovariance(store_precision=False)``). kind : {"covariance", "correlation", "spearman correlation", \ "partial correlation", "tangent", "precision"}, optional - The matrix kind. For the use of ``"tangent"`` see [1]_ - (default "correlation", which will use Pearson's correlation). - If "spearman correlation" is used, the data will be ranked before - estimating the covariance. + 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 their flattened lower triangular parts are returned (default False). -- 2.52.0 From 4022b9da51ef96f86bd8f1955087dbc6cd61c9cc Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Tue, 6 Aug 2024 12:44:32 +0200 Subject: [PATCH 3/4] chore: update tests for JuniferConnectivityMeasure --- .../external/nilearn/tests/test_junifer_connectivity_measure.py | 1 + 1 file changed, 1 insertion(+) 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 -- 2.52.0 From cc589c3ece7a6b49bd2fab6ac233740cba343a6a Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Tue, 6 Aug 2024 13:22:31 +0200 Subject: [PATCH 4/4] chore: add changelog 335.enh --- docs/changes/newsfragments/335.enh | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/changes/newsfragments/335.enh 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`_ -- 2.52.0