Skip to content

Commit e5d5f19

Browse files
committed
Update type annotations
1 parent 5416709 commit e5d5f19

14 files changed

+549
-478
lines changed

src/pycisTopic/cistopic_class.py

Lines changed: 67 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
from __future__ import annotations
2+
13
import collections as cl
24
import logging
35
import sys
4-
from typing import Dict, List, Optional, Union
6+
from typing import TYPE_CHECKING, Self
57

68
import numpy as np
79
import pandas as pd
@@ -18,6 +20,9 @@
1820
)
1921
from scipy import sparse
2022

23+
if TYPE_CHECKING:
24+
from pycisTopic.lda_models import CistopicLDAModel
25+
2126
dtype = pd.SparseDtype(int, fill_value=0)
2227
pd.options.mode.chained_assignment = None
2328

@@ -56,12 +61,12 @@ def __init__(
5661
self,
5762
fragment_matrix: sparse.csr_matrix,
5863
binary_matrix: sparse.csr_matrix,
59-
cell_names: List[str],
60-
region_names: List[str],
64+
cell_names: list[str],
65+
region_names: list[str],
6166
cell_data: pd.DataFrame,
6267
region_data: pd.DataFrame,
63-
path_to_fragments: Union[str, Dict[str, str]],
64-
project: Optional[str] = "cisTopic",
68+
path_to_fragments: str | dict[str, str],
69+
project: str = "cisTopic",
6570
):
6671
self.fragment_matrix = fragment_matrix
6772
self.binary_matrix = binary_matrix
@@ -81,7 +86,7 @@ def __str__(self):
8186
return descr
8287

