From a30833133cb9d53b45cc362eb9476ed7384777ef Mon Sep 17 00:00:00 2001 From: ashish615 Date: Mon, 6 May 2024 10:42:47 +0000 Subject: [PATCH 1/3] scikit-learn-intelex library added --- pyproject.toml | 1 + scanpy/preprocessing/__init__.py | 4 ++++ scanpy/tools/__init__.py | 4 +++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 0097b33aaa..790e9ff20b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -57,6 +57,7 @@ dependencies = [ "h5py>=3.1", "tqdm", "scikit-learn>=0.24", + "scikit-learn-intelex>=2024.3.0", "statsmodels>=0.13", "patsy", "networkx>=2.7", diff --git a/scanpy/preprocessing/__init__.py b/scanpy/preprocessing/__init__.py index 8c396d8640..a3532dabd2 100644 --- a/scanpy/preprocessing/__init__.py +++ b/scanpy/preprocessing/__init__.py @@ -21,6 +21,10 @@ subsample, ) +# Turn on scikit-learn optimizations with these 2 simple lines: +from sklearnex import patch_sklearn +patch_sklearn() + __all__ = [ "neighbors", "combat", diff --git a/scanpy/tools/__init__.py b/scanpy/tools/__init__.py index 9ca74faf81..37beda51dd 100644 --- a/scanpy/tools/__init__.py +++ b/scanpy/tools/__init__.py @@ -25,7 +25,9 @@ from ._sim import sim from ._tsne import tsne from ._umap import umap - +# Turn on scikit-learn optimizations with these 2 simple lines: +from sklearnex import patch_sklearn +patch_sklearn() def __getattr__(name: str) -> Any: if name == "pca": From 23d4f0c9477e70b7fbf74e014dbf3e1f5355217b Mon Sep 17 00:00:00 2001 From: ashish615 Date: Tue, 14 May 2024 14:26:07 +0000 Subject: [PATCH 2/3] adding sklearanex patch for tsne --- scanpy/preprocessing/__init__.py | 3 --- scanpy/tools/__init__.py | 3 --- scanpy/tools/_tsne.py | 8 ++++++-- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/scanpy/preprocessing/__init__.py b/scanpy/preprocessing/__init__.py index a3532dabd2..a7b496b7b3 100644 --- a/scanpy/preprocessing/__init__.py +++ b/scanpy/preprocessing/__init__.py @@ -21,9 +21,6 @@ subsample, ) -# Turn on scikit-learn optimizations with these 2 simple lines: -from sklearnex import patch_sklearn -patch_sklearn() __all__ = [ "neighbors", diff --git a/scanpy/tools/__init__.py b/scanpy/tools/__init__.py index 37beda51dd..41f4efb354 100644 --- a/scanpy/tools/__init__.py +++ b/scanpy/tools/__init__.py @@ -25,9 +25,6 @@ from ._sim import sim from ._tsne import tsne from ._umap import umap -# Turn on scikit-learn optimizations with these 2 simple lines: -from sklearnex import patch_sklearn -patch_sklearn() def __getattr__(name: str) -> Any: if name == "pca": diff --git a/scanpy/tools/_tsne.py b/scanpy/tools/_tsne.py index 6c2e1d57dd..dff6bbd327 100644 --- a/scanpy/tools/_tsne.py +++ b/scanpy/tools/_tsne.py @@ -141,8 +141,9 @@ def tsne( # deal with different tSNE implementations if use_fast_tsne: try: + from sklearnex import patch_sklearn,unpatch_sklearn + patch_sklearn() from MulticoreTSNE import MulticoreTSNE as TSNE - tsne = TSNE(**params_sklearn) logg.info(" using the 'MulticoreTSNE' package by Ulyanov (2017)") # need to transform to float64 for MulticoreTSNE... @@ -154,7 +155,10 @@ def tsne( "Could not import 'MulticoreTSNE'. Falling back to scikit-learn." ) ) + unpatch_sklearn() if use_fast_tsne is False: # In case MultiCore failed to import + from sklearnex import patch_sklearn,unpatch_sklearn + patch_sklearn() from sklearn.manifold import TSNE # unfortunately, sklearn does not allow to set a minimum number @@ -162,7 +166,7 @@ def tsne( tsne = TSNE(**params_sklearn) logg.info(" using sklearn.manifold.TSNE") X_tsne = tsne.fit_transform(X) - + unpatch_sklearn() # update AnnData instance adata.obsm["X_tsne"] = X_tsne # annotate samples with tSNE coordinates adata.uns["tsne"] = { From d8d47636cd2c1025733e5c85f3a4eb9fcf4da6b4 Mon Sep 17 00:00:00 2001 From: ashish615 Date: Tue, 21 May 2024 05:16:00 -0400 Subject: [PATCH 3/3] intel optimization applied on only skelearn TSNE --- scanpy/preprocessing/__init__.py | 1 - scanpy/tools/_tsne.py | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/scanpy/preprocessing/__init__.py b/scanpy/preprocessing/__init__.py index a7b496b7b3..8c396d8640 100644 --- a/scanpy/preprocessing/__init__.py +++ b/scanpy/preprocessing/__init__.py @@ -21,7 +21,6 @@ subsample, ) - __all__ = [ "neighbors", "combat", diff --git a/scanpy/tools/_tsne.py b/scanpy/tools/_tsne.py index de5c634fdb..6651f57b84 100644 --- a/scanpy/tools/_tsne.py +++ b/scanpy/tools/_tsne.py @@ -142,9 +142,8 @@ def tsne( # deal with different tSNE implementations if use_fast_tsne: try: - from sklearnex import patch_sklearn,unpatch_sklearn - patch_sklearn() from MulticoreTSNE import MulticoreTSNE as TSNE + tsne = TSNE(**params_sklearn) logg.info(" using the 'MulticoreTSNE' package by Ulyanov (2017)") # need to transform to float64 for MulticoreTSNE... @@ -156,7 +155,6 @@ def tsne( "Could not import 'MulticoreTSNE'. Falling back to scikit-learn." ) ) - unpatch_sklearn() if use_fast_tsne is False: # In case MultiCore failed to import from sklearnex import patch_sklearn,unpatch_sklearn patch_sklearn()