diff --git a/src/cal_tools/agipdlib.py b/src/cal_tools/agipdlib.py index 88c3413cc623efda893852293865abc2b90db701..c7191edcf238b2c87de070bab32fd875a7c29d58 100644 --- a/src/cal_tools/agipdlib.py +++ b/src/cal_tools/agipdlib.py @@ -298,7 +298,8 @@ class CellSelection: raise NotImplementedError def get_cells_on_trains( - self, train_sel: np.ndarray, nfrm: np.ndarray, cm: int = 0 + self, train_sel: np.ndarray, nfrm: np.ndarray, + cellid: np.ndarray, cm: int = 0 ) -> np.array: """Returns mask of cells selected for processing @@ -309,6 +310,8 @@ class CellSelection: for common-mode correction :return: boolean array with flags indicating images for processing + :return: integer array with number of frames to use in the second + step in two step selection procedure """ raise NotImplementedError @@ -517,7 +520,7 @@ class AgipdCorrections: n_valid_trains = len(valid_train_ids) data_dict["n_valid_trains"][0] = n_valid_trains data_dict["valid_trains"][:n_valid_trains] = valid_train_ids - data_dict["nimg_in_trains"][:n_valid_trains] = nimg_in_trains + #data_dict["nimg_in_trains"][:n_valid_trains] = nimg_in_trains if "AGIPD500K" in agipd_base: agipd_comp = components.AGIPD500K(im_dc) @@ -532,8 +535,11 @@ class AgipdCorrections: cm = (self.cell_sel.CM_NONE if apply_sel_pulses else self.cell_sel.CM_PRESEL) - img_selected = self.cell_sel.get_cells_on_trains( - np.array(valid_train_ids), nimg_in_trains, cm=cm) + cellid = np.squeeze(im_dc[agipd_base, "image.cellId"].ndarray()) + + img_selected, nimg_in_trains = self.cell_sel.get_cells_on_trains( + np.array(valid_train_ids), nimg_in_trains, cellid, cm=cm) + data_dict["nimg_in_trains"][:n_valid_trains] = nimg_in_trains frm_ix = np.flatnonzero(img_selected) data_dict["cm_presel"][0] = (cm == self.cell_sel.CM_PRESEL) @@ -1004,10 +1010,11 @@ class AgipdCorrections: ntrains = data_dict["n_valid_trains"][0] train_ids = data_dict["valid_trains"][:ntrains] nimg_in_trains = data_dict["nimg_in_trains"][:ntrains] + cellid = data_dict["cellId"][:n_img] # Initializing can_calibrate array - can_calibrate = self.cell_sel.get_cells_on_trains( - train_ids, nimg_in_trains, cm=self.cell_sel.CM_FINSEL + can_calibrate, _ = self.cell_sel.get_cells_on_trains( + train_ids, nimg_in_trains, cellid, cm=self.cell_sel.CM_FINSEL ) if np.all(can_calibrate): return n_img @@ -1606,6 +1613,7 @@ class CellRange(CellSelection): self.flag_cm[:self.max_cells] = self.flag self.flag_cm = (self.flag_cm.reshape(-1, self.row_size).any(1) .repeat(self.row_size)[:self.max_cells]) + self.sel_type = [self.flag, self.flag_cm, self.flag] def msg(self): return ( @@ -1615,10 +1623,24 @@ class CellRange(CellSelection): ) def get_cells_on_trains( - self, train_sel: np.ndarray, nfrm: np.ndarray, cm: int = 0 + self, train_sel: np.ndarray, nfrm: np.ndarray, + cellid: np.ndarray, cm: int = 0 ) -> np.array: - return np.tile(self._sel_for_cm(self.flag, self.flag_cm, cm), - len(train_sel)) + if cm < 0 or cm > 2: + raise ValueError("param 'cm' takes only 0,1,2") + + flag = self.sel_type[cm] + sel = np.zeros(np.sum(nfrm), bool) + counts = np.zeros(len(nfrm), int) + i0 = 0 + for i, nfrm_i in enumerate(nfrm): + iN = i0 + nfrm_i + f = flag[cellid[i0:iN]] + sel[i0:iN] = f + counts[i] = np.sum(f) + i0 = iN + + return sel, counts def filter_trains(self, train_sel: np.ndarray): return train_sel @@ -1695,12 +1717,14 @@ class LitFrameSelection(CellSelection): ) def get_cells_on_trains( - self, train_sel: np.ndarray, nfrm: np.ndarray, cm: int = 0 + self, train_sel: np.ndarray, nfrm: np.ndarray, + cellid: np.ndarray, cm: int = 0 ) -> np.array: cell_flags, cm_flags = self._sel.litframes_on_trains( train_sel, nfrm, [self.final_sel_type, self.cm_sel_type]) - return self._sel_for_cm(cell_flags, cm_flags, cm) + sel = self._sel_for_cm(cell_flags, cm_flags, cm) + return sel, nfrm def filter_trains(self, train_sel: np.ndarray): return self._sel.filter_trains(train_sel, drop_empty=True)