From 7d6fc21b5247b9bff975d869f479e3a2c6da5c71 Mon Sep 17 00:00:00 2001 From: Neil Vaytet Date: Wed, 4 Feb 2026 23:32:11 +0100 Subject: [PATCH 1/2] fix boundaries and label of slider with bin edge coord --- src/plopp/widgets/slice.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/plopp/widgets/slice.py b/src/plopp/widgets/slice.py index 6f85cd62..c8873ab3 100644 --- a/src/plopp/widgets/slice.py +++ b/src/plopp/widgets/slice.py @@ -34,6 +34,7 @@ def __init__( 'readout': False, 'layout': {"width": "15.2em", "margin": "0px 0px 0px 10px"}, } + self._is_bin_edges = coord.sizes[dim] > size self.dim_label = ipw.Label(value=dim) self.slider = slider_constr(**widget_args) self.continuous_update = ipw.Checkbox( @@ -42,9 +43,7 @@ def __init__( indent=False, layout={"width": "1.52em"}, ) - self.label = ipw.Label( - value=coord_element_to_string(coord[dim, self.slider.value]) - ) + self.label = ipw.Label() ipw.jslink( (self.continuous_update, 'value'), (self.slider, 'continuous_update') ) @@ -64,6 +63,7 @@ def __init__( self.dim = dim self.coord = coord + self._update_label({"new": self.slider.value}) self.slider.observe(self._update_label, names='value') super().__init__([ipw.HBox(children)]) @@ -73,7 +73,13 @@ def _update_label(self, change: dict[str, Any]): Update the readout label with the coordinate value, instead of the integer readout index. """ - self.label.value = coord_element_to_string(self.coord[self.dim, change['new']]) + inds = change["new"] + if self._is_bin_edges: + if isinstance(inds, tuple): + inds = (inds[0], inds[1] + 1) + else: + inds = (inds, inds + 1) + self.label.value = coord_element_to_string(self.coord[self.dim, inds]) @property def value(self) -> int | tuple[int, int]: @@ -180,6 +186,6 @@ def slice_dims(data_array: sc.DataArray, slices: dict[str, slice]) -> sc.DataArr out = data_array for dim, sl in slices.items(): if isinstance(sl, tuple): - sl = slice(*sl) + sl = slice(sl[0], sl[1] + 1) out = out[dim, sl] return out From c3d427b2f79a2ed8e76f83f5d878039b68934dda Mon Sep 17 00:00:00 2001 From: Neil Vaytet Date: Thu, 5 Feb 2026 11:50:49 +0100 Subject: [PATCH 2/2] fix test and clarify slicing behaviour --- src/plopp/widgets/slice.py | 2 ++ tests/widgets/slice_test.py | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/plopp/widgets/slice.py b/src/plopp/widgets/slice.py index c8873ab3..39ef8ce8 100644 --- a/src/plopp/widgets/slice.py +++ b/src/plopp/widgets/slice.py @@ -186,6 +186,8 @@ def slice_dims(data_array: sc.DataArray, slices: dict[str, slice]) -> sc.DataArr out = data_array for dim, sl in slices.items(): if isinstance(sl, tuple): + # Include the stop index in the slice, as we expect both slider handles to + # be inclusive. sl = slice(sl[0], sl[1] + 1) out = out[dim, sl] return out diff --git a/tests/widgets/slice_test.py b/tests/widgets/slice_test.py index 96d3053f..47208cfc 100644 --- a/tests/widgets/slice_test.py +++ b/tests/widgets/slice_test.py @@ -60,5 +60,8 @@ def test_slice_dims(): def test_range_slice_dims(): da = data_array(ndim=3) slices = {'xx': (8, 9), 'yy': (7, 10)} - expected = da['xx', slice(*slices['xx'])]['yy', slice(*slices['yy'])] + # Note that we want to include the stop index in the slice + expected = da['xx', slices['xx'][0] : slices['xx'][1] + 1][ + 'yy', slices['yy'][0] : slices['yy'][1] + 1 + ] assert identical(slice_dims().func(da, slices=slices), expected)