Skip to content

add Deleted as a graphic feature #404

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

Merged
merged 1 commit into from
Feb 20, 2024
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
11 changes: 10 additions & 1 deletion fastplotlib/graphics/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from pygfx import WorldObject

from ._features import GraphicFeature, PresentFeature, GraphicFeatureIndexable
from ._features import GraphicFeature, PresentFeature, GraphicFeatureIndexable, Deleted

# dict that holds all world objects for a given python kernel/session
# Graphic objects only use proxies to WorldObjects
Expand Down Expand Up @@ -45,6 +45,12 @@ def __init_subclass__(cls, **kwargs):


class Graphic(BaseGraphic):
feature_events = {}

def __init_subclass__(cls, **kwargs):
# all graphics give off a feature event when deleted
cls.feature_events = {*cls.feature_events, "deleted"}

def __init__(
self,
name: str = None,
Expand Down Expand Up @@ -72,6 +78,8 @@ def __init__(
# store hex id str of Graphic instance mem location
self.loc: str = hex(id(self))

self.deleted = Deleted(self, False)

@property
def world_object(self) -> WorldObject:
"""Associated pygfx WorldObject. Always returns a proxy, real object cannot be accessed directly."""
Expand Down Expand Up @@ -168,6 +176,7 @@ def _cleanup(self):
pass

def __del__(self):
self.deleted = True
del WORLD_OBJECTS[self.loc]


Expand Down
2 changes: 2 additions & 0 deletions fastplotlib/graphics/_features/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ._thickness import ThicknessFeature
from ._base import GraphicFeature, GraphicFeatureIndexable, FeatureEvent, to_gpu_supported_dtype
from ._selection_features import LinearSelectionFeature, LinearRegionSelectionFeature
from ._deleted import Deleted

__all__ = [
"ColorFeature",
Expand All @@ -23,4 +24,5 @@
"to_gpu_supported_dtype",
"LinearSelectionFeature",
"LinearRegionSelectionFeature",
"Deleted",
]
41 changes: 41 additions & 0 deletions fastplotlib/graphics/_features/_deleted.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from ._base import GraphicFeature, FeatureEvent


class Deleted(GraphicFeature):
"""
Used when a graphic is deleted, triggers events that can be useful to indicate this graphic has been deleted

**event pick info:**

==================== ======================== =========================================================================
key type description
==================== ======================== =========================================================================
"collection-index" int the index of the graphic within the collection that triggered the event
"world_object" pygfx.WorldObject world object
==================== ======================== =========================================================================
"""

def __init__(self, parent, value: bool):
super(Deleted, self).__init__(parent, value)

def _set(self, value: bool):
Copy link
Member

Choose a reason for hiding this comment

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

So, this will get triggered when __del__ is called and self.deleted = True is set?

Copy link
Member

Choose a reason for hiding this comment

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

How are we preventing users from if a graphic has been deleted to still having reference to the graphic and trying to say something silly like graphic.deleted=False? Will this throw an error if the graphic has already been deleted? I would assume so because there will be no reference.

Copy link
Member Author

Choose a reason for hiding this comment

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

So, this will get triggered when __del__ is called and self.deleted = True is set?

Yes, Graphic.__del__ sets self.deleted = True https://github.com/fastplotlib/fastplotlib/pull/404/files#diff-a57994ecff1cde4e05c8175265219f0b5200da144ce8733b9eb560c09bcea7ddR179

How are we preventing users from if a graphic has been deleted to still having reference to the graphic and trying to say something silly like graphic.deleted=False? Will this throw an error if the graphic has already been deleted? I would assume so because there will be no reference.

Since users are only given acccess to weakreferences, it will throw a reference error if they try to do anything with a deleted Graphic.

value = self._parse_set_value(value)
self._feature_changed(key=None, new_data=value)

def _feature_changed(self, key, new_data):
# this is a non-indexable feature so key=None

pick_info = {
"index": None,
"collection-index": self._collection_index,
Copy link
Member

Choose a reason for hiding this comment

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

What is the difference here between index vs collection-index? Also, collection-index could be None if the graphic is not a CollectionGraphic, no?

Copy link
Member

Choose a reason for hiding this comment

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

It seems like index will always be None?

Copy link
Member Author

Choose a reason for hiding this comment

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

index is buffer index, used for things like data and colors features. Not used here since it's not relevant

"world_object": self._parent.world_object,
"new_data": new_data,
}

event_data = FeatureEvent(type="deleted", pick_info=pick_info)

self._call_event_handlers(event_data)

def __repr__(self) -> str:
s = f"DeletedFeature for {self._parent}"
return s
7 changes: 4 additions & 3 deletions fastplotlib/graphics/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def _add_plot_area_hook(self, plot_area):


class ImageGraphic(Graphic, Interaction, _AddSelectorsMixin):
feature_events = ("data", "cmap", "present")
feature_events = {"data", "cmap", "present"}

def __init__(
self,
Expand Down Expand Up @@ -345,10 +345,11 @@ def col_chunk_index(self, index: int):


class HeatmapGraphic(Graphic, Interaction, _AddSelectorsMixin):
feature_events = (
feature_events = {
"data",
"cmap",
)
"present"
}

def __init__(
self,
Expand Down
2 changes: 1 addition & 1 deletion fastplotlib/graphics/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


class LineGraphic(Graphic, Interaction):
feature_events = ("data", "colors", "cmap", "thickness", "present")
feature_events = {"data", "colors", "cmap", "thickness", "present"}

def __init__(
self,
Expand Down
2 changes: 1 addition & 1 deletion fastplotlib/graphics/line_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class LineCollection(GraphicCollection, Interaction):
child_type = LineGraphic.__name__
feature_events = ("data", "colors", "cmap", "thickness", "present")
feature_events = {"data", "colors", "cmap", "thickness", "present"}

def __init__(
self,
Expand Down
2 changes: 1 addition & 1 deletion fastplotlib/graphics/scatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


class ScatterGraphic(Graphic):
feature_events = ("data", "sizes", "colors", "cmap", "present")
feature_events = {"data", "sizes", "colors", "cmap", "present"}

def __init__(
self,
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy