-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathfunctions.py
More file actions
304 lines (228 loc) · 7.9 KB
/
functions.py
File metadata and controls
304 lines (228 loc) · 7.9 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
from collections import OrderedDict
from pathlib import Path
import numpy as np
from pygfx import Texture, Color
# some funcs adapted from mesmerize
QUALITATIVE_CMAPS = [
"Pastel1",
"Pastel2",
"Paired",
"Accent",
"Dark2",
"Set1",
"Set2",
"Set3",
"tab10",
"tab20",
"tab20b",
"tab20c",
]
def get_cmap(name: str, alpha: float = 1.0) -> np.ndarray:
"""
Get a colormap as numpy array
Parameters
----------
name: str
name of colormap
alpha: float
alpha, 0.0 - 1.0
Returns
-------
np.ndarray
[n_colors, 4], i.e. [n_colors, RGBA]
"""
cmap_path = Path(__file__).absolute().parent.joinpath("colormaps", name)
if cmap_path.is_file():
cmap = np.loadtxt(cmap_path)
else:
try:
from .generate_colormaps import make_cmap
cmap = make_cmap(name, alpha)
except (ImportError, ModuleNotFoundError):
raise ModuleNotFoundError(
"Couldn't find colormap files, matplotlib is required to generate them "
"if they aren't found. Please install `matplotlib`"
)
cmap[:, -1] = alpha
return cmap.astype(np.float32)
def make_colors(n_colors: int, cmap: str, alpha: float = 1.0) -> np.ndarray:
"""
Get colors from a colormap. The returned colors are uniformly spaced, except
for qualitative colormaps where they are returned subsequently.
Parameters
----------
n_colors: int
number of colors to get
cmap: str
name of colormap
alpha: float, default 1.0
alpha value
Returns
-------
np.ndarray
shape is [n_colors, 4], where the last dimension is RGBA
"""
name = cmap
cmap = get_cmap(name, alpha)
if name in QUALITATIVE_CMAPS:
max_colors = cmap.shape[0]
if n_colors > cmap.shape[0]:
raise ValueError(
f"You have requested <{n_colors}> but only <{max_colors} existing for the "
f"chosen cmap: <{cmap}>"
)
return cmap[:n_colors]
cm_ixs = np.linspace(0, 255, n_colors, dtype=int)
return np.take(cmap, cm_ixs, axis=0).astype(np.float32)
def get_cmap_texture(name: str, alpha: float = 1.0) -> Texture:
cmap = get_cmap(name)
return Texture(cmap, dim=1)
def make_colors_dict(labels: iter, cmap: str, **kwargs) -> OrderedDict:
"""
Get a dict for mapping labels onto colors.
Parameters
----------
labels: Iterable[Any]
labels for creating a colormap. Order is maintained if it is a list of unique elements.
cmap: str
name of colormap
**kwargs
passed to make_colors()
Returns
-------
OrderedDict
keys are labels, values are colors
Examples
--------
.. code-block:: python
from fastplotlib.utils import get_colors_dict
labels = ["l1", "l2", "l3"]
labels_cmap = get_colors_dict(labels, cmap="tab10")
# illustration of what the `labels_cmap` dict would look like:
# keep in mind that the tab10 cmap was chosen here
{
"l1": <RGBA array for the blue 'tab10' color>,
"l2": <RGBA array for the orange 'tab10' color>,
"l3": <RGBA array for the green 'tab10' color>,
}
# another example with a non-qualitative cmap
labels_cmap_seismic = get_colors_dict(labels, cmap="bwr")
{
"l1": <RGBA array for the blue 'bwr' color>,
"l2": <RGBA array for the white 'bwr' color>,
"l3": <RGBA array for the red 'bwr' color>,
}
"""
if not len(set(labels)) == len(labels):
labels = list(set(labels))
else:
labels = list(labels)
colors = make_colors(len(labels), cmap, **kwargs)
return OrderedDict(zip(labels, colors))
def quick_min_max(data: np.ndarray) -> tuple[float, float]:
"""
Adapted from pyqtgraph.ImageView.
Estimate the min/max values of *data* by subsampling.
Parameters
----------
data: np.ndarray or array-like with `min` and `max` attributes
Returns
-------
(float, float)
(min, max)
"""
if hasattr(data, "min") and hasattr(data, "max"):
# if value is pre-computed
if isinstance(data.min, (float, int, np.number)) and isinstance(
data.max, (float, int, np.number)
):
return data.min, data.max
while data.size > 1e6:
ax = np.argmax(data.shape)
sl = [slice(None)] * data.ndim
sl[ax] = slice(None, None, 2)
data = data[tuple(sl)]
return float(np.nanmin(data)), float(np.nanmax(data))
def make_pygfx_colors(colors, n_colors):
"""
Parse and make colors array using pyfx.Color
Parameters
----------
colors: str, list, tuple, or np.ndarray
pygfx parseable color
n_colors: int
number of repeats of the color
Returns
-------
np.ndarray
shape is [n_colors, 4], i.e. [n_colors, RGBA]
"""
c = Color(colors)
colors_array = np.repeat(np.array([c]), n_colors, axis=0)
return colors_array
def calculate_figure_shape(n_subplots: int) -> tuple[int, int]:
"""
Returns ``(n_rows, n_cols)`` from given number of subplots ``n_subplots``
"""
sr = np.sqrt(n_subplots)
return (int(np.round(sr)), int(np.ceil(sr)))
def normalize_min_max(a):
"""normalize an array between 0 - 1"""
if np.unique(a).size == 1:
return np.zeros(a.size)
return (a - np.min(a)) / (np.max(a - np.min(a)))
def parse_cmap_values(
n_colors: int,
cmap_name: str,
cmap_values: np.ndarray | list[int | float] = None,
) -> np.ndarray:
"""
Parameters
----------
n_colors: int
number of graphics in collection
cmap_name: str
colormap name
cmap_values: np.ndarray | List[int | float], optional
cmap values
Returns
-------
"""
if cmap_values is None:
# use the cmap values linearly just along the collection indices
# for example, if len(data) = 10 and the cmap is "jet", then it will
# linearly go from blue to red from data[0] to data[-1]
colors = make_colors(n_colors, cmap_name)
return colors
else:
if not isinstance(cmap_values, np.ndarray):
cmap_values = np.array(cmap_values)
# use the values within cmap_values to set the color of the corresponding data
# each individual data[i] has its color based on the "relative cmap_value intensity"
if len(cmap_values) != n_colors:
raise ValueError(
f"len(cmap_values) != len(data): {len(cmap_values)} != {n_colors}"
)
colormap = get_cmap(cmap_name)
n_colors = colormap.shape[0] - 1
if cmap_name in QUALITATIVE_CMAPS:
# check that cmap_values are <int> and within the number of colors `n_colors`
# do not scale, use directly
if not np.issubdtype(cmap_values.dtype, np.integer):
raise TypeError(
f"<int> cmap_values should be used with qualitative colormaps, the dtype you "
f"have passed is {cmap_values.dtype}"
)
if max(cmap_values) > n_colors:
raise IndexError(
f"You have chosen the qualitative colormap <'{cmap_name}'> which only has "
f"<{n_colors}> colors, which is lower than the max value of your `cmap_values`."
f"Choose a cmap with more colors, or a non-quantitative colormap."
)
norm_cmap_values = cmap_values
else:
# scale between 0 - n_colors so we can just index the colormap as a LUT
norm_cmap_values = (normalize_min_max(cmap_values) * n_colors).astype(int)
# use colormap as LUT to map the cmap_values to the colormap index
colors = np.vstack([colormap[val] for val in norm_cmap_values])
return colors