update find_offset()

This commit is contained in:
2022-10-31 22:04:26 +00:00
parent 3f02a384fb
commit 3c435f6f9b

View File

@@ -142,29 +142,51 @@ def process_run(run_number, rois,detector='JF16T03V01', calculate =None, only_sh
Parallel(n_jobs=n_jobs,verbose=10)(delayed(process_step)(i) for i in range(len(scan)))
def find_offset(run_number, ch1,ch2, offsets=range(-7,7),step=0,verbose = False):
"""Attempts to find an ideal offset between pids of two channels. Offset is added to channel_2.
def find_offset(run_no_or_scan, ch1,ch2, offsets=range(-7,7),step=0,verbose = False, plot = False):
"""Attempts to find an ideal offset between pids of two channels. Offset is added to Channel_2. Channel_1 is used as reference.
Returns best offset, best_correlation, [all offsets, all correlations]."""
scan = scan_info(run_number)
scan = scan_from_run_number_or_scan(run_no_or_scan)
run_number = int(str(scan.fs)[-19:-15])
d = scan[step]
assert len(d[ch1].shape) ==1, "Channel 2 has more than 1 dimension, can't correlate"
assert len(d[ch2].shape) ==1, "Channel 2 has more than 1 dimension, can't correlate"
corrs = []
corrs,xs,ys = [],[],[]
for offset in offsets:
scan = scan_info(run_number)
scan = scan_from_run_number_or_scan(run_no_or_scan)
d = scan[step]
d[ch2].offset = offset
subset = d[ch1,ch2]
subset.drop_missing()
x = subset[ch1].data
y = subset[ch2].data
xs.append(x)
ys.append(y)
corrs.append(pearsonr(x,y)[0])
best_offset = offsets[np.argmax(corrs)]
best_corr = np.max(corrs)
if verbose:
print(f'Best correlation for the offset: {best_offset}')
print(f'Best correlation value: {best_corr:.2f}')
return [best_offset,best_corr,[offsets,corrs]]
if plot:
fig,ax = plt.subplots(1,2,figsize=(12,4))
for i,offset in enumerate(offsets):
if offset == best_offset:
col = 'tab:red'
alpha = 1
else:
col = 'tab:grey'
alpha =0.2
ax[0].scatter(xs[i],ys[i],color=col,alpha = alpha )
fig.suptitle(f'Run {run_number}, Best offset {best_offset}, Correlation: {best_corr:.3f}')
ax[0].set_xlabel(ch1)
ax[0].set_ylabel(ch2)
ax[1].plot(offsets,corrs,'-o',color='tab:blue')
ax[1].set_xlabel('Offset')
ax[1].set_ylabel('Correlation')
ax[1].plot(best_offset,best_corr,'o',markersize=13,color='tab:red',mfc='none')
return best_offset,best_corr,[offsets,corrs]
class ROI:
"""Definition of region of interest (ROI) in image coordinates.