Skip to content
Snippets Groups Projects
Commit f285bc07 authored by Arnaud HEMMERLE's avatar Arnaud HEMMERLE
Browse files

Add the tutorial on batch

parent 52d04c86
Branches
No related tags found
No related merge requests found
Showing
with 17654 additions and 2 deletions
.DS_Store
Example/.DS_Store
howto_parameters/.DS_Store
howto_parameters/.ipynb_checkpoints/
TutorialGenX.ipynb is a small tutorial on how to fill in the different parameters in GenX (https://aglavic.github.io/genx/index.html)
# Tutorials GenX
Here you will find small tutorials on how to use GenX (https://aglavic.github.io/genx/index.html).
## howto_batch
The notebook ```batch_genx.ipynb``` details the different steps on how to use GenX with command lines.
The aim is to batch fits for XRR without the GUI. To do so we edit the .hgx files, which are just hdf5 files.
The script ```batch_genx.ipy``` is a condensed script without all the details from the notebook.
## howto_parameters
Explains what are the different parameters in the GUI.
Source diff could not be displayed: it is too large. Options to address this: view the blob.
This diff is collapsed.
import numpy as np
import h5py
import os as os
scan0 = np.genfromtxt('data/scan0.dat')
scan1 = np.genfromtxt('data/scan1.dat')
scan2 = np.genfromtxt('data/scan2.dat')
scans = [scan0, scan1, scan2]
# We change the upper bound of the electron density for each scan
max_rho = [0.04, 0.05, 0.045]
is_first_loop = True
for i, scan in enumerate(scans):
f = h5py.File("data/model_for_fit.hgx", "a")
########################################################
# Load the scan data into the model
########################################################
f['current/data/datasets/0/x'][...] = scan[:,0]
f['current/data/datasets/0/y'][...] = scan[:,1]
f['current/data/datasets/0/error'][...] = scan[:,2]
#########################################################
# Change upper bound for electron density
#########################################################
# Index of columns (always the same)
# 0: Parameter (string)
# 1: Value (float)
# 2: Fit (bool)
# 3: Min (float)
# 4: Max (float)
# 5: Error (float)
# Extract the parameter names
col0 = [val.decode("UTF-8") for val in f['current/parameters/data col 0'].value]
# Find where the param is
arg_param = col0.index("Sub.setDens")
# Change its max value (needs to extract and update the whole array)
# 'Max' value is in data col 4 (see list above)
temp = f['current/parameters/data col 4'].value
temp[arg_param] = max_rho[i]
f['current/parameters/data col 4'][...] = temp
f.close()
#########################################################
# Do the fit
#########################################################
print(20*'#')
print('Fit %s'%str(i))
result_file = 'results_C/result'+str(i)+'.hgx'
os.system('genx --run --mgen=100 data/model_for_fit.hgx '+result_file)
#########################################################
# Extract and save the fit
#########################################################
f = h5py.File("results_C/result"+str(i)+".hgx", "r")
# Extract and save the curves
x = f['current/data/datasets/0/x'].value
y = f['current/data/datasets/0/y'].value
y_sim = f['current/data/datasets/0/y_sim'].value
error = f['current/data/datasets/0/error'].value
np.savetxt('results_C/fit'+str(i)+'.dat', np.transpose([x, y, y_sim, error]),
header='#x\t#y\t#y_fit\t#error' )
# Extract and save the parameters
col0 = [val.decode("UTF-8") for val in f['current/parameters/data col 0'].value]
col1 = [val for val in f['current/parameters/data col 1'].value]
col2 = [val for val in f['current/parameters/data col 2'].value]
col3 = [val for val in f['current/parameters/data col 3'].value]
col4 = [val for val in f['current/parameters/data col 4'].value]
col5 = [val for val in f['current/parameters/data col 5'].value]
col_labels = [val.decode("UTF-8") for val in f['current/parameters/data_labels'].value]
f.close()
# Reorganise as a table and print
tab0 = np.stack((col0, col1, col2, col3, col4, col5), axis = 1)
tab = np.vstack([col_labels, tab0])
print('Fit results')
print(10*'-')
for line in tab:
if line[0] != '':
try:
print('%s\t%g'%(line[0], float(line[1])))
except:
print('%s\t%s'%(line[0], line[1]))
print('')
# Save
if is_first_loop:
# Write the header
# Get rid of the empty lines in the parameter list
pos_arg = np.array([True if val != '' else False for val in col0])
# Mask the arrays to keep only the non-empty lines
col0 = np.array(col0)[pos_arg]
with open('results_C/summary.dat', 'w') as file:
file.write('#index_fit\t')
for item in col0:
file.write('#%s\t'%item)
file.write('\n')
is_first_loop = False
# Mask the arrays to keep only the non-empty lines
col1 = np.array(col1)[pos_arg]
with open('results_C/summary.dat', 'a') as file:
file.write('%s\t'%str(i))
for item in col1:
file.write('%g\t'%item)
file.write('\n')
\ No newline at end of file
Source diff could not be displayed: it is too large. Options to address this: view the blob.
File added
File added
1.793840902180771912e-01 8.732698599999999534e-01 1.464501900000000087e-03
2.367252594476897953e-01 9.347000100000000256e-01 1.168045500000000105e-03
2.921458168522443888e-01 9.835454999999999615e-01 1.039491499999999980e-03
3.482805432301176451e-01 1.352403299999999919e-01 9.462958899999999595e-05
4.052126320531627690e-01 3.990462699999999807e-02 5.176677299999999992e-05
4.603905074540116105e-01 2.250736500000000140e-02 3.514664299999999749e-05
6.626585434687698450e-01 3.736032099999999842e-03 3.717028600000000142e-06
8.625483462674990998e-01 1.209675099999999934e-03 2.135476500000000018e-06
1.062167735905238386e+00 4.917272600000000080e-04 1.395959399999999955e-06
1.261509825429290022e+00 2.515546700000000037e-04 2.586363799999999894e-07
1.461898834895760668e+00 1.340806899999999978e-04 1.948551199999999921e-07
1.661955070474819607e+00 7.558979799999999364e-05 1.524298899999999938e-07
1.861934988075566899e+00 4.620237700000000042e-05 6.472545600000000529e-08
2.090918258448954514e+00 2.865886299999999978e-05 5.719621699999999781e-08
2.318313862772034550e+00 1.788418499999999960e-05 4.300950300000000303e-08
2.547068408393605932e+00 1.187396300000000062e-05 4.891063499999999981e-08
2.775774367194150383e+00 8.534119000000000478e-06 6.519897200000000467e-08
3.004265466821520292e+00 5.768282899999999700e-06 7.865036100000000540e-08
3.233110081408486547e+00 3.978143899999999661e-06 9.924000600000000249e-08
3.461386207271123894e+00 3.081896699999999976e-06 1.085296899999999936e-07
3.863835200317784935e+00 1.761376800000000105e-06 1.683737900000000020e-07
4.263989268211829220e+00 1.084432799999999917e-06 2.259545000000000001e-07
4.662354447277915703e+00 6.527453399999999490e-07 1.942013500000000054e-07
5.060962216674460556e+00 4.237524399999999764e-07 1.920885899999999888e-07
5.464312701515980919e+00 2.442617600000000232e-07 1.988862200000000023e-07
1.793840902180771912e-01 9.223225883377778045e-01 1.464501900000000087e-03
2.367252594476897953e-01 8.818461597391207274e-01 1.168045500000000105e-03
2.921458168522443888e-01 8.028426516307998195e-01 1.039491499999999980e-03
3.482805432301176451e-01 1.318463585606144961e-01 9.462958899999999595e-05
4.052126320531627690e-01 3.607104221087183149e-02 5.176677299999999992e-05
4.603905074540116105e-01 1.890125181273407881e-02 3.514664299999999749e-05
6.626585434687698450e-01 3.053434289348383630e-03 3.717028600000000142e-06
8.625483462674990998e-01 1.233396941416462248e-03 2.135476500000000018e-06
1.062167735905238386e+00 4.347055593542751032e-04 1.395959399999999955e-06
1.261509825429290022e+00 3.019040013891578604e-04 2.586363799999999894e-07
1.461898834895760668e+00 1.302308190514183815e-04 1.948551199999999921e-07
1.661955070474819607e+00 8.775642622335128203e-05 1.524298899999999938e-07
1.861934988075566899e+00 5.008721032782196875e-05 6.472545600000000529e-08
2.090918258448954514e+00 3.500024321140524943e-05 5.719621699999999781e-08
2.318313862772034550e+00 1.416367310912500871e-05 4.300950300000000303e-08
2.547068408393605932e+00 1.378681818169006891e-05 4.891063499999999981e-08
2.775774367194150383e+00 1.320353087206086022e-05 6.519897200000000467e-08
3.004265466821520292e+00 7.668018576192791203e-06 7.865036100000000540e-08
3.233110081408486547e+00 4.593153625877316214e-06 9.924000600000000249e-08
3.461386207271123894e+00 3.372777675968512441e-06 1.085296899999999936e-07
3.863835200317784935e+00 2.804048026351535725e-06 1.683737900000000020e-07
4.263989268211829220e+00 2.311475353124972168e-06 2.259545000000000001e-07
4.662354447277915703e+00 1.668874410505344459e-06 1.942013500000000054e-07
5.060962216674460556e+00 1.341252022096695586e-06 1.920885899999999888e-07
5.464312701515980919e+00 1.238966221695893562e-06 1.988862200000000023e-07
1.793840902180771912e-01 8.530084168031087932e-01 1.464501900000000087e-03
2.367252594476897953e-01 9.079306174350811887e-01 1.168045500000000105e-03
2.921458168522443888e-01 9.474115773985223310e-01 1.039491499999999980e-03
3.482805432301176451e-01 1.208780779013008816e-01 9.462958899999999595e-05
4.052126320531627690e-01 4.448632094500919870e-02 5.176677299999999992e-05
4.603905074540116105e-01 2.442872859665153745e-02 3.514664299999999749e-05
6.626585434687698450e-01 2.856685432840379724e-03 3.717028600000000142e-06
8.625483462674990998e-01 1.296754260130458195e-03 2.135476500000000018e-06
1.062167735905238386e+00 5.216403725545225778e-04 1.395959399999999955e-06
1.261509825429290022e+00 2.677737030603582623e-04 2.586363799999999894e-07
1.461898834895760668e+00 1.372325655242257185e-04 1.948551199999999921e-07
1.661955070474819607e+00 9.585997012224056096e-05 1.524298899999999938e-07
1.861934988075566899e+00 4.764347266958849850e-05 6.472545600000000529e-08
2.090918258448954514e+00 4.163570665434518451e-05 5.719621699999999781e-08
2.318313862772034550e+00 3.052579318999415134e-05 4.300950300000000303e-08
2.547068408393605932e+00 2.435939965768592813e-05 4.891063499999999981e-08
2.775774367194150383e+00 1.743589891653640658e-05 6.519897200000000467e-08
3.004265466821520292e+00 1.705686527663769801e-05 7.865036100000000540e-08
3.233110081408486547e+00 1.356515560563320959e-05 9.924000600000000249e-08
3.461386207271123894e+00 1.322129740687811065e-05 1.085296899999999936e-07
3.863835200317784935e+00 1.178611179607542376e-05 1.683737900000000020e-07
4.263989268211829220e+00 1.086187310938285455e-05 2.259545000000000001e-07
4.662354447277915703e+00 1.067325714070453988e-05 1.942013500000000054e-07
5.060962216674460556e+00 1.050538232125075898e-05 1.920885899999999888e-07
5.464312701515980919e+00 1.023868972530020013e-05 1.988862200000000023e-07
File added
File added
File added
File added
# #x #y #y_fit #error
1.793840902180771912e-01 8.732698599999999534e-01 9.955653798768119289e-01 1.464501900000000087e-03
2.367252594476897953e-01 9.347000100000000256e-01 9.927467546031669698e-01 1.168045500000000105e-03
2.921458168522443888e-01 9.835454999999999615e-01 9.846229402262667296e-01 1.039491499999999980e-03
3.482805432301176451e-01 1.352403299999999919e-01 1.687116768339962380e-01 9.462958899999999595e-05
4.052126320531627690e-01 3.990462699999999807e-02 5.322598808123969044e-02 5.176677299999999992e-05
4.603905074540116105e-01 2.250736500000000140e-02 2.498922382476669035e-02 3.514664299999999749e-05
6.626585434687698450e-01 3.736032099999999842e-03 4.108017409623955681e-03 3.717028600000000142e-06
8.625483462674990998e-01 1.209675099999999934e-03 1.266326508947071897e-03 2.135476500000000018e-06
1.062167735905238386e+00 4.917272600000000080e-04 5.148527525977584151e-04 1.395959399999999955e-06
1.261509825429290022e+00 2.515546700000000037e-04 2.466226533057285235e-04 2.586363799999999894e-07
1.461898834895760668e+00 1.340806899999999978e-04 1.312671260258462685e-04 1.948551199999999921e-07
1.661955070474819607e+00 7.558979799999999364e-05 7.562506703652437079e-05 1.524298899999999938e-07
1.861934988075566899e+00 4.620237700000000042e-05 4.620237761547304567e-05 6.472545600000000529e-08
2.090918258448954514e+00 2.865886299999999978e-05 2.777028693550910877e-05 5.719621699999999781e-08
2.318313862772034550e+00 1.788418499999999960e-05 1.753296553377052799e-05 4.300950300000000303e-08
2.547068408393605932e+00 1.187396300000000062e-05 1.145016373964191031e-05 4.891063499999999981e-08
2.775774367194150383e+00 8.534119000000000478e-06 7.705687396406830976e-06 6.519897200000000467e-08
3.004265466821520292e+00 5.768282899999999700e-06 5.319116839886143233e-06 7.865036100000000540e-08
3.233110081408486547e+00 3.978143899999999661e-06 3.749420588928278424e-06 9.924000600000000249e-08
3.461386207271123894e+00 3.081896699999999976e-06 2.695812854042431189e-06 1.085296899999999936e-07
3.863835200317784935e+00 1.761376800000000105e-06 1.570426730258791023e-06 1.683737900000000020e-07
4.263989268211829220e+00 1.084432799999999917e-06 9.637336643020696486e-07 2.259545000000000001e-07
4.662354447277915703e+00 6.527453399999999490e-07 6.224972653985092029e-07 1.942013500000000054e-07
5.060962216674460556e+00 4.237524399999999764e-07 4.237524400000000294e-07 1.920885899999999888e-07
5.464312701515980919e+00 2.442617600000000232e-07 3.044996113825554148e-07 1.988862200000000023e-07
# #x #y #y_fit #error
1.793840902180771912e-01 9.223225883377778045e-01 9.954558861188474994e-01 1.464501900000000087e-03
2.367252594476897953e-01 8.818461597391207274e-01 9.924636840609748489e-01 1.168045500000000105e-03
2.921458168522443888e-01 8.028426516307998195e-01 9.825966399617939162e-01 1.039491499999999980e-03
3.482805432301176451e-01 1.318463585606144961e-01 1.446383006954861383e-01 9.462958899999999595e-05
4.052126320531627690e-01 3.607104221087183149e-02 4.790174517305179763e-02 5.176677299999999992e-05
4.603905074540116105e-01 1.890125181273407881e-02 2.281382912033697030e-02 3.514664299999999749e-05
6.626585434687698450e-01 3.053434289348383630e-03 3.809646563382829837e-03 3.717028600000000142e-06
8.625483462674990998e-01 1.233396941416462248e-03 1.179954629494505119e-03 2.135476500000000018e-06
1.062167735905238386e+00 4.347055593542751032e-04 4.812404143166112589e-04 1.395959399999999955e-06
1.261509825429290022e+00 3.019040013891578604e-04 2.313070313094801953e-04 2.586363799999999894e-07
1.461898834895760668e+00 1.302308190514183815e-04 1.236937465034923457e-04 1.948551199999999921e-07
1.661955070474819607e+00 8.775642622335128203e-05 7.174869765248500857e-05 1.524298899999999938e-07
1.861934988075566899e+00 5.008721032782196875e-05 4.426787994758421422e-05 6.472545600000000529e-08
2.090918258448954514e+00 3.500024321140524943e-05 2.704634168570679229e-05 5.719621699999999781e-08
2.318313862772034550e+00 1.416367310912500871e-05 1.747884598858427126e-05 4.300950300000000303e-08
2.547068408393605932e+00 1.378681818169006891e-05 1.179295955308116918e-05 4.891063499999999981e-08
2.775774367194150383e+00 1.320353087206086022e-05 8.292319854486642896e-06 6.519897200000000467e-08
3.004265466821520292e+00 7.668018576192791203e-06 6.060917541907001886e-06 7.865036100000000540e-08
3.233110081408486547e+00 4.593153625877316214e-06 4.593153625877500020e-06 9.924000600000000249e-08
3.461386207271123894e+00 3.372777675968512441e-06 3.607897832911232093e-06 1.085296899999999936e-07
3.863835200317784935e+00 2.804048026351535725e-06 2.555447232147717828e-06 1.683737900000000020e-07
4.263989268211829220e+00 2.311475353124972168e-06 1.988033790604390439e-06 2.259545000000000001e-07
4.662354447277915703e+00 1.668874410505344459e-06 1.668874033592744504e-06 1.942013500000000054e-07
5.060962216674460556e+00 1.341252022096695586e-06 1.482980064163719957e-06 1.920885899999999888e-07
5.464312701515980919e+00 1.238966221695893562e-06 1.371434808481737247e-06 1.988862200000000023e-07
# #x #y #y_fit #error
1.793840902180771912e-01 8.530084168031087932e-01 9.955480139852740384e-01 1.464501900000000087e-03
2.367252594476897953e-01 9.079306174350811887e-01 9.926873849981993070e-01 1.168045500000000105e-03
2.921458168522443888e-01 9.474115773985223310e-01 9.841836693058629848e-01 1.039491499999999980e-03
3.482805432301176451e-01 1.208780779013008816e-01 1.621254097966135788e-01 9.462958899999999595e-05
4.052126320531627690e-01 4.448632094500919870e-02 5.182718442092267008e-02 5.176677299999999992e-05
4.603905074540116105e-01 2.442872859665153745e-02 2.442872491989567241e-02 3.514664299999999749e-05
6.626585434687698450e-01 2.856685432840379724e-03 4.040322846989040373e-03 3.717028600000000142e-06
8.625483462674990998e-01 1.296754260130458195e-03 1.253751729916513513e-03 2.135476500000000018e-06
1.062167735905238386e+00 5.216403725545225778e-04 5.159588722580923156e-04 1.395959399999999955e-06
1.261509825429290022e+00 2.677737030603582623e-04 2.524675506210254800e-04 2.586363799999999894e-07
1.461898834895760668e+00 1.372325655242257185e-04 1.391159202879788375e-04 1.948551199999999921e-07
1.661955070474819607e+00 9.585997012224056096e-05 8.443036105198144692e-05 1.524298899999999938e-07
1.861934988075566899e+00 4.764347266958849850e-05 5.550994492176041200e-05 6.472545600000000529e-08
2.090918258448954514e+00 4.163570665434518451e-05 3.739090863099747523e-05 5.719621699999999781e-08
2.318313862772034550e+00 3.052579318999415134e-05 2.732678183885074995e-05 4.300950300000000303e-08
2.547068408393605932e+00 2.435939965768592813e-05 2.134659829902175749e-05 4.891063499999999981e-08
2.775774367194150383e+00 1.743589891653640658e-05 1.766515835343846310e-05 6.519897200000000467e-08
3.004265466821520292e+00 1.705686527663769801e-05 1.531869930779272172e-05 7.865036100000000540e-08
3.233110081408486547e+00 1.356515560563320959e-05 1.377535166173254983e-05 9.924000600000000249e-08
3.461386207271123894e+00 1.322129740687811065e-05 1.273941163284772604e-05 1.085296899999999936e-07
3.863835200317784935e+00 1.178611179607542376e-05 1.163287726027302003e-05 1.683737900000000020e-07
4.263989268211829220e+00 1.086187310938285455e-05 1.103633646166162646e-05 2.259545000000000001e-07
4.662354447277915703e+00 1.067325714070453988e-05 1.070080586117408399e-05 1.942013500000000054e-07
5.060962216674460556e+00 1.050538232125075898e-05 1.050538232125062346e-05 1.920885899999999888e-07
5.464312701515980919e+00 1.023868972530020013e-05 1.038812147098304682e-05 1.988862200000000023e-07
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment