Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Dec 8, 2024
1 parent e5c021e commit baca999
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 89 deletions.
116 changes: 33 additions & 83 deletions src/singlecellexperiment/SingleCellExperiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,24 @@

def _validate_reduced_dims(reduced_dims, shape):
if reduced_dims is None:
raise ValueError(
"'reduced_dims' cannot be `None`, must be assigned to an empty dictionary."
)
raise ValueError("'reduced_dims' cannot be `None`, must be assigned to an empty dictionary.")

if not isinstance(reduced_dims, dict):
raise TypeError("'reduced_dims' is not a dictionary.")

for rdname, mat in reduced_dims.items():
if not hasattr(mat, "shape"):
raise TypeError(
f"Reduced dimension: '{rdname}' must be a matrix-like object."
"Does not contain a `shape` property."
f"Reduced dimension: '{rdname}' must be a matrix-like object." "Does not contain a `shape` property."
)

if shape[1] != mat.shape[0]:
raise ValueError(
f"Reduced dimension: '{rdname}' does not contain embeddings for all cells."
)
raise ValueError(f"Reduced dimension: '{rdname}' does not contain embeddings for all cells.")


def _validate_alternative_experiments(alternative_experiments, shape):
if alternative_experiments is None:
raise ValueError(
"'alternative_experiments' cannot be `None`, must be assigned to an empty dictionary."
)
raise ValueError("'alternative_experiments' cannot be `None`, must be assigned to an empty dictionary.")

if not isinstance(alternative_experiments, dict):
raise TypeError("'alternative_experiments' is not a dictionary.")
Expand All @@ -67,10 +60,7 @@ def _validate_alternative_experiments(alternative_experiments, shape):
)

if shape[1] != alternative_experiment.shape[1]:
raise ValueError(
f"Alternative experiment: '{alt_name}' does not contain same number of"
" cells."
)
raise ValueError(f"Alternative experiment: '{alt_name}' does not contain same number of" " cells.")


