Commit 23322b8e authored by myron's avatar myron
Browse files

restructuring the XRS imaging interpolation scheme for ID20

parent e519bd69
import numpy as np
import h5py
import glob
import json
import os
BATCH_PARALLELISM = 4
import os
def main():
peaks_shifts = File("peaks_positions_for_analysers.h5","w")["peaks_positions"]
assert( len(peaks_shifts) == 72))
Enominal = np.median(peak_shifts)
peak_shifts-= Enominal
datadir = "/data/id20/inhouse/data/run3_20/run3_es949"
os.system("mkdir results")
roi_target_path = "results/myrois.h5:/ROIS"
filter_path : "mask.h5:/FILTER_MASK/filter"
roi_scan_num = 246
first_scan_num = 651
Ydim = 25
Zdim = 10
Edim = 7
monitorcolumn = "izero/0.000001"
signals_target_file = "results/signals.h5"
interpolated_signals_target_file = "results/interpolated_signals.h5"
steps_to_do = {
"do_step_make_roi": False,
"do_step_interpolation": True,
"do_step_sample_extraction": False,
"do_step_make_reference": False,
"do_step_scalar_products": False,
"do_step_finalise_for_fit": True
}
tools_sequencer( peaks_shifts = peaks_shifts ,
datadir = datadir ,
filter_path = filter_path ,
roi_scan_num = roi_scan_num ,
roi_target_path = roi_target_path ,
steps_to_do = steps_to_do,
first_scan_num = first_scan_num,
Ydim = Ydim ,
Zdim = Zdim ,
Edim = Edim ,
monitor_column = monitor_column,
signals_target_file = signals_target_file
interpolated_signals_target_file = interpolated_signals_target_file
)
def process_input(s, go=0, exploit_slurm_mpi = 0, stop_omp = False):
open("input_tmp_%d.par"%go, "w").write(s)
background_activator = ""
if (go % BATCH_PARALLELISM ):
background_activator = "&"
prefix=""
if stop_omp:
prefix = prefix +"export OMP_NUM_THREADS=1 ;"
if ( exploit_slurm_mpi==0 ):
os.system(prefix +"mpirun -n 1 XRS_swissknife input_tmp_%d.par %s"%(go, background_activator))
elif ( exploit_slurm_mpi>0 ):
os.system(prefix + "mpirun XRS_swissknife input_tmp_%d.par %s"%(go, background_activator) )
else:
os.system(prefix + "mpirun -n %d XRS_swissknife input_tmp_%d.par %s"%(abs( exploit_slurm_mpi ), go, background_activator) )
def select_rois( datadir = None, roi_scan_num=None, roi_target_path = None, filter_path = None):
inputstring = """
create_rois:
expdata : {expdata}
scans : [{roi_scan_num}]
roiaddress : {roi_target_path}
filter_path : {filter_path}
""".format(
expdata = os.path.join( datadir, "/hydra"),
roi_scan_num = roi_scan_num,
roi_target_path = roi_target_path,
filter_path = filter_path
)
process_input( inputstring, exploit_slurm_mpi = 0 )
extract_sample_givenrois(
roi_path = roi_path,
Start = first_scan_num ,
End = (first_scan_num + Zdim * Edim ) ,
Thickness = Zdim
)
def extract_sample_givenrois(
roi_path = None,
datadir = None,
Start = None,
End = None,
Thickness = None ,
monitor_columns = None,
signals_target_file = None
):
for start in range(Start,End, Thickness):
end = start+Thickness
signal_path = signals_target_file + ":/_{start}_{end}".format(start=start, end=end)
inputstring = """
loadscan_2Dimages :
expdata : {expdata}
roiaddress : {roi_path}
scan_interval : [{start}, {end}]
energy_column : sty
signaladdress : {signal_path}
monitor_column : {monitor_column}
sumto1D : 0
""".format(
expdata = os.path.join( datadir, "/hydra"),
roi_path = roi_path,
start = start,
end = end,
monitor_column = monitor_column,
signal_path = signal_path
)
process_input(s, exploit_slurm_mpi = 1)
class interpolate( peaks_shifts, interp_file, interp_file_target):
volum_list = list(interp_file.keys())
scan_num_list = np.array([ int( t.split("_") [1]) for t in volum_list])
ene_list = np.array([ interp_file[vn]["scans"]["Scan%03d"%sn ]["motorDict"]["energy"].value for vn,sn in zip(volum_list, scan_num_list ) ])
print ( " ecco la scannumlist " , scan_num_list)
print (" ecco ene_list", ene_list)
order = np.argsort( ene_list )
ene_list = ene_list [order]
scan_num_list = scan_num_list [order]
volum_list = [ volum_list [ii] for ii in order ]
print ( " ECCO I DATI ")
print ( ene_list )
print ( cenom )
# raise
for t_vn, t_sn, t_ene in list(zip(volum_list, scan_num_list, ene_list ))[0:]:
rois_coeffs={}
for roi_num, de in enumerate( cenom ):
print ( roi_num, "===== " , t_ene+de , ene_list .min() , t_ene+de , ene_list .max() )
if t_ene+de < ene_list .min() or t_ene+de > ene_list .max():
continue
print ( " CONTINUO ", t_ene+de, ene_list .min() ,ene_list .max() )
i0 = np.searchsorted( ene_list , t_ene+de )-1
assert(i0>=0)
i1=i0+1
print (i0, i1, len(ene_list))
print (ene_list)
assert(i1<len( ene_list ))
DE = ( ene_list[i1] - ene_list[i0] )
df = ( t_ene+de - ene_list[i0] )/ DE
rois_coeffs[ roi_num ] = [ i0,(1-df) , i1,df ]
print ( " for reinterpolation of ", t_vn ," interpolation scheme is the following ", rois_coeffs)
interp_file_target.require_group(target)
fscans = interp_file[ t_vn ]["scans"]
keys_list = list( fscans.keys() )
print ( " keylist ", keys_list)
# interp_file_target.flush()
fscans = interp_file_target[ target+"/"+t_vn ]["scans"]
keys_list = list( fscans.keys() )
print ( " keylist ", keys_list)
print ( " roislist keylist", list(rois_coeffs.keys()) )
for k in keys_list:
if k[:3]=="ROI":
if int(k[3:]) not in rois_coeffs:
print (" rimuovo ", k)
del fscans[k]
for sn in range(t_sn, t_sn+1):
fScan = fscans["Scan%03d"% sn]
keys_list = list( fScan.keys() )
for k in keys_list:
if k!="motorDict":
if int(k) not in rois_coeffs:
print (" rimuovo da scans", k)
del fScan[k]
for sn in range(t_sn, t_sn+1):
fScan = fscans["Scan%03d"% sn]
keys_list = list( fScan.keys() )
for k in keys_list:
if k!="motorDict":
assert( int(k) in rois_coeffs)
k = int(k)
i0,f0,i1,f1 = rois_coeffs[k]
matrix0 = interp_file[volum_list[i0] ]["scans"]["Scan%03d"%( scan_num_list[i0]+sn-t_sn) ][str(k)]["matrix"][:]
matrix1 = interp_file[volum_list[i1] ]["scans"]["Scan%03d"%( yyyscan_num_list[i1]+sn-t_sn) ][str(k)]["matrix"][:]
monitor = np.ones( matrix0.shape[0],"f" )
newmatrix = f0* matrix0+f1*matrix1
if "matrix" in fScan[str(k)] :
del fScan[str(k)]["matrix"]
if "monitor" in fScan[str(k)] :
del fScan[str(k)]["monitor"]
if "monitor_divider" in fScan[str(k)] :
del fScan[str(k)]["monitor_divider"]
fScan[str(k)]["matrix"] = newmatrix
fScan[str(k)]["monitor"] = monitor
fScan[str(k)]["monitor_divider"] = 1.0
def tools_sequencer( peaks_shifts = None,
datadir = None,
filter_path = None,
roi_scan_num = None,
roi_target_path = None,
first_scan_num = None ,
Ydim = None ,
Zdim = None ,
Edim = None ,
monitor_column = None,
signals_target_file = None,
interpolated_signals_target_file = None,
steps_to_do = None,
#####################################################
## can be left to None, will be set to the used target
roi_path = None,
signals_file = None
interpolated_signals_file = None
) :
if roi_path is None:
roi_path = roi_target_path
if signals_file is None:
signals_file = signals_target_file
if interpolated_signals_file is None:
interpolated_signals_file = interpolated_signals_target_file
if(steps_to_do["do_step_make_roi"]): # ROI selection and reference scan
select_rois(datadir = datadir ,
roi_scan_num = roi_scan_num ,
roi_target_path = roi_target_path,
filter_path = filter_path,
signals_target_file = signals_target_file
)
if(steps_to_do["do_step_sample_extraction"]):
extract_sample_givenrois(
roi_path = roi_path,
datadir = datadir,
Start = first_scan_num ,
End = (first_scan_num + Zdim * Edim ) ,
Thickness = Zdim
monitor_column = monitor_column,
signals_target_file = signals_target_file
)
if(steps_to_do["do_step_interpolation"]):
os.system("cp {signals_file} {interpolated_signals_target_file}".format(signals_file=signals_file, interpolated_signals_target_file =interpolated_signals_target_file) )
Interpolate( peaks_shift , signals_file , interpolated_signals_target_file )
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment