Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement code quality check #607

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
26 changes: 16 additions & 10 deletions .github/workflows/python-plain-run-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ on:
branches: [ master ]

jobs:
check-code:
name: Check code quality
runs-on: ubuntu-latest
steps:
- name: Checkout branch
uses: actions/checkout@v2
- name: Setup python
uses: actions/setup-python@v2
with:
python-version: '3.9'
architecture: x64
- name: Install dependencies
run: pip install isort black
- name: Check code quality
run: isort --profile black --check dynamo tests && black --check dynamo tests && echo OK
build:
runs-on: ubuntu-latest
strategy:
Expand All @@ -26,20 +41,11 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install git+https://github.com/pypa/pip.git@611e9253ff74ca5b8ce438c2423373ab0d44c961
pip install flake8 pytest
pip install pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Install package
run: |
pip install -e .[test]
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
# - name: Test with pytest
# run: |
# pytest
- name: Run tests and collect coverage
run: |
pip install pytest-cov
Expand Down
11 changes: 8 additions & 3 deletions dynamo/external/hodge.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from scipy.sparse import csr_matrix, issparse

from ..dynamo_logger import main_finish_progress, main_info, main_log_time

# from ..vectorfield.scVectorField import graphize_vecfld
from ..tools.graph_calculus import divergence, graphize_velocity, potential
from ..tools.sampling import sample_by_velocity, trn
Expand All @@ -21,7 +20,9 @@
div,
potential,
)"""
from ..tools.connectivity import check_and_recompute_neighbors, generate_neighbor_keys

from ..tools.connectivity import (generate_neighbor_keys,
check_and_recompute_neighbors)


def ddhodge(
Expand Down Expand Up @@ -218,7 +219,11 @@ def func(x):
W.dot(ddhodge_div),
W.dot(potential_),
)
(adata.obs[prefix + "ddhodge_sampled"], adata.obs[prefix + "ddhodge_div"], adata.obs[prefix + "potential"],) = (
(
adata.obs[prefix + "ddhodge_sampled"],
adata.obs[prefix + "ddhodge_div"],
adata.obs[prefix + "potential"],
) = (
False,
0,
0,
Expand Down
1 change: 0 additions & 1 deletion dynamo/prediction/fate.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,6 @@ def fate_bias(
while True:
is_dist_larger_than_threshold = distances.flatten() < dist_threshold * median_dist
if any(is_dist_larger_than_threshold):

# let us diffuse one step further to identify cells from terminal cell types in case
# cells with indices are all close to some random progenitor cells.
if hasattr(nbrs, "query"):
Expand Down
28 changes: 7 additions & 21 deletions dynamo/preprocessing/Preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,13 @@
from anndata import AnnData

from ..configuration import DKM
from ..dynamo_logger import (
LoggerManager,
main_debug,
main_info,
main_info_insert_adata,
main_warning,
)
from ..dynamo_logger import (LoggerManager, main_debug, main_info,
main_info_insert_adata, main_warning)
from ..tools.connectivity import neighbors as default_neighbors
from ..tools.utils import update_dict
from .cell_cycle import cell_cycle_scores
from .external import (
normalize_layers_pearson_residuals,
sctransform,
select_genes_by_pearson_residuals,
)
from .external import (normalize_layers_pearson_residuals, sctransform,
select_genes_by_pearson_residuals)
from .gene_selection import select_genes_by_seurat_recipe, select_genes_monocle
from .normalization import calc_sz_factor, normalize
from .pca import pca
Expand All @@ -32,15 +24,9 @@
from .QC import filter_genes_by_outliers as monocle_filter_genes_by_outliers
from .QC import regress_out_parallel
from .transform import Freeman_Tukey, log, log1p, log2
from .utils import (
_infer_labeling_experiment_type,
calc_new_to_total_ratio,
collapse_species_adata,
convert2symbol,
convert_layers2csr,
detect_experiment_datatype,
unique_var_obs_adata,
)
from .utils import (_infer_labeling_experiment_type, calc_new_to_total_ratio,
collapse_species_adata, convert2symbol, convert_layers2csr,
detect_experiment_datatype, unique_var_obs_adata)


class Preprocessor:
Expand Down
14 changes: 4 additions & 10 deletions dynamo/preprocessing/QC.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@
from scipy.sparse import issparse, spmatrix

from ..configuration import DKM
from ..dynamo_logger import (
LoggerManager,
main_debug,
main_exception,
main_finish_progress,
main_info,
main_info_insert_adata_obs,
main_log_time,
main_warning,
)
from ..dynamo_logger import (LoggerManager, main_debug, main_exception,
main_finish_progress, main_info,
main_info_insert_adata_obs, main_log_time,
main_warning)
from .utils import get_inrange_shared_counts_mask


Expand Down
29 changes: 8 additions & 21 deletions dynamo/preprocessing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,13 @@
"""

