Skip to content

Commit

Permalink
changed requirement of scikit-learn-extra to optional (#6). And, chan…
Browse files Browse the repository at this point in the history
…des whiten default from whiten=True to whiten=arbitrary-variancefollowing latest scikit-learn updates RobustICA class.
  • Loading branch information
MiqG committed Sep 12, 2024
1 parent b85d61b commit 0cb24e4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ In brackets, versions of packages used to develop `robustica`.
- `pandas` (1.1.2)
- `scipy` (1.6.2)
- `scikit-learn` (0.23.2)
- `scikit-learn-extra` (0.2.0)
- `joblib` (1.0.1)
- `tqdm` (4.59.0)
- (optional) `scikit-learn-extra` (0.2.0): required only for clustering algorithms KMedoids and CommonNNClustering

## Installation
### [required if numpy>2.0] `scikit-learn-extra` incompatibility
Install a forked version first to avoid incompatibility with the newest `numpy` (see [#6](https://github.com/CRG-CNAG/robustica/issues/6) for more info on this).
### [optional] `scikit-learn-extra` incompatibility
To use the clustering algorithms KMedoids and CommonNNClustering, install a forked version first to avoid incompatibility with the newest `numpy` (see [#6](https://github.com/CRG-CNAG/robustica/issues/6) for more info on this).
```shell
pip install git+https://github.com/TimotheeMathieu/scikit-learn-extra
```
Expand Down
40 changes: 35 additions & 5 deletions robustica/RobustICA.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,36 @@
from sklearn.metrics import silhouette_samples
from joblib import Parallel, delayed
from tqdm import tqdm
from sklearn.cluster import *
from sklearn_extra.cluster import *
from sklearn.cluster import (
AgglomerativeClustering,
AffinityPropagation,
Birch,
DBSCAN,
FeatureAgglomeration,
KMeans,
MiniBatchKMeans,
MeanShift,
OPTICS,
SpectralClustering
)
import time

def optional_import_sklearn_extra(method):
if method in ["KMedoids", "CommonNNClustering"]:
try:
from sklearn_extra.cluster import KMedoids, CommonNNClustering
return locals()[method]
except ImportError:
raise ImportError(
f"To use {method}, you need to install 'scikit-learn-extra'. "
f"Install it using `pip install scikit-learn-extra`"
f"or `pip install git+https://github.com/TimotheeMathieu/scikit-learn-extra`"
f"if you have numpy>=2 installed."
)
else:
raise ValueError(f"Clustering method {method} is not available.")


def abs_pearson_dist(X):
"""Compute Pearson dissimilarity between columns.
Expand Down Expand Up @@ -172,7 +197,9 @@ class RobustICA:
whiten : bool, default=True
If whiten is false, the data is already considered to be
whitened, and no whitening is performed.
whitened, and no whitening is performed. **WARNING**: if
you use scikit-learn>1.3 defaults were set
whiten='arbitrary-variance' to maintain the behavior.
fun : {'logcosh', 'exp', 'cube'} or callable, default='logcosh'
The functional form of the G function used in the
Expand Down Expand Up @@ -316,7 +343,7 @@ def __init__(
self,
n_components=None,
algorithm="parallel",
whiten=True,
whiten="arbitrary-variance",
fun="logcosh",
fun_args=None,
max_iter=200,
Expand Down Expand Up @@ -430,7 +457,10 @@ def _prep_precompdist_func(self):

def _prep_clustering_class(self):
if isinstance(self.robust_method, str):
self.clustering_class = eval(self.robust_method)
if self.robust_method in ["KMedoids", "CommonNNClustering"]:
self.clustering_class = optional_import_sklearn_extra(self.robust_method)
else:
self.clustering_class = eval(self.robust_method)
else:
self.clustering_class = self.robust_method

Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
"pandas",
"scipy",
"scikit-learn",
"scikit-learn-extra",
"joblib",
"tqdm",
],
extras_require={
"extra": ["scikit-learn-extra"]
},
author="Miquel Anglada Girotto",
author_email="[email protected]",
description="Fully cumstomizable robust Independent Components Analysis (ICA)",
Expand Down

0 comments on commit 0cb24e4

Please sign in to comment.