Skip to content

Commit 22c7d7e

Browse files
authored
Merge pull request #102 from compomics/fix/from-iter
PSMList: Use np.fromiter when using a string as accessor (e.g. `psm_list["peptidoform"]`)
2 parents 414688e + 0d47b14 commit 22c7d7e

File tree

2 files changed

+3
-8
lines changed

2 files changed

+3
-8
lines changed

psm_utils/psm_list.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,7 @@ def __getitem__(self, item) -> PSM | list[PSM]:
9898
return PSMList(psm_list=self.psm_list[item])
9999
elif isinstance(item, str):
100100
# Return PSM property as array across full PSMList
101-
try:
102-
# Let NumPy coerce dtype (e.g., multidimensional arrays)
103-
return np.array([psm[item] for psm in self.psm_list])
104-
except ValueError:
105-
# If dtype is not consistent, force dtype to be object
106-
return np.array([psm[item] for psm in self.psm_list], dtype=object)
101+
return np.fromiter([psm[item] for psm in self.psm_list], dtype=object, count=len(self))
107102
elif _is_iterable_of_bools(item):
108103
# Return new PSMList with items that were True
109104
return PSMList(psm_list=[self.psm_list[i] for i in np.flatnonzero(item)])

tests/test_psm_list.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22
import pytest
33

4-
from psm_utils import Peptidoform, PSM, PSMList
4+
from psm_utils import PSM, Peptidoform, PSMList
55

66
sample_psm_list = [
77
PSM(peptidoform="ACDK", spectrum_id=1, score=140.2),
@@ -42,7 +42,7 @@ def test___get_item__(self):
4242
# Multiple PSM properties as 2D array
4343
np.testing.assert_equal(
4444
psm_list[["spectrum_id", "score"]],
45-
np.array([["1", 140.2], ["2", 132.9], ["3", 55.7]]),
45+
np.array([["1", 140.2], ["2", 132.9], ["3", 55.7]], dtype=object),
4646
)
4747

4848
# Index by multiple indices

0 commit comments

Comments
 (0)