Skip to content

Commit 4d59041

Browse files
authored
Rename reduced dims (#64)
- Rename `reduced_dims` to `reduced_dimensions`. Constructor accepts both these arguments for backwards compatibility. - Additionally, SCE's constructor takes `kwargs` for future proofing any additional attributes in the upstream classes.
1 parent 357fe9d commit 4d59041

File tree

3 files changed

+80
-8
lines changed

3 files changed

+80
-8
lines changed

CHANGELOG.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
# Changelog
22

3+
## Version 0.5.8
4+
5+
- Rename `reduced_dims` to `reduced_dimensions`. Constructor accepts both these arguments for backwards compatibility.
6+
- Additionally, SCE's constructor takes kwargs for future proofing any additional attributes in the upstream classes.
7+
38
## Version 0.5.7
9+
410
- Support reading outputs from CellRanger version 3 and later in `read_tenx_mtx`.
511
- Handle both `genes.csv` and `features.csv` for gene annotations.
612

713
## Version 0.5.6
814

9-
- Check if the column names of the alternative experiments match with the column names of the main experiment. This is the equivalent to the ``withDimnames`` parameter in the R implementation.
15+
- Check if the column names of the alternative experiments match with the column names of the main experiment. This is the equivalent to the `withDimnames` parameter in the R implementation.
1016
- On **getters** of alternative experiments, if `with_dim_names` is True, column names of the alternative experiment are **replaced** with the
11-
column names of the main experiment.
17+
column names of the main experiment.
1218
- On **setters** of alternative experiments, if `with_dim_names` is True, column names of the alternative experiment are **checked** with the
13-
column names of the main experiment and an Exception is raised if they do not match.
19+
column names of the main experiment and an Exception is raised if they do not match.
1420

1521
## Version 0.5.1 - 0.5.5
1622

@@ -19,7 +25,6 @@
1925
- Add getters and setters to replace a specific alternative experiment or reduced dimension.
2026
- Fixed an issue with numpy arrays as slice arguments. Code now uses Biocutils's subset functions to perform these operations.
2127

22-
2328
## Version 0.5.0
2429

2530
- chore: Remove Python 3.8 (EOL)

src/singlecellexperiment/SingleCellExperiment.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class SingleCellExperiment(RangedSummarizedExperiment):
9494
parent experiment. Otherwise, these cells do not share the same sample or annotations
9595
and cannot be set in alternative experiments!
9696
97-
Note: Validation checks do not apply to ``row_pairs`` or ``col_pairs``.
97+
Note: Validation checks do not apply to ``row_pairs`` or ``column_pairs``.
9898
"""
9999

100100
def __init__(
@@ -106,13 +106,15 @@ def __init__(
106106
row_names: Optional[List[str]] = None,
107107
column_names: Optional[List[str]] = None,
108108
metadata: Optional[dict] = None,
109-
reduced_dims: Optional[Dict[str, Any]] = None,
109+
reduced_dimensions: Optional[Dict[str, Any]] = None,
110+
reduced_dims: Optional[Dict[str, Any]] = None, # deprecated name
110111
main_experiment_name: Optional[str] = None,
111112
alternative_experiments: Optional[Dict[str, Any]] = None,
112113
row_pairs: Optional[Any] = None,
113114
column_pairs: Optional[Any] = None,
114115
alternative_experiment_check_dim_names: bool = True,
115116
validate: bool = True,
117+
**kwargs,
116118
) -> None:
117119
"""Initialize a single-cell experiment.
118120
@@ -157,14 +159,17 @@ def __init__(
157159
Additional experimental metadata describing the methods.
158160
Defaults to None.
159161
160-
reduced_dims:
162+
reduced_dimensions:
161163
Slot for low-dimensionality embeddings.
162164
163165
Usually a dictionary with the embedding method as keys (e.g., t-SNE, UMAP)
164166
and the dimensions as values.
165167
166168
Embeddings may be represented as a matrix or a data frame, must contain a shape.
167169
170+
reduced_dims:
171+
Will be deprecated in the future versions. Use py:attr:`~reduced_dimensions` instead.
172+
168173
main_experiment_name:
169174
A string, specifying the main experiment name.
170175
@@ -195,6 +200,9 @@ def __init__(
195200
196201
validate:
197202
Internal use only.
203+
204+
kwargs:
205+
Additional arguments.
198206
"""
199207

200208
super().__init__(
@@ -206,10 +214,20 @@ def __init__(
206214
column_names=column_names,
207215
metadata=metadata,
208216
validate=validate,
217+
**kwargs,
209218
)
210219
self._main_experiment_name = main_experiment_name
211220

212-
self._reduced_dims = reduced_dims if reduced_dims is not None else {}
221+
_dims = None
222+
if reduced_dimensions is not None and reduced_dims is not None:
223+
raise ValueError("Either 'reduced_dims' or 'reduced_dimensions' should be provided, but not both.")
224+
elif reduced_dims is not None:
225+
warn("'reduced_dims' is deprecated, use 'reduced_dimensions' instead.", DeprecationWarning)
226+
_dims = reduced_dims
227+
elif reduced_dimensions is not None:
228+
_dims = reduced_dimensions
229+
230+
self._reduced_dims = _dims if _dims is not None else {}
213231

214232
self._alternative_experiments = alternative_experiments if alternative_experiments is not None else {}
215233

@@ -551,6 +569,10 @@ def reduced_dim(self, name: Union[str, int]) -> Any:
551569
"""Alias for :py:meth:`~get_reduced_dimension`, for back-compatibility."""
552570
return self.get_reduced_dimension(name=name)
553571

572+
def reduced_dimension(self, name: Union[str, int]) -> Any:
573+
"""Alias for :py:meth:`~get_reduced_dimension`, for back-compatibility."""
574+
return self.get_reduced_dimension(name=name)
575+
554576
def set_reduced_dimension(self, name: str, embedding: Any, in_place: bool = False) -> "SingleCellExperiment":
555577
"""Add or replace :py:attr:`~singlecellexperiment.SingleCellExperiment.reduced_dimension`'s.
556578

tests/test_sce.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,48 @@ def test_SCE_different_alt_names():
172172
column_data=pd.DataFrame(index = ["ChIP", "Input", "Input"] * 2),
173173
alternative_experiments={"alt": rse},
174174
)
175+
176+
def test_SCE_dims():
177+
embeds = np.random.rand(counts.shape[1], 4)
178+
tse = SingleCellExperiment(
179+
assays={"counts": counts},
180+
row_data=row_data,
181+
column_data=col_data,
182+
reduced_dimensions={
183+
"something": embeds
184+
}
185+
)
186+
187+
assert tse is not None
188+
assert isinstance(tse, sce)
189+
assert tse.get_reduced_dimension_names() == ["something"]
190+
191+
tse2 = SingleCellExperiment(
192+
assays={"counts": counts},
193+
row_data=row_data,
194+
column_data=col_data,
195+
reduced_dims={
196+
"something": embeds
197+
}
198+
)
199+
200+
assert tse2 is not None
201+
assert isinstance(tse2, sce)
202+
assert tse2.get_reduced_dimension_names() == ["something"]
203+
204+
print(tse.get_reduced_dimension("something"), tse2.get_reduced_dimension("something"))
205+
206+
assert np.allclose(tse.get_reduced_dimension("something"), tse2.get_reduced_dimension("something"))
207+
208+
with pytest.raises(Exception, match="Either 'reduced_dims' or 'reduced_dimensions' should be provided, but not both."):
209+
SingleCellExperiment(
210+
assays={"counts": counts},
211+
row_data=row_data,
212+
column_data=col_data,
213+
reduced_dims={
214+
"something": embeds
215+
},
216+
reduced_dimensions={
217+
"something": embeds
218+
}
219+
)

0 commit comments

Comments
 (0)