From 9b82aebdc909d806d685b4d31fbadcd44aed713c Mon Sep 17 00:00:00 2001 From: Andrew Robbins Date: Wed, 31 Jan 2024 09:49:26 -0500 Subject: [PATCH] Convert submodules to namespace packages improves import performance and circular dependency issues --- src/pyliger/__init__.py | 34 ++++++++++--------- src/pyliger/clustering/_leiden.py | 7 +++- src/pyliger/clustering/_louvain.py | 7 +++- .../clustering/{__init__.py => todo.py} | 4 --- src/pyliger/factorization/_iNMF_ANLS.py | 2 +- src/pyliger/factorization/_iNMF_HALS.py | 2 +- src/pyliger/factorization/_online_iNMF.py | 10 +++--- src/pyliger/factorization/_utilities.py | 2 +- .../factorization/{__init__.py => todo.py} | 3 -- src/pyliger/plotting/_gene.py | 2 +- src/pyliger/plotting/_gene_loadings.py | 3 +- src/pyliger/plotting/_spatial.py | 2 +- src/pyliger/plotting/{__init__.py => todo.py} | 7 ---- src/pyliger/preprocessing/__init__.py | 6 ---- src/pyliger/preprocessing/_initialization.py | 4 +-- src/pyliger/preprocessing/_normalization.py | 4 +-- src/pyliger/preprocessing/_scale.py | 2 +- src/pyliger/preprocessing/_select_genes.py | 2 +- src/pyliger/tools/_marker.py | 4 +-- src/pyliger/tools/{__init__.py => todo.py} | 7 ---- 20 files changed, 50 insertions(+), 64 deletions(-) rename src/pyliger/clustering/{__init__.py => todo.py} (95%) rename src/pyliger/factorization/{__init__.py => todo.py} (93%) rename src/pyliger/plotting/{__init__.py => todo.py} (89%) delete mode 100644 src/pyliger/preprocessing/__init__.py rename src/pyliger/tools/{__init__.py => todo.py} (83%) diff --git a/src/pyliger/__init__.py b/src/pyliger/__init__.py index 9792f0c..b7f99ec 100644 --- a/src/pyliger/__init__.py +++ b/src/pyliger/__init__.py @@ -1,17 +1,19 @@ -from .clustering import leiden_cluster, louvain_cluster -from .factorization import iNMF_HALS, online_iNMF, optimize_ALS -from .plotting import ( - plot_by_dataset_and_cluster, - plot_gene, - plot_gene_loadings, - plot_spatial, -) -from .preprocessing import ( - create_liger, - make_feature_matrix, - normalize, - scale_not_center, - select_genes, -) +from .clustering._leiden import leiden_cluster +from .clustering._louvain import louvain_cluster +from .factorization._iNMF_HALS import iNMF_HALS +from .factorization._online_iNMF import online_iNMF +from .factorization._iNMF_ANLS import optimize_ALS +from .plotting._dataset_cluster import plot_by_dataset_and_cluster +from .plotting._gene import plot_gene +from .plotting._gene_loadings import plot_gene_loadings +from .plotting._spatial import plot_spatial +from .preprocessing._initialization import create_liger +from .preprocessing._select_genes import select_genes +from .preprocessing._normalization import normalize +from .preprocessing._scale import scale_not_center +from .preprocessing._cal_feature import make_feature_matrix from .read_write import read_10X, read_10X_h5, read_10X_visium -from .tools import quantile_norm, run_umap, run_wilcoxon +from .tools._quantile_norm import quantile_norm +from .tools._umap import run_umap +from .tools._wilcoxon import run_wilcoxon +from .pyliger import Liger diff --git a/src/pyliger/clustering/_leiden.py b/src/pyliger/clustering/_leiden.py index 4685b5f..add7cbb 100644 --- a/src/pyliger/clustering/_leiden.py +++ b/src/pyliger/clustering/_leiden.py @@ -1,7 +1,12 @@ import leidenalg import numpy as np -from ._utilities import _assign_cluster, build_igraph, compute_snn, run_knn +from pyliger.clustering._utilities import ( + _assign_cluster, + build_igraph, + compute_snn, + run_knn, +) def leiden_cluster( diff --git a/src/pyliger/clustering/_louvain.py b/src/pyliger/clustering/_louvain.py index 87798c3..e5cb9c5 100644 --- a/src/pyliger/clustering/_louvain.py +++ b/src/pyliger/clustering/_louvain.py @@ -1,7 +1,12 @@ import louvain import numpy as np -from ._utilities import _assign_cluster, build_igraph, compute_snn, run_knn +from pyliger.clustering._utilities import ( + _assign_cluster, + build_igraph, + compute_snn, + run_knn, +) def louvain_cluster( diff --git a/src/pyliger/clustering/__init__.py b/src/pyliger/clustering/todo.py similarity index 95% rename from src/pyliger/clustering/__init__.py rename to src/pyliger/clustering/todo.py index 5e579c5..9406a3e 100644 --- a/src/pyliger/clustering/__init__.py +++ b/src/pyliger/clustering/todo.py @@ -1,7 +1,3 @@ -from ._leiden import leiden_cluster -from ._louvain import louvain_cluster - - #####TODO def imputeKNN( liger_object, reference, queries, knn_k=20, weight=True, norm=True, scale=False diff --git a/src/pyliger/factorization/_iNMF_ANLS.py b/src/pyliger/factorization/_iNMF_ANLS.py index b4d77c8..78a90a5 100644 --- a/src/pyliger/factorization/_iNMF_ANLS.py +++ b/src/pyliger/factorization/_iNMF_ANLS.py @@ -1,7 +1,7 @@ import numpy as np from tqdm import tqdm -from ._utilities import nnlsm_blockpivot +from pyliger.factorization._utilities import nnlsm_blockpivot def optimize_ALS( diff --git a/src/pyliger/factorization/_iNMF_HALS.py b/src/pyliger/factorization/_iNMF_HALS.py index 113d5cb..9139abc 100644 --- a/src/pyliger/factorization/_iNMF_HALS.py +++ b/src/pyliger/factorization/_iNMF_HALS.py @@ -2,7 +2,7 @@ import numpy as np -from ._utilities import ( +from pyliger.factorization._utilities import ( _init_H, _init_V, _init_W, diff --git a/src/pyliger/factorization/_online_iNMF.py b/src/pyliger/factorization/_online_iNMF.py index 1dfafb6..b72b47d 100644 --- a/src/pyliger/factorization/_online_iNMF.py +++ b/src/pyliger/factorization/_online_iNMF.py @@ -3,11 +3,11 @@ from scipy.sparse import vstack from tqdm import tqdm -from .._utilities import _h5_idx_generator -from ..preprocessing._initialization import _initialization_online -from ..preprocessing._normalization import _normalize_online -from ..preprocessing._scale import _scale_online -from ._utilities import ( +from pyliger._utilities import _h5_idx_generator +from pyliger.preprocessing._initialization import _initialization_online +from pyliger.preprocessing._normalization import _normalize_online +from pyliger.preprocessing._scale import _scale_online +from pyliger.factorization._utilities import ( _init_V_online, _init_W, _update_V_HALS, diff --git a/src/pyliger/factorization/_utilities.py b/src/pyliger/factorization/_utilities.py index a2b22a5..5c66b73 100644 --- a/src/pyliger/factorization/_utilities.py +++ b/src/pyliger/factorization/_utilities.py @@ -3,7 +3,7 @@ import scipy.linalg as sla import scipy.sparse as sps -from .._utilities import _h5_idx_generator +from pyliger._utilities import _h5_idx_generator def nonneg(x, eps=1e-16): diff --git a/src/pyliger/factorization/__init__.py b/src/pyliger/factorization/todo.py similarity index 93% rename from src/pyliger/factorization/__init__.py rename to src/pyliger/factorization/todo.py index bbd58bb..9fa0c05 100644 --- a/src/pyliger/factorization/__init__.py +++ b/src/pyliger/factorization/todo.py @@ -1,6 +1,3 @@ -from ._iNMF_ANLS import optimize_ALS -from ._iNMF_HALS import iNMF_HALS -from ._online_iNMF import online_iNMF ####################################################################################### #### Factorization TODO diff --git a/src/pyliger/plotting/_gene.py b/src/pyliger/plotting/_gene.py index e5405f7..277307b 100644 --- a/src/pyliger/plotting/_gene.py +++ b/src/pyliger/plotting/_gene.py @@ -21,7 +21,7 @@ ) from sklearn.preprocessing import scale -from ._utilities import get_gene_values +from pyliger.plotting._utilities import get_gene_values def plot_gene( diff --git a/src/pyliger/plotting/_gene_loadings.py b/src/pyliger/plotting/_gene_loadings.py index 6c6a2bc..3c7e364 100644 --- a/src/pyliger/plotting/_gene_loadings.py +++ b/src/pyliger/plotting/_gene_loadings.py @@ -18,7 +18,8 @@ ) from tqdm import tqdm -from ..tools import calc_dataset_specificity, get_factor_markers +from pyliger.tools._marker import get_factor_markers +from pyliger.tools._metrics import calc_dataset_specificity def plot_gene_loadings( diff --git a/src/pyliger/plotting/_spatial.py b/src/pyliger/plotting/_spatial.py index d9fd760..cbf88f8 100644 --- a/src/pyliger/plotting/_spatial.py +++ b/src/pyliger/plotting/_spatial.py @@ -13,7 +13,7 @@ ylim, ) -from ._utilities import get_gene_values +from pyliger.plotting._utilities import get_gene_values def plot_spatial( diff --git a/src/pyliger/plotting/__init__.py b/src/pyliger/plotting/todo.py similarity index 89% rename from src/pyliger/plotting/__init__.py rename to src/pyliger/plotting/todo.py index fe1e712..9fdf170 100644 --- a/src/pyliger/plotting/__init__.py +++ b/src/pyliger/plotting/todo.py @@ -1,11 +1,4 @@ # from ._qc import plot_qc -import numpy as np - -from ._dataset_cluster import plot_by_dataset_and_cluster -from ._gene import plot_gene -from ._gene_loadings import plot_gene_loadings -from ._go import plot_go_term -from ._spatial import plot_spatial ###############TODO diff --git a/src/pyliger/preprocessing/__init__.py b/src/pyliger/preprocessing/__init__.py deleted file mode 100644 index 6adeb09..0000000 --- a/src/pyliger/preprocessing/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -from ._cal_feature import make_feature_matrix -from ._initialization import create_liger -from ._normalization import normalize -from ._qc import calculate_mt_pct, filtering -from ._scale import scale_not_center -from ._select_genes import select_genes diff --git a/src/pyliger/preprocessing/_initialization.py b/src/pyliger/preprocessing/_initialization.py index 8b30812..8f52eb0 100644 --- a/src/pyliger/preprocessing/_initialization.py +++ b/src/pyliger/preprocessing/_initialization.py @@ -4,8 +4,8 @@ import numpy as np from scipy.sparse import csr_matrix -from .._utilities import _h5_idx_generator, _merge_sparse_data_all, _remove_missing_obs -from ..pyliger import Liger +from pyliger._utilities import _h5_idx_generator, _merge_sparse_data_all, _remove_missing_obs +from pyliger.pyliger import Liger def create_liger( diff --git a/src/pyliger/preprocessing/_normalization.py b/src/pyliger/preprocessing/_normalization.py index e9a1e43..dbc84df 100644 --- a/src/pyliger/preprocessing/_normalization.py +++ b/src/pyliger/preprocessing/_normalization.py @@ -5,8 +5,8 @@ import numpy as np from sklearn.preprocessing import normalize as sp_normalize -from .._utilities import _h5_idx_generator, _remove_missing_obs -from ..pyliger import Liger +from pyliger._utilities import _h5_idx_generator, _remove_missing_obs +from pyliger.pyliger import Liger PARRENT_PATH = Path(__file__).parent diff --git a/src/pyliger/preprocessing/_scale.py b/src/pyliger/preprocessing/_scale.py index f5dac1c..f828b6f 100644 --- a/src/pyliger/preprocessing/_scale.py +++ b/src/pyliger/preprocessing/_scale.py @@ -5,7 +5,7 @@ from scipy.sparse import csr_matrix from sklearn.utils.sparsefuncs import inplace_column_scale -from .._utilities import _h5_idx_generator +from pyliger._utilities import _h5_idx_generator def scale_not_center( diff --git a/src/pyliger/preprocessing/_select_genes.py b/src/pyliger/preprocessing/_select_genes.py index 59e2283..1a91f4c 100644 --- a/src/pyliger/preprocessing/_select_genes.py +++ b/src/pyliger/preprocessing/_select_genes.py @@ -8,7 +8,7 @@ from scipy.stats import norm from sklearn.utils.sparsefuncs import mean_variance_axis -from .._utilities import _h5_idx_generator +from pyliger._utilities import _h5_idx_generator def select_genes( diff --git a/src/pyliger/tools/_marker.py b/src/pyliger/tools/_marker.py index 77730f0..4eabf43 100644 --- a/src/pyliger/tools/_marker.py +++ b/src/pyliger/tools/_marker.py @@ -3,8 +3,8 @@ from scipy.sparse import vstack from sklearn.preprocessing import scale -from ..tools import _wilcoxon, calc_dataset_specificity - +from pyliger.tools import _wilcoxon +from pyliger.tools._metrics import calc_dataset_specificity # Find shared and dataset-specific markers def get_factor_markers( diff --git a/src/pyliger/tools/__init__.py b/src/pyliger/tools/todo.py similarity index 83% rename from src/pyliger/tools/__init__.py rename to src/pyliger/tools/todo.py index ed4947a..7fb3fd9 100644 --- a/src/pyliger/tools/__init__.py +++ b/src/pyliger/tools/todo.py @@ -1,10 +1,3 @@ -from ._go import run_GO_analysis -from ._marker import get_factor_markers -from ._metrics import calc_dataset_specificity -from ._quantile_norm import quantile_norm -from ._umap import run_umap -from ._wilcoxon import _wilcoxon, run_wilcoxon - ####################################################################################### #### Conversion/Transformation TODO