-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathutils.py
More file actions
79 lines (67 loc) · 2.91 KB
/
utils.py
File metadata and controls
79 lines (67 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import pygfx
import numpy as np
from ._base import to_gpu_supported_dtype
from ...utils import make_pygfx_colors
def parse_colors(
colors: str | np.ndarray | list[str] | tuple[str], n_colors: int | None
):
"""
Parameters
----------
colors
n_colors
Returns
-------
"""
# if provided as a numpy array of str
if isinstance(colors, np.ndarray):
if colors.dtype.kind in ["U", "S"]:
colors = colors.tolist()
# if the color is provided as a numpy array
if isinstance(colors, np.ndarray):
if colors.shape == (3,): # single RGB array
data = np.repeat(np.array([colors]), n_colors, axis=0)
elif colors.shape == (4,): # single RGBA array
data = np.repeat(np.array([colors]), n_colors, axis=0)
# else assume it's already a stack of RGBA arrays, keep this directly as the data
elif colors.ndim == 2:
if not (colors.shape[1] in (3, 4) and colors.shape[0] == n_colors):
raise ValueError(
f"Valid array color arguments must be a single RGBA array or a stack of "
f"RGB or RGBA arrays for each datapoint in the shape [n_datapoints, 3] or [n_datapoints, 4].\n"
f"n_datapoints is: {n_colors}, you passed a colors array of shape: {colors.shape}"
)
data = colors
else:
raise ValueError(
"Valid array color arguments must be a single RGB(A) array or a stack of "
"RGB(A) arrays for each datapoint in the shape [n_datapoints, 3] or [n_datapoints, 4]"
)
# if the color is provided as list or tuple
elif isinstance(colors, (list, tuple)):
# if iterable of str
if all([isinstance(val, str) for val in colors]):
if not len(colors) == n_colors:
raise ValueError(
f"Valid iterable color arguments must be a `tuple` or `list` of `str` "
f"where the length of the iterable is the same as the number of datapoints."
)
data = np.vstack([np.array(pygfx.Color(c)) for c in colors])
# if it's a single RGB/RGBA array as a tuple/list
elif len(colors) in (3, 4):
c = pygfx.Color(colors)
data = np.repeat(np.array([c]), n_colors, axis=0)
else:
raise ValueError(
f"Valid iterable color arguments must be a `tuple` or `list` representing RGBA values or "
f"an iterable of `str` with the same length as the number of datapoints."
)
elif isinstance(colors, str):
if colors == "random":
data = np.random.rand(n_colors, 3)
else:
data = make_pygfx_colors(colors, n_colors)
else:
# assume it's a single color, use pygfx.Color to parse it
data = make_pygfx_colors(colors, n_colors)
return to_gpu_supported_dtype(data)