Skip to content
Snippets Groups Projects

[AGIPD][CORRECT] Fixes for correcting AGIPD HIBEF data

Merged Karim Ahmed requested to merge feat/HIBEF_AGIPD_Correction into master
3 files
+ 95
53
Compare changes
  • Side-by-side
  • Inline
Files
3
from pathlib import Path
import os
from typing import Optional, Tuple
import h5py
@@ -264,15 +265,15 @@ class AgipdCorrections:
module_idx = int(file_name.split('/')[-1].split('-')[2][-2:])
agipd_base = self.h5_data_path.format(module_idx)
idx_base = self.h5_index_path.format(module_idx)
data_path = f'{agipd_base}/image'
data_path = os.path.join(agipd_base, 'image')
data_dict = self.shared_dict[i_proc]
try:
f = h5py.File(file_name, 'r')
group = f[data_path]
valid, first_index, last_index, valid_trains, valid_indices = \
self.get_valid_image_idx(idx_base, f)
valid, first_index, last_index, valid_trains, valid_indices, \
zero_count = self.get_valid_image_idx(idx_base, f)
firange = self.gen_valid_range(first_index, last_index,
self.max_cells, agipd_base, f,
valid_indices)
@@ -286,7 +287,15 @@ class AgipdCorrections:
data_dict['moduleIdx'][0] = module_idx
data_dict['nImg'][0] = n_img
raw_data = group['data'][firange]
# if zero_count is high reading performance through
# indexing will be slower compared to reading the whole
# image.
# 50 was chosen based on tested data with 100 zero_count
if zero_count > 50:
read_raw_data = group['data'][:]
raw_data = read_raw_data[firange]
else:
raw_data = group['data'][firange]
data_dict['data'][:n_img] = raw_data[:, 0]
data_dict['rawgain'][:n_img] = raw_data[:, 1]
@@ -598,0+607,0 @@
def get_valid_image_idx(self, idx_base, infile, index_v=2):
""" Return the indices of valid data
"""
zero_count = 0
if index_v == 2:
count = np.squeeze(infile[idx_base + "image/count"])
first = np.squeeze(infile[idx_base + "image/first"])
if np.count_nonzero(count != 0) == 0:
nonzero_count = np.count_nonzero(count != 0)
zero_count = count.shape[0] - nonzero_count
zero_indices = np.where(count == 0)[0]
if nonzero_count == 0:
raise IOError("File has no valid counts")
valid = count != 0
idxtrains = np.squeeze(infile["/INDEX/trainId"])
@@ -624,9 +636,10 @@ class AgipdCorrections:
elif index_v == 1:
status = np.squeeze(infile[idx_base + "image/status"])
if np.count_nonzero(status != 0) == 0:
raise IOError("File {} has no valid counts".format(
infile))
nonzero_count = np.count_nonzero(status != 0)
zero_count = count.shape[0] - nonzero_count
if nonzero_count == 0:
raise IOError(f"File {infile} has no valid counts")
last = np.squeeze(infile[idx_base + "image/last"])
first = np.squeeze(infile[idx_base + "image/first"])
valid = status != 0
@@ -641,9 +654,10 @@ class AgipdCorrections:
valid_indices = None
else:
raise AttributeError(
"Not a known raw format version: {}".format(index_v))
f"Not a known raw format version: {index_v}")
return valid, first_index, last_index, idxtrains, valid_indices
return (valid, first_index, last_index, idxtrains,
valid_indices, zero_count)
def gen_valid_range(self, first_index, last_index, max_cells, agipd_base,
infile, valid_indices=None):
Loading