Skip to content

indexable graphic attributes #78

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 11 commits into from
Dec 23, 2022
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
can directly set graphic features, Graphic has __setattr__, added Gra…
…phic.visible
  • Loading branch information
kushalkolar committed Dec 21, 2022
commit f7dc0639d385c31af7398b5832d3b582204012ef
33 changes: 31 additions & 2 deletions fastplotlib/graphics/_base.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Any
from typing import *

import numpy as np
import pygfx

from fastplotlib.utils import get_colors, map_labels_to_colors
from ._graphic_attribute import ColorFeature
from ._graphic_attribute import GraphicFeature, ColorFeature


class Graphic:
Expand Down Expand Up @@ -43,17 +43,46 @@ def __init__(
if colors is not False:
self.colors = ColorFeature(parent=self, colors=colors, n_colors=n_colors, alpha=alpha)

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

self._valid_features = tuple(valid_features)

@property
def world_object(self) -> pygfx.WorldObject:
return self._world_object

@property
def valid_features(self) -> Tuple[str]:
return self._valid_features

@property
def visible(self) -> bool:
return self.world_object.visible

@visible.setter
def visible(self, v):
self.world_object.visible = v

@property
def children(self) -> pygfx.WorldObject:
return self.world_object.children

def update_data(self, data: Any):
pass

def __setattr__(self, key, value):
if hasattr(self, key):
attr = getattr(self, key)
if isinstance(attr, GraphicFeature):
attr._set(value)
return

super().__setattr__(key, value)

def __repr__(self):
if self.name is not None:
return f"'{self.name}' fastplotlib.{self.__class__.__name__} @ {hex(id(self))}"
Expand Down
30 changes: 20 additions & 10 deletions fastplotlib/graphics/_graphic_attribute.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
from abc import ABC, abstractmethod
from typing import *
from pygfx import Color
from pygfx import Color, Scene
import numpy as np


class GraphicFeature(ABC):
def __init__(self, parent, data: Any):
self._parent = parent
self._data = data.astype(np.float32)
if isinstance(data, np.ndarray):
data = data.astype(np.float32)

def set_parent(self, parent: Any):
self._parent = parent
self._data = data

@property
def data(self):
return self._data

@abstractmethod
def __getitem__(self, item):
def _set(self, value):
pass

@abstractmethod
def __setitem__(self, key, value):
def __repr__(self):
pass


class GraphicFeatureIndexable(GraphicFeature):
"""And indexable Graphic Feature, colors, data, sizes etc."""
def _set(self, value):
self[:] = value

@abstractmethod
def _update_range(self, key):
def __getitem__(self, item):
pass

@abstractmethod
def __repr__(self):
def __setitem__(self, key, value):
pass

@abstractmethod
def _update_range(self, key):
pass


Expand Down Expand Up @@ -59,14 +69,14 @@ def cleanup_slice(slice_obj: slice, upper_bound) -> slice:
return slice(start, stop, step)


class ColorFeature(GraphicFeature):
class ColorFeature(GraphicFeatureIndexable):
def __init__(self, parent, colors, n_colors, alpha: float = 1.0):
"""
ColorFeature

Parameters
----------
parent
parent: Graphic or GraphicCollection

colors: str, array, or iterable
specify colors as a single human readable string, RGBA array,
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