Skip to content

selector garbage collection, use selection feature name, cleanup event handlers #209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 9 additions & 1 deletion fastplotlib/graphics/features/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot what this fixed 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clewis7 can you jog my memory? Is this when you do something like line_graphic.colors[xs > 0] = "w", but xs > 0 is an empty array?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, bc we kept getting an error when trying to set the colors and a behavior had not a occurred so the array was all zeroes, would never give True

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}`")
Expand Down
3 changes: 3 additions & 0 deletions fastplotlib/graphics/features/_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion fastplotlib/graphics/selectors/_base_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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()
7 changes: 3 additions & 4 deletions fastplotlib/graphics/selectors/_linear_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@


class LinearBoundsFeature(GraphicFeature):
feature_events = (
"data",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is supposed to be bounds, but as for #211 let's rename it to selection

)
"""
Feature for a linearly bounding region

Expand Down Expand Up @@ -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],
Expand Down
3 changes: 3 additions & 0 deletions fastplotlib/layouts/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down