Scanning Astra evaluations¶
In [1]:
Copied!
# Useful for debugging
%load_ext autoreload
%autoreload 2
# Useful for debugging
%load_ext autoreload
%autoreload 2
In [2]:
Copied!
from astra import evaluate_astra_with_distgen
import matplotlib.pyplot as plt
import numpy as np
import os
from astra import evaluate_astra_with_distgen
import matplotlib.pyplot as plt
import numpy as np
import os
In [3]:
Copied!
# Input files
ASTRA_IN = 'templates/dcgun/astra.in'
DISTGEN_IN = 'templates/dcgun/distgen.yaml'
# Input files
ASTRA_IN = 'templates/dcgun/astra.in'
DISTGEN_IN = 'templates/dcgun/distgen.yaml'
In [4]:
Copied!
# Run Astra with Distgen
# Basic settings
settings0 = {'zstop':1, 'zemit':10, 'zphase':10, 'phases':True}
settings0['distgen:n_particle'] = 2000
# Solenoid field
settings0['maxb(1)'] = 0.04
# Run Astra with Distgen
# Basic settings
settings0 = {'zstop':1, 'zemit':10, 'zphase':10, 'phases':True}
settings0['distgen:n_particle'] = 2000
# Solenoid field
settings0['maxb(1)'] = 0.04
In [5]:
Copied!
# This is the basic function we will use
outputs = evaluate_astra_with_distgen(settings0, astra_input_file=ASTRA_IN,
distgen_input_file=DISTGEN_IN)
outputs
# This is the basic function we will use
outputs = evaluate_astra_with_distgen(settings0, astra_input_file=ASTRA_IN,
distgen_input_file=DISTGEN_IN)
outputs
Out[5]:
{'error': False, 'end_mean_z': 1.0, 'end_mean_t': 4.0156e-09, 'end_mean_x': -8.1128e-11, 'end_sigma_x': 0.00034241, 'end_sigma_xp': 0.00058502, 'end_norm_emit_x': 1.7516000000000001e-07, 'end_cov_x__xp': 1.72026784e-07, 'end_mean_y': 5.7747e-09, 'end_sigma_y': 0.00034245, 'end_sigma_yp': 0.0005843, 'end_norm_emit_y': 1.7455e-07, 'end_cov_y__yp': 1.7197839e-07, 'end_mean_kinetic_energy': 499790.0, 'end_sigma_z': 0.0021695000000000004, 'end_sigma_energy': 1.7883, 'end_norm_emit_z': 0.0038791, 'end_cov_z__energy': 7.779827000000001e-05, 'end_n_particle_loss': 0, 'end_total_charge': 9.999999999999999e-11, 'end_higher_order_energy_spread': 1.7879755920478746, 'fingerprint': 'c71d39dfb11bd6db0f35e04ba612b590'}
In [6]:
Copied!
# Make a bare minimum function to evaluate
def f(b):
s = settings0.copy()
s['maxb(1)'] = b
return evaluate_astra_with_distgen(s, astra_input_file=ASTRA_IN,
distgen_input_file=DISTGEN_IN)
# Make a list to scan
blist = np.linspace(0, 0.1, 20)
f(0)
# Make a bare minimum function to evaluate
def f(b):
s = settings0.copy()
s['maxb(1)'] = b
return evaluate_astra_with_distgen(s, astra_input_file=ASTRA_IN,
distgen_input_file=DISTGEN_IN)
# Make a list to scan
blist = np.linspace(0, 0.1, 20)
f(0)
Out[6]:
{'error': False, 'end_mean_z': 1.0, 'end_mean_t': 4.0156e-09, 'end_mean_x': -3.6051e-09, 'end_sigma_x': 0.0006159000000000001, 'end_sigma_xp': 0.00065659, 'end_norm_emit_x': 1.7486999999999998e-07, 'end_cov_x__xp': 3.9120120300000003e-07, 'end_mean_y': -5.7092e-09, 'end_sigma_y': 0.00061615, 'end_sigma_yp': 0.00065711, 'end_norm_emit_y': 1.7486e-07, 'end_cov_y__yp': 3.917050395e-07, 'end_mean_kinetic_energy': 499790.0, 'end_sigma_z': 0.0021695000000000004, 'end_sigma_energy': 1.7883, 'end_norm_emit_z': 0.0038791, 'end_cov_z__energy': 7.776355800000001e-05, 'end_n_particle_loss': 0, 'end_total_charge': 9.999999999999999e-11, 'end_higher_order_energy_spread': 1.7879772256019293, 'fingerprint': 'f2d8e898c2ee41ce7a659b5a28b3ffb2'}
In [7]:
Copied!
# Get a parallel executor
# Processes. These lines are needed to get this to work on macOS
import multiprocessing as mp
mp.set_start_method("fork")
from concurrent.futures import ProcessPoolExecutor as Executor
#from concurrent.futures import ThreadPoolExecutor as Executor # DO NOT USE
#from dask.distributed import Client as Executor
executor=Executor()
# Get a parallel executor
# Processes. These lines are needed to get this to work on macOS
import multiprocessing as mp
mp.set_start_method("fork")
from concurrent.futures import ProcessPoolExecutor as Executor
#from concurrent.futures import ThreadPoolExecutor as Executor # DO NOT USE
#from dask.distributed import Client as Executor
executor=Executor()
In [8]:
Copied!
# Make a map object. Note that this does not execute immediately.
results = executor.map(f, blist)
# Make a map object. Note that this does not execute immediately.
results = executor.map(f, blist)
In [9]:
Copied!
# Actually evalute the funciton and gather the results
outputs = []
for r in results:
outputs.append(r)
# outputs.append(r.result())
# Actually evalute the funciton and gather the results
outputs = []
for r in results:
outputs.append(r)
# outputs.append(r.result())
In [10]:
Copied!
# Simple plot
plt.plot(blist, [o['end_sigma_x'] for o in outputs])
# Simple plot
plt.plot(blist, [o['end_sigma_x'] for o in outputs])
Out[10]:
[<matplotlib.lines.Line2D at 0x1471dc790>]
Better: robust evaluation, form dataset¶
In [11]:
Copied!
from tempfile import TemporaryDirectory
SCRATCH=TemporaryDirectory()
from tempfile import TemporaryDirectory
SCRATCH=TemporaryDirectory()
In [12]:
Copied!
# Make a robust evaluate
def evaluate(b):
try:
s = settings0.copy()
s['maxb(1)'] = b
output = evaluate_astra_with_distgen(s, astra_input_file=ASTRA_IN,
distgen_input_file=DISTGEN_IN, archive_path=SCRATCH.name)
# Add the input
output['maxb(1)'] = b
output['Exception'] = False
except:
output = {}
output['maxb(1)'] = b
output['Exception'] = True
return output
# Make a robust evaluate
def evaluate(b):
try:
s = settings0.copy()
s['maxb(1)'] = b
output = evaluate_astra_with_distgen(s, astra_input_file=ASTRA_IN,
distgen_input_file=DISTGEN_IN, archive_path=SCRATCH.name)
# Add the input
output['maxb(1)'] = b
output['Exception'] = False
except:
output = {}
output['maxb(1)'] = b
output['Exception'] = True
return output
In [13]:
Copied!
SCRATCH.name
SCRATCH.name
Out[13]:
'/var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn/T/tmp8jg8ann9'
In [14]:
Copied!
# This will cause a crash in Astra
evaluate(1e9)
# This will cause a crash in Astra
evaluate(1e9)
Out[14]:
{'maxb(1)': 1000000000.0, 'Exception': True}
In [15]:
Copied!
# Get a new executor
executor = Executor()
# Make a map object. Note that this does not execute immediately.
blist[-1] = 1e9 # Add something that will crash
results = executor.map(evaluate, blist)
# Actually evalute the funciton and gather the results
outputs = []
for r in results:
outputs.append(r)
# Get a new executor
executor = Executor()
# Make a map object. Note that this does not execute immediately.
blist[-1] = 1e9 # Add something that will crash
results = executor.map(evaluate, blist)
# Actually evalute the funciton and gather the results
outputs = []
for r in results:
outputs.append(r)
In [16]:
Copied!
# Archive files are here
os.listdir(SCRATCH.name)
# Archive files are here
os.listdir(SCRATCH.name)
Out[16]:
['d70364f093fe5d4a9d3acd0d0193fa91.h5', '284fa843911e69ab6122d23d79ad4806.h5', '926798389bda3d66c00620a39ddd3b8b.h5', 'c191147e9f05abf7a8ba60eb73fbddbd.h5', 'da89d10555a9b4c7b864fbb542ac497b.h5', '7b87b3f42160e0dc6a00aa7fa134604c.h5', '961dc67f12dc8c627e0a53ad04219881.h5', '1d54b0678b9ef6bfc42b0c2ff2120ae5.h5', '0a331d558139ac3a05c4cf676a8455b6.h5', '9a8e21bd4149448d46774270247b7c2b.h5', 'd6fd70f4af4f613844fd65600267f9fb.h5', '7c0baedf140c3dbeef2fc3a73c15ea7b.h5', 'dae144c959cdf177ec97e3b59f159015.h5', 'efffe3c12cb38f91092589b38d338d29.h5', 'aa14936ee8e37ca095d6d052bb7b2c04.h5', '47ce10b464da11518826a3c6d6654f42.h5', 'e6bd6c13a2d22ad150081863f1c6fb59.h5', '1c33e9ed18d9ff88f91eda547cfe96cd.h5', '3124ef1903c236f1962db3ec8dacd2fe.h5']
Pandas¶
In [17]:
Copied!
import pandas as pd
import pandas as pd
In [18]:
Copied!
# Make a DataFrame
df = pd.DataFrame(outputs)
df
# Make a DataFrame
df = pd.DataFrame(outputs)
df
Out[18]:
error | end_mean_z | end_mean_t | end_mean_x | end_sigma_x | end_sigma_xp | end_norm_emit_x | end_cov_x__xp | end_mean_y | end_sigma_y | ... | end_sigma_energy | end_norm_emit_z | end_cov_z__energy | end_n_particle_loss | end_total_charge | end_higher_order_energy_spread | fingerprint | archive | maxb(1) | Exception | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | False | 1.0 | 4.015600e-09 | -3.605100e-09 | 0.000616 | 0.000657 | 1.748700e-07 | 3.912012e-07 | -5.709200e-09 | 0.000616 | ... | 1.7883 | 0.003879 | 0.000078 | 0.0 | 1.000000e-10 | 1.787977 | d70364f093fe5d4a9d3acd0d0193fa91 | /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn... | 0.000000e+00 | False |
1 | False | 1.0 | 4.015600e-09 | -2.727100e-09 | 0.000608 | 0.000647 | 1.749400e-07 | 3.799807e-07 | -5.900700e-09 | 0.000608 | ... | 1.7883 | 0.003879 | 0.000078 | 0.0 | 1.000000e-10 | 1.787977 | 47ce10b464da11518826a3c6d6654f42 | /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn... | 5.263158e-03 | False |
2 | False | 1.0 | 4.015600e-09 | -1.654600e-09 | 0.000584 | 0.000621 | 1.750100e-07 | 3.477970e-07 | -5.564700e-09 | 0.000584 | ... | 1.7883 | 0.003879 | 0.000078 | 0.0 | 1.000000e-10 | 1.787977 | 284fa843911e69ab6122d23d79ad4806 | /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn... | 1.052632e-02 | False |
3 | False | 1.0 | 4.015600e-09 | -5.596500e-10 | 0.000546 | 0.000581 | 1.750700e-07 | 2.999282e-07 | -4.652900e-09 | 0.000545 | ... | 1.7883 | 0.003879 | 0.000078 | 0.0 | 1.000000e-10 | 1.787977 | d6fd70f4af4f613844fd65600267f9fb | /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn... | 1.578947e-02 | False |
4 | False | 1.0 | 4.015600e-09 | 3.749800e-10 | 0.000496 | 0.000535 | 1.751200e-07 | 2.448360e-07 | -3.170800e-09 | 0.000495 | ... | 1.7883 | 0.003879 | 0.000078 | 0.0 | 1.000000e-10 | 1.787976 | da89d10555a9b4c7b864fbb542ac497b | /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn... | 2.105263e-02 | False |
5 | False | 1.0 | 4.015600e-09 | 9.793000e-10 | 0.000439 | 0.000499 | 1.751500e-07 | 1.935768e-07 | -1.156600e-09 | 0.000439 | ... | 1.7883 | 0.003879 | 0.000078 | 0.0 | 1.000000e-10 | 1.787976 | 7c0baedf140c3dbeef2fc3a73c15ea7b | /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn... | 2.631579e-02 | False |
6 | False | 1.0 | 4.015600e-09 | 1.098900e-09 | 0.000384 | 0.000492 | 1.751700e-07 | 1.589409e-07 | 1.296300e-09 | 0.000384 | ... | 1.7883 | 0.003879 | 0.000078 | 0.0 | 1.000000e-10 | 1.787976 | 7b87b3f42160e0dc6a00aa7fa134604c | /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn... | 3.157895e-02 | False |
7 | False | 1.0 | 4.015600e-09 | 5.689600e-10 | 0.000347 | 0.000534 | 1.751700e-07 | 1.545434e-07 | 4.052900e-09 | 0.000347 | ... | 1.7883 | 0.003879 | 0.000078 | 0.0 | 1.000000e-10 | 1.787976 | dae144c959cdf177ec97e3b59f159015 | /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn... | 3.684211e-02 | False |
8 | False | 1.0 | 4.015600e-09 | -6.653800e-10 | 0.000349 | 0.000629 | 1.751500e-07 | 1.937603e-07 | 6.928300e-09 | 0.000349 | ... | 1.7883 | 0.003879 | 0.000078 | 0.0 | 1.000000e-10 | 1.787976 | aa14936ee8e37ca095d6d052bb7b2c04 | /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn... | 4.210526e-02 | False |
9 | False | 1.0 | 4.015600e-09 | -2.662300e-09 | 0.000399 | 0.000768 | 1.751300e-07 | 2.886720e-07 | 9.753800e-09 | 0.000399 | ... | 1.7883 | 0.003879 | 0.000078 | 0.0 | 1.000000e-10 | 1.787976 | efffe3c12cb38f91092589b38d338d29 | /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn... | 4.736842e-02 | False |
10 | False | 1.0 | 4.015600e-09 | -5.442600e-09 | 0.000491 | 0.000938 | 1.751000e-07 | 4.490753e-07 | 1.230300e-08 | 0.000490 | ... | 1.7883 | 0.003879 | 0.000078 | 0.0 | 1.000000e-10 | 1.787976 | 926798389bda3d66c00620a39ddd3b8b | /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn... | 5.263158e-02 | False |
11 | False | 1.0 | 4.015600e-09 | -8.943500e-09 | 0.000610 | 0.001129 | 1.750800e-07 | 6.815942e-07 | 1.438200e-08 | 0.000610 | ... | 1.7883 | 0.003879 | 0.000078 | 0.0 | 1.000000e-10 | 1.787976 | 1d54b0678b9ef6bfc42b0c2ff2120ae5 | /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn... | 5.789474e-02 | False |
12 | False | 1.0 | 4.015600e-09 | -1.305700e-08 | 0.000746 | 0.001333 | 1.750700e-07 | 9.890153e-07 | 1.580400e-08 | 0.000746 | ... | 1.7883 | 0.003879 | 0.000078 | 0.0 | 1.000000e-10 | 1.787976 | 0a331d558139ac3a05c4cf676a8455b6 | /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn... | 6.315789e-02 | False |
13 | False | 1.0 | 4.015600e-09 | -1.761700e-08 | 0.000890 | 0.001543 | 1.751000e-07 | 1.369874e-06 | 1.640300e-08 | 0.000891 | ... | 1.7883 | 0.003879 | 0.000078 | 0.0 | 1.000000e-10 | 1.787976 | e6bd6c13a2d22ad150081863f1c6fb59 | /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn... | 6.842105e-02 | False |
14 | False | 1.0 | 4.015600e-09 | -2.254200e-08 | 0.001038 | 0.001754 | 1.751800e-07 | 1.818200e-06 | 1.601500e-08 | 0.001040 | ... | 1.7883 | 0.003879 | 0.000078 | 0.0 | 1.000000e-10 | 1.787977 | 3124ef1903c236f1962db3ec8dacd2fe | /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn... | 7.368421e-02 | False |
15 | False | 1.0 | 4.015600e-09 | -2.742500e-08 | 0.001186 | 0.001962 | 1.753100e-07 | 2.323812e-06 | 1.459800e-08 | 0.001189 | ... | 1.7883 | 0.003879 | 0.000077 | 0.0 | 1.000000e-10 | 1.787978 | 961dc67f12dc8c627e0a53ad04219881 | /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn... | 7.894737e-02 | False |
16 | False | 1.0 | 4.015600e-09 | -3.215500e-08 | 0.001330 | 0.002161 | 1.755100e-07 | 2.871803e-06 | 1.223600e-08 | 0.001334 | ... | 1.7883 | 0.003879 | 0.000077 | 0.0 | 1.000000e-10 | 1.787978 | 9a8e21bd4149448d46774270247b7c2b | /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn... | 8.421053e-02 | False |
17 | False | 1.0 | 4.015600e-09 | -3.645000e-08 | 0.001467 | 0.002349 | 1.757800e-07 | 3.444868e-06 | 8.855100e-09 | 0.001472 | ... | 1.7883 | 0.003879 | 0.000077 | 0.0 | 1.000000e-10 | 1.787979 | c191147e9f05abf7a8ba60eb73fbddbd | /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn... | 8.947368e-02 | False |
18 | False | 1.0 | 4.015600e-09 | -4.010700e-08 | 0.001595 | 0.002522 | 1.761000e-07 | 4.022045e-06 | 4.511600e-09 | 0.001601 | ... | 1.7883 | 0.003879 | 0.000077 | 0.0 | 1.000000e-10 | 1.787980 | 1c33e9ed18d9ff88f91eda547cfe96cd | /var/folders/2f/l5_mybzs30j4qqvyj98w1_nw0000gn... | 9.473684e-02 | False |
19 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 1.000000e+09 | True |
20 rows × 25 columns
In [19]:
Copied!
# Get all runs without an exeption
filtered_df=df[~df['Exception']]
# Get all runs without an exeption
filtered_df=df[~df['Exception']]
Visualize with Bokeh¶
In [20]:
Copied!
from bokeh.plotting import figure, output_notebook, show
from bokeh.models import ColumnDataSource
# Allows plots to show up in the notebook
output_notebook()
from bokeh.plotting import figure, output_notebook, show
from bokeh.models import ColumnDataSource
# Allows plots to show up in the notebook
output_notebook()
In [21]:
Copied!
# Form a Bokeh style data source
source = ColumnDataSource(filtered_df)
source.column_names
# Form a Bokeh style data source
source = ColumnDataSource(filtered_df)
source.column_names
Out[21]:
['index', 'error', 'end_mean_z', 'end_mean_t', 'end_mean_x', 'end_sigma_x', 'end_sigma_xp', 'end_norm_emit_x', 'end_cov_x__xp', 'end_mean_y', 'end_sigma_y', 'end_sigma_yp', 'end_norm_emit_y', 'end_cov_y__yp', 'end_mean_kinetic_energy', 'end_sigma_z', 'end_sigma_energy', 'end_norm_emit_z', 'end_cov_z__energy', 'end_n_particle_loss', 'end_total_charge', 'end_higher_order_energy_spread', 'fingerprint', 'archive', 'maxb(1)', 'Exception']
In [22]:
Copied!
# A simple plot
p = figure()
p.circle(x='maxb(1)', y='end_sigma_x', source=source)
show(p)
# A simple plot
p = figure()
p.circle(x='maxb(1)', y='end_sigma_x', source=source)
show(p)
In [23]:
Copied!
# Fancier plot
from bokeh.models.tools import HoverTool
hover = HoverTool()
hover.tooltips=[
('sigma_x', '@end_sigma_x'),
('norm_emit_x', '@end_norm_emit_x'),
('archive', '@archive')
]
p = figure(tools='tap')
p.circle(x='maxb(1)', y='end_sigma_x', source=source, size=10, color='green')
p.add_tools(hover)
show(p)
# Fancier plot
from bokeh.models.tools import HoverTool
hover = HoverTool()
hover.tooltips=[
('sigma_x', '@end_sigma_x'),
('norm_emit_x', '@end_norm_emit_x'),
('archive', '@archive')
]
p = figure(tools='tap')
p.circle(x='maxb(1)', y='end_sigma_x', source=source, size=10, color='green')
p.add_tools(hover)
show(p)
In [24]:
Copied!
# Load all archives
from astra import Astra
Alist = []
for a in filtered_df['archive']:
A = Astra()
A.load_archive(a)
Alist.append(A)
# Load all archives
from astra import Astra
Alist = []
for a in filtered_df['archive']:
A = Astra()
A.load_archive(a)
Alist.append(A)
In [25]:
Copied!
plt.plot(
Alist[0].output['stats']['mean_z'],
Alist[0].output['stats']['sigma_x']
)
plt.plot(
Alist[0].output['stats']['mean_z'],
Alist[0].output['stats']['sigma_x']
)
Out[25]:
[<matplotlib.lines.Line2D at 0x15774f4f0>]