Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/plopp/widgets/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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')
)
Expand All @@ -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)])
Expand All @@ -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]:
Expand Down Expand Up @@ -180,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):
sl = slice(*sl)
# 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
5 changes: 4 additions & 1 deletion tests/widgets/slice_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading