-
Notifications
You must be signed in to change notification settings - Fork 50
/
plotter.py
424 lines (350 loc) · 15.1 KB
/
plotter.py
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
"""
This file is part of the openPMD-viewer.
It defines a set of methods which are useful for plotting
(and labeling the plots).
Copyright 2015-2016, openPMD-viewer contributors
Author: Remi Lehe
License: 3-Clause-BSD-LBNL
"""
import numpy as np
import math
try:
import warnings
import matplotlib
import matplotlib.pyplot as plt
matplotlib_installed = True
except ImportError:
matplotlib_installed = False
from .numba_wrapper import numba_installed
if numba_installed:
from .utilities import histogram_cic_1d, histogram_cic_2d
# Redefine the default matplotlib formatter for ticks
if matplotlib_installed:
from matplotlib.ticker import ScalarFormatter
class PowerOfThreeFormatter( ScalarFormatter ):
"""
Formatter for matplotlib's axes ticks,
that prints numbers as e.g. 1.5e3, 3.2e6, 0.2e-9,
where the exponent is always a multiple of 3.
This helps a human reader to quickly identify the closest units
(e.g. nanometer) of the plotted quantity.
This class derives from `ScalarFormatter`, which
provides a nice `offset` feature.
"""
def __init__( self, *args, **kwargs ):
ScalarFormatter.__init__( self, *args, **kwargs )
# Do not print the order of magnitude on the side of the axis
self.set_scientific(False)
# Reduce the threshold for printing an offset on side of the axis
self._offset_threshold = 2
def __call__(self, x, pos=None):
"""
Function called for each tick of an axis (for matplotlib>=3.1)
Returns the string that appears in the plot.
"""
return self.pprint_val( x, pos )
def pprint_val( self, x, pos=None):
"""
Function called for each tick of an axis (for matplotlib<3.1)
Returns the string that appears in the plot.
"""
# Calculate the exponent (power of 3)
xp = (x - self.offset)
if xp != 0:
exponent = int(3 * math.floor( math.log10(abs(xp)) / 3 ))
else:
exponent = 0
# Show 3 digits at most after decimal point
mantissa = round( xp * 10**(-exponent), 3)
# After rounding the exponent might change (e.g. 0.999 -> 1.)
if mantissa != 0 and math.log10(abs(mantissa)) == 3:
exponent += 3
mantissa /= 1000
string = "{:.3f}".format( mantissa )
if '.' in string:
# Remove trailing zeros and ., for integer mantissa
string = string.rstrip('0')
string = string.rstrip('.')
if exponent != 0:
string += "e{:d}".format( exponent )
return string
tick_formatter = PowerOfThreeFormatter()
class Plotter(object):
"""
Class which is used for plotting particles and fields
(and labeling the plots)
"""
def __init__(self, t, iterations):
"""
Initialize the object
Parameters
----------
t: 1darray of floats (seconds)
Time for each available iteration of the timeseries
iterations: 1darray of ints
Iteration number for each available iteration of the timeseries
"""
# Default fontsize
self.fontsize = 12
# Register the time array and iterations array
# (Useful when labeling the figures)
self.t = t
self.iterations = iterations
def hist1d(self, q1, w, quantity1, species, current_i, nbins, hist_range,
cmap='Blues', vmin=None, vmax=None, deposition='cic', **kw):
"""
Plot a 1D histogram of the particle quantity q1
Sets the proper labels
Parameters
----------
q1: 1darray of floats
An array with one element per macroparticle, representing
the quantity to be plotted.
w: 1darray of floats
An array with one element per macroparticle, representing
the number of real particles that correspond to each macroparticle
quantity1: string
The name of the quantity to be plotted (for labeling purposes)
species: string
The name of the species from which the data is taken
current_i: int
The index of this iteration, within the iterations list
nbins : int
Number of bins for the histograms
hist_range : list contains 2 lists of 2 floats
Extent of the histogram along each direction
deposition : string
Either `ngp` (Nearest Grid Point) or `cic` (Cloud-In-Cell)
When plotting the particle histogram, this determines how
particles affects neighboring bins.
`cic` (which is the default) leads to smoother results than `ngp`.
**kw : dict, otional
Additional options to be passed to matplotlib's bar function
"""
# Check if matplotlib is available
check_matplotlib()
# Find the iteration and time
iteration = self.iterations[current_i]
time = self.t[current_i]
# Check deposition method
if deposition == 'cic' and not numba_installed:
print_cic_unavailable()
deposition = 'ngp'
# Bin the particle data
q1 = q1.astype( np.float64 )
if deposition == 'ngp':
binned_data, _ = np.histogram(q1, nbins, hist_range[0], weights=w)
elif deposition == 'cic':
binned_data = histogram_cic_1d(
q1, w, nbins, hist_range[0][0], hist_range[0][1])
else:
raise ValueError('Unknown deposition method: %s' % deposition)
# Do the plot
bin_size = (hist_range[0][1] - hist_range[0][0]) / nbins
bin_coords = hist_range[0][0] + bin_size * ( 0.5 + np.arange(nbins) )
plt.bar( bin_coords, binned_data, width=bin_size, **kw )
plt.xlim( hist_range[0] )
plt.ylim( hist_range[1] )
plt.xlabel(quantity1, fontsize=self.fontsize)
plt.title("%s: t = %.2e s (iteration %d)"
% (species, time, iteration), fontsize=self.fontsize)
# Format the ticks
ax = plt.gca()
ax.get_xaxis().set_major_formatter( tick_formatter )
ax.get_yaxis().set_major_formatter( tick_formatter )
def hist2d(self, q1, q2, w, quantity1, quantity2, species, current_i,
nbins, hist_range, cmap='Blues', vmin=None, vmax=None,
deposition='cic', **kw):
"""
Plot a 2D histogram of the particle quantity q1
Sets the proper labels
Parameters
----------
q1: 1darray of floats
An array with one element per macroparticle, representing
the quantity to be plotted.
w: 1darray of floats
An array with one element per macroparticle, representing
the number of real particles that correspond to each macroparticle
quantity1, quantity2: strings
The name of the quantity to be plotted (for labeling purposes)
species: string
The name of the species from which the data is taken
current_i: int
The index of this iteration, within the iterations list
nbins : list of 2 ints
Number of bins along each direction, for the histograms
hist_range : list contains 2 lists of 2 floats
Extent of the histogram along each direction
deposition : string
Either `ngp` (Nearest Grid Point) or `cic` (Cloud-In-Cell)
When plotting the particle histogram, this determines how
particles affects neighboring bins.
`cic` (which is the default) leads to smoother results than `ngp`.
**kw : dict, otional
Additional options to be passed to matplotlib's imshow function
"""
# Check if matplotlib is available
check_matplotlib()
# Find the iteration and time
iteration = self.iterations[current_i]
time = self.t[current_i]
# Check deposition method
if deposition == 'cic' and not numba_installed:
print_cic_unavailable()
deposition = 'ngp'
# Bin the particle data
q1 = q1.astype( np.float64 )
q2 = q2.astype( np.float64 )
if deposition == 'ngp':
binned_data, _, _ = np.histogram2d(
q1, q2, nbins, hist_range, weights=w)
elif deposition == 'cic':
binned_data = histogram_cic_2d( q1, q2, w,
nbins[0], hist_range[0][0], hist_range[0][1],
nbins[1], hist_range[1][0], hist_range[1][1] )
else:
raise ValueError('Unknown deposition method: %s' % deposition)
# Do the plot
plt.imshow( binned_data.T, extent=hist_range[0] + hist_range[1],
origin='lower', interpolation='nearest', aspect='auto',
cmap=cmap, vmin=vmin, vmax=vmax, **kw )
plt.colorbar()
plt.xlabel(quantity1, fontsize=self.fontsize)
plt.ylabel(quantity2, fontsize=self.fontsize)
plt.title("%s: t = %.2e s (iteration %d)"
% (species, time, iteration), fontsize=self.fontsize)
# Format the ticks
ax = plt.gca()
ax.get_xaxis().set_major_formatter( tick_formatter )
ax.get_yaxis().set_major_formatter( tick_formatter )
def show_field_1d( self, F, info, field_label, current_i, plot_range,
vmin=None, vmax=None, **kw ):
"""
Plot the given field in 1D
Parameters
----------
F: 1darray of floats
Contains the field to be plotted
info: a FieldMetaInformation object
Contains the information about the plotted field
field_label: string
The name of the field plotted (for labeling purposes)
vmin, vmax: floats or None
The amplitude of the field
plot_range : list of lists
Indicates the values between which to clip the plot,
along the 1st axis (first list) and 2nd axis (second list)
"""
# Check if matplotlib is available
check_matplotlib()
# Find the iteration and time
iteration = self.iterations[current_i]
time = self.t[current_i]
# Get the x axis
xaxis = getattr( info, info.axes[0] )
# Plot the data
if np.issubdtype(F.dtype, np.complexfloating):
plot_data = abs(F) # For complex numbers, plot the absolute value
title = "|%s|" %field_label
else:
plot_data = F
title = "%s" %field_label
# Get the title and labels
title += " at %.2e s (iteration %d)" % (time, iteration)
plt.title(title, fontsize=self.fontsize)
# Add the name of the axes
plt.xlabel('$%s \;(m)$' % info.axes[0], fontsize=self.fontsize)
plt.plot( xaxis, plot_data )
# Get the limits of the plot
# - Along the first dimension
if (plot_range[0][0] is not None) and (plot_range[0][1] is not None):
plt.xlim( plot_range[0][0], plot_range[0][1] )
else:
plt.xlim( xaxis.min(), xaxis.max() ) # Full extent of the box
# - Along the second dimension
if (plot_range[1][0] is not None) and (plot_range[1][1] is not None):
plt.ylim( plot_range[1][0], plot_range[1][1] )
# Format the ticks
ax = plt.gca()
ax.get_xaxis().set_major_formatter( tick_formatter )
ax.get_yaxis().set_major_formatter( tick_formatter )
def show_field_2d(self, F, info, slice_across, m, field_label, geometry,
current_i, plot_range, **kw):
"""
Plot the given field in 2D
Parameters
----------
F: 2darray of floats
Contains the field to be plotted
info: a FieldMetaInformation object
Contains the information about the plotted field
slice_across : str, optional
Only used for 3dcartesian geometry
The direction across which the data is sliced
m: int
Only used for thetaMode geometry
The azimuthal mode used when plotting the fields
field_label: string
The name of the field plotted (for labeling purposes)
geometry: string
Either "2dcartesian", "3dcartesian" or "thetaMode"
plot_range : list of lists
Indicates the values between which to clip the plot,
along the 1st axis (first list) and 2nd axis (second list)
"""
# Check if matplotlib is available
check_matplotlib()
# Find the iteration and time
iteration = self.iterations[current_i]
time = self.t[current_i]
# Plot the data
if np.issubdtype(F.dtype, np.complexfloating):
plot_data = abs(F)
title = "|%s|" %field_label
else:
plot_data = F
title = "%s" %field_label
plt.imshow(plot_data, extent=info.imshow_extent, origin='lower',
interpolation='nearest', aspect='auto', **kw)
plt.colorbar()
# Get the title and labels
# Cylindrical geometry
if geometry == "thetaMode":
mode = str(m)
title += " in the mode %s at %.2e s (iteration %d)" \
% (mode, time, iteration)
# 2D Cartesian geometry
else:
title += " at %.2e s (iteration %d)" % (time, iteration)
plt.title(title, fontsize=self.fontsize)
# Add the name of the axes
plt.xlabel('$%s \;(m)$' % info.axes[1], fontsize=self.fontsize)
plt.ylabel('$%s \;(m)$' % info.axes[0], fontsize=self.fontsize)
# Get the limits of the plot
# - Along the first dimension
if (plot_range[0][0] is not None) and (plot_range[0][1] is not None):
plt.xlim( plot_range[0][0], plot_range[0][1] )
# - Along the second dimension
if (plot_range[1][0] is not None) and (plot_range[1][1] is not None):
plt.ylim( plot_range[1][0], plot_range[1][1] )
# Format the ticks
ax = plt.gca()
ax.get_xaxis().set_major_formatter( tick_formatter )
ax.get_yaxis().set_major_formatter( tick_formatter )
def print_cic_unavailable():
warnings.warn(
"\nCIC particle histogramming is unavailable because \n"
"Numba is not installed. NGP histogramming is used instead.\n"
"Please considering installing numba (e.g. `pip install numba`)")
def check_matplotlib():
"""Raise error messages or warnings when potential issues when
potenial issues with matplotlib are detected."""
if not matplotlib_installed:
raise RuntimeError( "Failed to import the openPMD-viewer plotter.\n"
"(Make sure that matplotlib is installed.)")
elif ('MacOSX' in matplotlib.get_backend()):
warnings.warn("\n\nIt seems that you are using the matplotlib MacOSX "
"backend. \n(This typically obtained when typing `%matplotlib`.)\n"
"With recent version of Jupyter, the plots might not appear.\nIn this "
"case, switch to `%matplotlib notebook` and restart the notebook.")