Skip to content
Snippets Groups Projects
Commit 295f706d authored by Egor Sobolev's avatar Egor Sobolev
Browse files

Fix missed param for nomrmalization of SPI hitscores on pulse energy

parent c37e18ec
No related branches found
No related tags found
1 merge request!1125[AGIPD][CORRECT][SPI] Fix missed param
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Summary of the AGIPD offline correction # # Summary of the AGIPD offline correction #
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
run = 11 # runs to process, required run = 11 # runs to process, required
in_folder = "/gpfs/exfel/exp/MID/202201/p002834/raw" # the folder to read data from, required in_folder = "/gpfs/exfel/exp/MID/202201/p002834/raw" # the folder to read data from, required
out_folder = "/gpfs/exfel/data/scratch/ahmedk/test/AGIPD_Corr" # path to output to, required out_folder = "/gpfs/exfel/data/scratch/ahmedk/test/AGIPD_Corr" # path to output to, 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
karabo_id = "SPB_DET_AGIPD1M-1" # karabo karabo_id karabo_id = "SPB_DET_AGIPD1M-1" # karabo karabo_id
modules = [-1] modules = [-1]
karabo_da = ['-1'] # a list of data aggregators names, Default [-1] for selecting all data aggregators karabo_da = ['-1'] # a list of data aggregators names, Default [-1] for selecting all data aggregators
rel_gain_mode = "off" # Select relative gain correction. Choices [`PC`, `CS`, `off`]. (`PC`: Pulse Capacitor, `CS`: Current Source, `off`: Disable relative gain correction). Default: off. rel_gain_mode = "off" # Select relative gain correction. Choices [`PC`, `CS`, `off`]. (`PC`: Pulse Capacitor, `CS`: Current Source, `off`: Disable relative gain correction). Default: off.
# Additional processing # Additional processing
count_lit_pixels = False # Count the number of pixels registering photons count_lit_pixels = False # Count the number of pixels registering photons
spi_hitfinding = False # Find hits using lit-pixel counter spi_hitfinding = False # Find hits using lit-pixel counter
# SPI hit-finder parameters # SPI hit-finder parameters
spi_hf_modules = [3, 4, 8, 15] # Use specified modules for SPI hitfinding spi_hf_modules = [3, 4, 8, 15] # Use specified modules for SPI hitfinding
spi_hf_mode = "adaptive" # The method to compute threshold for hitscores in SPI hitfinding: `fixed` or `adaptive` spi_hf_mode = "adaptive" # The method to compute threshold for hitscores in SPI hitfinding: `fixed` or `adaptive`
spi_hf_snr = 4.0 # Siginal-to-noise ration for adaptive threshold in SPI hitfinding spi_hf_snr = 4.0 # Siginal-to-noise ration for adaptive threshold in SPI hitfinding
spi_hf_min_scores = 100 # The minimal size of events to compute adaptive threshold in SPI hitfinding spi_hf_min_scores = 100 # The minimal size of events to compute adaptive threshold in SPI hitfinding
spi_hf_fixed_threshold = 0 # The fixed threshold value spi_hf_fixed_threshold = 0 # The fixed threshold value
spi_hf_hitrate_window_size = 200 # The window size for runnig average of hitrate in trains spi_hf_hitrate_window_size = 200 # The window size for runnig average of hitrate in trains
spi_hf_xgm_norm = False # Use XGM pulse energy for hitscore normalization spi_hf_xgm_norm = False # Use XGM pulse energy for hitscore normalization
spi_hf_miss_fraction = 1 # The fraction of misses to select along with hits spi_hf_miss_fraction = 1 # The fraction of misses to select along with hits
spi_hf_miss_fraction_base = "hit" # The base to compute the number of misses to select: the number of hits (`hit`) or misses (`miss`) spi_hf_miss_fraction_base = "hit" # The base to compute the number of misses to select: the number of hits (`hit`) or misses (`miss`)
# Lit-frame finder # Lit-frame finder
use_litframe_finder = 'off' # Process only illuminated frames: 'off' - disable, 'device' - use online device data, 'offline' - use offline algorithm, 'auto' - choose online/offline source automatically (default) use_litframe_finder = 'off' # Process only illuminated frames: 'off' - disable, 'device' - use online device data, 'offline' - use offline algorithm, 'auto' - choose online/offline source automatically (default)
litframe_device_id = '' # Device ID for a lit frame finder device, empty string to auto detection litframe_device_id = '' # Device ID for a lit frame finder device, empty string to auto detection
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from pathlib import Path from pathlib import Path
from logging import warning from logging import warning
import yaml import yaml
import tabulate import tabulate
from cal_tools.tools import CalibrationMetadata from cal_tools.tools import CalibrationMetadata
from IPython.display import Latex, display, Markdown from IPython.display import Latex, display, Markdown
import matplotlib import matplotlib
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
matplotlib.use("agg") matplotlib.use("agg")
%matplotlib inline %matplotlib inline
from extra_data import RunDirectory from extra_data import RunDirectory
from extra_redu.fileutils import StackedPulseSource, exdf_save from extra_redu.fileutils import StackedPulseSource, exdf_save
from extra_redu.spi import SpiHitfinder from extra_redu.spi import SpiHitfinder
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
out_folder = Path(out_folder) out_folder = Path(out_folder)
metadata = CalibrationMetadata(metadata_folder or out_folder) metadata = CalibrationMetadata(metadata_folder or out_folder)
const_dict = metadata.setdefault("retrieved-constants", {}) const_dict = metadata.setdefault("retrieved-constants", {})
time_dict = const_dict.setdefault("time-summary", {}) time_dict = const_dict.setdefault("time-summary", {})
# Extracting Instrument string # Extracting Instrument string
instrument = karabo_id.split("_")[0] instrument = karabo_id.split("_")[0]
# Evaluate detector instance for mapping # Evaluate detector instance for mapping
if instrument == "SPB": if instrument == "SPB":
dinstance = "AGIPD1M1" dinstance = "AGIPD1M1"
nmods = 16 nmods = 16
elif instrument == "MID": elif instrument == "MID":
dinstance = "AGIPD1M2" dinstance = "AGIPD1M2"
nmods = 16 nmods = 16
elif instrument == "HED": elif instrument == "HED":
dinstance = "AGIPD500K" dinstance = "AGIPD500K"
nmods = 8 nmods = 8
if karabo_da[0] == '-1': if karabo_da[0] == '-1':
if modules[0] == -1: if modules[0] == -1:
modules = list(range(nmods)) modules = list(range(nmods))
karabo_da = ["AGIPD{:02d}".format(i) for i in modules] karabo_da = ["AGIPD{:02d}".format(i) for i in modules]
else: else:
modules = [int(x[-2:]) for x in karabo_da] modules = [int(x[-2:]) for x in karabo_da]
# This is needed only if AGIPD Correction notebook had no precorrection notebooks for retrieving constants # This is needed only if AGIPD Correction notebook had no precorrection notebooks for retrieving constants
# gather all generated sequence yml files for time summary of retrieved constant under retrieved-constants in metadata.yml # gather all generated sequence yml files for time summary of retrieved constant under retrieved-constants in metadata.yml
for fn in sorted(out_folder.glob("retrieved_constants_*.yml")): for fn in sorted(out_folder.glob("retrieved_constants_*.yml")):
with fn.open("r") as fd: with fn.open("r") as fd:
fdict = yaml.safe_load(fd) fdict = yaml.safe_load(fd)
# append different sequences' time summary to the main yaml # append different sequences' time summary to the main yaml
time_dict.update(fdict["time-summary"]) time_dict.update(fdict["time-summary"])
fn.unlink() fn.unlink()
metadata.save() metadata.save()
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def print_const_table(const): def print_const_table(const):
print(f"{const} constants were injected on:") print(f"{const} constants were injected on:")
table_data = {} table_data = {}
for seq, mod_data in time_dict.items(): for seq, mod_data in time_dict.items():
for mod, const_data in mod_data.items(): for mod, const_data in mod_data.items():
timestamp = const_data[const] timestamp = const_data[const]
table_data.setdefault(timestamp, []).append(f"{seq}:{mod}") table_data.setdefault(timestamp, []).append(f"{seq}:{mod}")
table = [] table = []
if not len(table_data): if not len(table_data):
table.append(["No constants retrieved"]) table.append(["No constants retrieved"])
elif len(table_data) == 1: elif len(table_data) == 1:
table.append([[*table_data][0], "All modules"]) table.append([[*table_data][0], "All modules"])
else: else:
for timestamp, seqmod in table_data.items(): for timestamp, seqmod in table_data.items():
table.append([timestamp, seqmod[0]]) table.append([timestamp, seqmod[0]])
for further_module in seqmod[1:]: for further_module in seqmod[1:]:
table.append(["", further_module]) table.append(["", further_module])
display(Latex(tabulate.tabulate(table, display(Latex(tabulate.tabulate(table,
tablefmt="latex", tablefmt="latex",
headers=["Timestamps", "Modules and sequences"]))) headers=["Timestamps", "Modules and sequences"])))
rel_gain_alias = "CS" if rel_gain_mode.lower() == "cs" else "PC" # 'off' or 'pc' rel_gain_alias = "CS" if rel_gain_mode.lower() == "cs" else "PC" # 'off' or 'pc'
for const in ['Offset', f'Slopes{rel_gain_alias}', 'SlopesFF']: for const in ['Offset', f'Slopes{rel_gain_alias}', 'SlopesFF']:
print_const_table(const) print_const_table(const)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
if spi_hitfinding and not count_lit_pixels: if spi_hitfinding and not count_lit_pixels:
# Operators are not expected to enable SPI hitfidnig without lit-pixels # Operators are not expected to enable SPI hitfidnig without lit-pixels
# counting. If this happens, warn on the mismatch in the configuration. # counting. If this happens, warn on the mismatch in the configuration.
warning("SPI hitfinding will be skipped because the required lit-pixel " warning("SPI hitfinding will be skipped because the required lit-pixel "
"counting is disabled. To run hitfinding, enable also lit-pixel " "counting is disabled. To run hitfinding, enable also lit-pixel "
"counting with the `--count-lit-pixels` option.") "counting with the `--count-lit-pixels` option.")
if spi_hitfinding and count_lit_pixels: if spi_hitfinding and count_lit_pixels:
display(Markdown("# SPI hit finding")) display(Markdown("# SPI hit finding"))
litfrm_src = None litfrm_src = None
if use_litframe_finder != 'off': if use_litframe_finder != 'off':
from extra_redu import make_litframe_finder, LitFrameFinderError from extra_redu import make_litframe_finder, LitFrameFinderError
from extra_redu.fileutils import PulseSource from extra_redu.fileutils import PulseSource
dc = RunDirectory(Path(in_folder) / f'r{run:04d}') dc = RunDirectory(Path(in_folder) / f'r{run:04d}')
litfrm = make_litframe_finder(karabo_id[:3], dc, litframe_device_id) litfrm = make_litframe_finder(karabo_id[:3], dc, litframe_device_id)
try: try:
get_data = { get_data = {
'auto': litfrm.read_or_process, 'auto': litfrm.read_or_process,
'offline': litfrm.process, 'offline': litfrm.process,
'online': litfrm.read 'online': litfrm.read
} }
r = get_data[use_litframe_finder]() r = get_data[use_litframe_finder]()
litfrm_src = PulseSource( litfrm_src = PulseSource(
r.meta.trainId, r.output.nFrame, r.meta.trainId, r.output.nFrame,
r.output.detectorPulseId.ravel(), r.output.detectorPulseId.ravel(),
{ {
"energyPerFrame": r.output.energyPerFrame.ravel(), "energyPerFrame": r.output.energyPerFrame.ravel(),
} }
) )
except LitFrameFinderError as err: except LitFrameFinderError as err:
pass pass
try: try:
dc = RunDirectory(out_folder) dc = RunDirectory(out_folder)
except FileNotFoundError: except FileNotFoundError:
warning("No corrected files found to plot.") warning("No corrected files found to plot.")
else: else:
litpx_src = StackedPulseSource.from_datacollection( litpx_src = StackedPulseSource.from_datacollection(
dc, f"{karabo_id}/CORR/(?P<key>\d+)CH0:output", "litpx") dc, f"{karabo_id}/CORR/(?P<key>\d+)CH0:output", "litpx")
hitfinder = SpiHitfinder( hitfinder = SpiHitfinder(
modules=spi_hf_modules, modules=spi_hf_modules,
mode=spi_hf_mode, mode=spi_hf_mode,
snr=spi_hf_snr, snr=spi_hf_snr,
min_scores=spi_hf_min_scores, min_scores=spi_hf_min_scores,
fixed_threshold=spi_hf_fixed_threshold, fixed_threshold=spi_hf_fixed_threshold,
hitrate_window_size=spi_hf_hitrate_window_size, hitrate_window_size=spi_hf_hitrate_window_size,
xgm_norm=spi_hf_xgm_norm,
miss_fraction=spi_hf_miss_fraction, miss_fraction=spi_hf_miss_fraction,
miss_fraction_base=spi_hf_miss_fraction_base, miss_fraction_base=spi_hf_miss_fraction_base,
) )
hitfinder.find_hits(litpx_src) hitfinder.find_hits(litpx_src, litfrm_src)
# write hit-finder data in file # write hit-finder data in file
sources = { sources = {
f"{karabo_id}/REDU/SPI_HITFINDER": hitfinder, f"{karabo_id}/REDU/SPI_HITFINDER": hitfinder,
} }
exdf_save(out_folder, "REDU00", run, sources, sequence_size=3500) exdf_save(out_folder, "REDU00", run, sources, sequence_size=3500)
# draw plots # draw plots
display(Markdown("## Hit-rate plot")) display(Markdown("## Hit-rate plot"))
hitfinder.plot_hitrate() hitfinder.plot_hitrate()
plt.show() plt.show()
display(Markdown("## Hitscore histogram")) display(Markdown("## Hitscore histogram"))
hitfinder.plot_hitscore_hist() hitfinder.plot_hitscore_hist()
plt.show() plt.show()
display(Markdown("## Hitscore plots")) display(Markdown("## Hitscore plots"))
hitfinder.plot_hitscore() hitfinder.plot_hitscore()
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment