-
Notifications
You must be signed in to change notification settings - Fork 1
/
powerlawstudy-old.py
511 lines (407 loc) · 16.4 KB
/
powerlawstudy-old.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
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
import sys
sys.path.insert(0, 'lib')
sys.path.insert(0, 'src/evaluation')
from node.special import *
from node.node import *
from node.cache import PickleNodeCache
from pybatch.special.kruells import *
import proplot as pplt
import logging
import numpy as np
from dataclasses import dataclass
from typing import Callable
def fa_with(**kwargs):
fig = pplt.figure()
ax = fig.subplot()
ax.format(**kwargs)
return fig, ax
class PowerlawSeries:
def __init__(self, name, batch_cls, param_name, param_values, def_params, abstract_param={}, param_callback=None, confine_x=np.inf, reeval=False, param_human_name=None):
"""
param_callback: function(dict, dict) -> dict
is called once for each datapoint.
the first dict passed is a dictionary containing the values of the parameters
that are changed in the simulation, the second dict is the dict of default
parameters and the third dict is abstract_param.
has to return a dict with simulation parameters that have to be modified for
the respective run and their values and a second dict with other parameters
that are passed on to label_fmt_fields
"""
self.name = name
self.param_name = param_name
self.param_values = param_values
self.def_params = def_params
self.abstract_param = abstract_param
self._chains = None
self._datarow_chain = None
if param_human_name is None:
self.param_human_name = self.param_name
else:
self.param_human_name = param_human_name
self.confine_x = confine_x
self.cachepath = "pickle"
self.figpath = "figures"
if param_callback is None:
# simply return the first dict
self.param_callback = lambda p, d, a: (p, a)
else:
self.param_callback = param_callback
self._acquire_datapoint_chain(batch_cls, reeval)
def _get_chains(self):
d = {}
d_spatial = {}
for param_val in self.param_values:
param_dict = {self.param_name: param_val}
new_name = "_{}={}".format(self.param_name, param_val)
modified_params, additional_params = self.param_callback(param_dict, self.def_params, self.abstract_param)
d[param_val] = self.chain.copy(
new_name,
param=self.def_params | modified_params,
label_fmt_fields=modified_params | additional_params
)
d_spatial[param_val] = self.chain_spatial.copy(
new_name,
last_parents={'points': d[param_val].search_parent('points')}
)
return d, d_spatial
@property
def chains_spatial(self):
if self._chains is None:
self._chains = self._get_chains()
return self._chains[1]
@property
def chains(self):
if self._chains is None:
self._chains = self._get_chains()
return self._chains[0]
def _get_datarow_chain(self):
label = "Powerlaw indizes for different ${}$".format(self.param_human_name)
return ScatterNode('scatter', self.chains, label=label, plot=True)
@property
def datarow_chain(self):
if self._datarow_chain is None:
self._datarow_chain = self._get_datarow_chain()
return self._datarow_chain
def _acquire_datapoint_chain(self, batch_cls, reeval):
"""
Get the evaluation chain for one data point
"""
cycle = ColorCycle(['red', 'green', 'blue', 'yellow', 'black', 'violet'])
cache = PickleNodeCache(self.cachepath, self.name)
batch = BatchNode('batch',
batch_cls = batch_cls,
cache=cache,
ignore_cache=False
)
points = PointNode('points', {'batch' : batch}, cache=cache, ignore_cache=False)
valuesp = ValuesNode('valuesp',
{'points' : points},
index=1,
cache=cache,
ignore_cache=reeval,
confinements=[(0, lambda x : np.abs(x) < self.confine_x)]
)
histogramp = HistogramNode('histop',
{'values' : valuesp},
bin_count=15,
normalize='density',
log_bins=True,
plot=False,
cache=cache,
ignore_cache=False,
style='line',
color_cycle=cycle,
label="${}={{{}}}$".format(self.param_human_name, self.param_name)
)
powerlaw = PowerlawNode(
'pl',
{'dataset' : histogramp },
plot=False,
color_cycle=cycle
)
xparam_get = CommonCallbackNode(
'xparam_get',
parents=histogramp,
callback=lambda c: c['batch_param'][self.param_name]
)
self.chain = NodeGroup('group', {'x' : xparam_get, 'y': powerlaw[1], 'dy' : powerlaw[3]})
valuesx = ValuesNode('valuesx',
index=0,
cache=cache,
ignore_cache=False,
)
histogramx = HistogramNode('histox',
{'values' : valuesx},
bin_count=40,
normalize='width',
log_bins=False,
plot=True,
cache=cache,
ignore_cache=False,
style='line',
)
self.chain_spatial = histogramx
def get_histograms(self, title=None):
fig, axs = pplt.subplots(ncols=2, share=False, tight=True)
axs[1].format(
xscale='log',
yscale='log',
xformatter=pplt.SciFormatter(),
yformatter=pplt.SciFormatter(),
title=title,
xlabel='$p/p_\\textrm{inj}$',
ylabel='particle number density'
)
axs[0].format(yscale='log', xlabel='x', ylabel='particle number density')
for _, chain in self.chains.items():
hist = chain.search_parent("histop")
pl = chain.search_parent("pl")
hist.do_plot = True
pl.do_plot = True
pl(axs[1])
hist.do_plot = False
pl.do_plot = False
for _, node_chain in self.chains_spatial.items():
node_chain(axs[0])
axs[1].legend(loc='r', ncol=1)
fig.savefig("{}/{}-histograms.pdf".format(self.figpath, self.name))
def get_series(self, title=None):
fig, ax = fa_with(
xscale='log',
xformatter=pplt.SciFormatter(),
yformatter=pplt.SciFormatter(),
title=title,
xlabel="${}$".format(self.param_human_name),
ylabel='Powerlaw index'
)
self.datarow_chain(ax)
ax.legend(ncol=1)
fig.savefig("{}/{}-series.pdf".format(self.figpath, self.name))
@dataclass
class PowerlawStudyConfig:
datarows_name: str
# one datarow is generated for each value in this list
# when calling plot_momentum_spectra, one plot is created
# for each value in this list each containing one histogram
# for each value in datapoint_values
datarow_values: list
datarow_label: str
datapoints_name: str
# the values that are passed one at a time to param_callback as second parameter
datapoint_values: list
# the label for the momentum spectra histograms.
# .format is called on this label with the values of both dicts returned by param_callback
datapoint_label: str
xlabel: str
xscale: str
datapoint_id_fmt_str: str
# the default parameters passed to each pybatch instance (see param_callback)
def_param: dict
# parameters that param_callback needs for additional calculations
add_param: dict
confine_x: float
# param_callback is passed:
# - one value of datarow_values
# - one value of datapoint_values
# - def_param
# - add_param
# It is expected to return two dicts, one passed to the pybatch instance
# as parameters (in addition to def_param) and one that is passed to label_fmt_fields
# and to other String.format calls
param_callback: Callable
# this callable is passed to the CommonCallbackNode used to generate the x-axis values
# from the common dict (which is containing the return value of param_callback as
# label_fmt_fields and batch_param resp).
xparam_callback: Callable
figpath: str = "figures"
cachepath: str = "pickle"
class PowerlawStudy:
def __init__(self, name, batch_cls, config):
self.name = name
self.datarows_name = config.datarows_name
self.datarow_values = config.datarow_values
self.datarow_label = config.datarow_label
self.datapoints_name = config.datapoints_name
self.datapoint_values = config.datapoint_values
self.datapoint_label = config.datapoint_label
self.datapoint_id_fmt_str = config.datapoint_id_fmt_str
self.xlabel = config.xlabel
self.xscale= config.xscale
self.def_param = config.def_param
self.add_param = config.add_param
self.confine_x = config.confine_x
self.param_callback = config.param_callback
self.xparam_callback = config.xparam_callback
self.figpath = config.figpath
self.cachepath = config.cachepath
self._acquire_datapoint_chain(batch_cls)
self._datapoint_chains = None
self._datarow_chains = None
self._full_chain = None
def _get_datapoint_chains(self):
dr_dict = {}
dr_dict_spatial = {}
for dr_val in self.datarow_values:
d = {}
d_spatial = {}
for dp_val in self.datapoint_values:
new_param, new_meta_param = self.param_callback(dr_val, dp_val, self.def_param, self.add_param)
new_id = ('_' + self.datapoint_id_fmt_str).format(**(new_param | new_meta_param))
d[dp_val] = self.datapoint_chain.copy(
new_id,
param = self.def_param | new_param,
label_fmt_fields = new_param | new_meta_param
)
d_spatial[dp_val] = self.datapoint_chain_spatial.copy(
new_id,
last_parents={'points': d[dp_val].search_parent('points')}
)
dr_dict[dr_val] = d
dr_dict_spatial[dr_val] = d_spatial
return dr_dict, dr_dict_spatial
def _get_datarow_chains(self):
datarow_chains = []
for dr_val, dp_chain in self.datapoint_chains.items():
n = ScatterNode('scatter_{}={}'.format(self.datarows_name, dr_val), dp_chain, label=self.datarow_label.format(dr_val), plot=True)
datarow_chains.append(n)
return datarow_chains
@property
def datapoint_chains(self):
if self._datapoint_chains is None:
self._datapoint_chains = self._get_datapoint_chains()
return self._datapoint_chains[0]
@property
def datapoint_chains_spatial(self):
if self._datapoint_chains is None:
self._datapoint_chains = self._get_datapoint_chains()
return self._datapoint_chains[1]
@property
def datarow_chains(self):
if self._datarow_chains is None:
self._datarow_chains = self._get_datarow_chains()
return self._datarow_chains
@property
def full_chain(self):
if self._full_chain is None:
self._full_chain = NodeGroup('datarows', self.datarow_chains)
return self._full_chain
def plot_momentum_spectra(self, title_fmt_str=None):
if title_fmt_str is None:
title_fmt_str = 'Power laws for different diffusion steps. ${}={}$'
for dr_val in self.datapoint_chains.keys():
fig, axs = pplt.subplots(ncols=2, share=False, tight=True)
axs[1].format(
xscale='log',
yscale='log',
xformatter=pplt.SciFormatter(),
yformatter=pplt.SciFormatter(),
title=title_fmt_str.format(self.datarows_name, dr_val),
xlabel='$p/p_\\textrm{inj}$',
ylabel='particle number density'
)
axs[0].format(yscale='log', xlabel='x', ylabel='particle number density')
for _, node_chain in self.datapoint_chains[dr_val].items():
hist = node_chain.search_parent("histop")
pl = node_chain.search_parent("pl")
hist.do_plot = True
pl.do_plot = True
pl(axs[1])
for _, node_chain in self.datapoint_chains_spatial[dr_val].items():
node_chain(axs[0])
axs[1].legend(loc='r', ncol=1)
fig.savefig("{}/{}-{}={}.pdf".format(self.figpath, self.name, self.datarows_name, dr_val))
def _acquire_datapoint_chain(self, batch_cls):
"""
Get the evaluation chain for one data point
"""
cycle = ColorCycle(['red', 'green', 'blue', 'yellow', 'black', 'violet'])
cache = PickleNodeCache(self.cachepath, self.name)
batch = BatchNode('batch',
batch_cls = batch_cls,
cache=cache,
ignore_cache=False
)
points = PointNode('points', {'batch' : batch}, cache=cache, ignore_cache=False)
valuesp = ValuesNode('valuesp',
{'points' : points},
index=1,
cache=cache,
ignore_cache=False,
confinements=[(0, lambda x : np.abs(x) < self.confine_x)]
)
histogramp = HistogramNode('histop',
{'values' : valuesp},
bin_count=15,
normalize='density',
log_bins=True,
plot=False,
cache=cache,
ignore_cache=True,
style='line',
color_cycle=cycle,
label=self.datapoint_label
)
divp = LambdaNode('divp', [histogramp],
callback = lambda h: h,#(h[0], h[1] / h[0], h[2] / h[0]),
cache=cache,
ignore_cache=False
)
powerlaw = PowerlawNode(
'pl',
{'dataset' : divp },
plot=False,
color_cycle=cycle
)
xparam_get = CommonCallbackNode(
'xparam_get',
parents=histogramp,
callback=self.xparam_callback
)
self.datapoint_chain = NodeGroup('group', {'x' : xparam_get, 'y': powerlaw[1], 'dy' : powerlaw[3]})
valuesx = ValuesNode('valuesx',
index=0,
cache=cache,
ignore_cache=False,
)
histogramx = HistogramNode('histox',
{'values' : valuesx},
bin_count=15,
normalize='width',
log_bins=False,
plot=True,
cache=cache,
ignore_cache=False,
style='line',
)
self.datapoint_chain_spatial = histogramx
def plot_datarows(self, title=None, comparison=None, comparison_label=None):
if title is None:
title = "Momentum spectrum power law indices for\\\\different diffusion and advection lengths"
fig = pplt.figure(suptitle=title, tight=True)
ax = fig.subplot()
ax.format(
xlabel=self.xlabel,
ylabel='Powerlaw index $s$',
xscale=self.xscale,
#xscale=pplt.LogScale(base=2, subs=(1, 2)),
#xlocator=invdeltas,
xmargin=10
)
self.full_chain(ax)
xlim = ax.get_xlim()
ax.format(xlim=(xlim[0] * 0.8, xlim[1] * 1.2))
if not comparison is None and not comparison_label is None:
labels = []
for comp_val, s in comparison:
ax.hlines(s, *(ax.get_xlim()), c='gray', ls='dotted', lw=1)
labels.insert(0, "${}$".format(comp_val))
ox = ax.dualy(lambda x : x)
ox.format(
ylabel=comparison_label,
ylocator=np.array(comparison).T[1],
yminorlocator=[],
yticklabels=labels,
yticklabelsize='x-small'
)
ax.legend(loc='ll', ncol=1)
fig.savefig('{}/{}.pdf'.format(self.figpath, self.name))