Skip to content

Commit

Permalink
Satisfy ruff check
Browse files Browse the repository at this point in the history
  • Loading branch information
gtca committed Dec 16, 2024
1 parent 1f327fc commit 0fbcdea
Show file tree
Hide file tree
Showing 20 changed files with 282 additions and 279 deletions.
27 changes: 20 additions & 7 deletions src/muon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,31 @@


from mudata import MuData
from mudata._core.io import *
from mudata._core.io import (
read,
read_anndata,
read_h5ad,
read_h5mu,
read_zarr,
write_anndata,
write_h5ad,
write_h5mu,
write_zarr,
)

from . import _atac as atac
from . import _prot as prot
from . import _rna as rna
from ._core import plot as pl
from ._core import preproc as pp
from ._core import tools as tl
from ._core import plot as pl
from ._core import utils
from ._core.io import *
from ._core.config import set_options

from . import atac
from . import prot
from ._core.io import read_10x_h5, read_10x_mtx

__all__ = [
"__version__",

"MuData",
"pp",
"tl",
Expand All @@ -49,4 +61,5 @@
"set_options",
"atac",
"prot",
]
"rna",
]
6 changes: 4 additions & 2 deletions src/muon/_atac/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from . import plot as pl
from . import preproc as pp
from . import tools as tl
from . import plot as pl
from .io import *
from .io import read_10x_h5, read_10x_mtx, read_snap

__all__ = ["pp", "tl", "pl", "read_10x_h5", "read_10x_mtx", "read_snap"]
5 changes: 2 additions & 3 deletions src/muon/_atac/io.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from os import PathLike
from typing import Optional
from warnings import warn

import numpy as np
Expand All @@ -22,7 +21,7 @@ def read_10x_mtx(filename: PathLike, atac_only: bool = True, *args, **kwargs) ->
return adata


