diff --git a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb index 3e4d29c52420758b101a2167720f61a32c8acf78..7ef8ce2bc61ea1f6a6563bee9ea8cfed4bb6919e 100644 --- a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb +++ b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb @@ -566,9 +566,6 @@ "# Retrieve calibration constants to RAM\n", "agipd_corr.allocate_constants(modules, (3, mem_cells_db, 512, 128))\n", "\n", - "when = {}\n", - "cons_data = {}\n", - "variant = {}\n", "step_timer.start()\n", "agipd_cal = AGIPD_CalibrationData(\n", " detector_name=karabo_id,\n", @@ -595,9 +592,11 @@ "if agipd_corr.corr_bools.get('xray_corr'):\n", " gain_constants += agipd_cal.illuminated_calibrations\n", "\n", + "# First retrieve dark constants\n", "agipd_metadata = agipd_cal.metadata(dark_constants)\n", "if gain_constants:\n", - " # Gain constants doesn't have gain_mode condition.\n", + " # Then retrieve gain constants without\n", + " # using the `gain_mode` condition.\n", " agipd_cal.gain_mode = None\n", " try:\n", " illum_metadata = agipd_cal.metadata(gain_constants)\n", @@ -672,16 +671,14 @@ " (int, dict, str): Module index, {constant name: creation time}, Karabo DA\n", " \"\"\"\n", " const_data = dict()\n", - " when = dict()\n", - "\n", + " variant = dict()\n", " for cname, mdata in agipd_metadata[da].items():\n", - " when[cname] = mdata[\"begin_validity_at\"]\n", " dataset = mdata[\"dataset\"]\n", " with h5py.File(agipd_cal.caldb_root / mdata[\"path\"], \"r\") as cf: # noqa\n", " const_data[cname] = np.copy(cf[f\"{dataset}/data\"])\n", " variant[cname] = cf[dataset].attrs[\"variant\"] if cf[dataset].attrs.keys() else 0 # noqa\n", - " agipd_corr.init_constants(const_data, when, mod, variant)\n", - " return module, when, da\n", + " agipd_corr.init_constants(const_data, mod, variant)\n", + " return module, da\n", "\n", "step_timer.start()\n", "with multiprocessing.Pool(processes=len(modules)) as pool:\n", @@ -695,22 +692,23 @@ "metadata": {}, "outputs": [], "source": [ - "# if the yml file contains \"retrieved-constants\", that means a leading\n", - "# notebook got processed and the reporting would be generated from it.\n", - "fst_print = True\n", + "# Store timestamps for Offset, SlopesPC, and SlopesFF\n", + "# in YAML file for time-summary table.\n", "timestamps = {}\n", "\n", - "for i, (modno, when, k_da) in enumerate(const_out):\n", + "for mod, mod_mdata in agipd_metadata.items():\n", + " modno = int(mod[-2:])\n", "\n", " module_timestamps = {}\n", "\n", " # Store few time stamps if exists\n", " # Add NA to keep array structure\n", " for key in ['Offset', 'SlopesPC', 'SlopesFF']:\n", - " if key in when and when[key]:\n", - " module_timestamps[key] = when[key]\n", + " if key in mod_mdata:\n", + " module_timestamps[key] = mod_mdata[key][\"begin_validity_at\"]\n", " else:\n", " module_timestamps[key] = \"NA\"\n", + "\n", " timestamps[module_index_to_qm(modno)] = module_timestamps\n", "\n", "seq = sequences[0] if sequences else 0\n", diff --git a/src/cal_tools/agipdlib.py b/src/cal_tools/agipdlib.py index a99d5589f1a6f89f9c007f0a31ac86a6a5abe962..42ba25db41e2de4bf531b3c965c8df066f54d45d 100644 --- a/src/cal_tools/agipdlib.py +++ b/src/cal_tools/agipdlib.py @@ -478,20 +478,6 @@ class AgipdCorrections: self.corr_bools.get('blc_hmatch'), self.corr_bools.get('blc_stripes')] - self.dark_constants = {"Offset", "Noise", "BadPixelsDark"} - if not self.gain_mode: - self.dark_constants.add("ThresholdsDark") - - self.gain_constants = set() - if any(self.pc_bools): - self.gain_constants |= {"SlopesPC", "BadPixelsPC"} - if self.corr_bools.get("xray_corr"): - self.gain_constants |= {"SlopesFF", "BadPixelsFF"} - - self.all_constants = self.dark_constants.copy() - self.all_constants |= self.gain_constants - - def read_file(self, i_proc: int, file_name: str, apply_sel_pulses: Optional[bool] = True ) -> int: @@ -1139,7 +1125,7 @@ class AgipdCorrections: fletcher32=True) def init_constants( - self, cons_data: dict, when: dict, module_idx: int, variant: dict): + self, cons_data: dict, module_idx: int, variant: dict): """ For CI derived gain, a mean multiplication factor of 4.48 compared to medium gain is used, as no reliable CI data for all memory cells @@ -1178,8 +1164,6 @@ class AgipdCorrections: rel_low gain = _rel_medium gain * 4.48 :param cons_data: A dictionary for each retrieved constant value. - :param when: A dictionary for the creation time - of each retrieved constant. :param module_idx: A module_idx index :param variant: A dictionary for the variant of each retrieved CCV. :return: @@ -1190,19 +1174,19 @@ class AgipdCorrections: calgs_opts = dict(num_threads=os.cpu_count() // len(self.offset)) calgs.transpose_constant( - self.offset[module_idx], cons_data['Offset'], **calgs_opts) + self.offset[module_idx], cons_data["Offset"], **calgs_opts) # In case noise wasn't retrieved no need for transposing. - if 'Noise' in cons_data: + if "Noise" in cons_data: calgs.transpose_constant( - self.noise[module_idx], cons_data['Noise'], **calgs_opts) + self.noise[module_idx], cons_data["Noise"], **calgs_opts) if ( self.gain_mode is AgipdGainMode.ADAPTIVE_GAIN and - 'ThresholdsDark' in cons_data + "ThresholdsDark" in cons_data ): calgs.transpose_constant(self.thresholds[module_idx], - cons_data['ThresholdsDark'][..., :3], + cons_data["ThresholdsDark"][..., :3], **calgs_opts) if self.corr_bools.get("low_medium_gap"): @@ -1213,12 +1197,12 @@ class AgipdCorrections: bpixels = cons_data["BadPixelsDark"].astype(np.uint32) if self.corr_bools.get("xray_corr"): - if when.get("BadPixelsFF"): + if "BadPixelsFF" in cons_data: bpixels |= cons_data["BadPixelsFF"].astype(np.uint32)[..., :bpixels.shape[2], # noqa None] - if when.get("SlopesFF"): # Checking if constant was retrieved + if "SlopesFF" in cons_data: # Checking if constant was retrieved slopesFF = cons_data["SlopesFF"] # This could be used for backward compatibility @@ -1258,7 +1242,7 @@ class AgipdCorrections: # add additional bad pixel information if any(self.pc_bools): - if when.get("BadPixelsPC"): + if "BadPixelsPC" in cons_data: bppc = np.moveaxis(cons_data["BadPixelsPC"].astype(np.uint32), 0, 2) bpixels |= bppc[..., :bpixels.shape[2], None] @@ -1266,7 +1250,7 @@ class AgipdCorrections: # calculate relative gain from the constants rel_gain = np.ones((128, 512, self.max_cells, 3), np.float32) - if when.get("SlopesPC"): + if "SlopesPC" in cons_data: slopesPC = cons_data["SlopesPC"].astype(np.float32, copy=False) # This will handle some historical data in a different format @@ -1360,28 +1344,6 @@ class AgipdCorrections: return - def validate_constants_availability( - self, const_data: dict, karabo_da: str, - ) -> bool : - """ - Validate the constants availability and raise/warn correspondingly. - Validation is False, if dark constants are not retrieved. - """ - missing_dark_constants = self.dark_constants - set(const_data) - missing_gain_constants = self.gain_constants - set(const_data) - - if missing_gain_constants: - warning( - f"Gain constants {missing_gain_constants} are" - f" not retrieved for {karabo_da}") - - if missing_dark_constants: - warning(f"Dark constants {missing_dark_constants} are not available" - f" to correct {karabo_da}") - return False - else: - return True - def allocate_constants(self, modules, constant_shape): """ Allocate memory for correction constants