diff --git a/fastplotlib/graphics/features/_base.py b/fastplotlib/graphics/features/_base.py index 57cd15a1d..439cba484 100644 --- a/fastplotlib/graphics/features/_base.py +++ b/fastplotlib/graphics/features/_base.py @@ -138,6 +138,9 @@ def remove_event_handler(self, handler: callable): self._event_handlers.remove(handler) + def clear_event_handlers(self): + self._event_handlers.clear() + #TODO: maybe this can be implemented right here in the base class @abstractmethod def _feature_changed(self, key: Union[int, slice, Tuple[slice]], new_data: Any): @@ -227,10 +230,12 @@ def cleanup_slice(key: Union[int, slice], upper_bound) -> Union[slice, int]: # return slice(int(start), int(stop), int(step)) -def cleanup_array_slice(key: np.ndarray, upper_bound) -> np.ndarray: +def cleanup_array_slice(key: np.ndarray, upper_bound) -> Union[np.ndarray, None]: """ Cleanup numpy array used for fancy indexing, make sure key[-1] <= upper_bound. + Returns None if nothing to change. + Parameters ---------- key: np.ndarray @@ -254,6 +259,9 @@ def cleanup_array_slice(key: np.ndarray, upper_bound) -> np.ndarray: if key.dtype == bool: key = np.nonzero(key)[0] + if key.size < 1: + return None + # make sure indices within bounds of feature buffer range if key[-1] > upper_bound: raise IndexError(f"Index: `{key[-1]}` out of bounds for feature array of size: `{upper_bound}`") diff --git a/fastplotlib/graphics/features/_colors.py b/fastplotlib/graphics/features/_colors.py index 5ff82ca72..50bb0ce3f 100644 --- a/fastplotlib/graphics/features/_colors.py +++ b/fastplotlib/graphics/features/_colors.py @@ -129,6 +129,9 @@ def __setitem__(self, key, value): elif isinstance(key, np.ndarray): key = cleanup_array_slice(key, self._upper_bound) + if key is None: + return + indices = key else: diff --git a/fastplotlib/graphics/selectors/_base_selector.py b/fastplotlib/graphics/selectors/_base_selector.py index 64934b6fa..7d108efc6 100644 --- a/fastplotlib/graphics/selectors/_base_selector.py +++ b/fastplotlib/graphics/selectors/_base_selector.py @@ -324,4 +324,10 @@ def __del__(self): self._plot_area.renderer.remove_event_handler(self._key_up, "key_up") # remove animation func - self._plot_area.remove_animation(self._key_hold) \ No newline at end of file + self._plot_area.remove_animation(self._key_hold) + + if hasattr(self, "feature_events"): + feature_names = getattr(self, "feature_events") + for n in feature_names: + fea = getattr(self, n) + fea.clear_event_handlers() diff --git a/fastplotlib/graphics/selectors/_linear_region.py b/fastplotlib/graphics/selectors/_linear_region.py index 30f223fad..17751f51e 100644 --- a/fastplotlib/graphics/selectors/_linear_region.py +++ b/fastplotlib/graphics/selectors/_linear_region.py @@ -11,6 +11,9 @@ class LinearBoundsFeature(GraphicFeature): + feature_events = ( + "data", + ) """ Feature for a linearly bounding region @@ -121,10 +124,6 @@ def _feature_changed(self, key: Union[int, slice, Tuple[slice]], new_data: Any): class LinearRegionSelector(Graphic, BaseSelector): - feature_events = ( - "bounds" - ) - def __init__( self, bounds: Tuple[int, int], diff --git a/fastplotlib/layouts/_base.py b/fastplotlib/layouts/_base.py index f3781a4e7..105eabb64 100644 --- a/fastplotlib/layouts/_base.py +++ b/fastplotlib/layouts/_base.py @@ -394,6 +394,9 @@ def clear(self): for g in self.graphics: self.delete_graphic(g) + for s in self.selectors: + self.delete_graphic(s) + def __getitem__(self, name: str): for graphic in self.graphics: if graphic.name == name: