#*******************************************************************
# EVENT LOOP: OBSERVE ALL WIDGET VALUES AND MOUSE EVENTS
#*******************************************************************
#Observe the file load widget (local version)
def on_file_change(change):
global dirname, filename, th, iexp, cst, sp, dwp, old_strain, old_dw
#open data file
dirname = os.path.dirname(load_button.files[0])
filename = os.path.basename(load_button.files[0])
load_button.description = "File: "+ os.path.join(dirname, filename)
#save data file path
f = open("last_path", "w")
f.write(dirname)
f.close()
#load the xrd data
tth,iexp = np.loadtxt(load_button.files[0], unpack = True) #Read experimental data
iexp[iexp==0] = np.min(iexp[np.nonzero(iexp)]) #Replace any 0s with the minimum value
iexp /= iexp.max() #Normalize to 1
th = tth * np.pi/360 #Convert 2theta angle -> theta angles in radians
#check if there are widget values stored in the folder. If none keep defaults
try:
ipyw_values = np.load(os.path.join(dirname, "ipyw_values.npy"), allow_pickle=True).item()
ipyw_flag = 1
with out:
out.clear_output()
print("Found saved session")
except:
ipyw_flag = 0
with out:
out.clear_output()
print("No saved session found")
if ipyw_flag == 1:
for i,key in enumerate(ipyw_values):
w_list[i].value = ipyw_values[key]
#Check if there are saved B-spline weights. If none keep defaults
try:
sp, dwp = np.loadtxt(os.path.join(dirname, "weights.txt"), unpack=True)
#Create backup (used for to restore saved data when fit is cancelled)
sp_back, dwp_back = np.copy(sp), np.copy(dwp)
#compute the initial strain, DW and intensity
strain = f_strain(cst["z"],sp,cst["t"],cst["sdw_model"])
dw = f_DW(cst["z"],dwp,cst["t"],cst["sdw_model"])
control_sp_x, control_sp_y = control_sp(cst["z"],sp,cst["t"],cst["sdw_model"])
control_dwp_x, control_dwp_y = control_dwp(cst["z"],dwp,cst["t"],cst["sdw_model"])
with out:
print("Found saved B-spline weights")
except:
eps = max(auto_strain(tth, iexp, cst), 0.1)
sp = np.full(cst["sdw_basis"], eps)
dwp = np.ones(cst["sdw_basis"])
sp_back, dwp_back = np.copy(sp), np.copy(dwp)
strain = f_strain(cst["z"],sp,cst["t"],cst["sdw_model"])
dw = f_DW(cst["z"],dwp,cst["t"],cst["sdw_model"])
control_sp_x, control_sp_y = control_sp(cst["z"],sp,cst["t"],cst["sdw_model"])
control_dwp_x, control_dwp_y = control_dwp(cst["z"],dwp,cst["t"],cst["sdw_model"])
with out:
print("No B-spline weights found")
#update the strain/DW plots
with strain_line.hold_sync():
strain_line.x = (cst["t"]-cst["z"])/10
strain_line.y = strain*100
with strain_scat.hold_sync():
strain_scat.x = (cst["t"] - control_sp_x)/10
strain_scat.y = control_sp_y*100
with dw_line.hold_sync():
dw_line.x = (cst["t"]-cst["z"])/10
dw_line.y = dw
with dw_scat.hold_sync():
dw_scat.x = (cst["t"] - control_dwp_x)/10
dw_scat.y = control_dwp_y
#compute xrd
cst = compute_cst(w_list,th)
ical, t0 = f_Refl(th, np.concatenate((sp,dwp)), cst)
error = rmse(iexp,ical)
with out:
print("Computing time: %f4 sec. RMS error: %f4"%(t0,error))
#update xrd plot
with xrd_scat.hold_sync():
xrd_scat.x = th*360/np.pi
xrd_scat.y = np.log10(iexp)
with xrd_line.hold_sync():
xrd_line.x = th*360/np.pi
xrd_line.y = np.log10(ical)
# create a strain/dw backup (for shift)
old_strain = np.copy(strain)
old_dw = np.copy(dw)
load_button.observe(on_file_change, names='files')
#Observe the file load widget (online version)
@out.capture(clear_output=True)
def on_file_upload(change):
global dirname, filename, th, iexp, cst, sp, dwp, old_strain, old_dw
filename = (upload_button.metadata[0]['name']) #get the file name from the embedded metadata dict
raw_data = upload_button.value[filename]['content'].decode() #extract data byte string and convert to str
try:
data = np.fromstring(raw_data, dtype=float, sep = ' ') #convert str to 1D np array
data = data.reshape(int(len(data)/2),2) #reshape to 2cols format
tth,iexp = data[:,0], data[:,1] #Read experimental data
iexp[iexp==0] = np.min(iexp[np.nonzero(iexp)]) #Replace any 0s with the minimum value
iexp /= iexp.max() #Normalize to 1
th = tth * np.pi/360 #Convert 2theta angle -> theta angles in radians
# guess strain from data and generate profiles
eps = max(auto_strain(tth, iexp, cst), 0.1)
sp = np.full(cst["sdw_basis"], eps)
dwp = np.ones(cst["sdw_basis"])
strain = f_strain(cst["z"],sp,cst["t"],cst["sdw_model"])
dw = f_DW(cst["z"],dwp,cst["t"],cst["sdw_model"])
# generate control points
control_sp_x, control_sp_y = control_sp(cst["z"],sp,cst["t"],cst["sdw_model"])
control_dwp_x, control_dwp_y = control_dwp(cst["z"],dwp,cst["t"],cst["sdw_model"])
#compute XRD
ical, t0 = f_Refl(th, np.concatenate((sp,dwp)), cst)
error = rmse(iexp,ical)
with out:
print("Computing time: %f4 sec. RMS error: %f4"%(t0,error))
except:
with out:
print("ERROR. CHECK INPUT DATA: Numbers only in 2-columns space-separated values")
print("PLEASE REFRESH PAGE.")
#update xrd plot
#panzoom = PanZoom(scales={'x': [xscale], 'y': [yscale]})
xscale.min = th.min()*360/np.pi
xscale.max = th.max()*360/np.pi
yscale.min = np.log10(iexp.min())
yscale.max = np.log10(iexp.max())
with xrd_scat.hold_sync():
xrd_scat.x = th*360/np.pi
xrd_scat.y = np.log10(iexp)
with xrd_line.hold_sync():
xrd_line.x = th*360/np.pi
xrd_line.y = np.log10(ical)
#update strain/DW plot
with strain_line.hold_sync():
strain_line.x = (cst["t"]-cst["z"])/10
strain_line.y = strain*100
with strain_scat.hold_sync():
strain_scat.x = (cst["t"] - control_sp_x)/10
strain_scat.y = control_sp_y*100
with dw_line.hold_sync():
dw_line.x = (cst["t"]-cst["z"])/10
dw_line.y = dw
with dw_scat.hold_sync():
dw_scat.x = (cst["t"] - control_dwp_x)/10
dw_scat.y = control_dwp_y
# create a strain/dw backup (for shift)
old_strain = np.copy(strain)
old_dw = np.copy(dw)
upload_button.observe(on_file_upload, names='value')
#observe input widgets and update changes
@out.capture(clear_output=True)
def update_xrd(change):
#update the XRD curve only
global cst
cst = compute_cst(w_list,th)
ical, t0 = f_Refl(th, np.concatenate((sp,dwp)), cst)
xrd_line.y = np.log10(ical)
error = rmse(iexp,ical)
with out:
out.clear_output()
print("Computing time: %f4 sec. RMS error: %f4"%(t0,error))
@out.capture(clear_output=True)
def update_all(change):
#update the strain/DW and XRD curves upon widget modification (except Nb of Bsplines)
global cst, control_sp_x, control_sp_y, control_dwp_x, control_dwp_y, old_strain, old_dw
cst = compute_cst(w_list,th)
# update strain and DW
strain = f_strain(cst["z"],sp,cst["t"],cst["sdw_model"])
dw = f_DW(cst["z"],dwp,cst["t"],cst["sdw_model"])
# update the values of strain and DW at the control point coords (in1d, around)
control_sp_x, control_sp_y = control_sp(cst["z"],sp,cst["t"],cst["sdw_model"])
control_dwp_x, control_dwp_y = control_dwp(cst["z"],dwp,cst["t"],cst["sdw_model"])
# update the plots
with strain_line.hold_sync():
strain_line.x = (cst["t"]-cst["z"])/10
strain_line.y = strain*100
with strain_scat.hold_sync():
strain_scat.x = (cst["t"] - control_sp_x)/10
strain_scat.y = control_sp_y*100
with dw_line.hold_sync():
dw_line.x = (cst["t"]-cst["z"])/10
dw_line.y = dw
with dw_scat.hold_sync():
dw_scat.x = (cst["t"] - control_dwp_x)/10
dw_scat.y = control_dwp_y
# update XRD
ical, t0 = f_Refl(th, np.concatenate((sp,dwp)), cst)
xrd_line.y = np.log10(ical)
error = rmse(iexp,ical)
with out:
out.clear_output()
print("Computing time: %f4 sec. RMS error: %f4"%(t0,error))
# create a strain/dw backup (for shift)
old_strain = np.copy(strain)
old_dw = np.copy(dw)
@out.capture(clear_output=True)
def update_all_w_basis(change):
#update the strain/DW and XRD curves upon Nb of Bsplines modification
global cst, sp, dwp, control_sp_x, control_sp_y, control_dwp_x, control_dwp_y, old_strain, old_dw
#read the new value for the number of basis functions
cst = compute_cst(w_list,th)
#compute the new weights
sp_new = old2new_strain(cst["z"], sp, cst["t"], cst["sdw_basis"],cst["sdw_model"])
dwp_new = old2new_DW(cst["z"], dwp, cst["t"], cst["sdw_basis"],cst["sdw_model"])
sp, dwp = sp_new, dwp_new
#compute the corresponding strain and DW
strain = f_strain(cst["z"],sp,cst["t"],cst["sdw_model"])
dw = f_DW(cst["z"],dwp,cst["t"],cst["sdw_model"])
# update the values of strain and DW at the control point coords (in1d, around)
control_sp_x, control_sp_y = control_sp(cst["z"],sp,cst["t"],cst["sdw_model"])
control_dwp_x, control_dwp_y = control_dwp(cst["z"],dwp,cst["t"],cst["sdw_model"])
# update the plots
with strain_line.hold_sync():
strain_line.x = (cst["t"]-cst["z"])/10
strain_line.y = strain*100
with strain_scat.hold_sync():
strain_scat.x = (cst["t"] - control_sp_x)/10
strain_scat.y = control_sp_y*100
with dw_line.hold_sync():
dw_line.x = (cst["t"]-cst["z"])/10
dw_line.y = dw
with dw_scat.hold_sync():
dw_scat.x = (cst["t"] - control_dwp_x)/10
dw_scat.y = control_dwp_y
# update XRD
ical, t0 = f_Refl(th, np.concatenate((sp,dwp)), cst)
error = rmse(iexp,ical)
xrd_line.y = np.log10(ical)
with out:
out.clear_output()
print("Computing time: %f4 sec. RMS error: %f4"%(t0,error))
# create a strain/dw backup (for shift)
old_strain = np.copy(strain)
old_dw = np.copy(dw)
#observe all widgets
for i, widget in enumerate(w_list):
if i<=10:
widget.observe(update_xrd, names='value')
elif i==12:
widget.observe(update_all_w_basis, names='value')
else:
widget.observe(update_all, names='value')
#observe the strain slider
@out.capture(clear_output=True)
def update_strain_scale(change=None):
global sp, strain, control_sp_y, cst, old_strain
#get scale factor from widget and modify control points accordingly
control_sp_y = control_sp_y*strain_scale.value
strain_scat.y = control_sp_y*100
#update strain curve
sp = interp_and_fit_strain(control_sp_x, control_sp_y, cst["z"], sp, cst["sdw_model"]) #compute the weights
strain = f_strain(cst["z"],sp,cst["t"],cst["sdw_model"])
strain_line.y = strain*100 #redraw new strain
#switch slider back to default value = 1
strain_scale.value = 1
#update XRD curve
cst = compute_cst(w_list,th)
ical, t0 = f_Refl(th, np.concatenate((sp,dwp)), cst)
error = rmse(iexp,ical)
xrd_line.y = np.log10(ical)
with out:
out.clear_output()
print("Computing time: %f4 sec. RMS error: %f4"%(t0,error))
# create a strain/dw backup (for shift)
old_strain = np.copy(strain)
strain_scale.observe(update_strain_scale, names = 'value')
#observe the strain shift slider
@out.capture(clear_output=True)
def update_strain_shift(change=None):
global sp, strain, control_sp_y, cst, old_strain
# create an array twice the initial size to be shifted
length = int(len(old_strain))
expanded_strain = np.zeros(2*length)
expanded_strain[int(length/2):int(3*length/2)]=old_strain
expanded_strain[int(3*length/2):2*length]=old_strain[-1]
# get shift factor from widget and shift curve accordingly
cut = int(strain_shift.value * length)
shifted_strain = expanded_strain[int(length/2)+cut:int(3*length/2)+cut]
# compute new weights, new strain and update strain curve
sp = shift_strain(cst["z"], sp, cst["t"], shifted_strain, cst["sdw_model"])
control_sp_x, control_sp_y = control_sp(cst["z"],sp,cst["t"],cst["sdw_model"])
strain = f_strain(cst["z"], sp, cst["t"], cst["sdw_model"])
# update plot (for some reason there is no need to update the XRD. Its done via observe_strain_plot)
strain_line.y = strain*100 #redraw new strain
strain_scat.y = control_sp_y*100
# switch slider back to default value = 0
strain_shift.value = 0
strain_shift.observe(update_strain_shift, names = 'value')
#observe the dw slider
@out.capture(clear_output=True)
def update_dw_scale(change=None):
global dwp, dw, control_dwp_y, cst, old_dw
#get scale factor from widget and modify control points accordingly
control_dwp_y = control_dwp_y*dw_scale.value
# control_dwp_y[control_dwp_y>1] = 1 #TEMPORARILY REMOVE DW UPPER LIMIT
dw_scat.y = control_dwp_y
#update strain curve
dwp = interp_and_fit_dw(control_dwp_x, control_dwp_y, cst["z"], dwp, cst["sdw_model"]) #compute the weights
dw = f_DW(cst["z"],dwp,cst["t"],cst["sdw_model"])
dw_line.y = dw #redraw new strain
#switch slider back to default value = 1
dw_scale.value = 1.
#update XRD and update the curve
cst = compute_cst(w_list,th)
ical, t0 = f_Refl(th, np.concatenate((sp,dwp)), cst)
error = rmse(iexp,ical)
xrd_line.y = np.log10(ical)
with out:
out.clear_output()
print("Computing time: %f4 sec. RMS error: %f4"%(t0,error))
# create a strain/dw backup (for shift)
old_dw = np.copy(dw)
dw_scale.observe(update_dw_scale, names = 'value')
#observe the dw shift slider
@out.capture(clear_output=True)
def update_dw_shift(change=None):
global dwp, dw, control_dwp_y, cst, old_dw
# create an array twice the initial size to be shifted
length = int(len(old_dw))
expanded_dw = np.ones(2*length)
expanded_dw[int(length/2):int(3*length/2)]=old_dw
expanded_dw[int(3*length/2):2*length]=old_dw[-1]
# get shift factor from widget and shift curve accordingly
cut = int(dw_shift.value * length)
shifted_dw = expanded_dw[int(length/2)+cut:int(3*length/2)+cut]
# compute new weights, new strain and update strain curve
dwp = shift_dw(cst["z"], dwp, cst["t"], shifted_dw, cst["sdw_model"])
control_dwp_x, control_dwp_y = control_dwp(cst["z"],dwp,cst["t"],cst["sdw_model"])
dw = f_DW(cst["z"], dwp, cst["t"], cst["sdw_model"])
# update plot (for some reason there is no need to update the XRD. Its done via observe_strain_plot)
dw_line.y = dw #redraw new strain
dw_scat.y = control_dwp_y
# switch slider back to default value = 0
dw_shift.value = 0
dw_shift.observe(update_dw_shift, names = 'value')
#observe the strain interactive plot
@out.capture(clear_output=True)
def update_strain_xrd(change=None):
global sp, control_sp_x, control_sp_y, cst, old_strain
# get the new control point values
if np.any(strain_scat.selected):
old_control_sp_y = np.copy(control_sp_y)
if len(strain_scat.selected)>1:
with out:
print("Moving multiple points:", *strain_scat.selected)
delta_strain = change['new']/100 - old_control_sp_y #get old-new difference array
delta_strain = delta_strain[np.nonzero(delta_strain)] #find the delta value
new_control_sp_y = old_control_sp_y
new_control_sp_y[strain_scat.selected] += delta_strain #shift only selected points
else:
new_control_sp_y = change['new']/100 #get new strain values
#compute the new weights and update the curve
sp = interp_and_fit_strain(control_sp_x, new_control_sp_y, cst["z"], sp, cst["sdw_model"])
strain = f_strain(cst["z"],sp,cst["t"],cst["sdw_model"])
with strain_line.hold_sync():
strain_line.x = (cst["t"]-cst["z"])/10
strain_line.y = strain*100 #redraw new strain
with strain_scat.hold_sync():
strain_scat.x = (cst["t"] - control_sp_x)/10
strain_scat.y = new_control_sp_y*100
control_sp_y = new_control_sp_y #update the control point values
#update XRD
cst = compute_cst(w_list,th)
ical, t0 = f_Refl(th, np.concatenate((sp,dwp)), cst)
error = rmse(iexp,ical)
xrd_line.y = np.log10(ical)
with out:
out.clear_output()
print("Computing time: %f4 sec. RMS error: %f4"%(t0,error))
# create a strain/dw backup (for shift)
old_strain = np.copy(strain)
strain_scat.observe(update_strain_xrd, names=['y'])
#observe the dw interactive plot
@out.capture(clear_output=True)
def update_dw_xrd(change=None):
global dwp, control_dwp_x, control_dwp_y, cst, old_dw
# get the new control point values
if np.any(dw_scat.selected):
old_control_dwp_y = np.copy(control_dwp_y)
if len(dw_scat.selected)>1:
with out:
print("Moving multiple points:", *dw_scat.selected)
delta_dw = change['new'] - old_control_dwp_y #get old-new difference array
delta_dw = delta_dw[np.nonzero(delta_dw)] #find the delta value
new_control_dwp_y = old_control_dwp_y
new_control_dwp_y[dw_scat.selected] += delta_dw #shift only selected points
else:
new_control_dwp_y = change['new'] #get new strain values
#compute the weights
dwp = interp_and_fit_dw(control_dwp_x, new_control_dwp_y, cst["z"], dwp, cst["sdw_model"])
dw = f_DW(cst["z"],dwp,cst["t"],cst["sdw_model"])
with dw_line.hold_sync():
dw_line.x = (cst["t"]-cst["z"])/10
dw_line.y = dw #redraw new dw
with dw_scat.hold_sync():
dw_scat.x = (cst["t"] - control_dwp_x)/10
dw_scat.y = new_control_dwp_y
control_dwp_y = new_control_dwp_y #update the control point values
#update XRD and update the curve
cst = compute_cst(w_list,th)
ical, t0 = f_Refl(th, np.concatenate((sp,dwp)), cst)
error = rmse(iexp,ical)
xrd_line.y = np.log10(ical)
with out:
out.clear_output()
print("Computing time: %f4 sec. RMS error: %f4"%(t0,error))
# create a strain/dw backup (for shift)
old_dw = np.copy(dw)
dw_scat.observe(update_dw_xrd, names=['y'])
#observe the save button
def on_save_clicked(b):
#save the widget values
ipyw_values = {
"resol_fct": resol_fct.value,
"resol_width": resol_width.value,
"resol_shape": resol_shape.value,
"exp_wl": exp_wl.value,
"exp_offset": exp_offset.value,
"exp_bkg": exp_bkg.value,
"cryst_name": cryst_name.value,
"cryst_h": cryst_h.value,
"cryst_k": cryst_k.value,
"cryst_l": cryst_l.value,
"sample": sample.value,
"sdw_model": sdw_model.value,
"sdw_basis": sdw_basis.value,
"th_value": th_value.value,
"th_slices": th_slices.value,
"fth_value": fth_value.value,
"sub_name": sub_name.value,
"sub_h": sub_h.value,
"sub_k": sub_k.value,
"sub_l": sub_l.value,
"algo": algo.value,
"min_strain": min_strain.value,
"max_strain": max_strain.value,
"min_dw": min_dw.value,
"max_dw": max_dw.value,
"gsa_temp": gsa_temp.value,
"gsa_cycles": gsa_cycles.value,
"gsa_Tsteps": gsa_Tsteps.value
}
#if the notebook is runned locally, save the widget values to disk
if local:
np.save(os.path.join(dirname, "ipyw_values.npy"), ipyw_values)
with out:
out.clear_output()
print("Data saved.")
#save the B-spline weights. If the notebook is runned locally, save the data to disk
if local:
np.savetxt(os.path.join(dirname, "weights.txt"), np.column_stack((sp,dwp)), fmt="%10.8f")
#save the XRD simulation
#if the notebook is runned locally, save the data to disk
#otherwise, the data is downloaded via a base64-encoded data URL
ical = f_Refl(th, np.concatenate((sp,dwp)), cst)[0]
out_xrd = np.column_stack((th*360/np.pi,iexp,ical))
if local:
np.savetxt(os.path.join(dirname, "simul_xrd.txt"), out_xrd, fmt="%10.8f")
else:
with out:
out.clear_output()
out_xrd = np.array2string(out_xrd, threshold = 1e6) #convert the array to a str
out_xrd = re.sub('[\[\]]', ' ', out_xrd) #remove the [] brackets
out_xrd = out_xrd.replace("\n", "%0A") #thanks raphj (@linuxfr.org) for this hack
#The next 2 lines are needed for base64 encoding only
#out_xrd = base64.b64encode(out_xrd.encode('ascii')) #convert to base64
#out_xrd = str(out_xrd).replace("'","").replace("b","") #remove the b and the quotes from b64 string
xrd_link = """<a download='simul_xrd.txt' href='data:text/csv;charset=utf-8;ascii,"""+out_xrd+"""'>Download XRD simulation.</a>"""
dl_xrd.value = xrd_link
#save the strain/DW depth-profiles
#if the notebook is runned locally, save the data to disk
#otherwise, the data is downloaded via a base64-encoded data URL
strain = f_strain(cst["z"],sp,cst["t"],cst["sdw_model"])
dw = f_DW(cst["z"],dwp,cst["t"],cst["sdw_model"])
depth = (cst["t"]-cst["z"])/10
out_sdw = np.column_stack((depth,strain,dw))
if local:
np.savetxt(os.path.join(dirname, "simul_strain_dw.txt"),out_sdw , fmt="%10.8f")
else:
out_sdw = np.array2string(out_sdw, threshold = 1e6) #convert the array to a str
out_sdw = re.sub('[\[\]]', ' ', out_sdw) #remove the [] brackets
out_sdw = out_sdw.replace("\n", "%0A") #thanks raphj (@linuxfr.org) for this hack
#The next 2 lines are needed for base64 encoding only
#out_sdw = base64.b64encode(out_sdw.encode('ascii')) #convert to base64
#out_sdw = str(out_sdw).replace("'","").replace("b","") #remove the b and the quotes from b64 string
sdw_link = "<a download='simul_strain_dw.txt' href='data:text/csv;charset=utf-8;ascii,"+out_sdw+"'>Download strain/DW depth profiles.</a>"
dl_sdw.value = sdw_link
save.on_click(on_save_clicked)
#observe the fit button
@out.capture(clear_output=True)
def on_fit_clicked(b):
global cst, sp, sp_back, dwp_back, dwp, control_sp_x, control_sp_y, control_dwp_x, control_dwp_y, old_strain, old_dw
dl_xrd.value = ""
dl_sdw.value = ""
sp_back, dwp_back = np.copy(sp), np.copy(dwp)
cst = compute_cst(w_list,th)
with out:
out.clear_output()
print ("Fitting... Please Wait.")
t0 = time()
sp, dwp = fit_curve(th, iexp, np.concatenate((sp,dwp)), cst)
fit_time = time() - t0
# update XRD
ical = f_Refl(th, np.concatenate((sp,dwp)), cst)[0]
error = rmse(iexp, ical)
xrd_line.y = np.log10(ical)
# update strain and DW
strain = f_strain(cst["z"],sp,cst["t"],cst["sdw_model"])
dw = f_DW(cst["z"],dwp,cst["t"],cst["sdw_model"])
# update the values of strain and DW at the control point coords (in1d, around)
control_sp_x, control_sp_y = control_sp(cst["z"],sp,cst["t"],cst["sdw_model"])
control_dwp_x, control_dwp_y = control_dwp(cst["z"],dwp,cst["t"],cst["sdw_model"])
# update the plots
with strain_line.hold_sync():
strain_line.x = (cst["t"]-cst["z"])/10
strain_line.y = strain*100
with strain_scat.hold_sync():
strain_scat.x = (cst["t"] - control_sp_x)/10
strain_scat.y = control_sp_y*100
with dw_line.hold_sync():
dw_line.x = (cst["t"]-cst["z"])/10
dw_line.y = dw
with dw_scat.hold_sync():
dw_scat.x = (cst["t"] - control_dwp_x)/10
dw_scat.y = control_dwp_y
with out:
out.clear_output()
print("Done. Fitting time: %5.4f sec. RMS error: %f4" %(fit_time, error))
# create a strain/dw backup (for shift)
old_strain = np.copy(strain)
old_dw = np.copy(dw)
fit.on_click(on_fit_clicked)
#observe the cancel button
def on_cancel_clicked(b):
global cst, sp, dwp, control_sp_x, control_sp_y, control_dwp_x, control_dwp_y, old_strain, old_dw
dl_xrd.value = ""
dl_sdw.value = ""
cst = compute_cst(w_list,th)
sp, dwp = np.copy(sp_back), np.copy(dwp_back)
with out:
print("Fit cancelled.")
# update strain and DW
strain = f_strain(cst["z"],sp,cst["t"],cst["sdw_model"])
dw = f_DW(cst["z"],dwp,cst["t"],cst["sdw_model"])
# update the values of strain and DW at the control point coords (in1d, around)
control_sp_x, control_sp_y = control_sp(cst["z"],sp,cst["t"],cst["sdw_model"])
control_dwp_x, control_dwp_y = control_dwp(cst["z"],dwp,cst["t"],cst["sdw_model"])
# update the plots
with strain_line.hold_sync():
strain_line.x = (cst["t"]-cst["z"])/10
strain_line.y = strain*100
with strain_scat.hold_sync():
strain_scat.x = (cst["t"] - control_sp_x)/10
strain_scat.y = control_sp_y*100
with dw_line.hold_sync():
dw_line.x = (cst["t"]-cst["z"])/10
dw_line.y = dw
with dw_scat.hold_sync():
dw_scat.x = (cst["t"] - control_dwp_x)/10
dw_scat.y = control_dwp_y
# update XRD
ical = f_Refl(th, np.concatenate((sp,dwp)), cst)[0]
xrd_line.y = np.log10(ical)
# create a strain/dw backup (for shift)
old_strain = np.copy(strain)
old_dw = np.copy(dw)
cancel.on_click(on_cancel_clicked)