diff --git a/cal_tools/cal_tools/agipdlib.py b/cal_tools/cal_tools/agipdlib.py index 48156b02223fd0a95d3b7022d7ee0d1b04d6fff3..83f8dcb0d319e250bc943a8808b9106092f2eda3 100644 --- a/cal_tools/cal_tools/agipdlib.py +++ b/cal_tools/cal_tools/agipdlib.py @@ -15,8 +15,7 @@ from cal_tools.cython import agipdalgs as calgs def get_num_cells(fname, loc, module): with h5py.File(fname, "r") as f: - cells = \ - f[f"INSTRUMENT/{loc}/DET/{module}CH0:xtdf/image/cellId"][()] + cells = f[f"INSTRUMENT/{loc}/DET/{module}CH0:xtdf/image/cellId"][()] if cells.shape[0] == 0: return None maxcell = np.max(cells) @@ -88,7 +87,7 @@ def get_gain_setting(fname: str, h5path_ctrl: str) -> int: gain-setting 1: setupr@dark=8, setupr@slopespc=40 gain-setting 0: setupr@dark=0, setupr@slopespc=32 - patternTypeIndex 1: High-gian + patternTypeIndex 1: High-gain patternTypeIndex 2: Medium-gain patternTypeIndex 3: Low-gain patternTypeIndex 4: SlopesPC @@ -187,9 +186,7 @@ class AgipdCorrections: const_yaml = metadata["retrieved-constants"] for mod in modules: - qm = f"Q{mod // 4 + 1}M{mod % 4 + 1}" - agipd_corr.initialize_from_yaml(karabo_da, - const_yaml, mod) + agipd_corr.initialize_from_yaml(karabo_da, const_yaml, mod) data_shape = (n_images_max, 512, 128) agipd_corr.allocate_images(data_shape, n_cores_files) @@ -315,8 +312,7 @@ class AgipdCorrections: data_dict['rawgain'][:n_img] = raw_data[:, 1] data_dict['cellId'][:n_img] = allcells[firange] data_dict['pulseId'][:n_img] = allpulses[firange] - data_dict['trainId'][:n_img] = np.squeeze( - group['trainId'][:][firange]) + data_dict['trainId'][:n_img] = np.squeeze(group['trainId'][:][firange]) except Exception as e: print(f'Error during reading data from file {file_name}: {e}') print(f'Error traceback: {traceback.format_exc()}') @@ -354,10 +350,10 @@ class AgipdCorrections: # Copy any other data from the input file. # This includes indexes, so it's important that the corrected data # we write is aligned with the raw data. - with h5py.File(file_name, 'r') as infile: - self.copy_and_sanitize_non_cal_data(infile, outfile, - agipd_base, - idx_base, trains) + with h5py.File(file_name, "r") as infile: + self.copy_and_sanitize_non_cal_data( + infile, outfile, agipd_base, idx_base, trains + ) # All corrected data goes in a /INSTRUMENT/.../image group image_grp = outfile[data_path] @@ -545,9 +541,9 @@ class AgipdCorrections: cellid = self.shared_dict[i_proc]['cellId'][first:last] # output is saved in sharedmem to pass for correct_agipd() # as this function takes about 3 seconds. - self.shared_dict[i_proc]['msk'][first:last] = \ - calgs.gain_choose_int(gain, - self.mask[module_idx][:, cellid]) # noqa + self.shared_dict[i_proc]["msk"][first:last] = calgs.gain_choose_int( + gain, self.mask[module_idx][:, cellid] + ) if hasattr(self, "rel_gain"): # Get the correct rel_gain depending on cell-id @@ -620,14 +616,12 @@ class AgipdCorrections: # if baseline correction was not requested # msk and rel_corr will still be empty shared_mem arrays if not any(self.blc_bools): - msk = calgs.gain_choose_int(gain, - self.mask[module_idx][:, cellid]) + msk = calgs.gain_choose_int(gain, self.mask[module_idx][:, cellid]) # same for relative gain and then bad pixel mask if hasattr(self, "rel_gain"): # Get the correct rel_gain depending on cell-id - rel_corr = calgs.gain_choose(gain, - self.rel_gain[module_idx][:, cellid]) # noqa + rel_corr = calgs.gain_choose(gain, self.rel_gain[module_idx][:, cellid]) # Correct for relative gain if self.corr_bools.get("rel_gain") and hasattr(self, "rel_gain"): @@ -690,11 +684,9 @@ class AgipdCorrections: # Copy the data across into the existing shared-memory array mask[...] = msk[...] - def get_valid_image_idx(self, idx_base: str, infile: str, - index_v: Optional[int] = 2): - """ Return the indices of valid data - """ - if index_v == 2: + def get_valid_image_idx(self, idx_base: str, infile: str, raw_format_version: int = 2): + """Return the indices of valid data""" + if raw_format_version == 2: count = np.squeeze(infile[idx_base + "image/count"]) first = np.squeeze(infile[idx_base + "image/first"]) if np.count_nonzero(count != 0) == 0: @@ -719,13 +711,16 @@ class AgipdCorrections: # Creating an array of validated indices. # If all indices were validated this array will be the same, # as what is stored at /DET/image/trainId - valid_indices = np.concatenate([np.arange(validf[i], - validf[i]+validc[i]) - for i in range(validf.size)], - axis=0) + valid_indices = np.concatenate( + [ + np.arange(validf[i], validf[i] + validc[i]) + for i in range(validf.size) + ], + axis=0, + ) valid_indices = np.squeeze(valid_indices).astype(np.int32) - elif index_v == 1: + elif raw_format_version == 1: status = np.squeeze(infile[idx_base + "image/status"]) if np.count_nonzero(status != 0) == 0: raise IOError(f"File {infile} has no valid counts") @@ -743,10 +738,9 @@ class AgipdCorrections: valid_indices = None else: raise AttributeError( - f"Not a known raw format version: {index_v}") + f"Not a known raw format version: {raw_format_version}") - return (valid, first_index, last_index, idxtrains, - valid_indices) + return (valid, first_index, last_index, idxtrains, valid_indices) def apply_selected_pulses(self, i_proc: int) -> int: """Select sharedmem data indices to correct based on selected