pppy 1.1.0__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.
pppy/pppyComp.py ADDED
@@ -0,0 +1,867 @@
1
+ #!/usr/bin/env python3
2
+
3
+ # Copyright (c) Météo France (2018-)
4
+ # This software is governed by the CeCILL-C license under French law.
5
+ # http://www.cecill.info
6
+
7
+ """
8
+ The PPPYComp class is used to run the different parameterizations (instances of PPPY),
9
+ save the results and plot some variables.
10
+ Variables are defined using a string (e.g. temperature is commonly associated
11
+ with the 'T' string which is used as a key in the sate dictionary) but the plot
12
+ methods try to build legends using the long name of the variables. To do this, the
13
+ variable name is searched in the VAR_NAME dictionary and, if found, the long name
14
+ is set to the associated key, otherwise, the long name is set to the name.
15
+ The same mechanism is used for the unit of the variable which is searched in the
16
+ VAR_UNIT dictionary.
17
+ Both dictionaries can be modified before plotting to configure the long name
18
+ and the unit to use in the plot legends.
19
+
20
+ >>> import pppy
21
+ >>> pppy.VAR_NAME['T'] = "Temperature"
22
+ >>> pppy.VAR_UNIT['T'] = "K"
23
+ """
24
+
25
+ import numpy
26
+ import os
27
+ import h5py
28
+ import copy
29
+ import multiprocessing
30
+ import logging
31
+ from .pppy import PPPY
32
+ from pppy import VAR_NAME, VAR_UNIT, COLORS, STYLES
33
+
34
+ class PPPYComp():
35
+ """
36
+ This class is used to perform a comparison between several implementation
37
+ of a parameterization.
38
+ This class can be directly instantiated, there is no need to extend it,
39
+ unless you need to write some diagnostic methods (e.g. plots, statistics...).
40
+ In particular, the get_scheme_output method can be extended to compute
41
+ diagnostics from the variable computed by a scheme and make these
42
+ diagnostics available for plotting.
43
+ """
44
+
45
+ def __init__(self, schemes, output_dir, duration, init_state, name, tag):
46
+ """
47
+ At instantiation, we define the schemes to compare, the total duration of the
48
+ time integration, the initial conditions and the name of the experiment.
49
+
50
+ :param schemes: a list of PPPY instances
51
+ :param output_dir: directory in which results are stored
52
+ :param duration: total duration of the time integration (in seconds)
53
+ :param init_state: dictionary holding the initial state
54
+ :param name: name of the comparison experiment used in legends
55
+ :param tag: string to identify uniquely the experiment, the
56
+ string must be usable to build filenames
57
+
58
+ After instantiation, the different parameterizations can be run with the
59
+ run method. Results can be plotted (plot_evol, plot_comp and plot_multi
60
+ methods) or time series can be accessed (get_series method).
61
+ """
62
+
63
+ assert isinstance(schemes, list), "schemes must be a list"
64
+ for scheme in schemes:
65
+ assert isinstance(scheme, PPPY), "third element of tuple must be a PPPY instance"
66
+ if not len(schemes) == len(set([scheme.name for scheme in schemes])):
67
+ logging.warning("Scheme names are not unique")
68
+ assert len(schemes) == len(set([scheme.tag for scheme in schemes])), \
69
+ "all scheme tags must be different"
70
+
71
+ assert isinstance(duration, float) and duration > 0., \
72
+ "duration must be a float and must be > 0."
73
+ assert isinstance(init_state, dict), "init_state must be a dict"
74
+ assert isinstance(name, str) and isinstance(tag, str), \
75
+ "name and tag must be strings"
76
+
77
+ values = list(init_state.values())
78
+ assert all([isinstance(value, numpy.ndarray) for value in values]), \
79
+ "All init_state item must be ndarrays"
80
+
81
+ if not os.path.exists(output_dir):
82
+ raise IOError("output dir does not exist")
83
+ self._output_dir = os.path.join(output_dir, tag)
84
+ if not os.path.exists(self._output_dir):
85
+ os.mkdir(self._output_dir)
86
+
87
+ self._schemes = schemes
88
+ self._duration = duration
89
+ self._init_state = init_state
90
+ self.name = name
91
+ self.tag = tag
92
+ self._files = {}
93
+
94
+ def run(self, only_param_tag=None, force=False):
95
+ """
96
+ This methods runs the different parameterizations.
97
+
98
+ :param only_param_tag: (optional) list of parameterization tags to actually run.
99
+ By default (None), all parameterizations are run.
100
+ :param force: (optional) By default (False), parameterizations are run only
101
+ if needed (output file for same tag and
102
+ same parameterization tag not found). If force is True,
103
+ parameterizations are run anyway.
104
+ """
105
+ assert isinstance(only_param_tag, list) or only_param_tag is None, \
106
+ "only_param_tag must be a list or None"
107
+ if not only_param_tag is None:
108
+ assert all([isinstance(item, str) for item in only_param_tag]), \
109
+ "if only_param_tag is a list, each item must be a string"
110
+
111
+ for scheme in [scheme for scheme in self._schemes
112
+ if only_param_tag is None or scheme.tag in only_param_tag]:
113
+ output_file = os.path.join(self._output_dir, scheme.tag + '.hdf5')
114
+ if os.path.exists(output_file) and force:
115
+ os.remove(output_file)
116
+ if not os.path.exists(output_file):
117
+ #To isolate calls, we use multiprocessing instead of calling directly
118
+ #scheme(copy.deepcopy(self._initState), self._duration, output_file)
119
+ #Without this trick we can have issues with different schemes using
120
+ #same symbol names and eventually with other python libs (such as plt)
121
+ child = multiprocessing.Process(target=scheme,
122
+ args=(copy.deepcopy(self._init_state),
123
+ self._duration, output_file))
124
+ child.start()
125
+ child.join()
126
+
127
+ @staticmethod
128
+ def _var_legend(var, mfactor):
129
+ """
130
+ Internal method that return the string to use as a legend.
131
+
132
+ :param var: name of the variable
133
+ :param mfactor: multiplying factor applied to data
134
+
135
+ :returns: The method returns the string to use.
136
+ """
137
+ legend = VAR_NAME.get(var, var)
138
+ unit = VAR_UNIT.get(var, "??")
139
+ if unit is not None:
140
+ if mfactor == 1.:
141
+ legend += " (" + unit + ")"
142
+ else:
143
+ legend += " (" + str(1./mfactor) + " " + unit + ")"
144
+ return legend
145
+
146
+ def plot_multi(self, plot_shape, plot_spec, title=None, figsize=None, sharey=False):
147
+ """
148
+ This method is a wrapper around matplotlib features to deal with a
149
+ figure containing several plots.
150
+
151
+ :param plot_shape: tuple (nb_rows, nb_cols) to ask for a figure containing
152
+ nb_rows * nb_cols subplots
153
+ :param plot_spec: list of plot definitions. Each plot definition is a tuple
154
+ (kind, args, kwargs). Method will plot the subfigure using
155
+ method plot_<kind> which must accept a matplotlib axis
156
+ as first parameter then args and kwargs (args and/or
157
+ kwargs can be omitted)
158
+ :param title: (optional) main title to use for the figure
159
+ :param figsize: (optional) figsize argument to build the matplotlib figure
160
+ :param sharey: (optional) must we share the y axis (this option is active
161
+ only if all plots have the same kind)
162
+
163
+ :returns: The method returns the figure and the list of return values of each plot.
164
+ """
165
+ assert isinstance(plot_spec, list), "plot_spec must be a list"
166
+ assert isinstance(plot_shape, tuple), "plot_shape must be a tuple"
167
+ assert len(plot_shape) == 2, "plot_shape must have two elements"
168
+ assert len(plot_spec) <= plot_shape[0] * plot_shape[1], \
169
+ "number of plots must be inferior to plot_shape[0] * plot_shape[1]"
170
+
171
+ import matplotlib.pyplot as plt
172
+
173
+ fig = plt.figure(figsize=figsize)
174
+ if title is None:
175
+ fig.suptitle(self.name)
176
+ else:
177
+ fig.suptitle(title)
178
+ result = []
179
+ for iplot, plot_def in enumerate(plot_spec):
180
+ assert isinstance(plot_def, tuple), \
181
+ "Each plot definition must be a tuple"
182
+ kind = plot_def[0]
183
+ if len(plot_def) > 3:
184
+ raise ValueError("A plot definition is a tuple with a length of 1, 2 or 3")
185
+ elif len(plot_def) == 3:
186
+ plot_args, plot_kwargs = plot_def[1:]
187
+ assert isinstance(plot_args, list) and isinstance(plot_kwargs, dict), \
188
+ "Second item of a plot definition must be a list and third " + \
189
+ "item must be a dictionary"
190
+ elif len(plot_def) == 2:
191
+ assert isinstance(plot_def[1], list) or isinstance(plot_def[1], dict), \
192
+ "Second item of a plot definition must be a list or a dictionary"
193
+ if isinstance(plot_def[1], list):
194
+ plot_args = plot_def[1]
195
+ plot_kwargs = {}
196
+ else:
197
+ plot_kwargs = plot_def[1]
198
+ plot_args = []
199
+ else:
200
+ plot_args = []
201
+ plot_kwargs = {}
202
+ kwargs = {}
203
+ if iplot != 0 and len(set([item[0] for item in plot_spec])) == 1:
204
+ kwargs = {'sharex':ax}
205
+ if sharey:
206
+ kwargs['sharey'] = ax
207
+ ax = fig.add_subplot(plot_shape[0], plot_shape[1], iplot + 1, **kwargs)
208
+ method = getattr(self, "plot_" + kind)
209
+ result.append(method(ax, *plot_args, **plot_kwargs))
210
+ return fig, result
211
+
212
+ def common_times(self, only_param_tag=None, only_times=None, common_times=1):
213
+ """
214
+ This method extracts output times of the different parameterizations and
215
+ looks for common times.
216
+
217
+ :param only_param_tag: (optional) list of parameterization tags to actually consider.
218
+ By default (None), all parameterizations are used.
219
+ :param only_times: (optional) If set, result is limited to times listed in this list
220
+ :param common_times: (optional, defaults to 1) If 1, the time series is the list of all
221
+ encountered time values in all parameterizations; if 2, time values
222
+ are limited to values common to all parameterizations
223
+ :returns: The method returns a numpy array containing the time values.
224
+ """
225
+
226
+ assert isinstance(only_param_tag, list) or only_param_tag is None, \
227
+ "only_param_tag must be a list or None"
228
+ if not only_param_tag is None:
229
+ assert all([isinstance(item, str) for item in only_param_tag]), \
230
+ "if only_param_tag is a list, each item must be a string"
231
+ assert isinstance(only_times, list) or only_times is None, \
232
+ "only_times must be a list or None"
233
+ if not only_times is None:
234
+ assert all([isinstance(item, float) for item in only_times]), \
235
+ "if only_times is a list, each item must be a float"
236
+
237
+ schemes = [scheme for scheme in self._schemes
238
+ if only_param_tag is None or scheme.tag in only_param_tag]
239
+ if common_times == 1:
240
+ times = []
241
+ for scheme in schemes:
242
+ times.extend(list(self.get_scheme_output(scheme, 't')))
243
+ times = sorted(list(set(times)))
244
+ times = [t for t in times if only_times is None or t in only_times]
245
+ elif common_times == 2:
246
+ times = []
247
+ for scheme in schemes:
248
+ times = [t for t in list(self.get_scheme_output(scheme, 't')) if t in times]
249
+ times = sorted(times)
250
+ times = [t for t in times if only_times is None or t in only_times]
251
+
252
+ return times
253
+
254
+ def get_scheme_output(self, scheme, var):
255
+ """
256
+ This method returns the output of a scheme
257
+ :param scheme: scheme
258
+ :param val: variable to read
259
+ :returns: The corresponding array
260
+ """
261
+ filename = os.path.join(self._output_dir, scheme.tag + '.hdf5')
262
+ if not os.path.exists(filename):
263
+ raise IOError(f'Filename does not exist: {filename}')
264
+ if scheme.tag not in self._files:
265
+ try:
266
+ f = h5py.File(filename, 'r')
267
+ except OSError:
268
+ #This error can be due to too many files opened
269
+ self.close_files()
270
+ f = h5py.File(filename, 'r')
271
+ self._files[scheme.tag] = f
272
+ if var in self._files[scheme.tag]:
273
+ return self._files[scheme.tag][var][...]
274
+ else:
275
+ print(self._files[scheme.tag].items())
276
+ raise ValueError(("This variable (%s) is not present in " + \
277
+ "this output file (%s)") % (var, filename))
278
+
279
+ def get_series(self, var_names, slicing=None,
280
+ mfactor=1., only_param_tag=None,
281
+ only_times=None, common_times=0):
282
+ """
283
+ This method read output files and return time series of variables.
284
+
285
+ :param var_names: list of variables to read
286
+ :param slicing: (optional) slicing to apply to the data series.
287
+ This can be used to run a parameterization on a
288
+ multidimensional initial state and extract result
289
+ for only one dimension.
290
+ :param mfactor: (optional) multiplying factor to apply to data
291
+ :param only_param_tag: (optional) list of parameterization tags to actually consider.
292
+ By default (None), all parameterizations are used.
293
+ :param only_times: (optional) If set, result is limited to times listed
294
+ :param common_times: (optional, defaults to 0) If 0, each parameterization
295
+ has its own time values; If 1, the time series is the list
296
+ of all encountered time values in all parameterizations,
297
+ resulting values are masked where undefined; If 2, time values
298
+ are limited to values common to all parameterizations.
299
+ :returns: The method returns a 3-tuple whose components are:
300
+
301
+ - var_names
302
+ - parameterization tags
303
+ - a dictionary with:
304
+ - keys of the form (<var_name>, <param_tag>)
305
+ - values being (time_serie, values)
306
+ """
307
+ assert isinstance(only_param_tag, list) or only_param_tag is None, \
308
+ "only_param_tag must be a list or None"
309
+ if not only_param_tag is None:
310
+ assert all([isinstance(item, str) for item in only_param_tag]), \
311
+ "if only_param_tag is a list, each item must be a string"
312
+ assert isinstance(only_times, list) or only_times is None, \
313
+ "only_times must be a list or None"
314
+ if not only_times is None:
315
+ assert all([isinstance(item, float) for item in only_times]), \
316
+ "if only_times is a list, each item must be a float"
317
+ assert isinstance(var_names, list), "var_names must be a list"
318
+ assert all([isinstance(item, str) for item in var_names]), \
319
+ "var_names must be a list of variable names"
320
+ assert common_times in [0, 1, 2], "common_times must be 0, 1 or 2"
321
+
322
+ #common_times
323
+ if common_times in [1, 2]:
324
+ times = self.common_times(only_param_tag, only_times, common_times)
325
+
326
+ schemes = [scheme for scheme in self._schemes
327
+ if only_param_tag is None or scheme.tag in only_param_tag]
328
+
329
+ result = {}
330
+ for scheme in schemes:
331
+ simul_time = self.get_scheme_output(scheme, 't')
332
+ if common_times == 0:
333
+ if only_times is None:
334
+ time = simul_time
335
+ else:
336
+ time = numpy.intersect1d(time, numpy.array(only_times))
337
+ else:
338
+ time = times
339
+ used_time = numpy.intersect1d(time, simul_time, True)
340
+ index_simul = numpy.searchsorted(simul_time, used_time)
341
+ index_result = numpy.searchsorted(time, used_time)
342
+ for var in var_names:
343
+ simul_values = self.get_scheme_output(scheme, var) * mfactor
344
+ if slicing is not None:
345
+ simul_values = simul_values[:, slicing]
346
+ shape = tuple([len(time)] + list(simul_values.shape[1:]))
347
+ serie = numpy.ma.masked_all(shape)
348
+ serie[index_result] = simul_values[index_simul]
349
+ result[(var, scheme.tag)] = (time, serie)
350
+
351
+ return var_names, [scheme.tag for scheme in schemes], result
352
+
353
+ def plot_evol(self, ax, var_names, slicing=None, y_var=None,
354
+ mfactor=1., only_param_tag=None, enable_contourf=True,
355
+ contourlevels=None, title=None,
356
+ switch_cls=False, linewidth=None,
357
+ xlabel=None, ylabel=None, colorbar=True,
358
+ clabel=False, xlim=None, ylim=None,
359
+ legend='min', colors=None, styles=None,
360
+ kwargs_contourf=None, kwargs_contour=None, kwargs_plot=None,
361
+ kwargs_legend=None):
362
+ """
363
+ This method plots a time evolution (x-axis is time) of different variables
364
+ for different schemes.
365
+
366
+ If data is 0D, each couple variable/scheme is represented by a line.
367
+
368
+ If data is 1D, each couple variable/scheme is represented by contour lines.
369
+
370
+ :param ax: matplolib axis to use for plotting
371
+ :param var_names: list of variables to plot
372
+ :param slicing: (optional) slicing to apply to the data series.
373
+ This can be used to run a parameterization on a
374
+ multidimensional initial state and extract result
375
+ for only one dimension.
376
+ :param y_var: (optional) If data is 1D, use this variable on the y-axis
377
+ :param mfactor: (optional) multiplying factor to apply to data
378
+ :param only_param_tag: (optional) list of parameterization tags to actually consider.
379
+ By default (None), all parameterizations are used.
380
+ :param enable_contourf: (optional) If only one 1D variable is plotted, enables
381
+ use of contourf instead of contour
382
+ :param contourlevels: (optional) List of values to use with contour and contourf
383
+ :param title: (optional) Title to use on subplot, if None title is automatic
384
+ :param switch_cls: (optional, default to False) By default, A line style is assigned
385
+ to each parameterization and a color is assign to each variable.
386
+ Setting this variable to True, reverse this default behavior.
387
+ :param linewidth: Control the line width used in plots
388
+ :param xlabel: To impose an x-axis label (None to generate it automatically)
389
+ :param ylabel: To impose an y-axis label (None to generate it automatically)
390
+ :param colorbar: In case of contourf use, adds a colorbar
391
+ :param clabel: To add labels to line contours
392
+ :param xlim: tuple (xmin, xmax) or None
393
+ :param ylim: tuple (ymin, ymax) or None
394
+ :param legend: 'min' to label curves with the minimum needed to distinguish them
395
+ 'full' to label curves with the entire information
396
+ 'no' to not plot legend
397
+ :param colors: list of color names to use, None to use the default list
398
+ :param styles: list of line styles to use, None to use the default list
399
+ a line style is a string (recognized by matplotlib) or a
400
+ even length tuple of on and off ink in points
401
+ :param kwargs_contourf, kwargs_contour and kwargs_plot: optional dictionaries of
402
+ arguments to be passed to the contourf, contour and plot methods
403
+ :param kwargs_legend: optional dictionaries of arguments to be passed to ax.legend
404
+
405
+ :returns: The method returns the output of the plotting function (plot, contour or contourf)
406
+ """
407
+ assert isinstance(only_param_tag, list) or only_param_tag is None, \
408
+ "only_param_tag must be a list or None"
409
+ if not only_param_tag is None:
410
+ assert all([isinstance(item, str) for item in only_param_tag]), \
411
+ "if only_param_tag is a list, each item must be a string"
412
+ assert isinstance(var_names, list), "var_names must be a list"
413
+ assert all([isinstance(item, str) for item in var_names]), \
414
+ "var_names must be a list of variable names"
415
+ assert legend in ['min', 'full', 'no'], "legend must be 'min', 'full' or 'no'"
416
+
417
+ kwargs_contourf = {} if kwargs_contourf is None else kwargs_contourf
418
+ kwargs_contour = {} if kwargs_contour is None else kwargs_contour
419
+ kwargs_plot = {} if kwargs_plot is None else kwargs_plot
420
+ kwargs_legend = {} if kwargs_legend is None else kwargs_legend
421
+
422
+ plot_schemes = [scheme for scheme in self._schemes
423
+ if only_param_tag is None or scheme.tag in only_param_tag]
424
+
425
+ #line style and color
426
+ colors = COLORS if colors is None else colors
427
+ styles = STYLES if styles is None else styles
428
+ def get_color(var, scheme):
429
+ "returns the color associated to var or scheme"
430
+ if switch_cls:
431
+ #color by parameterization
432
+ if len(colors) >= len(self._schemes):
433
+ #We keep the same association between
434
+ #schemes and colors even if all schemes are not plotted
435
+ return {k:colors[i] for i, k in enumerate([sch.tag
436
+ for sch in self._schemes])}[scheme]
437
+ else:
438
+ return {k:colors[i] for i, k in enumerate([sch.tag
439
+ for sch in plot_schemes])}[scheme]
440
+ else:
441
+ #color by variable
442
+ return {k:colors[i] for i, k in enumerate(var_names)}[var]
443
+ def get_ls(var, scheme):
444
+ "returns the line style associated to var or scheme"
445
+ if switch_cls:
446
+ #line style by variable
447
+ return {k:styles[i] for i, k in enumerate(var_names)}[var]
448
+ else:
449
+ #line style by parameterization
450
+ if len(styles) >= len(self._schemes):
451
+ #We keep the same association between
452
+ #schemes and styles even if all schemes are not plotted
453
+ return {k:styles[i] for i, k in enumerate([sch.tag
454
+ for sch in self._schemes])}[scheme]
455
+ else:
456
+ return {k:styles[i] for i, k in enumerate([sch.tag
457
+ for sch in plot_schemes])}[scheme]
458
+
459
+ #Plotting output
460
+ _, _, series = self.get_series(var_names, slicing, mfactor, only_param_tag, common_times=0)
461
+ if y_var is not None:
462
+ _, _, series_y = self.get_series([y_var], slicing, 1., only_param_tag, common_times=0)
463
+ if title is not None:
464
+ ax.set_title(title)
465
+ result = []
466
+ handles_var = {}
467
+ handles_scheme = {}
468
+ plot_nb = 0
469
+ for var in var_names:
470
+ for scheme in plot_schemes:
471
+ plot_nb += 1
472
+ time, serie = series[(var, scheme.tag)]
473
+ shape = serie.shape
474
+ dim = sum([1 if l > 1 else 0 for l in shape])
475
+ if dim not in [1, 2]:
476
+ raise NotImplementedError("We only plot 0D and 1D variables, please " +
477
+ "use the slicing argument to reduce the problem size")
478
+
479
+ label = self._var_legend(var, mfactor) + " - " + scheme.name
480
+ if len(var_names) == len(plot_schemes) == 1:
481
+ if title is None:
482
+ ax.set_title(self._var_legend(var, mfactor) + " - " + scheme.name)
483
+ if legend == 'min':
484
+ legend = 'no'
485
+ elif len(var_names) == 1:
486
+ if title is None:
487
+ ax.set_title(self._var_legend(var, mfactor))
488
+ if legend != 'full':
489
+ label = scheme.name
490
+ elif len(plot_schemes) == 1:
491
+ if title is None:
492
+ ax.set_title(scheme.name)
493
+ if legend != 'full':
494
+ label = self._var_legend(var, mfactor)
495
+
496
+ if dim == 1:
497
+ ls_is_str = isinstance(get_ls(var, scheme.tag), str)
498
+ if linewidth is not None:
499
+ kwargs_plot['linewidth'] = linewidth
500
+ line, = ax.plot(time, serie.squeeze(),
501
+ color=get_color(var, scheme.tag),
502
+ linestyle=get_ls(var, scheme.tag) if ls_is_str else '',
503
+ label=label,
504
+ **kwargs_plot)
505
+ if not ls_is_str:
506
+ line.set_dashes(get_ls(var, scheme.tag))
507
+ handles_var[var] = line
508
+ handles_scheme[scheme.name] = line
509
+ result.append(line)
510
+ else:
511
+ X = numpy.repeat(time, serie.squeeze().shape[-1]).reshape(serie.squeeze().shape)
512
+ if y_var is None:
513
+ Y = numpy.mgrid[0:len(time), 0:serie.squeeze().shape[-1]][1]
514
+ else:
515
+ _, Y = series_y[(y_var, scheme.tag)]
516
+ Y = Y.squeeze()
517
+ if len(plot_schemes) == 1 and len(var_names) == 1 and enable_contourf:
518
+ qcs = ax.contourf(X, Y, serie.squeeze(), levels=contourlevels,
519
+ **kwargs_contourf)
520
+ if clabel:
521
+ qcs.clabel(inline=1, fontsize=10, colors='black')
522
+ if colorbar:
523
+ ax.figure.colorbar(qcs, ax=ax)
524
+ else:
525
+ ls_is_str = isinstance(get_ls(var, scheme.tag), str)
526
+ if linewidth is not None:
527
+ kwargs_contour['linewidths'] = linewidth
528
+ qcs = ax.contour(X, Y, serie.squeeze(),
529
+ colors=get_color(var, scheme.tag),
530
+ linestyles=get_ls(var, scheme.tag) if ls_is_str else None,
531
+ levels=contourlevels,
532
+ **kwargs_contour)
533
+ lc0 = qcs.collections[0]
534
+ lc0.set_label(label)
535
+ handles_var[var] = lc0
536
+ handles_scheme[scheme.name] = lc0
537
+ if clabel:
538
+ qcs.clabel(inline=1, fontsize=10)
539
+ if not ls_is_str:
540
+ for line in qcs.collections:
541
+ line.set_dashes([(0., get_ls(var, scheme.tag))])
542
+ result.append(qcs)
543
+ #Legend
544
+ if plot_nb > 0 and legend != 'no':
545
+ if len(var_names) == 1 or len(plot_schemes) == 1:
546
+ if not (dim == 2 and len(plot_schemes) == len(var_names) == 1 and enable_contourf):
547
+ ax.legend(**kwargs_legend)
548
+ else:
549
+ if switch_cls:
550
+ handles1 = [handles_scheme[scheme.name] for scheme in plot_schemes]
551
+ labels1 = [scheme.name for scheme in plot_schemes]
552
+ handles2 = [handles_var[var] for var in var_names]
553
+ labels2 = [self._var_legend(var, mfactor) for var in var_names]
554
+ title1 = "Schemes (colors)"
555
+ title2 = "Parameters (line styles)"
556
+ else:
557
+ handles1 = [handles_var[var] for var in var_names]
558
+ labels1 = [self._var_legend(var, mfactor) for var in var_names]
559
+ handles2 = [handles_scheme[scheme.name] for scheme in plot_schemes]
560
+ labels2 = [scheme.name for scheme in plot_schemes]
561
+ title1 = "Parameters (colors)"
562
+ title2 = "Schemes (line styles)"
563
+ legend1 = ax.legend(handles=handles1, labels=labels1,
564
+ title=title1, loc=1, **kwargs_legend)
565
+ for handle in legend1.legend_handles:
566
+ handle.set_linestyle('-')
567
+ legend2 = ax.legend(handles=handles2, labels=labels2,
568
+ title=title2, loc=2, **kwargs_legend)
569
+ for handle in legend2.legend_handles:
570
+ handle.set_color('black')
571
+ ax.add_artist(legend1)
572
+
573
+ ax.set_xlim(xlim)
574
+ ax.set_ylim(ylim)
575
+
576
+ ax.set_xlabel("Time" if xlabel is None else xlabel)
577
+ if ylabel is not None:
578
+ ax.set_ylabel(ylabel)
579
+ elif plot_nb > 0 and dim != 1:
580
+ ax.set_ylabel("Index" if y_var is None else y_var)
581
+
582
+ return result
583
+
584
+ def plot_comp(self, ax, var_names, slicing=None, y_var=None,
585
+ mfactor=1., only_param_tag=None, enable_contourf=True,
586
+ contourlevels=None, title=None, x_var=None, only_times=None,
587
+ switch_cls=False, linewidth=None,
588
+ xlabel=None, ylabel=None, remove_ma=False, colorbar=True,
589
+ clabel=False, xlim=None, ylim=None,
590
+ legend='min', colors=None, styles=None,
591
+ kwargs_contourf=None, kwargs_contour=None, kwargs_plot=None,
592
+ kwargs_legend=None):
593
+ """
594
+ This method plots a scheme comparison (x-axis is for scheme) of different variables
595
+ for different times.
596
+
597
+ If data is 0D, each couple variable/scheme is represented by a line.
598
+
599
+ If data is 1D, each couple variable/scheme is represented by contour lines.
600
+
601
+ :param ax: matplolib axis to use for plotting
602
+ :param var_names: list of variables to plot
603
+ :param slicing: (optional) slicing to apply to the data series.
604
+ This can be used to run a parameterization on a
605
+ multidimensional initial state and extract result
606
+ for only one dimension.
607
+ :param y_var: (optional) If data is 1D, use this variable on the y-axis
608
+ :param mfactor: (optional) multiplying factor to apply to data
609
+ :param only_param_tag: (optional) list of parameterization tags to actually consider.
610
+ By default (None), all parameterizations are used.
611
+ :param enable_contourf: (optional) If only one 1D variable is plotted, enables
612
+ use of contourf instead of contour
613
+ :param contourlevels: (optional) List of values to use with contour and contourf
614
+ :param title: (optional) Title to use on subplot, if None title is automatic
615
+ :param x_var: (optional) By default, the schemes are evenly spaced on the x-axis.
616
+ If this variable is set, it must be a list of position to use
617
+ (specified in the same order as schemes).
618
+ :param only_times: (optional) By default, all output times are plotted.
619
+ If set, plotting is limited to times listed.
620
+ :param switch_cls: (optional, default to False) By default, A line style is assigned
621
+ to each time and a color is assign to each variable.
622
+ Setting this variable to True, reverse this default behavior.
623
+ :param linewidth: Control the line width used in plots
624
+ :param xlabel: To impose an x-axis label (None to generate it automatically)
625
+ :param ylabel: To impose an y-axis label (None to generate it automatically)
626
+ :param remove_ma: If True, masked data are removed before plotting. This enable
627
+ a smoother plot but it is then not guaranteed that all
628
+ superimposed plots are plotted for the same values (if one scheme
629
+ does not provide output for a given time whereas other schemes do)
630
+ :param colorbar: In case of contourf use, adds a colorbar
631
+ :param clabel: To add labels to line contours
632
+ :param xlim: tuple (xmin, xmax) or None
633
+ :param ylim: tuple (ymin, ymax) or None
634
+ :param legend: 'min' to label curves with the minimum needed to distinguish them
635
+ 'full' to label curves with the entire information
636
+ 'no' to not plot legend
637
+ :param colors: list of color names to use, None to use the default list
638
+ :param styles: list of line styles to use, None to use the default list
639
+ a line style is a string (recognized by matplotlib) or a
640
+ even length tuple of on and off ink in points
641
+ :param kwargs_contourf, kwargs_contour and kwargs_plot: optional dictionaries of
642
+ arguments to be passed to the contourf, contour and plot methods
643
+ :param kwargs_legend: optional dictionaries of arguments to be passed to ax.legend
644
+
645
+ :returns: The method returns the output of the plotting function (plot, contour or contourf)
646
+ """
647
+
648
+ assert isinstance(only_param_tag, list) or only_param_tag is None, \
649
+ "only_param_tag must be a list or None"
650
+ if not only_param_tag is None:
651
+ assert all([isinstance(item, str) for item in only_param_tag]), \
652
+ "if only_param_tag is a list, each item must be a string"
653
+ assert isinstance(only_times, list) or only_times is None, \
654
+ "only_times must be a list or None"
655
+ if not only_times is None:
656
+ assert all([isinstance(item, float) for item in only_times]), \
657
+ "if only_times is a list, each item must be a float"
658
+ assert isinstance(var_names, list), "var_names must be a list"
659
+ assert all([isinstance(item, str) for item in var_names]), \
660
+ "var_names must be a list of variable names"
661
+ assert x_var is None or isinstance(x_var, list) or isinstance(x_var, numpy.ndarray), \
662
+ "x_var must be None, a list or a numpy array"
663
+ assert legend in ['min', 'full', 'no'], "legend must be 'min', 'full' or 'no'"
664
+
665
+ kwargs_contourf = {} if kwargs_contourf is None else kwargs_contourf
666
+ kwargs_contour = {} if kwargs_contour is None else kwargs_contour
667
+ kwargs_plot = {} if kwargs_plot is None else kwargs_plot
668
+ kwargs_legend = {} if kwargs_legend is None else kwargs_legend
669
+
670
+ plot_schemes = [scheme for scheme in self._schemes
671
+ if only_param_tag is None or scheme.tag in only_param_tag]
672
+ if x_var is not None:
673
+ assert len(x_var) == len(plot_schemes), \
674
+ "x_var must be None or must have the same " + \
675
+ "length as the number of schemes to plot"
676
+ else:
677
+ x_var = range(len(plot_schemes))
678
+
679
+ #Time and values
680
+ times = self.common_times(only_param_tag, None, common_times=1) #All possible times
681
+ plot_times = self.common_times(only_param_tag,
682
+ only_times,
683
+ common_times=1) #Only selected times
684
+ _, _, series = self.get_series(var_names, slicing, mfactor, only_param_tag, common_times=1)
685
+ if y_var is not None:
686
+ _, _, series_y = self.get_series([y_var], slicing, 1., only_param_tag, common_times=1)
687
+
688
+ #line style and color
689
+ colors = COLORS if colors is None else colors
690
+ styles = STYLES if styles is None else styles
691
+ def get_color(var, time):
692
+ "returns the color associated to var or time"
693
+ if switch_cls:
694
+ #color by time
695
+ if len(colors) >= len(times):
696
+ #We keep the same association between
697
+ #times and colors even if all times are not plotted
698
+ return {k:colors[i] for i, k in enumerate(times)}[time]
699
+ else:
700
+ return {k:colors[i] for i, k in enumerate(plot_times)}[time]
701
+ else:
702
+ #color by variable
703
+ return {k:colors[i] for i, k in enumerate(var_names)}[var]
704
+ def get_ls(var, time):
705
+ "returns the line style associated to var or time"
706
+ if switch_cls:
707
+ #line style by variable
708
+ return {k:styles[i] for i, k in enumerate(var_names)}[var]
709
+ else:
710
+ #line style by time
711
+ if len(styles) >= len(times):
712
+ #We keep the same association between
713
+ #times and styles even if all times are not plotted
714
+ return {k:styles[i] for i, k in enumerate(times)}[time]
715
+ else:
716
+ return {k:styles[i] for i, k in enumerate(plot_times)}[time]
717
+
718
+ #Plotting output
719
+ if title is not None:
720
+ ax.set_title(title)
721
+ result = []
722
+ handles_var = {}
723
+ handles_time = {}
724
+ plot_nb = 0
725
+ for var in var_names:
726
+ for time in plot_times:
727
+ plot_nb += 1
728
+ #time serie of the first scheme for var (all time series are equal)
729
+ time_values = series[(var, plot_schemes[0].tag)][0]
730
+ index_time = numpy.nonzero(time_values==time)[0][0]
731
+ serie = numpy.ma.array([series[(var, scheme.tag)][1][index_time]
732
+ for scheme in plot_schemes]).squeeze()
733
+ if y_var is not None:
734
+ Y = numpy.ma.array([series_y[(y_var, scheme.tag)][1][index_time]
735
+ for scheme in plot_schemes])
736
+ x_var_plt = numpy.array(x_var)
737
+ if remove_ma:
738
+ mask = numpy.array([numpy.all(numpy.ma.getmaskarray(serie[i]))
739
+ for i in range(len(plot_schemes))])
740
+ mask = numpy.logical_not(mask)
741
+ serie = serie[mask]
742
+ x_var_plt = x_var_plt[mask]
743
+ if y_var is not None:
744
+ Y = Y[mask]
745
+
746
+ dim = len(serie.shape)
747
+ if dim not in [1, 2]:
748
+ raise NotImplementedError("We only plot 0D and 1D variables, please " +
749
+ "use the slicing argument to reduce the problem size")
750
+
751
+ label = self._var_legend(var, mfactor) + " - t="+ str(time)
752
+ if len(var_names) == len(plot_times) == 1:
753
+ if title is None:
754
+ ax.set_title(self._var_legend(var, mfactor) + " - t=" + str(time))
755
+ if legend == 'min':
756
+ legend = 'no'
757
+ elif len(var_names) == 1:
758
+ if title is None:
759
+ ax.set_title(self._var_legend(var, mfactor))
760
+ if legend != 'full':
761
+ label = "t=" + str(time)
762
+ elif len(plot_times) == 1:
763
+ if title is None:
764
+ ax.set_title("t=" + str(time))
765
+ if legend != 'full':
766
+ label = self._var_legend(var, mfactor)
767
+
768
+ if dim == 1:
769
+ ls_is_str = isinstance(get_ls(var, time), str)
770
+ if linewidth is not None:
771
+ kwargs_plot['linewidth'] = linewidth
772
+ line, = ax.plot(x_var_plt, serie,
773
+ color=get_color(var, time),
774
+ linestyle=get_ls(var, time) if ls_is_str else '',
775
+ label=label,
776
+ **kwargs_plot)
777
+ if not ls_is_str:
778
+ line.set_dashes(get_ls(var, time))
779
+ handles_var[var] = line
780
+ handles_time[time] = line
781
+ result.append(line)
782
+ else:
783
+ X = numpy.repeat(x_var_plt, serie.shape[-1]).reshape(serie.shape)
784
+ if y_var is None:
785
+ Y = numpy.mgrid[0:len(x_var_plt), 0:serie.shape[-1]][1]
786
+ else:
787
+ Y = Y.squeeze()
788
+ if len(plot_times) == 1 and len(var_names) == 1 and enable_contourf:
789
+ qcs = ax.contourf(X, Y, serie, levels=contourlevels,
790
+ **kwargs_contourf)
791
+ if clabel:
792
+ qcs.clabel(inline=1, fontsize=10, colors='black')
793
+ if colorbar:
794
+ ax.figure.colorbar(qcs, ax=ax)
795
+ else:
796
+ ls_is_str = isinstance(get_ls(var, time), str)
797
+ if linewidth is not None:
798
+ kwargs_contour['linewidths'] = linewidth
799
+ qcs = ax.contour(X, Y, serie,
800
+ colors=get_color(var, time),
801
+ linestyles=get_ls(var, time) if ls_is_str else None,
802
+ levels=contourlevels,
803
+ **kwargs_contour)
804
+ lc0 = qcs.collections[0]
805
+ lc0.set_label(label)
806
+ handles_var[var] = lc0
807
+ handles_time[time] = lc0
808
+ if clabel:
809
+ qcs.clabel(inline=1, fontsize=10)
810
+ if not ls_is_str:
811
+ for line in qcs.collections:
812
+ line.set_dashes([(0., get_ls(var, time))])
813
+ result.append(qcs)
814
+ #Legend
815
+ if plot_nb > 0 and legend != 'no':
816
+ if len(var_names) == 1 or len(plot_times) == 1:
817
+ if not (dim == 2 and len(plot_times) == len(var_names) == 1 and enable_contourf):
818
+ ax.legend(**kwargs_legend)
819
+ else:
820
+ if switch_cls:
821
+ handles1 = [handles_time[time] for time in plot_times]
822
+ labels1 = ["t=" + str(time) for time in plot_times]
823
+ handles2 = [handles_var[var] for var in var_names]
824
+ labels2 = [self._var_legend(var, mfactor) for var in var_names]
825
+ title1 = "Times (colors)"
826
+ title2 = "Parameters (line styles)"
827
+ else:
828
+ handles1 = [handles_var[var] for var in var_names]
829
+ labels1 = [self._var_legend(var, mfactor) for var in var_names]
830
+ handles2 = [handles_time[time] for time in plot_times]
831
+ labels2 = ["t=" + str(time) for time in plot_times]
832
+ title1 = "Parameters (colors)"
833
+ title2 = "Times (line styles)"
834
+ legend1 = ax.legend(handles=handles1, labels=labels1,
835
+ title=title1, loc=1, **kwargs_legend)
836
+ for handle in legend1.legend_handles:
837
+ handle.set_linestyle('-')
838
+ legend2 = ax.legend(handles=handles2, labels=labels2,
839
+ title=title2, loc=2, **kwargs_legend)
840
+ for handle in legend2.legend_handles:
841
+ handle.set_color('black')
842
+ ax.add_artist(legend1)
843
+
844
+ ax.set_xlim(xlim)
845
+ ax.set_ylim(ylim)
846
+
847
+ ax.set_xlabel("Scheme" if xlabel is None else xlabel)
848
+ if ylabel is not None:
849
+ ax.set_ylabel(ylabel)
850
+ elif plot_nb > 0 and dim != 1:
851
+ ax.set_ylabel("Index" if y_var is None else y_var)
852
+
853
+ return result
854
+
855
+ def close_files(self):
856
+ """
857
+ This methods close the different hdf5 opened files
858
+ """
859
+ for hdf5_file in list(self._files.keys()):
860
+ self._files[hdf5_file].close()
861
+ del self._files[hdf5_file]
862
+
863
+ def __del__(self):
864
+ try:
865
+ self.close_files()
866
+ except:
867
+ pass