Skip to content
Snippets Groups Projects

[EPIX][CORR] Optimize histograms and plots

Merged Nuno Duarte requested to merge feat/epix_CorrectionNB_plots into master
1 file
+ 181
227
Compare changes
  • Side-by-side
  • Inline
``` python
``` python
in_folder = "/gpfs/exfel/exp/HED/202202/p003121/raw" # input folder, required
in_folder = "/gpfs/exfel/exp/MID/202301/p003346/raw" # input folder, required
out_folder = "" # output folder, required
out_folder = "" # output folder, required
metadata_folder = "" # Directory containing calibration_metadata.yml when run by xfel-calibrate
metadata_folder = "" # Directory containing calibration_metadata.yml when run by xfel-calibrate
sequences = [-1] # sequences to correct, set to -1 for all, range allowed
sequences = [-1] # sequences to correct, set to -1 for all, range allowed
sequences_per_node = 1 # number of sequence files per cluster node if run as slurm job, set to 0 to not run SLURM parallel
sequences_per_node = 1 # number of sequence files per cluster node if run as slurm job, set to 0 to not run SLURM parallel
run = 156 # which run to read data from, required
run = 55 # which run to read data from, required
# Parameters for accessing the raw data.
# Parameters for accessing the raw data.
karabo_id = "HED_IA1_EPX100-1" # karabo karabo_id
karabo_id = "MID_EXP_EPIX-1" # karabo karabo_id
karabo_da = "EPIX01" # data aggregators
karabo_da = "EPIX01" # data aggregators
db_module = "" # module id in the database
db_module = "" # module id in the database
receiver_template = "RECEIVER" # detector receiver template for accessing raw data files
receiver_template = "RECEIVER" # detector receiver template for accessing raw data files
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
from IPython.display import Latex, Markdown, display
from IPython.display import Latex, Markdown, display
from extra_data import RunDirectory, H5File
from extra_data import RunDirectory, H5File
 
from extra_geom import Epix100Geometry
 
from mpl_toolkits.axes_grid1 import make_axes_locatable
from pathlib import Path
from pathlib import Path
import cal_tools.restful_config as rest_cfg
import cal_tools.restful_config as rest_cfg
from XFELDetAna import xfelpyanatools as xana
from XFELDetAna import xfelpycaltools as xcal
from XFELDetAna import xfelpycaltools as xcal
from cal_tools.calcat_interface import EPIX100_CalibrationData
from cal_tools.calcat_interface import EPIX100_CalibrationData
from cal_tools.epix100 import epix100lib
from cal_tools.epix100 import epix100lib
from cal_tools.files import DataFile
from cal_tools.files import DataFile
 