8388
def add_cell_data(
84-
self, cell_data: pd.DataFrame, split_pattern: Optional[str] = "___"
89+
self, cell_data: pd.DataFrame, split_pattern: str = "___"
8590
):
8691
"""
8792
Add cell metadata to :class:`CistopicObject`. If the column already exist on the cell metadata, it will be overwritten.
@@ -181,14 +186,17 @@ def add_region_data(self, region_data: pd.DataFrame):
181186

182187
def subset(
183188
self,
184-
cells: Optional[List[str]] = None,
185-
regions: Optional[List[str]] = None,
186-
copy: Optional[bool] = False,
187-
split_pattern: Optional[str] = "___",
189+
cells: list[str] | None = None,
190+
regions: list[str] | None = None,
191+
copy: bool | None = False,
192+
split_pattern: str = "___",
188193
):
189194
"""
190-
Subset cells and/or regions from :class:`CistopicObject`. Existent :class:`CisTopicLDAModel` and projections will be deleted. This is to ensure that
191-
models contained in a :class:`CistopicObject` are derived from the cells it contains.
195+
Subset cells and/or regions from :class:`CistopicObject`.
196+
197+
Existent :class:`CisTopicLDAModel` and projections will be deleted. This is to
198+
ensure thatmodels contained in a :class:`CistopicObject` are derived from the
199+
cells it contains.
192200
193201
Parameters
194202
----------
@@ -280,11 +288,11 @@ def subset(
280288

281289
def merge(
282290
self,
283-
cistopic_obj_list: List["CistopicObject"],
284-
is_acc: Optional[int] = 1,
285-
project: Optional[str] = "cisTopic_merge",
286-
copy: Optional[bool] = False,
287-
split_pattern: Optional[str] = "___",
291+
cistopic_obj_list: list[Self],
292+
is_acc: int = 1,
293+
project: str = "cisTopic_merge",
294+
copy: bool = False,
295+
split_pattern: str = "___",
288296
):
289297
"""
290298
Merge a list of :class:`CistopicObject` to the input :class:`CistopicObject`. Reference coordinates must be the same between the objects. Existent :class:`cisTopicCGSModel` and projections will be deleted. This is to ensure that models contained in a :class:`CistopicObject` are derived from the cells it contains.
@@ -480,7 +488,7 @@ def merge(
480488
self.selected_model = []
481489
self.projections = {}
482490

483-
def add_LDA_model(self, model: "CistopicLDAModel"):
491+
def add_LDA_model(self, model: CistopicLDAModel):
484492
"""
485493
Add LDA model to a cisTopic object.
486494
@@ -497,17 +505,17 @@ def add_LDA_model(self, model: "CistopicLDAModel"):
497505

498506

499507
def create_cistopic_object(
500-
fragment_matrix: Union[pd.DataFrame, sparse.csr_matrix],
501-
cell_names: Optional[List[str]] = None,
502-
region_names: Optional[List[str]] = None,
503-
path_to_blacklist: Optional[str] = None,
504-
min_frag: Optional[int] = 1,
505-
min_cell: Optional[int] = 1,
506-
is_acc: Optional[int] = 1,
507-
path_to_fragments: Optional[Union[str, Dict[str, str]]] = {},
508-
project: Optional[str] = "cisTopic",
509-
tag_cells: Optional[bool] = True,
510-
split_pattern: Optional[str] = "___",
508+
fragment_matrix: pd.DataFrame | sparse.csr_matrix,
509+
cell_names: list[str] | None = None,
510+
region_names: list[str] | None = None,
511+
path_to_blacklist: str | None = None,
512+
min_frag: int = 1,
513+
min_cell: int = 1,
514+
is_acc: int = 1,
515+
path_to_fragments: str | dict[str, str] | None = None,
516+
project: str = "cisTopic",
517+
tag_cells: bool = True,
518+
split_pattern: str = "___",
511519
):
512520
"""
513521
Creates a CistopicObject from a count matrix.
@@ -629,6 +637,9 @@ def create_cistopic_object(
629637
region_data = region_data[selected_regions, :]
630638
region_names = region_data.index.to_list()
631639

640+
if path_to_fragments is None:
641+
path_to_fragments = {}
642+
632643
cistopic_obj = CistopicObject(
633644
fragment_matrix,
634645
binary_matrix,
@@ -645,15 +656,15 @@ def create_cistopic_object(
645656

646657
def create_cistopic_object_from_matrix_file(
647658
fragment_matrix_file: str,
648-
path_to_blacklist: Optional[str] = None,
649-
compression: Optional[str] = None,
650-
min_frag: Optional[int] = 1,
651-
min_cell: Optional[int] = 1,
652-
is_acc: Optional[int] = 1,
653-
path_to_fragments: Optional[Dict[str, str]] = {},
654-
sample_id: Optional[pd.DataFrame] = None,
655-
project: Optional[str] = "cisTopic",
656-
split_pattern: Optional[str] = "___",
659+
path_to_blacklist: str | None = None,
660+
compression: str | None = None,
661+
min_frag: int = 1,
662+
min_cell: int = 1,
663+
is_acc: int = 1,
664+
path_to_fragments: dict[str, str] | None = None,
665+
sample_id: pd.DataFrame = None,
666+
project: str = "cisTopic",
667+
split_pattern: str = "___",
657668
):
658669
"""
659670
Creates a CistopicObject from a count matrix file (tsv).
@@ -729,22 +740,22 @@ def create_cistopic_object_from_matrix_file(
729740
def create_cistopic_object_from_fragments(
730741
path_to_fragments: str,
731742
path_to_regions: str,
732-
path_to_blacklist: Optional[str] = None,
733-
metrics: Optional[Union[str, pd.DataFrame]] = None,
734-
valid_bc: Optional[List[str]] = None,
735-
n_cpu: Optional[int] = 1,
736-
min_frag: Optional[int] = 1,
737-
min_cell: Optional[int] = 1,
738-
is_acc: Optional[int] = 1,
739-
check_for_duplicates: Optional[bool] = True,
740-
project: Optional[str] = "cisTopic",
741-
partition: Optional[int] = 5,
742-
fragments_df: Optional[Union[pd.DataFrame, pr.PyRanges]] = None,
743-
split_pattern: Optional[str] = "___",
744-
use_polars: Optional[bool] = True,
743+
path_to_blacklist: str | None = None,
744+
metrics: str | pd.DataFrame | None = None,
745+
valid_bc: list[str] | None = None,
746+
n_cpu: int = 1,
747+
min_frag: int = 1,
748+
min_cell: int = 1,
749+
is_acc: int = 1,
750+
check_for_duplicates: bool = True,
751+
project: str = "cisTopic",
752+
partition: int = 5,
753+
fragments_df: pd.DataFrame | pr.PyRanges | None = None,
754+
split_pattern: str = "___",
755+
use_polars: bool = True,
745756
):
746757
"""
747-
Creates a CistopicObject from a fragments file and defined genomic intervals (compatible with CellRangerATAC output)
758+
Creates a CistopicObject from a fragments file and defined genomic intervals (compatible with CellRangerATAC output).
748759
749760
Parameters
750761
----------
@@ -957,10 +968,10 @@ def create_cistopic_object_chunk(
957968

958969

959970
def merge(
960-
cistopic_obj_list: List["CistopicObject"],
961-
is_acc: Optional[int] = 1,
962-
project: Optional[str] = "cisTopic_merge",
963-
split_pattern: Optional[str] = "___",
971+
cistopic_obj_list: list[CistopicObject],
972+
is_acc: int = 1,
973+
project: str = "cisTopic_merge",
974+
split_pattern: str = "___",
964975
):
965976
"""
966977
Merge a list of :class:`CistopicObject` to the input :class:`CistopicObject`. Reference coordinates must be the same between the objects. Existent :class:`cisTopicCGSModel` and projections will be deleted. This is to ensure that models contained in a :class:`CistopicObject` are derived from the cells it contains.

0 commit comments

Comments
 (0)