-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathartists.py
More file actions
553 lines (464 loc) · 18.5 KB
/
artists.py
File metadata and controls
553 lines (464 loc) · 18.5 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
import copy
from typing import Any, Self, cast
import numpy
import shapely
import xarray
from matplotlib.artist import Artist
from matplotlib.axes import Axes
from matplotlib.collections import PolyCollection, TriMesh
from matplotlib.quiver import Quiver
from matplotlib.tri import Triangulation, TriContourSet
from emsarray import conventions
class GridArtist(Artist):
"""
A matplotlib Artist subclass that knows what Grid it is associated with,
and has a `set_data_array()` method.
Users can call `GridArtist.set_data_array()` to update the data in a plot.
This is useful when making animations, for example.
"""
_grid: 'conventions.Grid'
def set_grid(self, grid: 'conventions.Grid') -> None:
if hasattr(self, '_grid'):
raise ValueError("_grid can not be changed once set")
self._grid = grid
def get_grid(self) -> 'conventions.Grid':
return self._grid
def set_data_array(self, data_array: Any) -> None:
"""
Update the data this artist is plotting.
The data array must be defined on the same :meth:`grid <GridArtist.get_grid>`,
and must not have any extra dimensions such as depth or time.
"""
raise NotImplementedError("Subclasses must implement this")
def make_polygon_scalar_collection(
axes: Axes,
grid: 'conventions.Grid',
data_array: xarray.DataArray | None = None,
add_colorbar: bool | None = None,
**kwargs: Any,
) -> 'PolygonScalarCollection':
"""
Make a :class:`PolygonScalarCollection` for a :class:`~emsarray.conventions.Grid`
and :class:`~xarray.DataArray` with some sensible defaults.
Parameters
----------
axes : matplotlib.axes.Axes
The axes to add the artist to
grid : emsarray.conventions.Grid
The grid containing the geometry to plot on.
data_array : xarray.DataArray or None
The data array to plot. Optional, will plot just the geometry if None.
add_colorbar : bool or None, default None
Whether to add a colorbar. Will add a colorbar by default if a data array is supplied.
kwargs : Any
Extra kwargs for styling the PolygonScalarCollection.
See :class:`matplotlib.collections.PolyCollection` for valid options.
Returns
-------
PolygonScalarCollection
"""
kwargs = {
'edgecolor': 'face',
'cmap': 'viridis',
'transform': grid.convention.data_crs,
**kwargs,
}
collection = PolygonScalarCollection.from_grid(grid, data_array=data_array, **kwargs)
axes.add_collection(collection)
if add_colorbar is None:
add_colorbar = data_array is not None
if add_colorbar:
if data_array is not None:
units = data_array.attrs.get('units')
axes.figure.colorbar(collection, ax=axes, location='right', label=units)
return collection
class PolygonScalarCollection(PolyCollection, GridArtist):
"""
A :class:`GridArtist` wrapper around a :class:`~matplotlib.collections.PolyCollection`.
This artist can plot scalar variables on grids with polygonal geometry.
"""
@classmethod
def from_grid(
cls,
grid: 'conventions.Grid',
data_array: xarray.DataArray | None = None,
**kwargs: Any,
) -> Self:
"""
Create a PolygonScalarCollection for a particular polygon grid of a dataset.
Parameters
----------
grid : emsarray.conventions.Grid
The grid to make the polygon collection for
data_array : xarray.DataArray
A data array, defined on the grid, with data to plot.
Optional, if not provided the polygons will be empty.
Returns
-------
PolygonScalarCollection
"""
if not issubclass(grid.geometry_type, shapely.Polygon):
raise ValueError("Grid must have polygon geometry")
if data_array is not None:
values = cls.ravel_data_array(grid, data_array)
kwargs['array'] = values
return cls(
verts=[
numpy.asarray(polygon.exterior.coords)
for polygon in grid.geometry[grid.mask]
],
closed=False,
grid=grid,
**kwargs,
)
def set_data_array(self, data_array: xarray.DataArray | None) -> None:
if data_array is None:
self.set_array(None)
else:
self.set_array(self.ravel_data_array(self._grid, data_array))
@staticmethod
def ravel_data_array(grid: 'conventions.Grid', data_array: xarray.DataArray) -> numpy.ndarray:
flattened = grid.ravel(data_array)
if len(flattened.dims) > 1:
unexpected_dimensions = set(data_array.dims) & set(flattened.dims)
raise ValueError(
"Data array has too many dimensions, "
"did you forget to select a single time step or depth layer? "
f"Unexpected dimensions: {unexpected_dimensions}")
return cast(numpy.ndarray, flattened.values[grid.mask])
def make_polygon_contour(
axes: Axes,
grid: 'conventions.Grid',
data_array: xarray.DataArray,
add_colorbar: bool = True,
**kwargs: Any,
) -> 'PolygonTriContourSet':
"""
Make a :func:`~matplotlib.pyplot.tricontour` plot
through the centres of a polygon grid.
This is a wrapper around making a :class:`PolygonTriContourSet`.
Parameters
----------
axes : matplotlib.axes.Axes
The axes to add the artist to
grid : emsarray.conventions.Grid
The grid containing the geometry to plot on.
data_array : xarray.DataArray or None
The data array to plot.
add_colorbar : bool, default True
Whether to add a colorbar.
kwargs : Any
Extra kwargs for styling the PolygonTriContourSet.
See :class:`matplotlib.tri.TriContourSet` for valid options.
Returns
-------
PolygonTriContourSet
"""
if 'transform' not in kwargs:
kwargs['transform'] = grid.convention.data_crs
artist = PolygonTriContourSet.from_grid(axes, grid, data_array, **kwargs)
axes.add_artist(artist)
if add_colorbar:
axes.figure.colorbar(artist._tri_contour_set)
return artist
class PolygonTriContourSet(GridArtist, Artist):
axes: Axes
triangulation: Triangulation
_tri_contour_set: TriContourSet
def __init__(
self,
axes: Axes,
triangulation: Triangulation,
data_array: xarray.DataArray,
grid: 'conventions.Grid',
**kwargs: Any,
) -> None:
super().__init__()
self.axes = axes
self.set_grid(grid)
self.triangulation = triangulation
self.tri_contour_set_properties = kwargs
self.set_data_array(data_array)
def get_children(self) -> list[Artist]:
return [self._tri_contour_set]
@classmethod
def from_grid(
cls,
axes: Axes,
grid: 'conventions.Grid',
data_array: xarray.DataArray,
**kwargs: Any,
) -> Self:
"""
Create a PolygonTriContourSet for a particular polygon grid of a dataset.
Parameters
----------
grid : emsarray.conventions.Grid
The grid to make the tricontour for
data_array : xarray.DataArray
A data array, defined on the grid, with data to plot.
kwargs : Any
Extra kwargs to configure the artist.
See :class:`matplotlib.tri.TriContourSet` for valid options.
Returns
-------
PolygonTriContourSet
"""
if not issubclass(grid.geometry_type, shapely.Polygon):
raise ValueError("Grid must have polygon geometry")
triangulation = cls.make_triangulation(grid)
return cls(
axes=axes,
triangulation=triangulation,
data_array=data_array,
grid=grid,
**kwargs)
@staticmethod
def make_triangulation(grid: 'conventions.Grid') -> Triangulation:
convention = grid.convention
# Compute the Delaunay triangulation of the face centres
centres = grid.centroid_coordinates
triangulation = Triangulation(centres[:, 0], centres[:, 1])
# Mask out any Triangles that are not contained within the dataset geometry.
# These are either in concave areas of the geometry (e.g. an inlet or bay)
# or cover holes in the geometry (e.g. islands).
valid_tris = convention.geometry.contains(shapely.polygons([
[[triangulation.x[i], triangulation.y[i]] for i in tri]
for tri in triangulation.triangles
]))
triangulation.set_mask(~valid_tris)
return triangulation
def set_data_array(self, data_array: xarray.DataArray) -> None:
triangulation = copy.copy(self.triangulation)
values = self.ravel_data_array(self._grid, data_array)
# TriContourSet does not handle nans within the data.
# These need to be masked out.
# Unfortunately it is not possible to modify the triangulation mask
# after the TriContourSet has been created
# so we need to recreate it.
# We reuse the original Triangulation (TriContourSet discards it, so we save a copy).
# We also reuse the kwargs passed in initially.
# The mask applies to triangles, but it is the vertices that have nans.
# We need to find all nan vertices,
# then find all triangles that use one of those vertices,
# then mask out those triangles,
# while also not clobbering the existing mask that removes triangles outside the geometry.
invalid_indices = numpy.flatnonzero(~numpy.isfinite(values))
invalid_tris = numpy.any(numpy.isin(triangulation.triangles, invalid_indices), axis=1)
triangulation.set_mask(triangulation.mask | invalid_tris)
# Remove the old TriContourSet, if set
if hasattr(self, '_tri_contour_set'):
self._tri_contour_set.remove()
# Make a new TriContourSet
self._tri_contour_set = TriContourSet(
self.axes, triangulation, values, **self.tri_contour_set_properties)
def __getattr__(self, name: str) -> Any:
# There is no good way to duplicate a TriContourSet,
# updating its triangulation while keeping its properties.
# There is also no good way of extracting all the properties that have been set.
# We record the initial properties passed in as kwargs,
# but we wouldn't track any properties that are modified after creation.
# Lets make some dynamic setter functions that can track this.
# I am so sorry.
if name.startswith('set_'):
prop = name[4:]
actual_setter = getattr(self._tri_contour_set, name)
def setter(value: Any) -> None:
self.tri_contour_set_properties[prop] = value
actual_setter(value)
return setter
raise AttributeError(name)
@staticmethod
def ravel_data_array(grid: 'conventions.Grid', data_array: xarray.DataArray) -> numpy.ndarray:
flattened = grid.ravel(data_array)
if len(flattened.dims) > 1:
unexpected_dimensions = set(data_array.dims) & set(flattened.dims)
raise ValueError(
"Data array has too many dimensions, "
"did you forget to select a single time step or depth layer? "
f"Unexpected dimensions: {unexpected_dimensions}")
return cast(numpy.ndarray, flattened.values[grid.mask])
type UVDataArray = tuple[xarray.DataArray, xarray.DataArray]
def make_polygon_vector_quiver(
axes: Axes,
grid: 'conventions.Grid',
data_array: UVDataArray | None = None,
**kwargs: Any,
) -> 'PolygonVectorQuiver':
"""
Make a :class:`PolygonVectorQuiver` for a :class:`~emsarray.conventions.Grid`
and :class:`~xarray.DataArray` with some sensible defaults.
Parameters
----------
axes : matplotlib.axes.Axes
The axes to add the artist to
grid : emsarray.conventions.Grid
The grid containing the geometry to plot on.
data_array : tuple of (xarray.DataArray, xarray.DataArray) or None
The data arrays to plot. Optional, will make some zero length arrows if not set.
kwargs : Any
Extra kwargs for styling the PolygonVectorQuiver
See :class:`matplotlib.collections.Quiver` for valid options.
Returns
-------
PolygonVectorQuiver
"""
if 'transform' not in kwargs:
kwargs['transform'] = grid.convention.data_crs
collection = PolygonVectorQuiver.from_grid(axes, grid, data_array, **kwargs)
axes.add_collection(collection)
return collection
class PolygonVectorQuiver(Quiver, GridArtist):
@classmethod
def from_grid(
cls,
axes: Axes,
grid: 'conventions.Grid',
data_array: UVDataArray | None = None,
**kwargs: Any,
) -> Self:
"""
Create a PolygonVectorQuiver for a particular polygon grid of a dataset.
Parameters
----------
grid : emsarray.conventions.Grid
The grid to make the quiver for
data_array : tuple of (xarray.DataArray, xarray.DataArray) or None
A data arrays, defined on the grid, with data to plot.
Returns
-------
PolygonVectorQuiver
"""
if not issubclass(grid.geometry_type, shapely.Polygon):
raise ValueError("Grid must have polygon geometry")
coords = grid.centroid_coordinates
# A Quiver needs some values when being initialized.
# We don't always want to provide values to the quiver,
# sometimes preferring to fill them in later,
# so `u` and `v` are optional.
# If they are not provided, we set default quiver values of `numpy.nan`.
values: tuple[numpy.ndarray, numpy.ndarray] | tuple[float, float]
values = numpy.nan, numpy.nan
if data_array is not None:
values = cls.ravel_data_array(grid, data_array)
return cls(axes, coords[:, 0], coords[:, 1], grid=grid, *values, **kwargs)
def set_data_array(self, data_array: UVDataArray | None) -> None:
if data_array is None:
return
values = self.ravel_data_array(self._grid, data_array)
self.set_UVC(*values)
@staticmethod
def ravel_data_array(
grid: 'conventions.Grid',
data_array: UVDataArray,
) -> tuple[numpy.ndarray, numpy.ndarray]:
u, v = data_array
if u.dims != v.dims:
raise ValueError(
"Vector data array dimensions must be identical!\n"
f"u dimensions: {tuple(u.dims)}\n"
f"v dimensions: {tuple(v.dims)}"
)
u, v = grid.ravel(u), grid.ravel(v)
if len(u.dims) > 1:
raise ValueError(
"Vector data arrays have too many dimensions - did you forget to "
"select a single timestep or a single depth layer?")
return u.values, v.values
def make_node_scalar_artist(
axes: Axes,
grid: 'conventions.Grid',
data_array: xarray.DataArray | None = None,
*,
add_colorbar: bool | None = None,
**kwargs: Any,
) -> 'NodeTriMesh':
"""
Make a :class:`NodeTriMesh` for a :class:`~emsarray.conventions.Grid`
and :class:`~xarray.DataArray` with some sensible defaults.
Parameters
----------
axes : matplotlib.axes.Axes
The axes to add the artist to
grid : emsarray.conventions.Grid
The grid containing the geometry to plot on.
data_array : xarray.DataArray
The data array to plot.
add_colorbar : bool, default True
Whether to add a color bar to the plot.
kwargs : Any
Extra kwargs for styling the NodeTriMesh
See :class:`matplotlib.collections.TriMesh` for valid options.
Returns
-------
NodeTriMesh
"""
if 'transform' not in kwargs:
kwargs['transform'] = grid.convention.data_crs
trimesh = NodeTriMesh.from_grid(
grid=grid,
data_array=data_array,
**kwargs,
)
axes.add_collection(trimesh)
if add_colorbar is None:
add_colorbar = data_array is not None
if add_colorbar:
if data_array is not None:
units = data_array.attrs.get('units')
axes.figure.colorbar(trimesh, ax=axes, location='right', label=units)
return trimesh
class NodeTriMesh(TriMesh, GridArtist):
"""
A :class:`.GridArtist` wrapper around :class:`~matplotlib.collections.TriMesh`
that can plot on the vertices of a dataset triangulation.
"""
@classmethod
def from_grid(
cls,
grid: 'conventions.Grid',
data_array: xarray.DataArray | None = None,
**kwargs: Any,
) -> Self:
"""
Create a NodeTriMesh for a particular node grid of a dataset.
Parameters
----------
grid : emsarray.conventions.Grid
The grid to make the trimesh for
data_array : xarray.DataArray
A data array, defined on the grid, with data to plot.
Returns
-------
NodeTriMesh
"""
if not issubclass(grid.geometry_type, shapely.Point):
raise ValueError("NodeTriMesh can only plot data on node geometries")
triangulation = grid.convention.make_triangulation()
if triangulation.vertex_grid_kind is not grid.grid_kind:
raise ValueError(f"Expected dataset triangulation vertices to have the grid kind {grid.grid_kind}")
mpl_triangulation = Triangulation(
triangulation.vertices[:, 0], triangulation.vertices[:, 1], triangulation.triangles)
if data_array is not None:
values = cls.ravel_data_array(grid, data_array)
kwargs['array'] = values
return cls(
mpl_triangulation,
grid=grid,
**kwargs,
)
def set_data_array(self, data_array: xarray.DataArray | None) -> None:
if data_array is None:
self.set_array(None)
else:
self.set_array(self.ravel_data_array(self._grid, data_array))
@staticmethod
def ravel_data_array(grid: 'conventions.Grid', data_array: xarray.DataArray) -> numpy.ndarray:
flattened = grid.ravel(data_array)
if len(flattened.dims) > 1:
extra_dimensions = ", ".join(map(str, set(data_array.dims) & set(flattened.dims)))
raise ValueError(
"Node data array has too many dimensions - did you forget to "
"select a single timestep or a single depth layer? "
f"Extra dimensions: {extra_dimensions}.")
return cast(numpy.ndarray, flattened.values)