Skip to content

Commit

Permalink
Merge pull request #22 from kevin931/fix_issue_18
Browse files Browse the repository at this point in the history
[BUG] Fix the issue of not updating lineage channel indices
  • Loading branch information
kevin931 authored Dec 28, 2024
2 parents eff8306 + 9196d87 commit 028d62f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions PyCytoData/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ def subset(self, channels: Optional[ArrayLike]=None, sample: Optional[ArrayLike]
self.channels = self.channels[channel_filter_condition]
if self.lineage_channels is not None:
self.lineage_channels = self.lineage_channels[np.isin(self.lineage_channels, self.channels)]
else:
self._lineage_channels_indices = np.arange(self.n_channels)
self.sample_index = self.sample_index[filter_condition]
self.cell_types = self.cell_types[filter_condition]

Expand Down Expand Up @@ -786,6 +788,7 @@ def lineage_channels(self, lineage_channels: ArrayLike):
if not np.all(np.isin(lineage_channels, self._channels)):
raise ValueError("Some lineage channels are not listed in channel names.")
self._lineage_channels: Optional[np.ndarray] = lineage_channels if lineage_channels is None else np.array(lineage_channels).flatten()
self._lineage_channels_indices = np.where(np.isin(self.lineage_channels, self.channels))


@property
Expand Down
14 changes: 14 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,21 @@ def test_subset_channels(self):
assert exprs.n_channels == 2
assert exprs.lineage_channels is not None
assert exprs.lineage_channels.shape[0] == 2
assert np.all(np.equal(exprs._lineage_channels_indices, np.array([0, 1])))
assert not np.isin("Channel0", exprs.lineage_channels)


def test_subset_channels_lineage_indices(self):
exprs_matrix: np.ndarray = np.random.rand(100, 10)
channels: np.ndarray = np.arange(10).astype(str)
exprs = PyCytoData(exprs_matrix, channels=channels)
exprs.subset(channels=["1", "2"])
assert exprs.n_cells == 100
assert exprs.n_channels == 2
assert exprs.lineage_channels is None
assert exprs._lineage_channels_indices.shape[0] == 2
assert np.all(np.equal(exprs._lineage_channels_indices, np.array([0, 1])))
assert not np.isin("0", exprs.lineage_channels)


def test_subset_cell_types(self):
Expand Down

0 comments on commit 028d62f

Please sign in to comment.