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

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
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
Next Next commit
started graphic attributes, colors work
  • Loading branch information
kushalkolar committed Dec 19, 2022
commit 75cc2c78a115e8f1fd2a3754c33d97d6b08c0ce1
92 changes: 92 additions & 0 deletions 92 fastplotlib/graphics/_graphic_attribute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from abc import ABC, abstractmethod
from typing import *
from pygfx import Color
import numpy as np


class GraphicFeature(ABC):
def __init__(self, parent, data: Any):
self._parent = parent
self._data = data

@abstractmethod
def __getitem__(self, item):
pass

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

@abstractmethod
def _update_range(self, key):
pass


class ColorFeature(GraphicFeature):
def __init__(self, parent, data):
data = parent.geometry.colors.data
super(ColorFeature, self).__init__(parent, data)

self._bounds = data.shape[0]

def __setitem__(self, key, value):
if abs(key.start) > self._bounds or abs(key.stop) > self._bounds:
raise IndexError

if isinstance(key, slice):
start = key.start
stop = key.stop
step = key.step
if step is None:
step = 1

indices = range(start, stop, step)

elif isinstance(key, int):
indices = [key]

else:
raise TypeError("Graphic features only support integer and numerical fancy indexing")

new_data_size = len(indices)

if not isinstance(value, np.ndarray):
new_colors = np.repeat(np.array([Color(value)]), new_data_size, axis=0)

elif isinstance(value, np.ndarray):
if value.shape == (4,):
new_colors = value.astype(np.float32)
if new_data_size > 1:
new_colors = np.repeat(np.array([new_colors]), new_data_size, axis=0)

elif value.shape[1] == 4 and value.ndim == 2:
if not value.shape[0] == new_data_size:
raise ValueError("numpy array passed to color must be of shape (4,) or (n_colors_modify, 4)")
if new_data_size == 1:
new_colors = value.ravel().astype(np.float32)
else:
new_colors = value.astype(np.float32)

else:
raise ValueError("numpy array passed to color must be of shape (4,) or (n_colors_modify, 4)")

self._parent.geometry.colors.data[key] = new_colors

self._update_range(key)

def _update_range(self, key):
if isinstance(key, int):
self._parent.geometry.colors.update_range(key, size=1)
if key.step is None:
# update range according to size using the offset
self._parent.geometry.colors.update_range(offset=key.start, size=key.stop - key.start)

else:
step = key.step
ixs = range(key.start, key.stop, step)
# convert slice to indices
for ix in ixs:
self._parent.geometry.colors.update_range(ix, size=1)

def __getitem__(self, item):
return self._parent.geometry.colors.data[item]
Morty Proxy This is a proxified and sanitized view of the page, visit original site.