From efb808cbacee36f1544b90346c1c50250fe26e1f Mon Sep 17 00:00:00 2001 From: Marwan Zouinkhi Date: Tue, 13 Feb 2024 13:32:23 -0500 Subject: [PATCH 1/4] fix undefined padded --- gunpowder/nodes/pad.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/gunpowder/nodes/pad.py b/gunpowder/nodes/pad.py index 758fd04a..0439ef96 100644 --- a/gunpowder/nodes/pad.py +++ b/gunpowder/nodes/pad.py @@ -129,8 +129,11 @@ def __expand(self, a, from_roi, to_roi, value): lower_pad = from_roi.begin - to_roi.begin upper_pad = to_roi.end - from_roi.end pad_width = [(0, 0)] * num_channels + list(zip(lower_pad, upper_pad)) - if self.mode == "constant": - padded = np.pad(a, pad_width, "constant", constant_values=value) - elif self.mode == "reflect": + if self.mode == "reflect": padded = np.pad(a, pad_width, "reflect") + elif self.mode == "constant": + padded = np.pad(a, pad_width, "constant", constant_values=value) + else: + logger.error("Unknown mode %s, using 'constant' instead" % self.mode) + padded = np.pad(a, pad_width, "constant", constant_values=value) return padded From d8f0cc97d7a3d03b28ac2748e3ebdeae1596657d Mon Sep 17 00:00:00 2001 From: Marwan Zouinkhi Date: Wed, 14 Feb 2024 08:53:05 -0500 Subject: [PATCH 2/4] more informative bug --- gunpowder/nodes/pad.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/gunpowder/nodes/pad.py b/gunpowder/nodes/pad.py index 0439ef96..5608aa98 100644 --- a/gunpowder/nodes/pad.py +++ b/gunpowder/nodes/pad.py @@ -44,6 +44,8 @@ def __init__(self, key, size, mode="constant", value=None): self.key = key self.size = size self.mode = mode + if self.mode not in ["constant", "reflect"]: + raise ValueError("Invalid padding mode %s provided. Must be 'constant' or 'reflect'." % self.mode) self.value = value def setup(self): @@ -134,6 +136,5 @@ def __expand(self, a, from_roi, to_roi, value): elif self.mode == "constant": padded = np.pad(a, pad_width, "constant", constant_values=value) else: - logger.error("Unknown mode %s, using 'constant' instead" % self.mode) - padded = np.pad(a, pad_width, "constant", constant_values=value) + raise ValueError("Invalid padding mode %s provided. Must be 'constant' or 'reflect'." % self.mode) return padded From 995d11fdfce5d04b15d9dbd4319965a8441da883 Mon Sep 17 00:00:00 2001 From: Marwan Zouinkhi Date: Wed, 14 Feb 2024 08:55:29 -0500 Subject: [PATCH 3/4] black format --- gunpowder/contrib/nodes/add_vector_map.py | 6 +++--- gunpowder/nodes/defect_augment.py | 16 ++++++++++------ gunpowder/nodes/pad.py | 10 ++++++++-- gunpowder/nodes/simple_augment.py | 16 ++++++++++------ gunpowder/nodes/specified_location.py | 6 +++--- 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/gunpowder/contrib/nodes/add_vector_map.py b/gunpowder/contrib/nodes/add_vector_map.py index f837669b..37ab3bf6 100644 --- a/gunpowder/contrib/nodes/add_vector_map.py +++ b/gunpowder/contrib/nodes/add_vector_map.py @@ -215,9 +215,9 @@ def __get_vector_map(self, batch, request, vector_map_array_key): if num_src_vectors_per_trg_loc > 0: dist_to_locs = {} for phys_loc in relevant_partner_loc: - dist_to_locs[ - np.linalg.norm(node.location - phys_loc) - ] = phys_loc + dist_to_locs[np.linalg.norm(node.location - phys_loc)] = ( + phys_loc + ) for nr, dist in enumerate( reversed(np.sort(list(dist_to_locs.keys()))) ): diff --git a/gunpowder/nodes/defect_augment.py b/gunpowder/nodes/defect_augment.py index 8f8fefd3..d6988519 100644 --- a/gunpowder/nodes/defect_augment.py +++ b/gunpowder/nodes/defect_augment.py @@ -159,9 +159,11 @@ def prepare(self, request): logger.debug("before growth: %s" % spec.roi) growth = Coordinate( tuple( - 0 - if d == self.axis - else raw_voxel_size[d] * self.deformation_strength + ( + 0 + if d == self.axis + else raw_voxel_size[d] * self.deformation_strength + ) for d in range(spec.roi.dims) ) ) @@ -267,9 +269,11 @@ def process(self, batch, request): old_roi = request[self.intensities].roi logger.debug("resetting roi to %s" % old_roi) crop = tuple( - slice(None) - if d == self.axis - else slice(self.deformation_strength, -self.deformation_strength) + ( + slice(None) + if d == self.axis + else slice(self.deformation_strength, -self.deformation_strength) + ) for d in range(raw.spec.roi.dims) ) raw.data = raw.data[crop] diff --git a/gunpowder/nodes/pad.py b/gunpowder/nodes/pad.py index 5608aa98..e6115af1 100644 --- a/gunpowder/nodes/pad.py +++ b/gunpowder/nodes/pad.py @@ -45,7 +45,10 @@ def __init__(self, key, size, mode="constant", value=None): self.size = size self.mode = mode if self.mode not in ["constant", "reflect"]: - raise ValueError("Invalid padding mode %s provided. Must be 'constant' or 'reflect'." % self.mode) + raise ValueError( + "Invalid padding mode %s provided. Must be 'constant' or 'reflect'." + % self.mode + ) self.value = value def setup(self): @@ -136,5 +139,8 @@ def __expand(self, a, from_roi, to_roi, value): elif self.mode == "constant": padded = np.pad(a, pad_width, "constant", constant_values=value) else: - raise ValueError("Invalid padding mode %s provided. Must be 'constant' or 'reflect'." % self.mode) + raise ValueError( + "Invalid padding mode %s provided. Must be 'constant' or 'reflect'." + % self.mode + ) return padded diff --git a/gunpowder/nodes/simple_augment.py b/gunpowder/nodes/simple_augment.py index f5a97333..efb23329 100644 --- a/gunpowder/nodes/simple_augment.py +++ b/gunpowder/nodes/simple_augment.py @@ -203,9 +203,11 @@ def process(self, batch, request): location_in_total_offset = np.asarray(node.location) - total_roi_offset node.location = np.asarray( [ - total_roi_end[dim] - location_in_total_offset[dim] - if m - else node.location[dim] + ( + total_roi_end[dim] - location_in_total_offset[dim] + if m + else node.location[dim] + ) for dim, m in enumerate(self.mirror) ], dtype=graph.spec.dtype, @@ -255,9 +257,11 @@ def __mirror_roi(self, roi, total_roi, mirror): end_of_roi_in_total = roi_in_total_offset + roi_shape roi_in_total_offset_mirrored = total_roi_shape - end_of_roi_in_total roi_offset = Coordinate( - total_roi_offset[d] + roi_in_total_offset_mirrored[d] - if mirror[d] - else roi_offset[d] + ( + total_roi_offset[d] + roi_in_total_offset_mirrored[d] + if mirror[d] + else roi_offset[d] + ) for d in range(self.dims) ) diff --git a/gunpowder/nodes/specified_location.py b/gunpowder/nodes/specified_location.py index cc5e5844..255703f7 100644 --- a/gunpowder/nodes/specified_location.py +++ b/gunpowder/nodes/specified_location.py @@ -106,9 +106,9 @@ def process(self, batch, request): for array_key, spec in request.array_specs.items(): batch.arrays[array_key].spec.roi = spec.roi if self.extra_data is not None: - batch.arrays[array_key].attrs[ - "specified_location_extra_data" - ] = self.extra_data[self.loc_i] + batch.arrays[array_key].attrs["specified_location_extra_data"] = ( + self.extra_data[self.loc_i] + ) for graph_key, spec in request.graph_specs.items(): batch.points[graph_key].spec.roi = spec.roi From b1d4412767ce0ffe538d1156f87c2add3e58d508 Mon Sep 17 00:00:00 2001 From: Marwan Zouinkhi Date: Wed, 14 Feb 2024 08:57:08 -0500 Subject: [PATCH 4/4] black format tests --- tests/cases/simple_augment.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/cases/simple_augment.py b/tests/cases/simple_augment.py index 0696213c..9243b5d2 100644 --- a/tests/cases/simple_augment.py +++ b/tests/cases/simple_augment.py @@ -233,9 +233,9 @@ def test_mismatched_voxel_multiples(): test_array = ArrayKey("TEST_ARRAY") data = np.zeros([3, 3]) - data[ - 2, 1 - ] = 1 # voxel has Roi((4, 2) (2, 2)). Contained in Roi((0, 0), (6, 4)). at 2, 1 + data[2, 1] = ( + 1 # voxel has Roi((4, 2) (2, 2)). Contained in Roi((0, 0), (6, 4)). at 2, 1 + ) source = ArraySource( test_array, Array(