Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
interactivity modification
  • Loading branch information
Caitlin Lewis
Caitlin Lewis committed Dec 28, 2022
commit 76a82f187f8611d159b14da2066ac7456e33b81c
43 changes: 19 additions & 24 deletions fastplotlib/graphics/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pygfx.linalg import Vector3

from .features import GraphicFeature, PresentFeature
#from ._collection import GraphicCollection

from abc import ABC, abstractmethod
from dataclasses import dataclass
Expand All @@ -16,6 +17,10 @@ def __init_subclass__(cls, **kwargs):


class Graphic(BaseGraphic):
pygfx_events = [
"click"
]

def __init__(
self,
name: str = None
Expand All @@ -33,16 +38,6 @@ def __init__(
self.registered_callbacks = dict()
self.present = PresentFeature(parent=self)

#valid_features = ["visible"]
self._feature_events = list()
for attr_name in self.__dict__.keys():
attr = getattr(self, attr_name)
if isinstance(attr, GraphicFeature):
self._feature_events.append(attr_name)

self._feature_events = tuple(self._feature_events)
self._pygfx_events = ("click",)

@property
def world_object(self) -> WorldObject:
return self._world_object
Expand Down Expand Up @@ -95,43 +90,43 @@ def _set_feature(self, feature: str, new_data: Any, indices: Any):
def _reset_feature(self, feature: str):
pass

def link(self, event_type: str, target: Any, feature: str, new_data: Any, indices_mapper: callable = None):
if event_type in self._pygfx_events:
def link(self, event_type: str, target: Any, feature: str, new_data: Any, callback_function: callable = None):
if event_type in self.pygfx_events:
self.world_object.add_event_handler(self.event_handler, event_type)
elif event_type in self._feature_events:
elif event_type in self.feature_events:
feature = getattr(self, event_type)
Comment thread
clewis7 marked this conversation as resolved.
Outdated
feature.add_event_handler(self.event_handler, event_type)
else:
raise ValueError("event not possible")

if event_type in self.registered_callbacks.keys():
self.registered_callbacks[event_type].append(
CallbackData(target=target, feature=feature, new_data=new_data, indices_mapper=indices_mapper))
CallbackData(target=target, feature=feature, new_data=new_data, callback_function=callback_function))
else:
self.registered_callbacks[event_type] = list()
self.registered_callbacks[event_type].append(
CallbackData(target=target, feature=feature, new_data=new_data, indices_mapper=indices_mapper))
CallbackData(target=target, feature=feature, new_data=new_data, callback_function=callback_function))

def event_handler(self, event):
event_info = event.pick_info
#click_info = np.array(event.pick_info["index"])
if event.type in self.registered_callbacks.keys():
for target_info in self.registered_callbacks[event.type]:
if target_info.indices_mapper is not None:
indices = target_info.indices_mapper(source=self, indices=click_info)
if target_info.callback_function is not None:
# if callback_function is not None, then callback function should handle the entire event
target_info.callback_function(source=self, event=event, new_data=target_info.new_data)
# elif isinstance(self, GraphicCollection):
# indices = event.pick_info["collection_index"]
# target_info.target._set_feature(feature=target_info.feature, new_data=target_info.new_data, indices=indices)
else:
indices = None
# set feature of target at indice using new data
target_info.target._set_feature(feature=target_info.feature, new_data=target_info.new_data, indices=indices)

target_info.target._set_feature(feature=target_info.feature, new_data=target_info.new_data,
indices=None)
Comment thread
clewis7 marked this conversation as resolved.

@dataclass
class CallbackData:
"""Class for keeping track of the info necessary for interactivity after event occurs."""
target: Any
feature: str
new_data: Any
indices_mapper: callable = None
callback_function: callable = None


@dataclass
Expand Down
19 changes: 17 additions & 2 deletions fastplotlib/graphics/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

import pygfx

from ._base import Graphic
from ._base import Graphic, Interaction, PreviouslyModifiedData
from .features import ImageCmapFeature, ImageDataFeature
from ..utils import quick_min_max


