py-pluto 1.1.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyPLUTO/__init__.py +22 -0
- pyPLUTO/amr.py +745 -0
- pyPLUTO/baseloadmixin.py +258 -0
- pyPLUTO/baseloadstate.py +45 -0
- pyPLUTO/codes/echo_load.py +161 -0
- pyPLUTO/configure.py +261 -0
- pyPLUTO/gui/config.py +174 -0
- pyPLUTO/gui/custom_var.py +435 -0
- pyPLUTO/gui/globals.py +108 -0
- pyPLUTO/gui/main.py +17 -0
- pyPLUTO/gui/main_window.py +177 -0
- pyPLUTO/gui/panels.py +66 -0
- pyPLUTO/gui/utils.py +273 -0
- pyPLUTO/h_pypluto.py +84 -0
- pyPLUTO/image.py +302 -0
- pyPLUTO/imagefuncs/colorbar.py +240 -0
- pyPLUTO/imagefuncs/contour.py +254 -0
- pyPLUTO/imagefuncs/create_axes.py +464 -0
- pyPLUTO/imagefuncs/display.py +306 -0
- pyPLUTO/imagefuncs/figure.py +395 -0
- pyPLUTO/imagefuncs/imagetools.py +487 -0
- pyPLUTO/imagefuncs/interactive.py +403 -0
- pyPLUTO/imagefuncs/legend.py +250 -0
- pyPLUTO/imagefuncs/plot.py +311 -0
- pyPLUTO/imagefuncs/range.py +242 -0
- pyPLUTO/imagefuncs/scatter.py +270 -0
- pyPLUTO/imagefuncs/set_axis.py +497 -0
- pyPLUTO/imagefuncs/streamplot.py +297 -0
- pyPLUTO/imagefuncs/zoom.py +428 -0
- pyPLUTO/imagemixin.py +259 -0
- pyPLUTO/imagestate.py +45 -0
- pyPLUTO/load.py +447 -0
- pyPLUTO/loadfuncs/baseloadtools.py +71 -0
- pyPLUTO/loadfuncs/codeselection.py +48 -0
- pyPLUTO/loadfuncs/defpluto.py +123 -0
- pyPLUTO/loadfuncs/descriptor.py +102 -0
- pyPLUTO/loadfuncs/findfiles.py +182 -0
- pyPLUTO/loadfuncs/findformat.py +245 -0
- pyPLUTO/loadfuncs/initload.py +203 -0
- pyPLUTO/loadfuncs/loadvars.py +227 -0
- pyPLUTO/loadfuncs/offsetdata.py +87 -0
- pyPLUTO/loadfuncs/offsetfluid.py +408 -0
- pyPLUTO/loadfuncs/read_files.py +213 -0
- pyPLUTO/loadfuncs/readdata.py +619 -0
- pyPLUTO/loadfuncs/readdata_old.py +567 -0
- pyPLUTO/loadfuncs/readdefplini.py +101 -0
- pyPLUTO/loadfuncs/readfluid.py +479 -0
- pyPLUTO/loadfuncs/readformat.py +277 -0
- pyPLUTO/loadfuncs/readgridalone.py +224 -0
- pyPLUTO/loadfuncs/readgridfile.py +255 -0
- pyPLUTO/loadfuncs/readgridout.py +451 -0
- pyPLUTO/loadfuncs/readpart.py +419 -0
- pyPLUTO/loadfuncs/readtab.py +105 -0
- pyPLUTO/loadfuncs/write_files.py +283 -0
- pyPLUTO/loadmixin.py +419 -0
- pyPLUTO/loadpart.py +233 -0
- pyPLUTO/loadstate.py +68 -0
- pyPLUTO/newload.py +81 -0
- pyPLUTO/pytools.py +145 -0
- pyPLUTO/toolfuncs/findlines.py +551 -0
- pyPLUTO/toolfuncs/fourier.py +149 -0
- pyPLUTO/toolfuncs/nabla.py +676 -0
- pyPLUTO/toolfuncs/parttools.py +152 -0
- pyPLUTO/toolfuncs/transform.py +638 -0
- pyPLUTO/utils/annotator.py +27 -0
- pyPLUTO/utils/inspector.py +145 -0
- pyPLUTO/utils/make_docstrings.py +3 -0
- py_pluto-1.1.4.dist-info/METADATA +218 -0
- py_pluto-1.1.4.dist-info/RECORD +73 -0
- py_pluto-1.1.4.dist-info/WHEEL +5 -0
- py_pluto-1.1.4.dist-info/entry_points.txt +2 -0
- py_pluto-1.1.4.dist-info/licenses/LICENSE +27 -0
- py_pluto-1.1.4.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
"""Module providing image tools for saving figures and adding text."""
|
|
2
|
+
|
|
3
|
+
import importlib
|
|
4
|
+
import inspect
|
|
5
|
+
import warnings
|
|
6
|
+
from collections.abc import Sequence
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any, cast
|
|
9
|
+
|
|
10
|
+
import matplotlib.colors as mcol
|
|
11
|
+
import matplotlib.pyplot as plt
|
|
12
|
+
from matplotlib.axes import Axes
|
|
13
|
+
from matplotlib.colors import Normalize
|
|
14
|
+
from matplotlib.text import Text
|
|
15
|
+
|
|
16
|
+
from pyPLUTO.imagefuncs.create_axes import CreateAxesManager
|
|
17
|
+
from pyPLUTO.imagemixin import ImageMixin
|
|
18
|
+
from pyPLUTO.imagestate import ImageState
|
|
19
|
+
from pyPLUTO.utils.inspector import track_kwargs
|
|
20
|
+
|
|
21
|
+
try:
|
|
22
|
+
_pm = importlib.import_module("pastamarkers")
|
|
23
|
+
salsa = getattr(_pm, "salsa", None)
|
|
24
|
+
except (ImportError, ModuleNotFoundError, AttributeError):
|
|
25
|
+
salsa = None
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class ImageToolsManager(ImageMixin):
|
|
29
|
+
"""ImageToolsManager class.
|
|
30
|
+
|
|
31
|
+
It provides methods to save figures, add text.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
def __init__(self, state: ImageState) -> None:
|
|
35
|
+
"""Initialize the ImageToolsManager with the given state."""
|
|
36
|
+
self.state = state
|
|
37
|
+
self.CreateAxesManager = CreateAxesManager(state)
|
|
38
|
+
|
|
39
|
+
def savefig(
|
|
40
|
+
self,
|
|
41
|
+
filename: str = "img.png",
|
|
42
|
+
bbox: str | None = "tight",
|
|
43
|
+
dpi: int = 300,
|
|
44
|
+
script_relative: bool = False,
|
|
45
|
+
) -> None:
|
|
46
|
+
"""Create a .png image file of the figure created with the Image class.
|
|
47
|
+
|
|
48
|
+
Returns
|
|
49
|
+
-------
|
|
50
|
+
- None
|
|
51
|
+
|
|
52
|
+
Parameters
|
|
53
|
+
----------
|
|
54
|
+
- bbox: {'tight', None}, default 'tight'
|
|
55
|
+
Crops the white borders of the Image to create a more balanced image
|
|
56
|
+
file.
|
|
57
|
+
- filename: str, default 'img.png'
|
|
58
|
+
The name of the saved image file.
|
|
59
|
+
- script_relative: bool, default False
|
|
60
|
+
If True, the image is saved in the same directory as the script
|
|
61
|
+
calling this method. If False, the image is saved in the current
|
|
62
|
+
working directory.
|
|
63
|
+
- dpi: int, default 300
|
|
64
|
+
The resolution of the saved image in dots per inch (DPI).
|
|
65
|
+
|
|
66
|
+
----
|
|
67
|
+
|
|
68
|
+
Examples
|
|
69
|
+
--------
|
|
70
|
+
- Example #1: save an empty image
|
|
71
|
+
|
|
72
|
+
>>> import pyPLUTO as pp
|
|
73
|
+
>>> I = pp.Image()
|
|
74
|
+
>>> I.savefig("namefile.png")
|
|
75
|
+
|
|
76
|
+
"""
|
|
77
|
+
if not self.fig:
|
|
78
|
+
raise ValueError("No figure to save. Please create a figure first.")
|
|
79
|
+
out_path = Path(filename)
|
|
80
|
+
|
|
81
|
+
if script_relative and not out_path.is_absolute():
|
|
82
|
+
# Find the path of the script calling this method
|
|
83
|
+
caller_file = Path(inspect.stack()[1].filename).resolve()
|
|
84
|
+
base_dir = caller_file.parent
|
|
85
|
+
out_path = base_dir / out_path
|
|
86
|
+
|
|
87
|
+
self.fig.savefig(out_path, bbox_inches=bbox, dpi=dpi)
|
|
88
|
+
|
|
89
|
+
def show(
|
|
90
|
+
self,
|
|
91
|
+
) -> None:
|
|
92
|
+
"""Show the figure created with the Image class.
|
|
93
|
+
|
|
94
|
+
This method is deprecated and will be removed in future versions.
|
|
95
|
+
Please use pp.show instead.
|
|
96
|
+
"""
|
|
97
|
+
raise NotImplementedError(
|
|
98
|
+
"Image show is deprecated, please use pp.show instead"
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
@track_kwargs
|
|
102
|
+
def text(
|
|
103
|
+
self,
|
|
104
|
+
text: str,
|
|
105
|
+
x: float = 0.85,
|
|
106
|
+
y: float = 0.85,
|
|
107
|
+
ax: Axes | int | None = None,
|
|
108
|
+
check: bool = True,
|
|
109
|
+
**kwargs: Any,
|
|
110
|
+
) -> None:
|
|
111
|
+
"""Insert a text box inside the figure created with Image class.
|
|
112
|
+
|
|
113
|
+
Returns
|
|
114
|
+
-------
|
|
115
|
+
- None
|
|
116
|
+
|
|
117
|
+
Parameters
|
|
118
|
+
----------
|
|
119
|
+
- ax: axis object, default None
|
|
120
|
+
The axis where to insert the text box. If None, the last considered
|
|
121
|
+
axis will be used.
|
|
122
|
+
- c: str, default 'k'
|
|
123
|
+
Determines the text color.
|
|
124
|
+
- horalign: str, default 'left'
|
|
125
|
+
The horizontal alignment. Possible values are 'left', 'center',
|
|
126
|
+
'right'.
|
|
127
|
+
- text (not optional): str
|
|
128
|
+
The text that will appear on the text box
|
|
129
|
+
- textsize: float, default fontsize
|
|
130
|
+
Sets the text fontsize. The default value corresponds to the value
|
|
131
|
+
of the actual fontsize in the figure.
|
|
132
|
+
- veralign: str, default 'baseline'
|
|
133
|
+
The vertical alignment. Possible values are 'baseline', 'bottom',
|
|
134
|
+
'center', 'center_baseline', 'top'.
|
|
135
|
+
- x: float, default 0.85
|
|
136
|
+
The horizontal starting position of the text box, in units of figure
|
|
137
|
+
size.
|
|
138
|
+
- xycoords: str, default 'fraction'
|
|
139
|
+
The coordinate system used. Possible values are 'figure fraction',
|
|
140
|
+
which sets the position as a fraction of the axis (inside the axis
|
|
141
|
+
lie values between 0 and 1), 'points', which sets the position in
|
|
142
|
+
units of the x/y coordinate system, and 'figure', which sets the
|
|
143
|
+
position as a fraction of the figure.
|
|
144
|
+
- y: float, default 0.85
|
|
145
|
+
The vertical starting position of the text box, in units of figure
|
|
146
|
+
size.
|
|
147
|
+
|
|
148
|
+
----
|
|
149
|
+
|
|
150
|
+
Examples
|
|
151
|
+
--------
|
|
152
|
+
- Example #1: Insert text inside a specific axis
|
|
153
|
+
|
|
154
|
+
>>> I.text("text", x=0.5, y=0.5, ax=ax)
|
|
155
|
+
|
|
156
|
+
- Example #2: Insert text inside the last axis
|
|
157
|
+
|
|
158
|
+
>>> I.text("text", x=0.5, y=0.5)
|
|
159
|
+
|
|
160
|
+
- Example #3: Insert text inside the last axis with a specific fontsize
|
|
161
|
+
|
|
162
|
+
>>> I.text("text", x=0.5, y=0.5, textsize=20)
|
|
163
|
+
|
|
164
|
+
- Example #4: Insert text inside the last axis with a specific fontsize
|
|
165
|
+
and a specific color
|
|
166
|
+
|
|
167
|
+
>>> I.text("text", x=0.5, y=0.5, textsize=20, c="r")
|
|
168
|
+
|
|
169
|
+
- Example #5: Insert text inside the last axis with a points position
|
|
170
|
+
|
|
171
|
+
>>> I.text("text", x=0.5, y=0.5, xycoords="points")
|
|
172
|
+
|
|
173
|
+
"""
|
|
174
|
+
kwargs.pop("check", check)
|
|
175
|
+
|
|
176
|
+
# Find figure and number of the axis
|
|
177
|
+
ax, nax = self.assign_ax(ax, **kwargs)
|
|
178
|
+
|
|
179
|
+
if self.fig is None:
|
|
180
|
+
raise ValueError(
|
|
181
|
+
"No figure is present. Please create a figure first."
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
# Dictionary with the possible 'xycoords' values
|
|
185
|
+
coordinates = {
|
|
186
|
+
"fraction": ax.transAxes,
|
|
187
|
+
"points": ax.transData,
|
|
188
|
+
"figure": self.fig.transFigure,
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
# Set the 'xycoords' keyword
|
|
192
|
+
xycoord = kwargs.get("xycoords", "fraction")
|
|
193
|
+
|
|
194
|
+
# If the text is inside a specific axis, hide the text of the
|
|
195
|
+
# create_axes function
|
|
196
|
+
if xycoord != "figure":
|
|
197
|
+
self.hide_text(nax, ax.texts)
|
|
198
|
+
|
|
199
|
+
# Set the 'xycoords' value
|
|
200
|
+
coord = coordinates[xycoord]
|
|
201
|
+
|
|
202
|
+
# Set the 'veralign' and 'horalign' values
|
|
203
|
+
hortx = kwargs.get("horalign", "left")
|
|
204
|
+
vertx = kwargs.get("veralign", "baseline")
|
|
205
|
+
|
|
206
|
+
bbox = kwargs.get("bbox")
|
|
207
|
+
|
|
208
|
+
# Insert the text
|
|
209
|
+
ax.text(
|
|
210
|
+
x,
|
|
211
|
+
y,
|
|
212
|
+
text,
|
|
213
|
+
c=kwargs.get("c", "k"),
|
|
214
|
+
transform=coord,
|
|
215
|
+
fontsize=kwargs.get("textsize", self.fontsize),
|
|
216
|
+
horizontalalignment=hortx,
|
|
217
|
+
verticalalignment=vertx,
|
|
218
|
+
bbox=kwargs.get("bbox", bbox),
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
# End of the function
|
|
222
|
+
|
|
223
|
+
def assign_ax(
|
|
224
|
+
self, ax: Axes | list[Axes] | int | None, **kwargs: Any
|
|
225
|
+
) -> tuple[Axes, int]:
|
|
226
|
+
"""Set the axes of the figure where the plot/feature should go.
|
|
227
|
+
|
|
228
|
+
If no axis is present, an axis is created. If the axis is
|
|
229
|
+
present but no axis is seletced, the last axis is selected.
|
|
230
|
+
|
|
231
|
+
Returns
|
|
232
|
+
-------
|
|
233
|
+
- ax: ax | list[ax] | int | None
|
|
234
|
+
The selected set of axes.
|
|
235
|
+
- nax: int
|
|
236
|
+
The number of the selected set of axes.
|
|
237
|
+
|
|
238
|
+
Parameters
|
|
239
|
+
----------
|
|
240
|
+
- ax (not optional): ax | int | list[ax] | None
|
|
241
|
+
The selected set of axes.
|
|
242
|
+
- **kwargs: Any
|
|
243
|
+
The keyword arguments to be passed to the create_axes function
|
|
244
|
+
(not written here since is not public method).
|
|
245
|
+
|
|
246
|
+
----
|
|
247
|
+
|
|
248
|
+
Examples
|
|
249
|
+
--------
|
|
250
|
+
- Example #1: Set the axes of the figure
|
|
251
|
+
|
|
252
|
+
>>> _assign_ax(ax, **kwargs)
|
|
253
|
+
|
|
254
|
+
- Example #2: Set the axes of the figure (no axis selected)
|
|
255
|
+
|
|
256
|
+
>>> _assign_ax(None, **kwargs)
|
|
257
|
+
|
|
258
|
+
- Example #3: Set the axes of the figure (axis is a list)
|
|
259
|
+
|
|
260
|
+
>>> _assign_ax([ax], **kwargs)
|
|
261
|
+
|
|
262
|
+
"""
|
|
263
|
+
if self.fig is None:
|
|
264
|
+
raise ValueError(
|
|
265
|
+
"No figure is present. Please create a figure first."
|
|
266
|
+
)
|
|
267
|
+
# Check if the axis is None and no axis is present (and create one)
|
|
268
|
+
if ax is None and len(self.ax) == 0:
|
|
269
|
+
ax = self.CreateAxesManager.create_axes(
|
|
270
|
+
ncol=1, nrow=1, check=False, **kwargs
|
|
271
|
+
)
|
|
272
|
+
|
|
273
|
+
# Check if the axis is None and an axis is present (and select the last
|
|
274
|
+
# one, the current axis if it belongs to the one saved in the figure or
|
|
275
|
+
# the last one saved
|
|
276
|
+
elif ax is None and len(self.ax) > 0:
|
|
277
|
+
ax = self.fig.gca() if self.fig.gca() in self.ax else self.ax[-1]
|
|
278
|
+
|
|
279
|
+
# Check if the axis is a list and select the first element
|
|
280
|
+
elif isinstance(ax, list):
|
|
281
|
+
ax = ax[0]
|
|
282
|
+
|
|
283
|
+
# Check if the axis is an int, and select the corresponding axis from
|
|
284
|
+
# the list of axes
|
|
285
|
+
elif isinstance(ax, int):
|
|
286
|
+
ax = self.ax[ax]
|
|
287
|
+
|
|
288
|
+
# If none of the previous cases is satisfied assert that ax is an axis
|
|
289
|
+
if not isinstance(ax, Axes):
|
|
290
|
+
raise ValueError("The provided axis is not valid.")
|
|
291
|
+
|
|
292
|
+
# Get the figure associated to the axes
|
|
293
|
+
fig = ax.get_figure()
|
|
294
|
+
|
|
295
|
+
# Check if the figure is the same as the one in the class
|
|
296
|
+
if fig != self.fig:
|
|
297
|
+
text = "The provided axis does not belong to the expected figure."
|
|
298
|
+
raise ValueError(text)
|
|
299
|
+
|
|
300
|
+
# Find the number of the axes and return it
|
|
301
|
+
nax = self.ax.index(ax)
|
|
302
|
+
|
|
303
|
+
# Return the axis and its index
|
|
304
|
+
return ax, nax
|
|
305
|
+
|
|
306
|
+
def hide_text(self, nax: int, txts: Sequence[Text] | None) -> None:
|
|
307
|
+
"""Hide the text placed when an axis is created (the axis index).
|
|
308
|
+
|
|
309
|
+
Returns
|
|
310
|
+
-------
|
|
311
|
+
- None
|
|
312
|
+
|
|
313
|
+
Parameters
|
|
314
|
+
----------
|
|
315
|
+
- nax (not optional): int
|
|
316
|
+
The number of the selected set of axes.
|
|
317
|
+
- txts (not optional): str | None
|
|
318
|
+
The text of the selected set of axes.
|
|
319
|
+
|
|
320
|
+
----
|
|
321
|
+
|
|
322
|
+
Examples
|
|
323
|
+
--------
|
|
324
|
+
- Example #1: Hide the text of the selected set of axes
|
|
325
|
+
|
|
326
|
+
>>> _hide_text(nax, txts)
|
|
327
|
+
|
|
328
|
+
"""
|
|
329
|
+
# Check if the text has already been removed
|
|
330
|
+
if self.ntext[nax] is None and txts is not None:
|
|
331
|
+
for txt in txts:
|
|
332
|
+
txt.set_visible(False)
|
|
333
|
+
|
|
334
|
+
# Set the text as removed
|
|
335
|
+
self.ntext[nax] = 1
|
|
336
|
+
|
|
337
|
+
# End of the function
|
|
338
|
+
|
|
339
|
+
def set_cscale(
|
|
340
|
+
self,
|
|
341
|
+
cscale: str,
|
|
342
|
+
vmin: float,
|
|
343
|
+
vmax: float,
|
|
344
|
+
tresh: float,
|
|
345
|
+
lint: float | None = None,
|
|
346
|
+
) -> Normalize:
|
|
347
|
+
"""Set the color scale and limits of a pcolormesh given the scale.
|
|
348
|
+
|
|
349
|
+
Returns
|
|
350
|
+
-------
|
|
351
|
+
- norm: Normalize
|
|
352
|
+
The normalization of the colormap
|
|
353
|
+
|
|
354
|
+
Parameters
|
|
355
|
+
----------
|
|
356
|
+
- cscale : {'linear','log','symlog','twoslope'}, default 'linear'
|
|
357
|
+
Sets the colorbar scale. Default is the linear ('norm') scale.
|
|
358
|
+
- tresh (not optional): float
|
|
359
|
+
Sets the threshold for the colormap. If not defined, the threshold
|
|
360
|
+
will be set to 1% of the maximum absolute value of the variable.
|
|
361
|
+
The default cases are the following:
|
|
362
|
+
- twoslope colorscale: sets the limit between the two linear
|
|
363
|
+
regimes.
|
|
364
|
+
- symlog: sets the limit between the logaitrhmic and the linear
|
|
365
|
+
regime.
|
|
366
|
+
- vmax (not optional): float
|
|
367
|
+
The maximum value of the colormap.
|
|
368
|
+
- vmin (not optional): float
|
|
369
|
+
The minimum value of the colormap.
|
|
370
|
+
|
|
371
|
+
----
|
|
372
|
+
|
|
373
|
+
Examples
|
|
374
|
+
--------
|
|
375
|
+
- Example #1: set a linear colormap between 0 and 1
|
|
376
|
+
|
|
377
|
+
>>> _set_cscale("linear", 0.0, 1.0)
|
|
378
|
+
|
|
379
|
+
- Example #2: set a logarithmic colormap between 0.1 and 1
|
|
380
|
+
|
|
381
|
+
>>> _set_cscale("log", 0.1, 1.0)
|
|
382
|
+
|
|
383
|
+
- Example #3: set a twoslope colormap between -1 and 1 with threshold
|
|
384
|
+
0.1
|
|
385
|
+
|
|
386
|
+
>>> _set_cscale("twoslope", -1.0, 1.0, 0.1)
|
|
387
|
+
|
|
388
|
+
"""
|
|
389
|
+
if lint is not None:
|
|
390
|
+
warnings.warn(
|
|
391
|
+
"'lint' keyword is deprecated, please use \
|
|
392
|
+
'tresh' instead",
|
|
393
|
+
UserWarning,
|
|
394
|
+
stacklevel=2,
|
|
395
|
+
)
|
|
396
|
+
|
|
397
|
+
norm: Normalize
|
|
398
|
+
|
|
399
|
+
if cscale == "log":
|
|
400
|
+
norm = mcol.LogNorm(vmin=vmin, vmax=vmax)
|
|
401
|
+
elif cscale == "symlog":
|
|
402
|
+
# Pylint wrong warning!!! Disabled cause not compatible with modern
|
|
403
|
+
# matplotlib versions
|
|
404
|
+
# pylint: disable=redundant-keyword-arg
|
|
405
|
+
norm = mcol.SymLogNorm(tresh, vmin=vmin, vmax=vmax)
|
|
406
|
+
# pylint: enable=redundant-keyword-arg
|
|
407
|
+
elif cscale in ("twoslope", "2slope"):
|
|
408
|
+
norm = mcol.TwoSlopeNorm(vmin=vmin, vmax=vmax, vcenter=tresh)
|
|
409
|
+
elif cscale == "power":
|
|
410
|
+
norm = mcol.PowerNorm(gamma=tresh, vmin=vmin, vmax=vmax)
|
|
411
|
+
elif cscale == "asinh":
|
|
412
|
+
norm = mcol.AsinhNorm(tresh, vmin, vmax)
|
|
413
|
+
else:
|
|
414
|
+
norm = mcol.Normalize(vmin=vmin, vmax=vmax)
|
|
415
|
+
|
|
416
|
+
return norm
|
|
417
|
+
|
|
418
|
+
def find_cmap(
|
|
419
|
+
self, name: str | mcol.Colormap | None
|
|
420
|
+
) -> mcol.Colormap | None:
|
|
421
|
+
"""Find a colormap by name.
|
|
422
|
+
|
|
423
|
+
Returns
|
|
424
|
+
-------
|
|
425
|
+
- cmap: Colormap | None
|
|
426
|
+
The colormap found by name.
|
|
427
|
+
|
|
428
|
+
Parameters
|
|
429
|
+
----------
|
|
430
|
+
- name (not optional): str | Colormap | None
|
|
431
|
+
The name of the colormap.
|
|
432
|
+
|
|
433
|
+
----
|
|
434
|
+
|
|
435
|
+
Examples
|
|
436
|
+
--------
|
|
437
|
+
- Example #1: find a colormap by name
|
|
438
|
+
|
|
439
|
+
>>> _find_cmap("viridis")
|
|
440
|
+
|
|
441
|
+
- Example #2: find a colormap by name
|
|
442
|
+
|
|
443
|
+
>>> _find_cmap("viridis_r")
|
|
444
|
+
|
|
445
|
+
"""
|
|
446
|
+
# Find a colormap by name or return a default one if not found.
|
|
447
|
+
if isinstance(name, mcol.Colormap) or name is None:
|
|
448
|
+
return name
|
|
449
|
+
|
|
450
|
+
# First, try matplotlib colormap
|
|
451
|
+
try:
|
|
452
|
+
return plt.get_cmap(name)
|
|
453
|
+
except ValueError:
|
|
454
|
+
pass # Not a matplotlib colormap
|
|
455
|
+
|
|
456
|
+
if salsa is None:
|
|
457
|
+
warn = (
|
|
458
|
+
"salsa is not installed, cannot find colormap. "
|
|
459
|
+
"Defaulting to 'plasma'."
|
|
460
|
+
)
|
|
461
|
+
warnings.warn(warn, UserWarning, stacklevel=2)
|
|
462
|
+
return plt.get_cmap("plasma")
|
|
463
|
+
|
|
464
|
+
# Try salsa colormap
|
|
465
|
+
reverse = False
|
|
466
|
+
base_name = name
|
|
467
|
+
if name.endswith("_r"):
|
|
468
|
+
base_name = name[:-2]
|
|
469
|
+
reverse = True
|
|
470
|
+
|
|
471
|
+
cmap = getattr(salsa, base_name, None)
|
|
472
|
+
print(cmap)
|
|
473
|
+
if cmap is not None:
|
|
474
|
+
if reverse:
|
|
475
|
+
# Prefer .reversed() method if available
|
|
476
|
+
rev = getattr(cmap, "reversed", None)
|
|
477
|
+
if callable(rev):
|
|
478
|
+
return cast(mcol.Colormap, rev())
|
|
479
|
+
return cast(mcol.Colormap, cmap)
|
|
480
|
+
|
|
481
|
+
# Gigantic warning!
|
|
482
|
+
warn = (
|
|
483
|
+
f"Colormap '{name}' not found in matplotlib or salsa! "
|
|
484
|
+
"Defaulting to 'plasma'."
|
|
485
|
+
)
|
|
486
|
+
warnings.warn(warn, UserWarning, stacklevel=2)
|
|
487
|
+
return plt.get_cmap("plasma")
|