Skip to content
Snippets Groups Projects

Improved BOZ analysis

Merged Loïc Le Guyader requested to merge boz-improved into master
Files
8
%% Cell type:code id: tags:
``` python
import numpy as np
%matplotlib notebook
import matplotlib.pyplot as plt
plt.rcParams['figure.constrained_layout.use'] = True
import dask
print(f'dask: {dask.__version__}')
from psutil import virtual_memory
mem = virtual_memory()
print(f'Physical memory: {mem.total/1024/1024/1024:.0f} Gb') # total physical memory available
import logging
logging.basicConfig(filename='example.log', level=logging.DEBUG)
import toolbox_scs as tb
print(tb.__file__)
import toolbox_scs.routines.boz as boz
%% Cell type:markdown id: tags:
# Create parameters object
%% Cell type:code id: tags:parameters
``` python
proposal = 2937
darkrun = 478
run = 477
module = 15
gain = 0.5
sat_level = 500
rois_th = 1
%% Cell type:code id: tags:
``` python
params = boz.parameters(proposal=proposal, darkrun=darkrun, run=run, module=module, gain=gain)
%% Cell type:code id: tags:
``` python
from extra_data.read_machinery import find_proposal
root = find_proposal(f'p{proposal:06d}')
path = root + '/usr/processed_runs/' + f'r{params.run}/'
print(path)
%% Cell type:code id: tags:
``` python
print(params)
%% Cell type:markdown id: tags:
### load data persistently
%% Cell type:code id: tags:
``` python
params.dask_load()
%% Cell type:markdown id: tags:
# Dark run inspection
%% Cell type:markdown id: tags:
The aim is to check dark level and extract bad pixel map.
%% Cell type:code id: tags:
``` python
dark = boz.average_module(params.arr_dark).compute()
%% Cell type:code id: tags:
``` python
pedestal = np.mean(dark)
pedestal
%% Cell type:code id: tags:
``` python
mean_th = (pedestal-25, pedestal+30)
f = boz.inspect_dark(params.arr_dark, mean_th=mean_th)
f.savefig(path+f'p{params.proposal}-r{params.run}-d{params.darkrun}-inspect-dark.png', dpi=300)
%% Cell type:code id: tags:
``` python
params.mean_th = mean_th
params.set_mask(boz.bad_pixel_map(params))
%% Cell type:code id: tags:
``` python
print(params)
%% Cell type:markdown id: tags:
# Histogram
%% Cell type:code id: tags:
``` python
h, f = boz.inspect_histogram(params.arr,
params.arr_dark,
mask=params.get_mask() #, extra_lines=True
)
f.suptitle(f'p:{params.proposal} r:{params.run} d:{params.darkrun}')
f.savefig(path+f'p{params.proposal}-r{params.run}-d{params.darkrun}-histogram.png', dpi=300)
%% Cell type:markdown id: tags:
adding guide to the eye
%% Cell type:markdown id: tags:
# ROIs extraction
%% Cell type:code id: tags:
``` python
params.rois_th = rois_th
params.rois = boz.find_rois_from_params(params)
%% Cell type:code id: tags:
``` python
print(params)
%% Cell type:code id: tags:
``` python
data = boz.average_module(params.arr, dark=dark).compute()
dataM = data.mean(axis=0) # mean over pulseId
%% Cell type:code id: tags:
``` python
f = boz.inspect_rois(dataM, params.rois, params.rois_th)
f.savefig(path+f'p{params.proposal}-r{params.run}-d{params.darkrun}-rois.png', dpi=300)
%% Cell type:markdown id: tags:
# Flat field extraction
%% Cell type:markdown id: tags:
The first step is to compute a good average image, this mean remove saturated shots and ignoring bad pixels
%% Cell type:code id: tags:
``` python
params.sat_level = sat_level
res = boz.average_module(params.arr, dark=dark,
ret='mean', mask=params.get_mask(), sat_roi=params.rois['sat'],
sat_level=params.sat_level)
avg = res.compute().mean(axis=0)
%% Cell type:markdown id: tags:
The second step is from that good average image to fit the plane field on n/0 and p/0 rois. We have to make sure that the rois have same width.
%% Cell type:code id: tags:
``` python
f = boz.inspect_plane_fitting(avg, params.rois)
%% Cell type:code id: tags:
``` python
f.savefig(path+f'p{params.proposal}-r{params.run}-d{params.darkrun}-inspect-noflatfield.png', dpi=300)
%% Cell type:markdown id: tags:
fit the plane field correction
%% Cell type:code id: tags:
``` python
plane = boz.plane_fitting(params)
%% Cell type:code id: tags:
``` python
plane
%% Cell type:markdown id: tags:
compute the correction and inspect the result of its application
%% Cell type:code id: tags:
``` python
params.set_flat_field(plane.x)
ff = boz.compute_flat_field_correction(params.rois, params.get_flat_field())
%% Cell type:code id: tags:
``` python
f = boz.inspect_plane_fitting(avg/ff, params.rois)
%% Cell type:code id: tags:
``` python
f.savefig(path+f'p{params.proposal}-r{params.run}-d{params.darkrun}-inspect-withflatfield.png', dpi=300)
%% Cell type:markdown id: tags:
# Non-linearities correction extraction
%% Cell type:markdown id: tags:
To speed up online analysis, we save the corrections with a dummy non-linearity correction. The saved file can be used for online analysis as soon as it appears.
%% Cell type:code id: tags:
``` python
params.set_Fnl(np.arange(2**9))
params.save(path=path)
%% Cell type:code id: tags:
``` python
N = 80
domain = boz.nl_domain(N, 40, 511)
params.alpha = 0.5
params.max_iter = 25
%% Cell type:markdown id: tags:
## minimizing
%% Cell type:code id: tags:
``` python
res, fit_res = boz.nl_fit(params, domain)
%% Cell type:code id: tags:
``` python
params.set_Fnl(boz.nl_lut(domain, res.x))
%% Cell type:code id: tags:
``` python
print(params)
%% Cell type:code id: tags:
``` python
f = boz.inspect_correction(params, gain=params.gain)
%% Cell type:code id: tags:
``` python
f.savefig(path+f'p{params.proposal}-r{params.run}-d{params.darkrun}-inspect-correction.png', dpi=300)
%% Cell type:markdown id: tags:
### plotting the fitted correction
%% Cell type:code id: tags:
``` python
f = boz.inspect_Fnl(params.get_Fnl())
%% Cell type:code id: tags:
``` python
f.savefig(path+f'p{params.proposal}-r{params.run}-d{params.darkrun}-inspect-Fnl.png', dpi=300)
%% Cell type:markdown id: tags:
### plotting the fit progresion
%% Cell type:code id: tags:
``` python
f = boz.inspect_nl_fit(fit_res)
%% Cell type:code id: tags:
``` python
f.savefig(path+f'p{params.proposal}-r{params.run}-d{params.darkrun}-inspect-nl-fit.png', dpi=300)
%% Cell type:markdown id: tags:
# Save the analysis parameters
%% Cell type:code id: tags:
``` python
params.save(path=path)
Loading