def _validate_pairs(pairs):
Expand Down Expand Up @@ -204,18 +194,14 @@ def __init__(

self._reduced_dims = reduced_dims if reduced_dims is not None else {}

self._alternative_experiments = (
alternative_experiments if alternative_experiments is not None else {}
)
self._alternative_experiments = alternative_experiments if alternative_experiments is not None else {}

self._row_pairs = row_pairs if row_pairs is not None else {}
self._column_pairs = column_pairs if column_pairs is not None else {}

if validate:
_validate_reduced_dims(self._reduced_dims, self._shape)
_validate_alternative_experiments(
self._alternative_experiments, self._shape
)
_validate_alternative_experiments(self._alternative_experiments, self._shape)
_validate_pairs(self._row_pairs)
_validate_pairs(self._column_pairs)

Expand Down Expand Up @@ -309,14 +295,10 @@ def __repr__(self) -> str:
output += ", row_ranges=" + self._row_ranges.__repr__()

if self._alternative_experiments is not None:
output += ", alternative_experiments=" + ut.print_truncated_list(
self.alternative_experiment_names
)
output += ", alternative_experiments=" + ut.print_truncated_list(self.alternative_experiment_names)

if self._reduced_dims is not None:
output += ", reduced_dims=" + ut.print_truncated_list(
self.reduced_dim_names
)
output += ", reduced_dims=" + ut.print_truncated_list(self.reduced_dim_names)

if self._main_experiment_name is not None:
output += ", main_experiment_name=" + self._main_experiment_name
Expand Down Expand Up @@ -344,10 +326,14 @@ def __str__(self) -> str:

output += f"assays({len(self.assay_names)}): {ut.print_truncated_list(self.assay_names)}\n"

output += f"row_data columns({len(self._rows.column_names)}): {ut.print_truncated_list(self._rows.column_names)}\n"
output += (
f"row_data columns({len(self._rows.column_names)}): {ut.print_truncated_list(self._rows.column_names)}\n"
)
output += f"row_names({0 if self._row_names is None else len(self._row_names)}): {' ' if self._row_names is None else ut.print_truncated_list(self._row_names)}\n"

output += f"column_data columns({len(self._cols.column_names)}): {ut.print_truncated_list(self._cols.column_names)}\n"
output += (
f"column_data columns({len(self._cols.column_names)}): {ut.print_truncated_list(self._cols.column_names)}\n"
)
output += f"column_names({0 if self._column_names is None else len(self._column_names)}): {' ' if self._column_names is None else ut.print_truncated_list(self._column_names)}\n"

output += f"main_experiment_name: {' ' if self._main_experiment_name is None else self._main_experiment_name}\n"
Expand All @@ -373,9 +359,7 @@ def get_reduced_dims(self) -> Dict[str, Any]:
"""
return self._reduced_dims

def set_reduced_dims(
self, reduced_dims: Dict[str, Any], in_place: bool = False
) -> "SingleCellExperiment":
def set_reduced_dims(self, reduced_dims: Dict[str, Any], in_place: bool = False) -> "SingleCellExperiment":
"""Set new reduced dimensions.
Args:
Expand Down Expand Up @@ -421,9 +405,7 @@ def get_reduced_dim_names(self) -> List[str]:
"""
return list(self._reduced_dims.keys())

def set_reduced_dim_names(
self, names: List[str], in_place: bool = False
) -> "SingleCellExperiment":
def set_reduced_dim_names(self, names: List[str], in_place: bool = False) -> "SingleCellExperiment":
"""Replace :py:attr:`~.reduced_dims`'s names.
Args:
Expand All @@ -439,9 +421,7 @@ def set_reduced_dim_names(
"""
current_names = self.get_reduced_dim_names()
if len(names) != len(current_names):
raise ValueError(
"Length of 'names' does not match the number of `reduced_dims`."
)
raise ValueError("Length of 'names' does not match the number of `reduced_dims`.")

new_reduced_dims = OrderedDict()
for idx in range(len(names)):
Expand Down Expand Up @@ -499,9 +479,7 @@ def reduced_dim(self, dimension: Union[str, int]) -> Any:

return self._reduced_dims[dimension]

raise TypeError(
f"'dimension' must be a string or integer, provided '{type(dimension)}'."
)
raise TypeError(f"'dimension' must be a string or integer, provided '{type(dimension)}'.")

################################
######>> main_expt_name <<######
Expand All @@ -515,9 +493,7 @@ def get_main_experiment_name(self) -> Optional[str]:
"""
return self._main_experiment_name

def set_main_experiment_name(
self, name: Optional[str], in_place: bool = False
) -> "SingleCellExperiment":
def set_main_experiment_name(self, name: Optional[str], in_place: bool = False) -> "SingleCellExperiment":
"""Set new experiment data (assays).
Args:
Expand Down Expand Up @@ -609,9 +585,7 @@ def get_alternative_experiment_names(self) -> List[str]:
"""
return list(self._alternative_experiments.keys())

def set_alternative_experiment_names(
self, names: List[str], in_place: bool = False
) -> "SingleCellExperiment":
def set_alternative_experiment_names(self, names: List[str], in_place: bool = False) -> "SingleCellExperiment":
"""Replace :py:attr:`~.alternative_experiment`'s names.
Args:
Expand All @@ -627,15 +601,11 @@ def set_alternative_experiment_names(
"""
current_names = self.get_alternative_experiment_names()
if len(names) != len(current_names):
raise ValueError(
"Length of 'names' does not match the number of `alternative_experiments`."
)
raise ValueError("Length of 'names' does not match the number of `alternative_experiments`.")

new_alt_expts = OrderedDict()
for idx in range(len(names)):
new_alt_expts[names[idx]] = self._alternative_experiments.pop(
current_names[idx]
)
new_alt_expts[names[idx]] = self._alternative_experiments.pop(current_names[idx])

output = self._define_output(in_place)
output._alternative_experiments = new_alt_expts
Expand Down Expand Up @@ -680,13 +650,9 @@ def alternative_experiment(self, name: Union[str, int]) -> Any:
raise IndexError("Index cannot be negative.")

if name > len(self.alternative_experiment_names):
raise IndexError(
"Index greater than the number of alternative experiments."
)
raise IndexError("Index greater than the number of alternative experiments.")

return self._alternative_experiments[
self.alternative_experiment_names[name]
]
return self._alternative_experiments[self.alternative_experiment_names[name]]
elif isinstance(name, str):
if name not in self._alternative_experiments:
raise AttributeError(f"Alternative experiment: {name} does not exist.")
Expand All @@ -707,9 +673,7 @@ def get_row_pairs(self) -> Dict[str, Any]:
"""
return self._row_pairs

def set_row_pairs(
self, pairs: Dict[str, Any], in_place: bool = False
) -> "SingleCellExperiment":
def set_row_pairs(self, pairs: Dict[str, Any], in_place: bool = False) -> "SingleCellExperiment":
"""Replace :py:attr:`~.row_pairs`'s names.
Args:
Expand Down Expand Up @@ -755,9 +719,7 @@ def get_row_pair_names(self) -> List[str]:
"""
return list(self._row_pairs.keys())

def set_row_pair_names(
self, names: List[str], in_place: bool = False
) -> "SingleCellExperiment":
def set_row_pair_names(self, names: List[str], in_place: bool = False) -> "SingleCellExperiment":
"""Replace :py:attr:`~.row_pair`'s names.
Args:
Expand All @@ -773,9 +735,7 @@ def set_row_pair_names(
"""
current_names = self.get_row_pair_names()
if len(names) != len(current_names):
raise ValueError(
"Length of 'names' does not match the number of `row_pairs`."
)
raise ValueError("Length of 'names' does not match the number of `row_pairs`.")

new_row_pairs = OrderedDict()
for idx in range(len(names)):
Expand Down Expand Up @@ -811,9 +771,7 @@ def get_column_pairs(self) -> Dict[str, Any]:
"""
return self._column_pairs

def set_column_pairs(
self, pairs: Dict[str, Any], in_place: bool = False
) -> "SingleCellExperiment":
def set_column_pairs(self, pairs: Dict[str, Any], in_place: bool = False) -> "SingleCellExperiment":
"""Replace :py:attr:`~.column_pairs`'s names.
Args:
Expand Down Expand Up @@ -859,9 +817,7 @@ def get_column_pair_names(self) -> List[str]:
"""
return list(self._column_pairs.keys())

def set_column_pair_names(
self, names: List[str], in_place: bool = False
) -> "SingleCellExperiment":
def set_column_pair_names(self, names: List[str], in_place: bool = False) -> "SingleCellExperiment":
"""Replace :py:attr:`~.column_pair`'s names.
Args:
Expand All @@ -877,9 +833,7 @@ def set_column_pair_names(
"""
current_names = self.get_column_pair_names()
if len(names) != len(current_names):
raise ValueError(
"Length of 'names' does not match the number of `column_pairs`."
)
raise ValueError("Length of 'names' does not match the number of `column_pairs`.")

new_column_pairs = OrderedDict()
for idx in range(len(names)):
Expand Down Expand Up @@ -1252,9 +1206,7 @@ def relaxed_combine_columns(

_new_rdim = None
try:
_new_rdim = relaxed_merge_numpy_generic(
x, by="row", attr="reduced_dims", names_attr="reduced_dim_names"
)
_new_rdim = relaxed_merge_numpy_generic(x, by="row", attr="reduced_dims", names_attr="reduced_dim_names")
except Exception as e:
warn(
f"Cannot combine 'reduced_dimensions' across experiments, {str(e)}",
Expand All @@ -1263,9 +1215,7 @@ def relaxed_combine_columns(

_new_alt_expt = None
try:
_new_alt_expt = relaxed_merge_generic(
x, by="column", attr="alternative_experiments"
)
_new_alt_expt = relaxed_merge_generic(x, by="column", attr="alternative_experiments")
except Exception as e:
warn(
f"Cannot combine 'alternative_experiments' across experiments, {str(e)}",
Expand Down
8 changes: 2 additions & 6 deletions src/singlecellexperiment/io/tenx.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ def read_tenx_h5(path: str, realize_assays: bool = False) -> SingleCellExperimen
# read the matrix
shape = tuple(h5["matrix"]["shape"][:])

counts = Hdf5CompressedSparseMatrix(
path=path, group_name="matrix", by_column=True, shape=shape
)
counts = Hdf5CompressedSparseMatrix(path=path, group_name="matrix", by_column=True, shape=shape)

if realize_assays is True:
counts = da.to_scipy_sparse_matrix(counts, "csr")
Expand Down Expand Up @@ -111,6 +109,4 @@ def read_tenx_h5(path: str, realize_assays: bool = False) -> SingleCellExperimen

h5.close()

return SingleCellExperiment(
assays={"counts": counts}, row_data=features, column_data=barcodes
)
return SingleCellExperiment(assays={"counts": counts}, row_data=features, column_data=barcodes)

0 comments on commit baca999

Please sign in to comment.