-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelperFuncs.py
348 lines (294 loc) · 12.5 KB
/
helperFuncs.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
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors as colors
import sys, os
from scipy.interpolate import griddata
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)),'src/'))
from hist_helpers import *
def hist1D(x, range=None, bins=100, weights=None, xscale=None, yscale=None, ax=None, **kwargs):
"""
Histogram takes a value and plots a line on the given ax. If ax is none,
plt.gca() is used. All **kwargs are passed to plot() so they should be something you can send there
"""
xvalmin = x.min() if range is None else range[0]
xvalmax = x.max() if range is None else range[1]
if weights is None:
if xscale =='log' and xvalmin >0:
hist = FastHist(np.log10(x), np.log10(xvalmin), np.log10(xvalmax), int(bins))
else:
hist = FastHist(x, xvalmin, xvalmax, int(bins))
else:
#calculate unweighed histogram
if xscale =='log' and xvalmin >0:
hist = FastWeightedHist(np.log10(x), weights, np.log10(xvalmin), np.log10(xvalmax), int(bins))
else:
hist = FastWeightedHist(x, weights, xvalmin, xvalmax, int(bins))
if xscale =='log' and xvalmin >0:
binEdges = np.logspace(np.log10(xvalmin), np.log10(xvalmax), int(bins)+1)
else:
binEdges = np.linspace(xvalmin, xvalmax, int(bins)+1)
if ax is None:
ax = plt.gca()
if xscale=='log':
ax.set_xscale('log')
if yscale=='log':
ax.set_yscale('log')
# return an Line2D object like matplotlib.plot does
return ax.plot(*stepifyHist(binEdges, hist), **kwargs)
def hist2D(x, y, xrange=None, yrange=None, bins=[200,200],
weights=None, cnorm = 'log', normhist=True, colorbar=True, clabel='',
ax=None, cax = None, mask_empty=True, aspect='auto', origin='lower', **kwargs):
"""
Histogram takes a value and plots a line on the given ax. If ax is none,
plt.gca() is used. All **kwargs are passed to plot() so they should be something you can send there
"""
xvalmin = x.min() if xrange is None else xrange[0]
xvalmax = x.max() if xrange is None else xrange[1]
yvalmin = y.min() if yrange is None else yrange[0]
yvalmax = y.max() if yrange is None else yrange[1]
if weights is None:
hist = Fast2DHist(y, x, yvalmin, yvalmax, int(bins[1]), xvalmin, xvalmax, int(bins[0]))
else:
#calculate unweighed histogram
hist = Fast2DWeightedHist(y, x, weights, yvalmin, yvalmax, int(bins[1]), xvalmin, xvalmax, int(bins[0]))
####
#
# Now we have the histogram, we need to turn it into an image
#
###
if normhist == True and hist.max() != 0:
hist *= hist.max()**-1
if mask_empty == True:
hist[hist==0] = np.nan
if ax is None:
ax = plt.gca()
im = ax.imshow(hist, extent = (xvalmin, xvalmax, yvalmin, yvalmax), origin=origin,
aspect=aspect, **kwargs)
if cnorm == 'log':
im.set_norm(colors.LogNorm(vmin =im.get_clim()[0], vmax=im.get_clim()[1]))
if colorbar:
if cax == None:
plt.colorbar(im, ax=ax, label = clabel)
else:
plt.colorbar(im, cax=cax, label = clabel)
return im
def avg1D(x, y, range=None, bins=100, weights=None, xscale=None, yscale=None, ax=None, **kwargs):
"""
return an array that is <y> averaged in bins of x
"""
xvalmin = x.min() if range is None else range[0]
xvalmax = x.max() if range is None else range[1]
if weights is None:
if xscale =='log' and xvalmin >0:
hist = CalcMoments(np.log10(x), y, np.log10(xvalmin), np.log10(xvalmax), int(bins))
else:
hist = CalcMoments(x, y, xvalmin, xvalmax, int(bins))
else:
#calculate unweighed histogram
if xscale =='log' and xvalmin >0:
hist = CalcWeightedMoments(np.log10(x), y, weights, np.log10(xvalmin), np.log10(xvalmax), int(bins))
else:
hist = CalcWeightedMoments(x, y, weights, xvalmin, xvalmax, int(bins))
if xscale =='log' and xvalmin >0:
binEdges = np.logspace(np.log10(xvalmin), np.log10(xvalmax), int(bins)+1)
else:
binEdges = np.linspace(xvalmin, xvalmax, int(bins)+1)
if ax is None:
ax = plt.gca()
if xscale=='log':
ax.set_xscale('log')
if yscale=='log':
ax.set_yscale('log')
# return an Line2D object like matplotlib.plot does
return ax.plot(*stepifyMoment(binEdges, hist), **kwargs)
def avg2D(x, y, z, xrange=None, yrange=None, bins=[200,200],
weights=None, cnorm = '', colorbar=True, clabel='',
ax=None, cax = None, aspect='auto', origin='lower',
interpolate=False, **kwargs):
"""
Take the z average in x & y bin
"""
xvalmin = x.min() if xrange is None else xrange[0]
xvalmax = x.max() if xrange is None else xrange[1]
yvalmin = y.min() if yrange is None else yrange[0]
yvalmax = y.max() if yrange is None else yrange[1]
if weights is None:
hist = Calc2DMoments(y, x, z, yvalmin, yvalmax, int(bins[1]), xvalmin, xvalmax, int(bins[0]))
else:
#calculate unweighed histogram
hist = Calc2DWeightedMoments(y, x, z, weights, yvalmin, yvalmax, int(bins[1]), xvalmin, xvalmax, int(bins[0]))
if interpolate:
xbins = 0.5*(
np.linspace(xvalmin, xvalmax, num =bins[0]+1)[0:-1]
+ np.linspace(xvalmin, xvalmax, num=bins[0]+1)[1:]
)
ybins = 0.5*(
np.linspace(yvalmin, yvalmax, num =bins[1]+1)[0:-1]
+ np.linspace(yvalmin, yvalmax, num=bins[1]+1)[1:]
)
xv, yv = np.meshgrid(xbins, ybins)
hist = hist.ravel()
xv = xv.ravel()
yv = yv.ravel()
bad_vals = np.isnan(hist)
hist = griddata((xv[~bad_vals], yv[~bad_vals]), hist[~bad_vals], (xv, yv))
hist = hist.reshape((bins[1], bins[0]))
####
#
# Now we have the histogram, we need to turn it into an image
#
###
if ax is None:
ax = plt.gca()
im = ax.imshow(hist, extent = (xvalmin, xvalmax, yvalmin, yvalmax), origin=origin,
aspect=aspect, **kwargs)
if cnorm == 'log':
im.set_norm(colors.LogNorm(vmin =im.get_clim()[0], vmax=im.get_clim()[1]))
if colorbar:
if cax == None:
plt.colorbar(im, ax=ax, label = clabel)
else:
plt.colorbar(im, cax=cax, label = clabel)
return im
class PowerNorm(colors.Normalize):
''' Custom color Norm: This one normalizes the negative data differently
from the positive, and extends the power norm to negative values. The main
idea is that it plots the power_norm of the data. If stretch_colors is
true, then for diverging cmaps, the entire cmap is used, otherwise it only
uses the amount so that -b and b are the same distance from the midpoint.'''
def __init__(self, gamma=1.0, vmin=None, vmax=None, clip=False, div_cmap=True, midpoint = 0.0, stretch_colors= True):
colors.Normalize.__init__(self, vmin, vmax, clip)
self.gamma = gamma
self.div_cmap = div_cmap
self.midpoint = midpoint
self.stretch_colors = stretch_colors
def __call__(self, value, clip=None):
# I'm ignoring masked values and all kinds of edge cases to make a
# simple example...
# First see if there is a sign change:
ans = PowerNormFunc(value, gamma=self.gamma, vmin=self.vmin,
vmax=self.vmax, div_cmap=self.div_cmap,
midpoint=self.midpoint, stretch_colors = self.stretch_colors)
if type(value) == np.ma.core.MaskedArray:
ans.mask = value.mask
return ans
def PowerNormFunc(data, gamma=1.0, vmin=None, vmax=None, div_cmap=True, midpoint=0.0, stretch_colors=True):
''' Helper function for the PowerNorm The main idea is that it norms data
using np.sign(data-midpoint)*np.abs(data-midpoint)**gamma. If
stretch_colors is true, then for diverging cmaps, the entire cmap is used.
If stretch colors is false then it so that -b+midpoint and b-midpoint are
the same distance from the midpoint in the color space.'''
vmin -= midpoint
vmax -= midpoint
left_clip = 0.0
right_clip = 1.0
if not stretch_colors:
if np.sign(vmin) < 0 and np.sign(vmax) > 0:
v_absmax = max(np.abs(vmin), np.abs(vmax))
left_clip = 0.5*(1 - np.abs(vmin)**gamma/np.abs(v_absmax)**gamma)
right_clip = 0.5*(1 + np.abs(vmax)**gamma/np.abs(v_absmax)**gamma)
if div_cmap == True:
if np.sign(vmin) != np.sign(vmax) and np.sign(vmin) != 0 and np.sign(vmax) != 0:
x, y = [np.sign(vmin)*np.abs(vmin)**gamma,
0,
np.sign(vmax)*np.abs(vmax)**gamma
], [left_clip, 0.5, right_clip]
elif np.sign(vmin) >= 0:
# The data must be totally positive, extrapolate from midpoint
x, y = [np.sign(vmin)*np.abs(vmin)**gamma, np.sign(vmax)*np.abs(vmax)**gamma], [0.5, right_clip]
elif np.sign(vmax) <= 0:
# The data must be totally negative
x, y = [np.sign(vmin)*np.abs(vmin)**gamma, np.sign(vmax)*np.abs(vmax)**gamma], [left_clip, 0.5]
else:
x, y = [np.sign(vmin)*np.abs(vmin)**gamma, np.sign(vmax)*np.abs(vmax)**gamma], [0, 1]
if np.abs(midpoint)<=1E-8 and np.abs(gamma-1.0)<=1E-8:
ans = np.ma.masked_array(np.interp(data, x, y))
elif np.abs(gamma-1.0)<=1E-8:
ans = np.ma.masked_array(np.interp(data-midpoint, x, y))
else:
ans = np.ma.masked_array(np.interp(np.sign(data-midpoint)*np.abs(data-midpoint)**gamma, x, y))
return ans
def tristanSpect(o, species='lec', spectType='Energy', normed = False,
restSpect=False, xLeft = -10000, xRight=+100000, ax = None, **kwargs):
c_omp = o.c_omp
istep = o.istep
xsl = o.xsl/c_omp
gamma = o.gamma
# massRatio = o.mi/o.me
momentum=np.sqrt((gamma+1)**2-1.)
spectKey = 'spec'
if species == 'lec':
spectKey += 'e'
else:
spectKey += 'p'
if restSpect:
spectKey += 'rest'
spec = np.copy(getattr(o, spectKey))
for i in range(len(xsl)):
spec[:,i] *= gamma
###############################
###### energy spectra, f=(dN/dE)/N
###############################
dgamma = np.empty(len(gamma))
delta=np.log10(gamma[-1]/gamma[0])/len(gamma)
for i in range(len(dgamma)):
dgamma[i]=gamma[i]*(10**delta-1.)
indLeft = xsl.searchsorted(xLeft)
indRight = xsl.searchsorted(xRight, side='right')
if indLeft >= indRight:
indLeft = indRight
indRight += 1
# energy distribution, f(E)=(dn/dE)/N
fE=np.empty(len(gamma))
norm = np.ones(len(xsl))
# total particles in each linear x bin
for i in range(len(norm)):
norm[i]=sum(spec[:,i])
for k in range(len(fE)):
fE[k]=sum(spec[k][indLeft:indRight])
if sum(norm[indLeft:indRight]) > 0:
if normed:
fE *= 1.0/sum(norm[indLeft:indRight])
eDist=np.copy(fE)
fE *= dgamma**(-1)
fmom=fE/(4*np.pi*momentum)/(gamma+1)
momDist=fmom*momentum**4
if ax is None:
ax = plt.gca()
ax.set_yscale('log')
ax.set_xscale('log')
if spectType != 'Energy':
return ax.plot(momentum, momDist, **kwargs)
else:
return ax.plot(gamma, eDist, **kwargs)
if __name__=='__main__':
from tristanSim import TristanSim
mySim = TristanSim('../Iseult/output')
xmin = min(mySim[0].gammae.min(),mySim[0].gammai.min())
xmax = max(mySim[0].gammae.max(),mySim[0].gammai.max())
hist1D(mySim[0].gammae-1, range=(xmin,xmax), bins=200, xscale='log', yscale='log', c='r', label ='lecs')
hist1D(mySim[0].gammai-1, range=(xmin,xmax), bins=200, xscale='log', yscale='log', c='b', label ='ions')
plt.xlabel('$\gamma-1$')
plt.ylabel('$EdN/dE$')
plt.legend()
plt.show()
hist2D(mySim[0].xi, mySim[0].gammai-1, clabel='$f_i(p)$')
plt.xlabel('$x_i$')
plt.ylabel('$KE_i$')
plt.show()
avg1D(mySim[0].xi, mySim[0].gammai-1)
plt.xlabel('$x_i$')
plt.ylabel(r'$\langle \gamma_i-1 \rangle$')
plt.show()
plt.figure()
plt.subplot(211)
avg2D(mySim[0].xe, mySim[0].ye, mySim[0].gammae-1, cnorm = 'log', bins = [100,50], clabel=r'$\langle KE \rangle$', interpolate=True)
plt.xlabel('$x_e$')
plt.ylabel('$y_e$')
plt.subplot(212)
avg2D(mySim[0].xe, mySim[0].ye, mySim[0].gammae-1, cnorm = 'log', bins = [100,50], clabel=r'$\langle KE \rangle$', interpolate=False)
plt.xlabel('$x_e$')
plt.ylabel('$y_e$')
plt.show()
tristanSpect(mySim[1])
plt.show()