Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

beginning base logic for interactivity impl #61

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 23 commits into from
Closed
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
ugly but functional
  • Loading branch information
clewis7 committed Dec 20, 2022
commit 9309b415e45da2dd611e7b8ff0f32ea7dde8fa01
247 changes: 247 additions & 0 deletions 247 examples/linecollection_event.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions 4 fastplotlib/graphics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"LineGraphic",
"HistogramGraphic",
"HeatmapGraphic",
"LineCollection",
"TextGraphic"
"TextGraphic",
"LineCollection"
]
40 changes: 28 additions & 12 deletions 40 fastplotlib/graphics/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from abc import ABC, abstractmethod
from dataclasses import dataclass
# from .linecollection import LineCollection

class Graphic:
def __init__(
Expand Down Expand Up @@ -74,14 +73,12 @@ def __repr__(self):

class Interaction(ABC):
@property
@abstractmethod
def indices(self) -> Any:
pass
return self.indices

@indices.setter
@abstractmethod
def indices(self, indices: Any):
pass
self.indices = indices

@property
@abstractmethod
Expand All @@ -93,34 +90,53 @@ def _set_feature(self, feature: str, new_data: Any, indices: Any):
pass

clewis7 marked this conversation as resolved.
Show resolved Hide resolved
@abstractmethod
def _reset_feature(self, feature: str, old_data: Any, indices: Any):
def _reset_feature(self, feature: str, old_data: Any):
pass

def link(self, event_type: str, target: Graphic, feature: str, new_data: Any, indices_mapper: callable = None):
def link(self, event_type: str, target: Any, feature: str, new_data: Any, indices_mapper: callable = None):
valid_events = ["click"]
if event_type in valid_events:
self.world_object.add_event_handler(self.event_handler, event_type)
else:
raise ValueError("event not possible")

if isinstance(target.data, List):
old_data = list()
for line in target.data:
old_data.append(getattr(line, feature).copy())
else:
old_data = getattr(target, feature).copy()

if event_type in self.registered_callbacks.keys():
self.registered_callbacks[event_type].append(
CallbackData(target=target, feature=feature, new_data=new_data, old_data=getattr(target, feature).copy()))
CallbackData(target=target, feature=feature, new_data=new_data, old_data=old_data, indices_mapper=indices_mapper))
Copy link
Member

Choose a reason for hiding this comment

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

old_data shouldn't be in CallbackData, old_data should be managed by the target graphic event time it handles an event. When the target graphic handles an event, it should have a copy of the feature data at the indices that are being modified and reset the feature at those indices, and then set the feature w.r.t. to the new CallbackData.

To elaborate:

  1. The target graphic keeps a private dict of (features, orig_vals) that were modified at certain indices from the previous callback, something in the form {"feature": (indices, orig_vals_at_indices)}, you could call it self._previous_modified. And with the new GraphicFeature class from indexable graphic attributes #78 we won't even need a dict for it, we can just add _modified_indices and _orig_vals_at_modified_indices as private attributes to GraphicFeature 😄 .

  2. In _set_feature() it first uses the information from above to reset the feature at those indices to their original values

Copy link
Member

Choose a reason for hiding this comment

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

with #78 it might be possible for us to just pass the GraphicFeature instance directly instead of the string name when calling link, for example:

graphic.link(event_type, target=some_graphic, feature=some_graphic.color...

Copy link
Member Author

Choose a reason for hiding this comment

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

If info for resetting feature is moved to _set_feature() graphic, then we will no longer need method for _reset_feature()?

Copy link
Member

Choose a reason for hiding this comment

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

we should still have a _reset_feature() to separate functionality

else:
self.registered_callbacks[event_type] = list()
self.registered_callbacks[event_type].append(
CallbackData(target=target, feature=feature, new_data=new_data, old_data=getattr(target, feature).copy()))
CallbackData(target=target, feature=feature, new_data=new_data, old_data=old_data, indices_mapper=indices_mapper))

def event_handler(self, event):
clewis7 marked this conversation as resolved.
Show resolved Hide resolved
if event.type == "click":
# storing click information for each click in self.indices
#self.indices(np.array(event.pick_info["index"]))
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]:
target_info.target._reset_feature(feature=target_info.feature, old_data=target_info.old_data, indices=None)
target_info.target._set_feature(feature=target_info.feature, new_data=target_info.new_data, indices=None)
# need to map the indices to the target using indices_mapper
if target_info.indices_mapper is not None:
indices = target_info.indices_mapper(target=target_info.target, indices=click_info)
Copy link
Member

Choose a reason for hiding this comment

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

an indices_mapper function should be given source and target, something like this:

