Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access experiments by index #35

Merged
merged 5 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Version 0.4.1 - 0.4.3

- Access an experiment by index.
- Helper methods to create sample mapping if not provided.
- Subset operations on samples.
- Update sphinx configuration to run snippets in the documentation.
Expand Down
33 changes: 27 additions & 6 deletions src/multiassayexperiment/MultiAssayExperiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,32 +450,53 @@ def experiment_names(self, names: List[str]):
######>> experiment accessor <<######
#####################################

def experiment(self, name: str, with_sample_data: bool = False) -> Any:
def experiment(self, name: Union[int, str], with_sample_data: bool = False) -> Any:
"""Get an experiment by name.

Args:
name:
Experiment name.
Name or index position of the experiment.

with_sample_data:
Whether to merge column data of the experiment with
:py:attr:`~sample_data` from the MAE.

Defaults to False.

Raises:
AttributeError:
If the experiment name does not exist.
IndexError:
If index is greater than the number of experiments.

Returns:
The experiment object.

If ``with_sample_data`` is `True`, a copy of the experiment object is returned.
"""
if name not in self._experiments:
raise ValueError(f"'{name}' is not a valid experiment name.")
_name = name
if isinstance(name, int):
if name < 0:
raise IndexError("Index cannot be negative.")

if name > len(self.experiment_names):
raise IndexError("Index greater than the number of assays.")

_name = self.experiment_names[name]
expt = self._experiments[_name]
elif isinstance(name, str):
if name not in self._experiments:
raise ValueError(f"'{name}' is not a valid experiment name.")

expt = self.experiments[name]
expt = self.experiments[name]
else:
raise TypeError(
f"'experiment' must be a string or integer, provided '{type(name)}'."
)

if with_sample_data is True:
assay_splits = self.sample_map.split("assay", only_indices=True)
subset_map = self.sample_map[assay_splits[name],]
subset_map = self.sample_map[assay_splits[_name],]
subset_map = subset_map.set_row_names(subset_map.get_column("colname"))

expt_column_data = expt.column_data
Expand Down
12 changes: 12 additions & 0 deletions tests/test_with_coldata.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,15 @@ def test_access_expt_with_column_data():
assert sce.shape == tsce.shape

assert len(sce.column_data.columns) >= len(tsce.column_data.columns)


def test_access_expt_with_int_index():
assert mae is not None

se = mae.experiment(0)
assert se.shape == tse2.shape

sce = mae.experiment(1, with_sample_data=True)
assert sce.shape == tsce.shape

assert len(sce.column_data.columns) >= len(tsce.column_data.columns)
Loading