class ImageGraphic(Graphic):
class ImageGraphic(Graphic, Interaction):
feature_events = [
"data-changed",
"color-changed",
"cmap-changed",
]
def __init__(
self,
data: Any,
Expand Down Expand Up @@ -88,3 +93,13 @@ def vmax(self, value: float):
self.world_object.material.clim[0],
value
)

def _set_feature(self, feature: str, new_data: Any, indices: Any):
pass

def _reset_feature(self, feature: str):
pass




28 changes: 14 additions & 14 deletions fastplotlib/graphics/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,21 @@ def _set_feature(self, feature: str, new_data: Any, indices: Any = None):
self._previous_data = {}
Comment thread
clewis7 marked this conversation as resolved.
Outdated
elif hasattr(self, "_previous_data"):
self._reset_feature(feature)
if feature in self._feature_events:
feature_instance = getattr(self, feature)
if indices is not None:
previous = feature_instance[indices].copy()
feature_instance[indices] = new_data
else:
previous = feature_instance[:].copy()
feature_instance[:] = new_data
if feature in self._previous_data.keys():
self._previous_data[feature].previous_data = previous
self._previous_data[feature].previous_indices = indices
else:
self._previous_data[feature] = PreviouslyModifiedData(previous_data=previous, previous_indices=indices)
# if feature in self._feature_events:
feature_instance = getattr(self, feature)
if indices is not None:
previous = feature_instance[indices].copy()
feature_instance[indices] = new_data
else:
previous = feature_instance[:].copy()
Comment thread
clewis7 marked this conversation as resolved.
Outdated
feature_instance[:] = new_data
Comment thread
clewis7 marked this conversation as resolved.
Outdated
if feature in self._previous_data.keys():
self._previous_data[feature].previous_data = previous
self._previous_data[feature].previous_indices = indices
else:
raise ValueError("name arg is not a valid feature")
self._previous_data[feature] = PreviouslyModifiedData(previous_data=previous, previous_indices=indices)
# else:
# raise ValueError("name arg is not a valid feature")

def _reset_feature(self, feature: str):
if feature not in self._previous_data.keys():
Expand Down
38 changes: 34 additions & 4 deletions fastplotlib/graphics/linecollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
class LineCollection(GraphicCollection, Interaction):
"""Line Collection graphic"""
child_type = LineGraphic

feature_events = [
"data-changed",
"color-changed",
"cmap-changed",
]
def __init__(
self,
data: List[np.ndarray],
Expand Down Expand Up @@ -115,8 +119,34 @@ def __init__(

self.add_graphic(lg, reset_index=False)

def _set_feature(self, feature: str, new_data: Any, indices: Any):
pass
def _set_feature(self, feature: str, new_data: Any, indices: Union[int, range]):
if not hasattr(self, "_previous_data"):
self._previous_data = {}
elif hasattr(self, "_previous_data"):
self._reset_feature(feature)
#if feature in # need a way to check if feature is in
feature_instance = getattr(self, feature)
Comment thread
clewis7 marked this conversation as resolved.
Outdated
if indices is not None:
previous = feature_instance[indices].copy()
Comment thread
clewis7 marked this conversation as resolved.
Outdated
feature_instance[indices] = new_data
Comment thread
clewis7 marked this conversation as resolved.
Outdated
else:
previous = feature_instance[:].copy()
Comment thread
clewis7 marked this conversation as resolved.
Outdated
feature_instance[:] = new_data
if feature in self._previous_data.keys():
self._previous_data[feature].previous_data = previous
self._previous_data[feature].previous_indices = indices
else:
self._previous_data[feature] = PreviouslyModifiedData(previous_data=previous, previous_indices=indices)
# else:
# raise ValueError("name arg is not a valid feature")

def _reset_feature(self, feature: str):
pass
if feature not in self._previous_data.keys():
raise ValueError("no previous data registered for this feature")
else:
feature_instance = getattr(self, feature)
Comment thread
clewis7 marked this conversation as resolved.
Outdated
if self._previous_data[feature].previous_indices is not None:
feature_instance[self._previous_data[feature].previous_indices] = self._previous_data[
feature].previous_data
else:
feature_instance[:] = self._previous_data[feature].previous_data