from cal_tools.restful_config import restful_config
from cal_tools.tools import (
from cal_tools.tools import (
calcat_creation_time,
calcat_creation_time,
write_constants_fragment,
write_constants_fragment,
prettyPlotting = True
prettyPlotting = True
 
%matplotlib inline
%matplotlib inline
``` python
``` python
 
prop_str = in_folder[in_folder.find('/p')+1:in_folder.find('/p')+8]
 
in_folder = Path(in_folder)
in_folder = Path(in_folder)
out_folder = Path(out_folder)
out_folder = Path(out_folder)
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Retrieving calibration constants
## Retrieving calibration constants
As a first step, calibration constants have to be loaded.
As a first step, dark maps have to be loaded.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
# Initializing some parameters.
# Initializing some parameters.
hscale = 1
hscale = 1
stats = True
stats = True
hrange = np.array([-50, 1000])
bins = np.arange(-50,1000)
nbins = hrange[1] - hrange[0]
hist = {'O': 0} # dictionary to store histograms
commonModeBlockSize = [x//2, y//2]
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
histCalOffsetCor = xcal.HistogramCalculator(
if common_mode:
sensorSize,
bins=nbins,
range=hrange,
parallel=run_parallel,
nCells=memoryCells,
blockSize=blockSize
)
# *****************Histogram Calculators****************** #
histCalCor = xcal.HistogramCalculator(
sensorSize,
bins=1050,
range=[-50, 1000],
parallel=run_parallel,
nCells=memoryCells,
blockSize=blockSize
)
%% Cell type:code id: tags:
commonModeBlockSize = [x//2, y//8]
``` python
if common_mode:
histCalCMCor = xcal.HistogramCalculator(
sensorSize,
bins=nbins,
range=hrange,
parallel=run_parallel,
nCells=memoryCells,
blockSize=blockSize,
)
cmCorrectionB = xcal.CommonModeCorrection(
cmCorrectionB = xcal.CommonModeCorrection(
shape=sensorSize,
shape=sensorSize,
blockSize=commonModeBlockSize,
blockSize=commonModeBlockSize,
stats=stats,
stats=stats,
minFrac=cm_min_frac,
minFrac=cm_min_frac,
noiseSigma=cm_noise_sigma,
noiseSigma=cm_noise_sigma,
)
)
 
 
hist['CM'] = 0
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
if relative_gain:
if relative_gain:
gain_cnst = np.median(const_data["RelativeGainEPix100"])
hscale = gain_cnst
# gain constant is given by the mode of the gain map
plot_unit = 'keV'
# because all bad pixels are masked using this value
if photon_energy > 0:
_vals,_counts = np.unique(const_data["RelativeGainEPix100"], return_counts=True)
plot_unit = '$\gamma$'
gain_cnst = _vals[np.argmax(_counts)]
hscale /= photon_energy
gainCorrection = xcal.RelativeGainCorrection(
gainCorrection = xcal.RelativeGainCorrection(
sensorSize,
sensorSize,
blockSize=blockSize,
blockSize=blockSize,
gains=None,
gains=None,
)
)
histCalRelGainCor = xcal.HistogramCalculator(
hist['RG'] = 0
sensorSize,
bins=nbins,
range=hrange,
parallel=run_parallel,
nCells=memoryCells,
blockSize=blockSize
)
if absolute_gain:
if absolute_gain:
histCalAbsGainCor = xcal.HistogramCalculator(
hscale = gain_cnst
sensorSize,
plot_unit = 'keV'
bins=nbins,
if photon_energy > 0:
range=hrange*hscale,
plot_unit = '$\gamma$'
parallel=run_parallel,
hscale /= photon_energy
nCells=memoryCells,
hist['AG'] = 0
blockSize=blockSize
)
%% Cell type:code id: tags:
%% Cell type:code id: tags:
blockSize=[x, y],
blockSize=[x, y],
parallel=run_parallel,
parallel=run_parallel,
)
)
histCalCSCor = xcal.HistogramCalculator(
hist['CS'] = 0
sensorSize,
hist['S'] = 0
bins=nbins,
range=hrange,
parallel=run_parallel,
nCells=memoryCells,
blockSize=blockSize,
)
histCalGainCorClusters = xcal.HistogramCalculator(
sensorSize,
bins=nbins,
range=hrange*hscale,
parallel=run_parallel,
nCells=memoryCells,
blockSize=blockSize
)
histCalGainCorSingles = xcal.HistogramCalculator(
sensorSize,
bins=nbins,
range=hrange*hscale,
parallel=run_parallel,
nCells=memoryCells,
blockSize=blockSize
)
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
d = d[..., np.newaxis].astype(np.float32)
d = d[..., np.newaxis].astype(np.float32)
d = np.compress(
d = np.compress(
np.any(d > 0, axis=(0, 1)), d, axis=2)
np.any(d > 0, axis=(0, 1)), d, axis=2)
# Offset correction.
# Offset correction.
d -= const_data["OffsetEPix100"]
d -= const_data["OffsetEPix100"]
 
hist['O'] += np.histogram(d,bins=bins)[0]
histCalOffsetCor.fill(d)
# Common Mode correction.
# Common Mode correction.
if common_mode:
if common_mode:
# Block CM
# Block CM
d = cmCorrectionR.correct(d)
d = cmCorrectionR.correct(d)
# COL CM
# COL CM
d = cmCorrectionC.correct(d)
d = cmCorrectionC.correct(d)
histCalCMCor.fill(d)
# relative gain correction.
hist['CM'] += np.histogram(d,bins=bins)[0]
 
 
 
# Relative gain correction.
if relative_gain:
if relative_gain:
d = gainCorrection.correct(d)
d = gainCorrection.correct(d)
histCalRelGainCor.fill(d)
 
hist['RG'] += np.histogram(d,bins=bins)[0]
"""The gain correction is currently applying
"""The gain correction is currently applying
an absolute correction (not a relative correction
an absolute correction (not a relative correction
data_clu[index, ...] = np.squeeze(d_clu)
data_clu[index, ...] = np.squeeze(d_clu)
data_patterns[index, ...] = np.squeeze(patterns)
data_patterns[index, ...] = np.squeeze(patterns)
histCalCSCor.fill(d_clu)
hist['CS'] += np.histogram(d_clu,bins=bins)[0]
# absolute gain correction
d_sing = d_clu[patterns==100] # pattern 100 corresponds to single photons events
 
if len(d_sing):
 
hist['S'] += np.histogram(d_sing,bins=bins)[0]
 
 
# Absolute gain correction
# changes data from ADU to keV (or n. of photons)
# changes data from ADU to keV (or n. of photons)
if absolute_gain:
if absolute_gain:
d = d * gain_cnst
d = d * gain_cnst
if photon_energy > 0:
if photon_energy > 0:
d /= photon_energy
d /= photon_energy
histCalAbsGainCor.fill(d)
 
hist['AG'] += np.histogram(d,bins=bins)[0]
if pattern_classification:
if pattern_classification:
# Modify pattern classification.
# Modify pattern classification.
d_clu = d_clu * gain_cnst
d_clu = d_clu * gain_cnst
if photon_energy > 0:
if photon_energy > 0:
d_clu /= photon_energy
d_clu /= photon_energy
data_clu[index, ...] = np.squeeze(d_clu)
data_clu[index, ...] = np.squeeze(d_clu)
histCalGainCorClusters.fill(d_clu)
d_sing = d_clu[patterns==100] # pattern 100 corresponds to single photons events
if len(d_sing):
histCalGainCorSingles.fill(d_sing)
data[index, ...] = np.squeeze(d)
data[index, ...] = np.squeeze(d)
histCalCor.fill(d)
%% Cell type:code id: tags:
%% Cell type:code id: tags:
exit(0)
exit(0)
 
%% Cell type:markdown id: tags:
 
## Plot Histograms
 
%% Cell type:code id: tags:
 
``` python
 
bins_ADU = bins[:-1]+np.diff(bins)[0]/2
 
bins_keV = bins_ADU*hscale
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
ho, eo, co, so = histCalCor.get()
# Histogram in ADU
d = [{
plt.figure(figsize=(12,8))
'x': co,
plt.plot(bins_ADU,hist['O'], label='Offset corr')
'y': ho,
'y_err': np.sqrt(ho[:]),
'drawstyle': 'steps-mid',
'errorstyle': 'bars',
'errorcoarsing': 2,
'label': 'Total corr.'
}]
ho, eo, co, so = histCalOffsetCor.get()
d.append({
'x': co,
'y': ho,
'y_err': np.sqrt(ho[:]),
'drawstyle': 'steps-mid',
'errorstyle': 'bars',
'errorcoarsing': 2,
'label': 'Offset corr.'
})
if common_mode:
if common_mode:
ho, eo, co, so = histCalCMCor.get()
plt.plot(bins_ADU,hist['CM'], label='CM corr')
d.append({
if relative_gain:
'x': co,
plt.plot(bins_ADU,hist['RG'], label='Relative Gain corr')
'y': ho,
'y_err': np.sqrt(ho[:]),
'drawstyle': 'steps-mid',
'errorstyle': 'bars',
'errorcoarsing': 2,
'label': 'CM corr.'
})
if relative_gain :
ho, eo, co, so = histCalRelGainCor.get()
d.append({
'x': co,
'y': ho,
'y_err': np.sqrt(ho[:]),
'drawstyle': 'steps-mid',
'errorstyle': 'bars',
'errorcoarsing': 2,
'label': 'Relative gain corr.'
})
if pattern_classification:
if pattern_classification:
ho, eo, co, so = histCalCSCor.get()
plt.plot(bins_ADU[bins_ADU>10],hist['CS'][bins_ADU>10], label='Charge Sharing corr')
d.append({
if np.any(hist['S']):
'x': co,
plt.plot(bins_ADU,hist['S'], label='Singles')
'y': ho,
'y_err': np.sqrt(ho[:]),
xtick_step = 50
'drawstyle': 'steps-mid',
plt.xlim(bins[0], bins[-1]+1)
'errorstyle': 'bars',
plt.xticks(np.arange(bins[0],bins[-1]+2,xtick_step))
'errorcoarsing': 2,
plt.xlabel('ADU',fontsize=12)
'label': 'Charge sharing corr.'
})
plt.yscale('log')
plt.title(f'{karabo_id} | {prop_str}, r{run}', fontsize=14, fontweight='bold')
fig = xana.simplePlot(
plt.legend(fontsize=12)
d, aspect=1, x_label=f'Energy (ADU)',
plt.grid(ls=':')
y_label='Number of occurrences', figsize='2col',
y_log=True, x_range=(-50, 500),
legend='top-center-frame-2col',
)
plt.title(f'run {run} - {karabo_da}')
plt.grid()
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
if absolute_gain :
# Histogram in keV/number of photons
d = []
ho, eo, co, so = histCalAbsGainCor.get()
if absolute_gain:
if co is not None: # avoid adding None array, if calculator is empty.
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
d.append({
plt.figure(figsize=(12,8))
'x': co,
'y': ho,
if relative_gain:
'y_err': np.sqrt(ho[:]),
plt.plot(bins_keV,hist['RG'], label='Absolute Gain corr', c=colors[2])
'drawstyle': 'steps-mid',
'errorstyle': 'bars',
'errorcoarsing': 2,
'label': 'Absolute gain corr.'
})
if pattern_classification:
if pattern_classification:
ho, eo, co, so = histCalGainCorClusters.get()
plt.plot(bins_keV[bins_keV>.5],hist['CS'][bins_keV>.5], label='Charge Sharing corr', c=colors[3])
if co is not None: # avoid adding None array, if calculator is empty.
if np.any(hist['S']):
d.append({
plt.plot(bins_keV[bins_keV>.5],hist['S'][bins_keV>.5], label='Singles', c=colors[4])
'x': co,
'y': ho,
if photon_energy==0: # if keV instead of #photons
'y_err': np.sqrt(ho[:]),
xtick_step = 5
'drawstyle': 'steps-mid',
plt.xlim(left=-2)
'errorstyle': 'bars',
plt.xticks(np.arange(0,plt.gca().get_xlim()[1],xtick_step))
'errorcoarsing': 2,
plt.xlabel(plot_unit,fontsize=12)
'label': 'Charge sharing corr.'
})
plt.yscale('log')
plt.title(f'{karabo_id} | {prop_str}, r{run}', fontsize=14, fontweight='bold')
ho, eo, co, so = histCalGainCorSingles.get()
plt.legend(fontsize=12)
if co is not None: # avoid adding None array, if calculator is empty.
plt.grid(ls=':')
d.append({
'x': co,
'y': ho,
'y_err': np.sqrt(ho[:]),
'drawstyle': 'steps-mid',
'errorstyle': 'bars',
'errorcoarsing': 2,
'label': 'Isolated photons (singles)'
})
fig = xana.simplePlot(
d, aspect=1, x_label=f'Energy ({plot_unit})',
y_label='Number of occurrences', figsize='2col',
y_log=True,
x_range=np.array((-50, 500))*hscale,
legend='top-center-frame-2col',
)
plt.grid()
plt.title(f'run {run} - {karabo_da}')
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
``` python
``` python
step_timer.start()
geom = Epix100Geometry.from_relative_positions(top=[386.5, 364.5, 0.], bottom=[386.5, -12.5, 0.])
fig = xana.heatmapPlot(
np.nanmedian(data, axis=0),
if pattern_classification:
x_label='Columns', y_label='Rows',
plt.subplots(1,2,figsize=(18,18)) if pattern_classification else plt.subplots(1,1,figsize=(9,9))
lut_label=f'Signal ({plot_unit})',
ax = plt.subplot(1,2,1)
x_range=(0, y),
ax.set_title(f'Before CS correction',fontsize=12,fontweight='bold');
y_range=(0, x),
else:
vmin=-50, vmax=50)
plt.subplots(1,1,figsize=(9,9))
step_timer.done_step(f'Plotting mean image of {data.shape[0]} trains.')
ax = plt.subplot(1,1,1)
 
ax.set_title(f'{karabo_id} | {prop_str}, r{run} | Average of {data.shape[0]} trains',fontsize=12,fontweight='bold');
 
 
# Average image before charge sharing corrcetion
 
divider = make_axes_locatable(ax)
 
cax = divider.append_axes('bottom', size='5%', pad=0.5)
 
 
image = data.mean(axis=0)
 
vmin = max(image.mean()-2*image.std(),0)
 
vmax = image.mean()+3*image.std()
 
geom.plot_data(image,
 
ax=ax,
 
colorbar={'cax': cax, 'label': plot_unit, 'orientation': 'horizontal'},
 
origin='upper',
 
vmin=vmin,
 
vmax=vmax)
 
 
# Average image after charge sharing corrcetion
 
if pattern_classification:
 
 
ax = plt.subplot(1,2,2)
 
divider = make_axes_locatable(ax)
 
cax = divider.append_axes('bottom', size='5%', pad=0.5)
 
 
image = data_clu.mean(axis=0)
 
geom.plot_data(image,
 
ax=ax,
 
colorbar={'cax': cax, 'label': plot_unit, 'orientation': 'horizontal'},
 
origin='upper',
 
vmin=vmin,
 
vmax=vmax)
 
ax.set_title(f'After CS correction',fontsize=12,fontweight='bold');
 
 
plt.suptitle(f'{karabo_id} | {prop_str}, r{run} | Average of {data.shape[0]} trains',fontsize=14,fontweight='bold',y=.72);
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
``` python
``` python
step_timer.start()
train_idx = -1
fig = xana.heatmapPlot(
data[0, ...],
if pattern_classification:
x_label='Columns', y_label='Rows',
plt.subplots(1,2,figsize=(18,18)) if pattern_classification else plt.subplots(1,1,figsize=(9,9))
lut_label=f'Signal ({plot_unit})',
ax = plt.subplot(1,2,1)
x_range=(0, y),
ax.set_title(f'Before CS correction',fontsize=12,fontweight='bold');
y_range=(0, x),
else:
vmin=-50, vmax=50)
plt.subplots(1,1,figsize=(9,9))
step_timer.done_step(f'Plotting single shot of corrected data.')
ax = plt.subplot(1,1,1)
 
ax.set_title(f'{karabo_id} | {prop_str}, r{run} | Single frame',fontsize=12,fontweight='bold');
 
 
# Average image before charge sharing corrcetion
 
divider = make_axes_locatable(ax)
 
cax = divider.append_axes('bottom', size='5%', pad=0.5)
 
 
image = data[train_idx]
 
vmin = max(image.mean()-2*image.std(),0)
 
vmax = image.mean()+3*image.std()
 
geom.plot_data(image,
 
ax=ax,
 
colorbar={'cax': cax, 'label': plot_unit, 'orientation': 'horizontal'},
 
origin='upper',
 
vmin=vmin,
 
vmax=vmax)
 
 
# Average image after charge sharing corrcetion
 
if pattern_classification:
 
 
ax = plt.subplot(1,2,2)
 
divider = make_axes_locatable(ax)
 
cax = divider.append_axes('bottom', size='5%', pad=0.5)
 
 
image = data_clu[train_idx]
 
geom.plot_data(image,
 
ax=ax,
 
colorbar={'cax': cax, 'label': plot_unit, 'orientation': 'horizontal'},
 
origin='upper',
 
vmin=vmin,
 
vmax=vmax)
 
ax.set_title(f'After CS correction',fontsize=12,fontweight='bold');
 
 
plt.suptitle(f'{karabo_id} | {prop_str}, r{run} | Single frame',fontsize=14,fontweight='bold',y=.72);
Loading