indices_mapper(source=self, target=target_info.target, event_indices=click_info

And the user can write their function to take in all 3 and do whatever they want.

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, I assumed that some general form would need to be established

else:
indices = None
# reset feature of target using stored old data
Copy link
Member

Choose a reason for hiding this comment

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

ah I see you've done it like this, but it's better for the target graphic to handle old data because we want the "feature resetting at previously modified indices" to happen regardless of the event type. For example, if we've registered different types of callbacks to keyboard events, and different mouse events, we want the same reset to happen regardless of what previous event.type triggered the previous feature data to change.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree! Just wasn't sure at the time the best way to implement, but now that graphic features exist it should be a lot easier to store old data!

target_info.target._reset_feature(feature=target_info.feature, old_data=target_info.old_data)
# 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=indices)

@dataclass
class CallbackData:
"""Class for keeping track of the info necessary for interactivity after event occurs."""
target: Graphic
target: Any
feature: str
new_data: Any
old_data: Any
indices_mapper: callable = None
12 changes: 2 additions & 10 deletions 12 fastplotlib/graphics/image.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
<<<<<<< HEAD
from typing import Tuple, Any, List
=======
from typing import *
clewis7 marked this conversation as resolved.
Show resolved Hide resolved
>>>>>>> a9990946faa8b990ee2f5ab0f2fff93d99f18d9e

import numpy as np
import pygfx
Expand Down Expand Up @@ -76,18 +72,14 @@ def __init__(
pygfx.ImageBasicMaterial(clim=(vmin, vmax), map=get_cmap_texture(cmap))
)

@property
def indices(self) -> Any:
pass

@property
def features(self) -> List[str]:
pass
return ["cmap", "data"]
clewis7 marked this conversation as resolved.
Show resolved Hide resolved

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

def _reset_feature(self):
def _reset_feature(self, feature: str, old_data: Any):
pass

@property
Expand Down
12 changes: 4 additions & 8 deletions 12 fastplotlib/graphics/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,19 @@ def update_colors(self, colors: np.ndarray):
self.world_object.geometry.colors.data[:] = self.colors
self.world_object.geometry.colors.update_range()

@property
def indices(self) -> Any:
return None

@property
def features(self) -> List[str]:
return None
return ["colors", "data"]

def _set_feature(self, feature: str, new_data: Any, indices: Any = None):
if feature in ["colors", "data"]:
if feature in self.features:
update_func = getattr(self, f"update_{feature}")
update_func(new_data)
else:
raise ValueError("name arg is not a valid feature")

def _reset_feature(self, feature: str, old_data: Any, indices: Any = None):
if feature in ["colors", "data"]:
def _reset_feature(self, feature: str, old_data: Any):
if feature in self.features:
update_func = getattr(self, f"update_{feature}")
update_func(old_data)
else:
Expand Down
40 changes: 18 additions & 22 deletions 40 fastplotlib/graphics/linecollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
import pygfx
from typing import Union, List

from .line import LineGraphic
from fastplotlib.graphics.line import LineGraphic
clewis7 marked this conversation as resolved.
Show resolved Hide resolved
from typing import *
from ._base import Interaction

from fastplotlib.graphics._base import Interaction
clewis7 marked this conversation as resolved.
Show resolved Hide resolved
from abc import ABC, abstractmethod


Expand All @@ -26,7 +25,7 @@ def __init__(self, data: List[np.ndarray], z_position: Union[List[float], float]
if not len(data) == len(cmap):
raise ValueError("args must be the same length")

self.collection = list()
self.data = list()

for i, d in enumerate(data):
if isinstance(z_position, list):
Expand All @@ -49,33 +48,30 @@ def __init__(self, data: List[np.ndarray], z_position: Union[List[float], float]
else:
_cmap = cmap

self.collection.append(LineGraphic(d, _z, _size, _colors, _cmap))

def _reset_feature(self):
pass

@property
def indices(self) -> Any:
pass

@indices.setter
@abstractmethod
def indices(self, indices: Any):
pass
self.data.append(LineGraphic(d, _z, _size, _colors, _cmap))

@property
def features(self) -> List[str]:
pass
return ["colors", "data"]

def _set_feature(self, feature: str, new_data: Any, indices: Any):
Copy link
Member

Choose a reason for hiding this comment

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

I think that for the _set_feature method in each Graphic or GraphicCollection we can replace Any for the indices to the indices that are appropriate for that graphic type.

if feature in ["colors", "data"]:
update_func = getattr(self, f"update_{feature}")
self.collection[indices].update_func(new_data)
if feature in self.features:
update_func = getattr(self.data[indices], f"update_{feature}")
Copy link
Member

Choose a reason for hiding this comment

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

the way it's written it'll only work with single integer indices, we can chat about it

Copy link
Member

Choose a reason for hiding this comment

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

once we implement this it'll be much easier #76 (comment)

it would become getattr(self, feature)[indices] = new_data

# if indices is a single indices or list of indices
self.data[indices].update_colors(new_data)
else:
raise ValueError("name arg is not a valid feature")

def _reset_feature(self, feature: str, old_data: Any):
if feature in self.features:
#update_func = getattr(self, f"update_{feature}")
for i, line in enumerate(self.data):
line.update_colors(old_data[i])
else:
raise ValueError("name arg is not a valid feature")

def __getitem__(self, item):
return self.collection[item]
return self.data[item]



Morty Proxy This is a proxified and sanitized view of the page, visit original site.