-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplotmethods.py
More file actions
1406 lines (1176 loc) · 46.6 KB
/
plotmethods.py
File metadata and controls
1406 lines (1176 loc) · 46.6 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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Plotmethod widgets.
This module defines the widgets to interface with the mapplot, plot2d and
lineplot plotmethods.
"""
# Disclaimer
# ----------
#
# Copyright (C) 2021 Helmholtz-Zentrum Hereon
# Copyright (C) 2020-2021 Helmholtz-Zentrum Geesthacht
#
# This file is part of psy-view and is released under the GNU LGPL-3.O license.
# See COPYING and COPYING.LESSER in the root of the repository for full
# licensing details.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3.0 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU LGPL-3.0 license for more details.
#
# You should have received a copy of the GNU LGPL-3.0 license
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations
import os.path as osp
from typing import (
TYPE_CHECKING,
ClassVar,
Callable,
Optional,
Union,
List,
Hashable,
Dict,
Any,
Tuple,
Iterator,
TypeVar,
Type,
)
from enum import Enum
from functools import partial
from itertools import chain, cycle
import contextlib
import textwrap
import dataclasses
import xarray as xr
from psyplot.utils import unique_everseen
import psyplot.data as psyd
from PyQt5 import QtWidgets, QtCore, QtGui
import psy_view.dialogs as dialogs
import psy_view.utils as utils
from psy_view.rcsetup import rcParams
from psyplot_gui.common import get_icon as get_psy_icon
import psy_simple.widgets.colors as pswc
import matplotlib.colors as mcol
if TYPE_CHECKING:
from xarray import DataArray, Dataset, Variable
from psyplot.project import PlotterInterface, Project
from psyplot.data import InteractiveList
from psyplot.plotter import Plotter
T = TypeVar("T", bound="GridCell")
class PlotType(str, Enum):
"""A value for the 2D ``plot`` formatoption.
See Also
--------
psy_simple.plotters.Simple2DPlotter.plot
psy_maps.plotters.FieldPlotter.plot
"""
mesh = "mesh"
contourf = "contourf"
contour = "contour"
poly = "poly"
#: A mapping from projection identifier to the text we want to use in the GUI
projection_map = {
"cf": "Default",
"cyl": "Cylindric",
"robin": "Robinson",
"ortho": "Orthographic",
"moll": "Mollweide",
"northpole": "Arctic (Northpole)",
"southpole": "Antarctic (Southpole)",
}
@dataclasses.dataclass
class GridCell:
"""A grid cell within a QGridLayout managing one QWidget."""
#: the :class:`PyQt5.QtWidgets.QWidget` instance (i.e. the widget) or a
#: layout (:class:`PyQt5.QtWidgets.QLayout`)
qobject: Union[QtWidgets.QWidget, QtWidgets.QLayout]
#: The starting column for the widget. If None, it will be estimated based
#: on the other widgets in the row
column: Optional[int] = None
#: The number of columns to cover
colspan: int = 1
#: A boolean whether to add stretch or not
stretch: bool = False
@classmethod
def from_alias(
cls: Type[T],
o: Union[QtWidgets.QWidget, QtWidgets.QLayout],
c: Optional[int] = None,
cs: int = 1,
s: bool = False
) -> T:
"""Create a :class:`GridCell` from shorter kws.
Parameters
----------
o: QWidget or QLayout
The alias for :attr:`qobject`
c: int or None, optional
The alias for :attr:`column`
cs: int, optional
The alias for :attr:`colspan`
s: bool, optional
The alias for :attr:`stretch`
Returns
-------
GridCell
The generated instance of :class:`GridCell`
"""
return cls(qobject=o, column=c, colspan=cs, stretch=s)
class PlotMethodWidget(QtWidgets.QWidget):
"""Base class for interfacing a psyplot plotmethod.
This method serves as a base class for interfacing any of the psyplot
plot methods registered via :func:`psyplot.project.register_plotter`.
The name of the plotmethod should be implemented as the :attr:`plotmethod`
attribute.
"""
plotmethod: ClassVar[str] = ''
#: trigger a replot of this widget. This can be emitted with the
#: :meth:`trigger_replot` method
replot = QtCore.pyqtSignal(str)
#: trigger a replot of this widget. This can be emitted with the
#: :meth:`trigger_reset` method
reset = QtCore.pyqtSignal(str)
#: signalize that the widget has been changed but not plot changes are
#: needed
changed = QtCore.pyqtSignal(str)
array_info = None
layout: QtWidgets.QGridLayout = None
def __init__(
self, get_sp: Callable[[], Optional[Project]],
ds: Optional[Dataset]):
super().__init__()
self._get_sp = get_sp
self.setup()
if hasattr(self, "layout"):
self.setLayout(self.layout)
self.refresh(ds)
def setup(self) -> None:
"""Set up the widget during initialization."""
self.layout = QtWidgets.QGridLayout()
self.setup_widgets()
self.setup_widget_grid()
@property
def formatoption_rows(self) -> List[List[GridCell]]:
"""Get a mapping from row name to a row of :class:`GridCells`."""
rows: List[List[GridCell]] = []
for func in self.fmt_setup_functions:
rows.extend(self.get_rows(func))
return rows
def get_rows(self, func: Callable) -> List[List[GridCell]]:
"""Get the rows of the formatoption widgets.
This method should take callable from the :attr:`fmt_setup_functions`
list and return the rows corresponding to :attr:`formatoption_rows`.
"""
return [[]]
@property
def fmt_setup_functions(self) -> List[Callable]:
"""Get a list of rows for formatoptions.
This property returns a list of callable. Each callable should setup
a horizonal (or widget) that is added to the :attr:`layout` vbox.
"""
return []
def setup_widgets(self) -> None:
"""Set up the widgets for this plotmethod."""
for func in self.fmt_setup_functions:
func()
def setup_separation_line(self) -> None:
"""Just a convenience function to create a separation line.
This method does nothing but tells :meth:`get_rows` to add an instance
of :class:`QHline`.
"""
pass
def setup_widget_grid(self) -> None:
"""Setup the widget grid based on :attr:`formatoption_rows`."""
rows = self.formatoption_rows
layout = self.layout
for i, row in enumerate(rows):
col: int = 0
for gc in row:
col = gc.column if gc.column is not None else col
if isinstance(gc.qobject, QtWidgets.QLayout):
layout.addLayout(gc.qobject, i, col, 1, gc.colspan)
else:
layout.addWidget(gc.qobject, i, col, 1, gc.colspan)
col += gc.colspan
layout.setRowStretch(len(rows), 1)
@property
def sp(self) -> Optional[Project]:
"""Get the subproject of this plotmethod interface."""
return getattr(self._get_sp(), self.plotmethod, None)
@property
def data(self) -> Union[DataArray, InteractiveList]:
"""Get the data of this plotmethod interface."""
if self.sp is None:
raise ValueError("No plot has yet been initialized")
else:
return self.sp[0]
@property
def plotter(self) -> Optional[Plotter]:
"""Get the first plotter of the :attr:`sp` project."""
if self.sp and self.sp.plotters:
return self.sp.plotters[0]
else:
return None
@property
def formatoptions(self) -> List[str]:
"""Get the formatoption keys of this plotmethod."""
if self.plotter is not None:
return list(self.plotter)
else:
import psyplot.project as psy
return list(getattr(psy.plot, self.plotmethod).plotter_cls())
def get_fmts(
self, var: DataArray, init: bool = False
) -> Dict[Union[Hashable, str, Any], Any]:
"""Get the formatoptions for a new plot.
Parameters
----------
var: xarray.Variable
The variable in the base dataset
init: bool
If True, call the :meth:`init_dims` method to inject necessary
formatoptions and dimensions for the initialization.
Returns
-------
dict
A mapping from formatoption or dimension to the corresponding value
for the plotmethod.
"""
ret = {}
if init:
ret.update(self.init_dims(var))
return ret
def init_dims(
self, var: DataArray
) -> Dict[Union[Hashable, str, Any], Any]:
"""Get the formatoptions for a new plot.
Parameters
----------
var: xarray.Variable
The variable in the base dataset
Returns
-------
dict
A mapping from formatoption or dimension to the corresponding value
for the plotmethod.
"""
return {}
def refresh(self, ds: Optional[Dataset]) -> None:
"""Refresh this widget from the given dataset."""
self.setEnabled(bool(self.sp))
def trigger_replot(self) -> None:
"""Emit the :attr:`replot` signal to replot the project."""
self.replot.emit(self.plotmethod)
def trigger_reset(self):
"""Emit the :attr:`reset` signal to reinitialize the project."""
self.array_info = self.sp.array_info(
standardize_dims=False)[self.sp[0].psy.arr_name]
self.reset.emit(self.plotmethod)
def trigger_refresh(self):
"""Emit the :attr:`changed` signal to notify changes in the plot."""
self.changed.emit(self.plotmethod)
def get_slice(
self, x: float, y: float
) -> Optional[Dict[Hashable, Union[int, slice]]]:
"""Get the slice for the selected coordinates.
This method is called when the user clicks on the coordinates in the
plot.
See Also
--------
psy_view.ds_widget.DatasetWidget.display_line
Notes
-----
This is reimplemented in the :class:`MapPlotWidget`.
"""
return None
def valid_variables(self, ds: Dataset) -> List[Hashable]:
"""Get a list of variables that can be visualized with this plotmethod.
Parameters
----------
ds: xarray.Dataset
The dataset to use
Returns
-------
list of str
List of variable names to plot
"""
ret = []
plotmethod = getattr(ds.psy.plot, self.plotmethod)
for v in list(ds):
init_kws = self.init_dims(ds[v]) # type: ignore
dims = init_kws.get('dims', {})
decoder = init_kws.get('decoder')
if plotmethod.check_data(ds, v, dims, decoder)[0][0]:
ret.append(v)
return ret
class QHLine(QtWidgets.QFrame):
"""A horizontal seperation line."""
def __init__(self):
super().__init__()
self.setMinimumWidth(1)
self.setFixedHeight(20)
self.setFrameShape(QtWidgets.QFrame.HLine)
self.setFrameShadow(QtWidgets.QFrame.Sunken)
self.setSizePolicy(
QtWidgets.QSizePolicy.Preferred,
QtWidgets.QSizePolicy.Minimum
)
class MapPlotWidget(PlotMethodWidget):
"""A widget to control the mapplot plotmethod."""
plotmethod = 'mapplot'
@property
def sp(self) -> Optional[Project]:
sp = super().sp
if sp:
arrays: List[str] = [
data.psy.arr_name for data in sp
if not isinstance(data, psyd.InteractiveList)
]
return sp(arr_name=arrays)
return sp
def get_rows(self, func: Callable) -> List[List[GridCell]]:
"""Get the rows of the formatoption widgets.
This method should take callable from the :attr:`fmt_setup_functions`
list and return the rows corresponding to :attr:`formatoption_rows`.
"""
if func == self.setup_color_buttons:
row = [
GridCell(QtWidgets.QLabel("Colormap")),
GridCell(self.btn_cmap),
GridCell(self.btn_cmap_settings, stretch=True),
]
elif func == self.setup_plot_buttons:
row = [
GridCell(QtWidgets.QLabel("Plot type")),
GridCell(self.combo_plot),
GridCell(self.btn_datagrid, stretch=True),
]
elif func == self.setup_projection_buttons:
row = [
GridCell(QtWidgets.QLabel("Projection")),
GridCell(self.btn_proj),
GridCell(self.btn_proj_settings, stretch=True),
]
elif func == self.setup_labels_button:
row = [GridCell(self.btn_labels, colspan=3)]
elif func == self.setup_separation_line:
row = [GridCell(QHLine(), colspan=3)]
elif func == self.setup_dimension_box:
row = [GridCell(self.dimension_box, colspan=3)]
else:
raise ValueError(f"Unknown function {func}")
return [row]
@property
def fmt_setup_functions(self) -> List[Callable]:
"""Get a list of rows for formatoptions.
This property returns a list of callable. Each callable should setup
a horizonal (or widget) that is added to the :attr:`layout` vbox.
"""
return [
self.setup_color_buttons, self.setup_plot_buttons,
self.setup_projection_buttons, self.setup_labels_button,
self.setup_separation_line,
self.setup_dimension_box,
]
def setup_labels_button(self) -> None:
"""Add a button to modify the text labels."""
self.btn_labels = utils.add_pushbutton(
"Edit labels", self.edit_labels, "Edit title, colorbar labels, etc."
)
def setup_plot_buttons(self) -> None:
"""Setup the second row of formatoption widgets."""
self.combo_plot = QtWidgets.QComboBox()
self.plot_types: List[Optional[PlotType]] = [
PlotType.mesh,
PlotType.contourf,
PlotType.contour,
PlotType.poly,
None
]
self.combo_plot.setEditable(False)
self.combo_plot.addItems([
"Default", "Filled contours", "Contours", "Gridcell polygons",
"Disable"
])
self.combo_plot.currentIndexChanged.connect(self._set_plot_type)
self.btn_datagrid = utils.add_pushbutton(
"Gridcell boundaries", self.toggle_datagrid,
"Toggle the visibility of grid cell boundaries")
self.btn_datagrid.setCheckable(True)
return
def setup_color_buttons(self) -> None:
"""Set up the buttons to change the colormap, etc."""
self.btn_cmap = pswc.CmapButton()
self.btn_cmap.setSizePolicy(
QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding
)
self.btn_cmap.setToolTip("Select a different colormap")
self.btn_cmap.colormap_changed.connect(self.set_cmap)
self.btn_cmap.colormap_changed[mcol.Colormap].connect(self.set_cmap)
self.setup_cmap_menu()
self.btn_cmap_settings = utils.add_pushbutton(
utils.get_icon('color_settings'), self.edit_color_settings,
"Edit color settings",
icon=True)
def setup_cmap_menu(self) -> QtWidgets.QMenu:
"""Set up the menu to change the colormaps."""
menu = self.btn_cmap.cmap_menu
menu.addSeparator()
self.select_cmap_action = menu.addAction(
'More colormaps', self.open_cmap_dialog)
self.color_settings_action = menu.addAction(
QtGui.QIcon(utils.get_icon('color_settings')), 'More options',
self.edit_color_settings)
return menu
def open_cmap_dialog(self, N: int = 10) -> None:
"""Open the dialog to change the colormap.
Parameters
----------
N: int
The number of colormaps to show
See Also
--------
psy_simple.widgets.colors.CmapButton
"""
if self.plotter:
N = self.plotter.plot.mappable.get_cmap().N
else:
N = 10
self.btn_cmap.open_cmap_dialog(N)
def setup_projection_menu(self) -> QtWidgets.QMenu:
"""Set up the menu to modify the basemap."""
menu = QtWidgets.QMenu()
for projection in rcParams['projections']:
menu.addAction(
projection_map.get(projection, projection),
partial(self.set_projection, projection),
)
menu.addSeparator()
self.proj_settings_action = menu.addAction(
QtGui.QIcon(utils.get_icon('proj_settings')),
"Customize basemap...", self.edit_basemap_settings)
return menu
def get_projection_label(self, proj: str) -> str:
"""Get the label for a projection in the GUI.
Parameters
----------
proj: str
The projection value for the
:attr:`~psy_maps.plotters.FieldPlotter.plot` formatoption
Returns
-------
str
The label of the projection in the GUI
See Also
--------
get_projection_label
projection_map
psy_maps.plotters.FieldPlotter.plot
"""
return projection_map.get(proj, proj)
def get_projection_value(self, label: str) -> str:
"""Get the value for the `projection` formatoption.
This method is the inverse of :meth:`get_projection_label`.
Parameters
----------
label: str
The projection label that is used in the GUI
:attr:`~psy_maps.plotters.FieldPlotter.plot` formatoption
Returns
-------
str
The value to use for the ``projection`` formatoption
See Also
--------
get_projection_label
projection_map
psy_maps.plotters.FieldPlotter.plot
"""
inv_map = {lbl: proj for proj, lbl in projection_map.items()}
return inv_map.get(label, rcParams["projections"][0])
def setup_projection_buttons(self) -> None:
"""Set up the buttons to modify the basemap."""
self.btn_proj = utils.add_pushbutton(
self.get_projection_label(rcParams["projections"][0]),
self.choose_next_projection, "Change the basemap projection",
toolbutton=True)
self.btn_proj.setMenu(self.setup_projection_menu())
self.btn_proj.setSizePolicy(
QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding
)
self.btn_proj.setPopupMode(QtWidgets.QToolButton.MenuButtonPopup)
self.btn_proj_settings = utils.add_pushbutton(
utils.get_icon('proj_settings'), self.edit_basemap_settings,
"Edit basemap settings",
icon=True)
def setup_dimension_box(self) -> None:
"""Set up a box to control, what is the x and y-dimension."""
self.dimension_box = QtWidgets.QGridLayout()
self.dimension_box.addWidget(QtWidgets.QLabel('x-Dimension:'), 0, 0)
self.combo_xdim = QtWidgets.QComboBox()
self.dimension_box.addWidget(self.combo_xdim, 0, 1)
self.dimension_box.addWidget(QtWidgets.QLabel('y-Dimension:'), 0, 2)
self.combo_ydim = QtWidgets.QComboBox()
self.dimension_box.addWidget(self.combo_ydim, 0, 3)
self.dimension_box.addWidget(QtWidgets.QLabel('x-Coordinate:'), 1, 0)
self.combo_xcoord = QtWidgets.QComboBox()
self.dimension_box.addWidget(self.combo_xcoord, 1, 1)
self.dimension_box.addWidget(QtWidgets.QLabel('y-Coordinate:'), 1, 2)
self.combo_ycoord = QtWidgets.QComboBox()
self.dimension_box.addWidget(self.combo_ycoord, 1, 3)
self.combo_xdim.currentTextChanged.connect(self.set_xcoord)
self.combo_ydim.currentTextChanged.connect(self.set_ycoord)
for combo in self.coord_combos:
combo.currentIndexChanged.connect(self.trigger_refresh)
def set_xcoord(self, text: str) -> None:
"""Set the name of the x-coordinate."""
self.set_combo_text(self.combo_xcoord, text)
def set_ycoord(self, text: str) -> None:
"""Set the name of the y-coordinate."""
self.set_combo_text(self.combo_ycoord, text)
def set_combo_text(self, combo: QtWidgets.QComboBox, text: str) -> None:
"""Convenience function to update set the current text of a combobox.
Parameters
----------
combo: PyQt5.QtWidgets.QComboBox
The combobox to modify
text: str
The item to use"""
items = list(map(combo.itemText, range(combo.count())))
if text in items:
combo.setCurrentIndex(items.index(text))
def init_dims(
self, var: DataArray
) -> Dict[Union[Hashable, str, Any], Any]:
"""Get the formatoptions for a new plot.
This method updates the coordinates combo boxes with the
x- and y-coordinate of the variable.
Parameters
----------
var: xarray.Variable
The variable in the base dataset
Returns
-------
dict
A mapping from formatoption or dimension to the corresponding value
for the plotmethod.
"""
ret = super().init_dims(var)
dims: Dict[Hashable, Union[int, slice]] = {}
xdim = ydim = None
if self.combo_xdim.currentIndex():
xdim = self.combo_xdim.currentText()
if xdim in var.dims:
dims[xdim] = slice(None)
if self.combo_ydim.currentIndex():
ydim = self.combo_ydim.currentText()
if ydim in var.dims:
dims[ydim] = slice(None)
if dims:
missing = [dim for dim in var.dims if dim not in dims]
for dim in missing:
dims[dim] = 0
if len(dims) == 1 and xdim != ydim:
if xdim is None:
xdim = missing[-1]
else:
ydim = missing[-1]
dims[missing[-1]] = slice(None) # keep the last dimension
ret['dims'] = dims
if self.combo_xcoord.currentIndex():
xcoord = self.combo_xcoord.currentText()
ret['decoder'] = {'x': {xcoord}}
if self.combo_ycoord.currentIndex():
ycoord = self.combo_ycoord.currentText()
ret.setdefault('decoder', {})
ret['decoder']['y'] = {ycoord}
if (xdim is not None and xdim in var.dims and
ydim is not None and ydim in var.dims):
ret['transpose'] = var.dims.index(xdim) < var.dims.index(ydim)
return ret
def valid_variables(self, ds: Dataset) -> List[Hashable]:
"""Get a list of variables that can be visualized with this plotmethod.
Parameters
----------
ds: xarray.Dataset
The dataset to use
Returns
-------
list of str
List of variable names to plot
"""
valid = super().valid_variables(ds)
if (not any(combo.count() for combo in self.coord_combos) or
not any(combo.currentIndex() for combo in self.coord_combos)):
return valid
if self.combo_xdim.currentIndex():
xdim = self.combo_xdim.currentText()
valid = [v for v in valid if xdim in ds[v].dims]
if self.combo_ydim.currentIndex():
ydim = self.combo_xdim.currentText()
valid = [v for v in valid if ydim in ds[v].dims]
if self.combo_xcoord.currentIndex():
xc_dims = set(ds[self.combo_xcoord.currentText()].dims)
valid = [v for v in valid
if xc_dims.intersection(ds[v].dims)]
if self.combo_ycoord.currentIndex():
yc_dims = set(ds[self.combo_ycoord.currentText()].dims)
valid = [v for v in valid
if yc_dims.intersection(ds[v].dims)]
return valid
@property
def coord_combos(self) -> List[QtWidgets.QComboBox]:
"""Get the combo boxes for x- and y-dimension and -coordinates."""
return [self.combo_xdim, self.combo_ydim, self.combo_xcoord,
self.combo_ycoord]
@contextlib.contextmanager
def block_combos(self) -> Iterator[None]:
"""Temporarilly block any signal of the :attr:`coord_combos`."""
for combo in self.coord_combos:
combo.blockSignals(True)
yield
for combo in self.coord_combos:
combo.blockSignals(False)
def setEnabled(self, b: bool) -> None:
"""Enable or disable the projection and color buttons.
Parameters
----------
b: bool
If True, enable the buttons, else disable.
"""
self.btn_proj_settings.setEnabled(b)
self.proj_settings_action.setEnabled(b)
self.btn_datagrid.setEnabled(b)
self.color_settings_action.setEnabled(b)
self.btn_cmap_settings.setEnabled(b)
self.btn_labels.setEnabled(b)
def set_cmap(self, cmap: str) -> None:
"""Update the plotter with the given colormap.
Parameters
----------
cmap: str
The colormap name.
"""
plotter = self.plotter
if plotter and 'cmap' in plotter:
plotter.update(cmap=cmap)
def toggle_datagrid(self) -> None:
"""Toggle the visibility of the grid cell boundaries."""
if self.plotter:
if self.btn_datagrid.isChecked():
self.plotter.update(datagrid='k-')
else:
self.plotter.update(datagrid=None)
def edit_labels(self) -> None:
"""Open the dialog to edit the text labels in the plot."""
dialogs.LabelDialog.update_project(
self.sp, 'figtitle', 'title', 'clabel')
def edit_color_settings(self) -> None:
"""Open the dialog to edit the color settings."""
if self.sp and self.plotter:
dialogs.CmapDialog.update_project(self.sp)
if isinstance(self.plotter.cmap.value, str):
self.btn_cmap.setText(self.plotter.cmap.value)
else:
self.btn_cmap.setText('Custom')
def choose_next_projection(self) -> None:
"""Choose the next projection from the rcParams `projection` value."""
select = False
nprojections = len(rcParams['projections'])
current = self.get_projection_value(self.btn_proj.text())
for i, proj in enumerate(cycle(rcParams['projections'])):
if proj == current:
select = True
elif select or i == nprojections:
break
self.set_projection(proj)
def set_projection(self, proj: str) -> None:
"""Update the projection of the plot with the given projection.
Parameters
----------
projection: str
The projection name for the
:attr:`~psy_maps.plotters.FieldPlotter.projection` formatoption.
"""
self.btn_proj.setText(self.get_projection_label(proj))
plotter = self.plotter
if plotter and 'projection' in plotter:
plotter.update(projection=proj)
def _set_plot_type(self, i: int) -> None:
"""Set the plot type from the index in :attr:`combo_plot`."""
self.set_plot_type(self.plot_types[i])
def set_plot_type(self, plot_type: Optional[PlotType]) -> None:
"""Update the value for the ``plot`` formatoption
Parameters
----------
ploy_type: :class:`PlotType` or None
The value for the :attr:`~psy_simple.plotters.Simple2DPlotter.plot`
formatoption.
"""
plotter = self.plotter
if plotter:
plotter.update(plot=plot_type)
def edit_basemap_settings(self) -> None:
"""Open a dialog to edit the basemap and projection settings."""
if self.plotter:
dialogs.BasemapDialog.update_plotter(self.plotter)
def get_fmts(
self, var: DataArray,
init: bool = False
) -> Dict[Union[Hashable, str, Any], Any]:
"""Get the formatoptions for a new plot.
Parameters
----------
var: xarray.Variable
The variable in the base dataset
init: bool
If True, call the :meth:`init_dims` method to inject necessary
formatoptions and dimensions for the initialization.
Returns
-------
dict
A mapping from formatoption or dimension to the corresponding value
for the plotmethod.
"""
fmts: Dict[Union[Hashable, str, Any], Any] = {}
fmts['cmap'] = self.btn_cmap.text()
if 'projection' in self.formatoptions:
fmts['projection'] = self.get_projection_value(
self.btn_proj.text()
)
if 'time' in var.dims:
fmts['title'] = '%(time)s'
if 'long_name' in var.attrs:
fmts['clabel'] = '%(long_name)s'
else:
fmts['clabel'] = '%(name)s'
if 'units' in var.attrs:
fmts['clabel'] += ' %(units)s'
fmts['plot'] = self.plot_types[self.combo_plot.currentIndex()]
if fmts['plot'] == 'contour':
# we need to set a global map extend, see
# https://github.com/SciTools/cartopy/issues/1673
fmts['map_extent'] = 'global'
if init:
fmts.update(self.init_dims(var))
return fmts
def refresh(self, ds: Optional[Dataset]) -> None:
"""Refresh this widget from the given dataset."""
self.setEnabled(bool(self.sp))
auto = 'Set automatically'
self.refresh_from_sp()
with self.block_combos():
if ds is None:
ds = xr.Dataset()
current_dims = set(map(
self.combo_xdim.itemText, range(1, self.combo_xdim.count())))
ds_dims = list(
map(str, (dim for dim, n in ds.dims.items() if n > 1)))
if current_dims != set(ds_dims):
self.combo_xdim.clear()
self.combo_ydim.clear()
self.combo_xdim.addItems([auto] + ds_dims)
self.combo_ydim.addItems([auto] + ds_dims)
current_coords = set(map(
self.combo_xcoord.itemText, range(1, self.combo_xcoord.count())))
ds_coords = list(
map(str, (c for c, arr in ds.coords.items() if arr.ndim)))
if current_coords != set(ds_coords):
self.combo_xcoord.clear()
self.combo_ycoord.clear()
self.combo_xcoord.addItems([auto] + ds_coords)
self.combo_ycoord.addItems([auto] + ds_coords)
enable_combos = not bool(self.sp)
if not enable_combos and self.combo_xdim.isEnabled():
self.reset_combos = [combo.currentIndex() == 0
for combo in self.coord_combos]
elif enable_combos and not self.combo_xdim.isEnabled():
for reset, combo in zip(self.reset_combos, self.coord_combos):
if reset:
combo.setCurrentIndex(0)
self.reset_combos = [False] * len(self.coord_combos)
for combo in self.coord_combos:
combo.setEnabled(enable_combos)
if not enable_combos:
data = self.data
xdim = str(data.psy.get_dim('x'))
ydim = str(data.psy.get_dim('y'))
self.combo_xdim.setCurrentText(xdim)
self.combo_ydim.setCurrentText(ydim)
xcoord = data.psy.get_coord('x')
xcoord = xcoord.name if xcoord is not None else xdim
ycoord = data.psy.get_coord('y')
ycoord = ycoord.name if ycoord is not None else ydim
self.combo_xcoord.setCurrentText(xcoord)
self.combo_ycoord.setCurrentText(ycoord)
def refresh_from_sp(self) -> None:
"""Refresh this widget from the plotters project."""
plotter = self.plotter
if plotter:
if isinstance(plotter.projection.value, str):
self.btn_proj.setText(
self.get_projection_label(plotter.projection.value)
)
else:
self.btn_proj.setText('Custom')
if isinstance(plotter.cmap.value, str):
self.btn_cmap.setText(plotter.cmap.value)
else:
self.btn_cmap.setText('Custom')