Skip to content

Commit db6f694

Browse files
authored
Merge pull request #124 from Exabyte-io/feature/SOF-7662
Feature/sof 7662 add reset ids
2 parents 5ef787c + b1c32b6 commit db6f694

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ dependencies = [
1919
"numpy",
2020
"jsonschema>=2.6.0",
2121
"pydantic>=2.7.1",
22-
"mat3ra-esse>=2025.4.15.post0",
22+
"mat3ra-esse>=2025.7.1-0",
2323
"mat3ra-utils>=2024.5.15.post0",
2424
]
2525

src/py/mat3ra/code/array_with_ids.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def filter_by_indices(self, indices: Union[List[int], int]):
7474
self.values = [self.values[i] for i in range(len(self.values)) if i in index_set]
7575
self.ids = [self.ids[i] for i in range(len(self.ids)) if i in index_set]
7676

77-
def filter_by_ids(self, ids: Union[List[int], int], invert: bool = False):
77+
def filter_by_ids(self, ids: Union[List[int], int], invert: bool = False, reset_ids: bool = False):
7878
if isinstance(ids, int):
7979
ids = [ids]
8080
if not invert:
@@ -84,6 +84,8 @@ def filter_by_ids(self, ids: Union[List[int], int], invert: bool = False):
8484
keep_indices = [index for index, id_ in enumerate(self.ids) if id_ in ids_set]
8585
self.values = [self.values[index] for index in keep_indices]
8686
self.ids = [self.ids[index] for index in keep_indices]
87+
if reset_ids:
88+
self.ids = list(range(len(self.values)))
8789

8890
def __eq__(self, other: object) -> bool:
8991
return isinstance(other, ArrayWithIds) and self.values == other.values and self.ids == other.ids

tests/py/unit/test_array_with_ids.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,32 @@ def test_filter_by_ids():
208208
)
209209

210210

211+
def test_filter_by_ids_reset_ids():
212+
"""Test that reset_ids parameter resets IDs to consecutive integers starting from 0"""
213+
instance = ArrayWithIds(**ARRAY_WITH_IDS_ARRAYS_OF_FLOAT_VALUES_CONFIG_NON_CONSECUTIVE)
214+
# Filter to keep only first and third elements (ids 2 and 6)
215+
instance.filter_by_ids([2, 6], reset_ids=True)
216+
217+
# Values should be filtered correctly
218+
assert instance.values == [
219+
ARRAY_WITH_IDS_ARRAYS_OF_FLOAT_VALUES_CONFIG_NON_CONSECUTIVE["values"][0],
220+
ARRAY_WITH_IDS_ARRAYS_OF_FLOAT_VALUES_CONFIG_NON_CONSECUTIVE["values"][2],
221+
]
222+
# IDs should be reset to consecutive integers starting from 0
223+
assert instance.ids == [0, 1]
224+
225+
# Test with invert=True
226+
instance = ArrayWithIds(**ARRAY_WITH_IDS_ARRAYS_OF_FLOAT_VALUES_CONFIG_NON_CONSECUTIVE)
227+
# Filter to exclude first element (id 2), keep second and third (ids 4, 6)
228+
instance.filter_by_ids([2], invert=True, reset_ids=True)
229+
230+
assert instance.values == [
231+
ARRAY_WITH_IDS_ARRAYS_OF_FLOAT_VALUES_CONFIG_NON_CONSECUTIVE["values"][1],
232+
ARRAY_WITH_IDS_ARRAYS_OF_FLOAT_VALUES_CONFIG_NON_CONSECUTIVE["values"][2],
233+
]
234+
assert instance.ids == [0, 1]
235+
236+
211237
def test_filter_by_ids_invert():
212238
instance = ArrayWithIds(**ARRAY_WITH_IDS_ARRAYS_OF_FLOAT_VALUES_CONFIG)
213239
instance.filter_by_ids([0, 2], invert=True)

0 commit comments

Comments
 (0)