def read_snap(filename: PathLike, matrix: str, bin_size: Optional[int] = None):
def read_snap(filename: PathLike, matrix: str, bin_size: int | None = None):
"""
Read a matrix from a .snap file.
Expand All @@ -47,8 +46,8 @@ def read_snap(filename: PathLike, matrix: str, bin_size: Optional[int] = None):
"SnapTools library is not available. Install SnapTools from PyPI (`pip install snaptools`) or from GitHub (`pip install git+https://github.com/r3fang/SnapTools`)"
)

from scipy.sparse import csr_matrix
import h5py
from scipy.sparse import csr_matrix

# Allow both PM and pm
matrix = matrix.lower()
Expand Down
65 changes: 32 additions & 33 deletions src/muon/_atac/plot.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
from collections import defaultdict
from typing import Union, Optional, List, Iterable, Mapping, Sequence
import warnings
from collections import defaultdict
from collections.abc import Iterable, Mapping, Sequence

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from scipy.sparse import issparse
import scanpy as sc
import seaborn as sns
from anndata import AnnData

import matplotlib.pyplot as plt
from matplotlib.axes import Axes
import seaborn as sns

from mudata import MuData
from scipy.sparse import issparse

from . import tools


def _average_peaks(
adata: AnnData,
keys: List[str],
average: Optional[str],
keys: list[str],
average: str | None,
func: str,
use_raw: bool,
layer: Optional[str],
layer: str | None,
):
# Define the function to be used for aggregation
if average:
Expand Down Expand Up @@ -131,13 +130,13 @@ def _average_peaks(


def embedding(
data: Union[AnnData, MuData],
data: AnnData | MuData,
basis: str,
color: Optional[Union[str, List[str]]] = None,
average: Optional[str] = "total",
func: Optional[str] = "mean",
color: str | list[str] | None = None,
average: str | None = "total",
func: str | None = "mean",
use_raw: bool = True,
layer: Optional[str] = None,
layer: str | None = None,
**kwargs,
):
"""
Expand Down Expand Up @@ -176,7 +175,7 @@ def embedding(
return sc.pl.embedding(adata, basis=basis, use_raw=use_raw, layer=layer, **kwargs)


def pca(data: Union[AnnData, MuData], **kwargs) -> Union[Axes, List[Axes], None]:
def pca(data: AnnData | MuData, **kwargs) -> Axes | list[Axes] | None:
"""
Scatter plot for principle components
Expand All @@ -185,7 +184,7 @@ def pca(data: Union[AnnData, MuData], **kwargs) -> Union[Axes, List[Axes], None]
return embedding(data, basis="pca", **kwargs)


def lsi(data: Union[AnnData, MuData], **kwargs) -> Union[Axes, List[Axes], None]:
def lsi(data: AnnData | MuData, **kwargs) -> Axes | list[Axes] | None:
"""
Scatter plot for latent semantic indexing components
Expand All @@ -194,7 +193,7 @@ def lsi(data: Union[AnnData, MuData], **kwargs) -> Union[Axes, List[Axes], None]
return embedding(data, basis="lsi", **kwargs)


def umap(data: Union[AnnData, MuData], **kwargs) -> Union[Axes, List[Axes], None]:
def umap(data: AnnData | MuData, **kwargs) -> Axes | list[Axes] | None:
"""
Scatter plot in UMAP space
Expand All @@ -203,7 +202,7 @@ def umap(data: Union[AnnData, MuData], **kwargs) -> Union[Axes, List[Axes], None
return embedding(data, basis="umap", **kwargs)


def mofa(mdata: MuData, **kwargs) -> Union[Axes, List[Axes], None]:
def mofa(mdata: MuData, **kwargs) -> Axes | list[Axes] | None:
"""
Scatter plot in MOFA factors coordinates
Expand All @@ -213,13 +212,13 @@ def mofa(mdata: MuData, **kwargs) -> Union[Axes, List[Axes], None]:


def dotplot(
data: Union[AnnData, MuData],
var_names: Union[str, Sequence[str], Mapping[str, Union[str, Sequence[str]]]],
groupby: Optional[Union[str]] = None,
average: Optional[str] = "total",
func: Optional[str] = "mean",
use_raw: Optional[Union[bool]] = None,
layer: Optional[str] = None,
data: AnnData | MuData,
var_names: str | Sequence[str] | Mapping[str, str | Sequence[str]],
groupby: str | None = None,
average: str | None = "total",
func: str | None = "mean",
use_raw: bool | None = None,
layer: str | None = None,
**kwargs,
):
"""
Expand Down Expand Up @@ -257,9 +256,9 @@ def dotplot(

def tss_enrichment(
data: AnnData,
color: Optional[Union[str]] = None,
color: str | None = None,
title: str = "TSS Enrichment",
ax: Optional[Axes] = None,
ax: Axes | None = None,
):
"""
Plot relative enrichment scores around a TSS.
Expand Down Expand Up @@ -314,12 +313,12 @@ def _tss_enrichment_single(data: AnnData, ax: Axes, sd: bool = False, *args, **k


def fragment_histogram(
data: Union[AnnData, MuData],
data: AnnData | MuData,
region: str = "chr1-1-2000000",
groupby: Optional[Union[str]] = None,
barcodes: Optional[str] = None,
show: Optional[bool] = None,
save: Union[str, bool, None] = None,
groupby: str | None = None,
barcodes: str | None = None,
show: bool | None = None,
save: str | bool | None = None,
):
"""
Plot Histogram of Fragment lengths within specified region.
Expand Down
19 changes: 8 additions & 11 deletions src/muon/_atac/preproc.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
from typing import Union, Optional
from warnings import warn

import numpy as np
from scipy.sparse import csr_matrix, dia_matrix, issparse

from anndata import AnnData
from mudata import MuData

from scanpy._utils import view_to_actual

from scipy.sparse import csr_matrix, dia_matrix, issparse

# Computational methods for preprocessing


def tfidf(
data: Union[AnnData, MuData],
data: AnnData | MuData,
log_tf: bool = True,
log_idf: bool = True,
log_tfidf: bool = False,
scale_factor: Union[int, float] = 1e4,
scale_factor: int | float = 1e4,
inplace: bool = True,
copy: bool = False,
from_layer: Optional[str] = None,
to_layer: Optional[str] = None,
from_layer: str | None = None,
to_layer: str | None = None,
):
"""
Transform peak counts with TF-IDF (Term Frequency - Inverse Document Frequency).
Expand Down Expand Up @@ -129,7 +125,7 @@ def tfidf(
return adata


def binarize(data: Union[AnnData, MuData]):
def binarize(data: AnnData | MuData):
"""
Transform peak counts to the binary matrix (all the non-zero values become 1).
Expand All @@ -153,7 +149,7 @@ def binarize(data: Union[AnnData, MuData]):


def scopen(
data: Union[AnnData, MuData],
data: AnnData | MuData,
n_components: int = 30,
max_iter: int = 500,
min_rho: float = 0.0,
Expand Down Expand Up @@ -182,6 +178,7 @@ def scopen(

try:
import time

from scopen.MF import non_negative_factorization
except ImportError:
raise ImportError(
Expand Down
Loading

0 comments on commit 0fbcdea

Please sign in to comment.