from .cell_cycle import cell_cycle_scores
from .deprecated import (
calc_sz_factor_legacy,
cook_dist,
filter_cells_legacy,
normalize_cell_expr_by_size_factors,
recipe_monocle,
recipe_velocyto,
)
from .deprecated import (calc_sz_factor_legacy, cook_dist, filter_cells_legacy,
normalize_cell_expr_by_size_factors, recipe_monocle,
recipe_velocyto)
from .dynast import lambda_correction
from .external import (
harmony_debatch,
integrate,
normalize_layers_pearson_residuals,
sctransform,
select_genes_by_pearson_residuals,
)
from .external import (harmony_debatch, integrate,
normalize_layers_pearson_residuals, sctransform,
select_genes_by_pearson_residuals)
from .normalization import calc_sz_factor, normalize
from .pca import pca, top_pca_genes
from .QC import (
Expand Down Expand Up @@ -45,12 +36,8 @@
normalize_cells = normalize

from .CnmfPreprocessor import CnmfPreprocessor
from .gene_selection import (
calc_dispersion_by_svr,
calc_Gini,
highest_frac_genes,
select_genes_monocle,
)
from .gene_selection import (calc_dispersion_by_svr, calc_Gini,
highest_frac_genes, select_genes_monocle)
from .Preprocessor import Preprocessor

__all__ = [
Expand Down
35 changes: 12 additions & 23 deletions dynamo/preprocessing/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,24 @@
from sklearn.decomposition import FastICA

from ..configuration import DKM, DynamoAdataConfig, DynamoAdataKeyManager
from ..dynamo_logger import (
LoggerManager,
main_debug,
main_info,
main_info_insert_adata_obsm,
main_warning,
)
from ..dynamo_logger import (LoggerManager, main_debug, main_info,
main_info_insert_adata_obsm, main_warning)
from ..tools.utils import update_dict
from ..utils import copy_adata
from .cell_cycle import cell_cycle_scores
from .gene_selection import calc_dispersion_by_svr
from .normalization import calc_sz_factor, get_sz_exprs, normalize_mat_monocle, sz_util
from .normalization import (calc_sz_factor, get_sz_exprs,
normalize_mat_monocle, sz_util)
from .pca import pca
from .QC import basic_stats, filter_genes_by_clusters, filter_genes_by_outliers
from .transform import _Freeman_Tukey
from .utils import (
_infer_labeling_experiment_type,
add_noise_to_duplicates,
calc_new_to_total_ratio,
collapse_species_adata,
compute_gene_exp_fraction,
convert2symbol,
convert_layers2csr,
detect_experiment_datatype,
get_inrange_shared_counts_mask,
get_nan_or_inf_data_bool_mask,
get_svr_filter,
merge_adata_attrs,
unique_var_obs_adata,
)
from .utils import (_infer_labeling_experiment_type, add_noise_to_duplicates,
calc_new_to_total_ratio, collapse_species_adata,
compute_gene_exp_fraction, convert2symbol,
convert_layers2csr, detect_experiment_datatype,
get_inrange_shared_counts_mask,
get_nan_or_inf_data_bool_mask, get_svr_filter,
merge_adata_attrs, unique_var_obs_adata)


def deprecated(func):
Expand Down
6 changes: 2 additions & 4 deletions dynamo/preprocessing/external/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from .integration import harmony_debatch, integrate
from .pearson_residual_recipe import (
normalize_layers_pearson_residuals,
select_genes_by_pearson_residuals,
)
from .pearson_residual_recipe import (normalize_layers_pearson_residuals,
select_genes_by_pearson_residuals)
from .sctransform import sctransform

__all__ = [
Expand Down
12 changes: 4 additions & 8 deletions dynamo/preprocessing/external/pearson_residual_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
from scipy.sparse import issparse

from ...configuration import DKM
from ...dynamo_logger import (
LoggerManager,
main_info,
main_info_insert_adata_layer,
main_warning,
)
from ...preprocessing.utils import is_nonnegative_integer_arr, seurat_get_mean_var
from ...dynamo_logger import (LoggerManager, main_info,
main_info_insert_adata_layer, main_warning)
from ...preprocessing.utils import (is_nonnegative_integer_arr,
seurat_get_mean_var)
from ..QC import filter_genes_by_outliers

main_logger = LoggerManager.main_logger
Expand Down Expand Up @@ -109,7 +106,6 @@ def _highly_variable_pearson_residuals(
# Get pearson residuals for each batch separately
residual_gene_vars_by_batch = []
for batch in np.unique(batch_info):

adata_subset = adata[batch_info == batch]

# Filter out zero genes
Expand Down
3 changes: 2 additions & 1 deletion dynamo/preprocessing/external/sctransform.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from scipy import stats

from ...configuration import DKM
from ...dynamo_logger import main_info, main_info_insert_adata_layer, main_warning
from ...dynamo_logger import (main_info, main_info_insert_adata_layer,
main_warning)
from ..utils import get_gene_selection_filter

_EPS = np.finfo(float).eps
Expand Down
29 changes: 11 additions & 18 deletions dynamo/preprocessing/gene_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,13 @@
from scipy.sparse import csr_matrix, issparse

from ..configuration import DKM
from ..dynamo_logger import (
LoggerManager,
main_critical,
main_debug,
main_info,
main_info_insert_adata_uns,
main_info_insert_adata_var,
main_warning,
)
from ..dynamo_logger import (LoggerManager, main_critical, main_debug,
main_info, main_info_insert_adata_uns,
main_info_insert_adata_var, main_warning)
from .pca import pca
from .utils import (
compute_gene_exp_fraction,
get_gene_selection_filter,
get_nan_or_inf_data_bool_mask,
get_svr_filter,
merge_adata_attrs,
seurat_get_mean_var,
)
from .utils import (compute_gene_exp_fraction, get_gene_selection_filter,
get_nan_or_inf_data_bool_mask, get_svr_filter,
merge_adata_attrs, seurat_get_mean_var)


def calc_Gini(adata: AnnData, layers: Union[Literal["all"], List[str]] = "all") -> AnnData:
Expand Down Expand Up @@ -247,7 +236,11 @@ def calc_dispersion_by_svr(
adata_ori.uns[key] = {"mean": mean, "cv": cv, "svr_gamma": svr_gamma}

prefix = "" if layer == "X" else layer + "_"
(adata.var[prefix + "log_m"], adata.var[prefix + "log_cv"], adata.var[prefix + "score"],) = (
(
adata.var[prefix + "log_m"],
adata.var[prefix + "log_cv"],
adata.var[prefix + "score"],
) = (
np.nan,
np.nan,
-np.inf,
Expand Down
8 changes: 2 additions & 6 deletions dynamo/preprocessing/normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@
from scipy.sparse import issparse

from ..configuration import DKM
from ..dynamo_logger import (
main_debug,
main_info_insert_adata_layer,
main_info_insert_adata_obsm,
main_warning,
)
from ..dynamo_logger import (main_debug, main_info_insert_adata_layer,
main_info_insert_adata_obsm, main_warning)
from .utils import merge_adata_attrs


Expand Down
3 changes: 2 additions & 1 deletion dynamo/preprocessing/pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
from sklearn.utils.sparsefuncs import mean_variance_axis

from ..configuration import DKM
from ..dynamo_logger import main_info_insert_adata_obsm, main_info_insert_adata_var
from ..dynamo_logger import (main_info_insert_adata_obsm,
main_info_insert_adata_var)


def _truncatedSVD_with_center(
Expand Down
30 changes: 8 additions & 22 deletions dynamo/simulation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
from .evaluation import evaluate
from .Gillespie import Gillespie
from .ODE import (
Simulator,
Ying_model,
jacobian_bifur2genes,
neurongenesis,
ode_bifur2genes,
state_space_sampler,
toggle,
)
from .simulate_anndata import (
AnnDataSimulator,
BifurcationTwoGenes,
CellularModelSimulator,
KinLabelingSimulator,
Neurongenesis,
OscillationTwoGenes,
bifur2genes_params,
bifur2genes_splicing_params,
neurongenesis_params,
osc2genes_params,
osc2genes_splicing_params,
)
from .ODE import (Simulator, Ying_model, jacobian_bifur2genes, neurongenesis,
ode_bifur2genes, state_space_sampler, toggle)
from .simulate_anndata import (AnnDataSimulator, BifurcationTwoGenes,
CellularModelSimulator, KinLabelingSimulator,
Neurongenesis, OscillationTwoGenes,
bifur2genes_params, bifur2genes_splicing_params,
neurongenesis_params, osc2genes_params,
osc2genes_splicing_params)
from .utils import CellularSpecies, directMethod
Loading
Loading