Skip to content

Commit

Permalink
Update ruff with annotation (#140)
Browse files Browse the repository at this point in the history
* adds mask to pca

* added mask to scaling

* fixes scale

* adds test for clip

* better support for csc

* solves memory issue with masks

* updates inplace behavior

* adds type checking

* add _empty

* add release note

* return to scanpy Empty

* add Empty to ignore

* init ruff update

* update annotation

* add annotation

* add annotation

* updated notebooks

* updated decoupler for ruff

* fix autocorr

* updated decoupler

* added release note
  • Loading branch information
Intron7 committed Mar 19, 2024
1 parent 1a3a04f commit 2945ce6
Show file tree
Hide file tree
Showing 51 changed files with 442 additions and 281 deletions.
11 changes: 9 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ repos:
rev: v0.3.3
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format
types_or: [python, pyi, jupyter]
args: ["--fix"]
- id: ruff-format
types_or: [python, pyi, jupyter]
# The following can be removed once PLR0917 is out of preview
- name: ruff preview rules
id: ruff
types_or: [python, pyi, jupyter]
args: ["--preview", "--select=PLR0917"]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
Expand Down
30 changes: 8 additions & 22 deletions docs/notebooks/autocorr_benchmark.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"outputs": [],
"source": [
"import rapids_singlecell as rsc\n",
"import scanpy as sc\n",
"import squidpy as sq\n",
"import cupy as cp"
]
Expand All @@ -37,10 +36,11 @@
"source": [
"import rmm\n",
"from rmm.allocators.cupy import rmm_cupy_allocator\n",
"\n",
"rmm.reinitialize(\n",
" managed_memory=False, # Allows oversubscription\n",
" pool_allocator=False, # default is False\n",
" devices=0, # GPU device IDs to register. By default registers only GPU 0.\n",
" managed_memory=False, # Allows oversubscription\n",
" pool_allocator=False, # default is False\n",
" devices=0, # GPU device IDs to register. By default registers only GPU 0.\n",
")\n",
"cp.cuda.set_allocator(rmm_cupy_allocator)"
]
Expand Down Expand Up @@ -133,11 +133,7 @@
],
"source": [
"%%timeit\n",
"rsc.gr.spatial_autocorr(adata,\n",
" mode=\"moran\",\n",
" genes=genes,\n",
" n_perms=100,\n",
" use_sparse=False)"
"rsc.gr.spatial_autocorr(adata, mode=\"moran\", genes=genes, n_perms=100, use_sparse=False)"
]
},
{
Expand Down Expand Up @@ -396,10 +392,7 @@
],
"source": [
"%%time\n",
"sq.gr.spatial_autocorr(adata,\n",
" mode=\"moran\",\n",
" genes=genes,\n",
" n_perms=100,n_jobs=32)"
"sq.gr.spatial_autocorr(adata, mode=\"moran\", genes=genes, n_perms=100, n_jobs=32)"
]
},
{
Expand Down Expand Up @@ -655,11 +648,7 @@
],
"source": [
"%%timeit\n",
"rsc.gr.spatial_autocorr(adata,\n",
" mode=\"geary\",\n",
" genes=genes,\n",
" n_perms=100,\n",
" use_sparse=False)"
"rsc.gr.spatial_autocorr(adata, mode=\"geary\", genes=genes, n_perms=100, use_sparse=False)"
]
},
{
Expand Down Expand Up @@ -918,10 +907,7 @@
],
"source": [
"%%time\n",
"sq.gr.spatial_autocorr(adata,\n",
" mode=\"geary\",\n",
" genes=genes,\n",
" n_perms=100, n_jobs=32)"
"sq.gr.spatial_autocorr(adata, mode=\"geary\", genes=genes, n_perms=100, n_jobs=32)"
]
},
{
Expand Down
59 changes: 32 additions & 27 deletions docs/notebooks/demo_gpu-PR.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
"outputs": [],
"source": [
"import scanpy as sc\n",
"import anndata\n",
"import cupy as cp\n",
"\n",
"import time\n",
"import rapids_singlecell as rsc\n",
"\n",
"import warnings\n",
"\n",
"warnings.filterwarnings(\"ignore\")"
]
},
Expand All @@ -46,12 +46,13 @@
"source": [
"import rmm\n",
"from rmm.allocators.cupy import rmm_cupy_allocator\n",
"\n",
"rmm.reinitialize(\n",
" managed_memory=False, # Allows oversubscription\n",
" pool_allocator=False, # default is False\n",
" devices=0, # GPU device IDs to register. By default registers only GPU 0.\n",
" managed_memory=False, # Allows oversubscription\n",
" pool_allocator=False, # default is False\n",
" devices=0, # GPU device IDs to register. By default registers only GPU 0.\n",
")\n",
"cp.cuda.set_allocator(rmm_cupy_allocator)\n"
"cp.cuda.set_allocator(rmm_cupy_allocator)"
]
},
{
Expand Down Expand Up @@ -176,7 +177,7 @@
],
"source": [
"data_load_time = time.time()\n",
"print(\"Total data load and format time: %s\" % (data_load_time-data_load_start))"
"print(\"Total data load and format time: %s\" % (data_load_time - data_load_start))"
]
},
{
Expand Down Expand Up @@ -230,7 +231,7 @@
],
"source": [
"%%time\n",
"rsc.pp.flag_gene_family(adata,gene_family_name=\"MT\", gene_family_prefix=\"mt-\")"
"rsc.pp.flag_gene_family(adata, gene_family_name=\"MT\", gene_family_prefix=\"mt-\")"
]
},
{
Expand All @@ -250,7 +251,7 @@
],
"source": [
"%%time\n",
"rsc.pp.calculate_qc_metrics(adata,qc_vars=[\"MT\"])"
"rsc.pp.calculate_qc_metrics(adata, qc_vars=[\"MT\"])"
]
},
{
Expand Down Expand Up @@ -281,8 +282,8 @@
}
],
"source": [
"sc.pl.scatter(adata,\"total_counts\",\"pct_counts_MT\")\n",
"sc.pl.scatter(adata,\"total_counts\",\"n_genes_by_counts\")"
"sc.pl.scatter(adata, \"total_counts\", \"pct_counts_MT\")\n",
"sc.pl.scatter(adata, \"total_counts\", \"n_genes_by_counts\")"
]
},
{
Expand Down Expand Up @@ -323,9 +324,9 @@
}
],
"source": [
"sc.pl.violin(adata,keys=\"n_genes_by_counts\")\n",
"sc.pl.violin(adata,keys=\"total_counts\")\n",
"sc.pl.violin(adata,keys=\"pct_counts_MT\")"
"sc.pl.violin(adata, keys=\"n_genes_by_counts\")\n",
"sc.pl.violin(adata, keys=\"total_counts\")\n",
"sc.pl.violin(adata, keys=\"pct_counts_MT\")"
]
},
{
Expand Down Expand Up @@ -395,8 +396,8 @@
],
"source": [
"%%time\n",
"rsc.pp.filter_cells(adata,qc_var=\"n_genes_by_counts\", min_count=500,max_count= 4000)\n",
"rsc.pp.filter_cells(adata,qc_var=\"total_counts\", max_count= 20000)\n",
"rsc.pp.filter_cells(adata, qc_var=\"n_genes_by_counts\", min_count=500, max_count=4000)\n",
"rsc.pp.filter_cells(adata, qc_var=\"total_counts\", max_count=20000)\n",
"adata = adata[adata.obs[\"pct_counts_MT\"] < 20]"
]
},
Expand Down Expand Up @@ -453,7 +454,7 @@
],
"source": [
"%%time\n",
"rsc.pp.normalize_total(adata,target_sum=1e4)"
"rsc.pp.normalize_total(adata, target_sum=1e4)"
]
},
{
Expand Down Expand Up @@ -555,7 +556,9 @@
],
"source": [
"%%time\n",
"rsc.pp.highly_variable_genes(adata,n_top_genes=5000,flavor=\"pearson_residuals\",layer = \"counts\")"
"rsc.pp.highly_variable_genes(\n",
" adata, n_top_genes=5000, flavor=\"pearson_residuals\", layer=\"counts\"\n",
")"
]
},
{
Expand Down Expand Up @@ -642,7 +645,9 @@
],
"source": [
"%%time\n",
"adata.layers[\"pearson_residuals\"] = rsc.pp.normalize_pearson_residuals(adata,layer= \"counts\",inplace = False)"
"adata.layers[\"pearson_residuals\"] = rsc.pp.normalize_pearson_residuals(\n",
" adata, layer=\"counts\", inplace=False\n",
")"
]
},
{
Expand Down Expand Up @@ -686,7 +691,7 @@
],
"source": [
"%%time\n",
"rsc.pp.pca(adata, n_comps = 150, layer = \"pearson_residuals\")"
"rsc.pp.pca(adata, n_comps=150, layer=\"pearson_residuals\")"
]
},
{
Expand All @@ -707,7 +712,7 @@
}
],
"source": [
"sc.pl.pca_variance_ratio(adata, log=True,n_pcs=150)"
"sc.pl.pca_variance_ratio(adata, log=True, n_pcs=150)"
]
},
{
Expand Down Expand Up @@ -754,7 +759,7 @@
],
"source": [
"preprocess_time = time.time()\n",
"print(\"Total Preprocessing time: %s\" % (preprocess_time-preprocess_start))"
"print(\"Total Preprocessing time: %s\" % (preprocess_time - preprocess_start))"
]
},
{
Expand Down Expand Up @@ -876,7 +881,7 @@
],
"source": [
"%%time\n",
"rsc.tl.louvain(adata,resolution=1.0)"
"rsc.tl.louvain(adata, resolution=1.0)"
]
},
{
Expand All @@ -896,7 +901,7 @@
],
"source": [
"%%time\n",
"rsc.tl.leiden(adata,resolution=1.0)"
"rsc.tl.leiden(adata, resolution=1.0)"
]
},
{
Expand Down Expand Up @@ -926,7 +931,7 @@
],
"source": [
"%%time\n",
"sc.pl.umap(adata, color=[\"louvain\",\"leiden\"],legend_loc = 'on data')"
"sc.pl.umap(adata, color=[\"louvain\", \"leiden\"], legend_loc=\"on data\")"
]
},
{
Expand Down Expand Up @@ -956,7 +961,7 @@
],
"source": [
"%%time\n",
"sc.pl.umap(adata, color=[\"Stmn2\",\"Hes1\"],legend_loc = 'on data', cmap=\"Reds\")"
"sc.pl.umap(adata, color=[\"Stmn2\", \"Hes1\"], legend_loc=\"on data\", cmap=\"Reds\")"
]
},
{
Expand Down Expand Up @@ -997,7 +1002,7 @@
}
],
"source": [
"adata.obsm[\"X_diffmap_\"] = adata.obsm[\"X_diffmap\"][:, 1:] \n",
"adata.obsm[\"X_diffmap_\"] = adata.obsm[\"X_diffmap\"][:, 1:]\n",
"sc.pl.embedding(adata, \"diffmap_\", color=\"louvain\")"
]
},
Expand Down Expand Up @@ -1072,7 +1077,7 @@
}
],
"source": [
"print(\"Total Processing time: %s\" % (time.time()-preprocess_start))"
"print(\"Total Processing time: %s\" % (time.time() - preprocess_start))"
]
}
],
Expand Down

0 comments on commit 2945ce6

Please sign in to comment.