Skip to content

Commit 9c71fd7

Browse files
committed
Better split syntax and allow accessing MeasurementImages from groups
1 parent 25ee9de commit 9c71fd7

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

SEImplementation/python/sextractorxx/config/measurement_images.py

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ def __iter__(self):
262262

263263
def split(self, grouping_method):
264264
"""
265-
Splits the group in various subgroups, applying a filter on the contained images.
265+
Splits the group in various subgroups, applying a filter on the contained images. If the group has
266+
already been split, applies the split to each subgroup.
266267
267268
Parameters
268269
----------
@@ -278,20 +279,23 @@ def split(self, grouping_method):
278279
Raises
279280
-------
280281
ValueError
281-
If the group has been already split, or if some images have not been grouped by the callable.
282+
If some images have not been grouped by the callable.
282283
"""
283284
if self.__subgroups:
284-
raise ValueError('ImageGroup is already subgrouped')
285-
subgrouped_images = grouping_method(self.__images)
286-
if sum(len(p[1]) for p in subgrouped_images) != len(self.__images):
287-
self.__subgroups = None
288-
raise ValueError('Some images were not grouped')
289-
self.__subgroups = []
290-
for k, im_list in subgrouped_images:
291-
assert k not in self.__subgroup_names
292-
self.__subgroup_names.add(k)
293-
self.__subgroups.append((k, ImageGroup(images=im_list)))
294-
self.__images = []
285+
#if we are already subgrouped, apply the split to the subgroups
286+
for _, sub_group in self.__subgroups:
287+
sub_group.split(grouping_method)
288+
else:
289+
subgrouped_images = grouping_method(self.__images)
290+
if sum(len(p[1]) for p in subgrouped_images) != len(self.__images):
291+
self.__subgroups = None
292+
raise ValueError('Some images were not grouped')
293+
self.__subgroups = []
294+
for k, im_list in subgrouped_images:
295+
assert k not in self.__subgroup_names
296+
self.__subgroup_names.add(k)
297+
self.__subgroups.append((k, ImageGroup(images=im_list)))
298+
self.__images = []
295299

296300
def add_images(self, images):
297301
"""
@@ -715,32 +719,35 @@ def __iter__(self):
715719
else:
716720
return self.__images.__iter__()
717721

718-
def __getitem__(self, name):
722+
def __getitem__(self, index):
719723
"""
720-
The subgroup with the given name.
724+
The subgroup with the given name or image with the given index depending on whether this is a leaf group.
721725
722726
Parameters
723727
----------
724-
name : str
725-
Subgroup name
728+
index : str or int
729+
Subgroup name or image index
726730
727731
Returns
728732
-------
729-
MeasurementGroup
733+
MeasurementGroup or MeasurementImage
730734
731735
Raises
732736
------
733-
ValueError
734-
If the group does not have subgroups.
735737
KeyError
736-
If the group has not been found.
738+
If we can't find what we want
737739
"""
738-
if self.__subgroups is None:
739-
raise ValueError('Does not contain subgroups')
740-
try:
741-
return next(x for x in self.__subgroups if x[0] == name)[1]
742-
except StopIteration:
743-
raise KeyError('Group {} not found'.format(name))
740+
741+
if self.__subgroups:
742+
try:
743+
return next(x for x in self.__subgroups if x[0] == index)[1]
744+
except StopIteration:
745+
raise KeyError('Group {} not found'.format(index))
746+
else:
747+
try:
748+
return self.__images[index]
749+
except:
750+
raise KeyError('Image #{} not found'.format(index))
744751

745752
def __len__(self):
746753
"""

0 commit comments

Comments
 (0)