From 0dcc1e88a5fcd03761b0765f942feb3fd8d5515e Mon Sep 17 00:00:00 2001 From: Ana Rodrigues Date: Fri, 1 Mar 2024 12:37:39 +0100 Subject: [PATCH 1/9] test --- bblib/methods.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bblib/methods.py b/bblib/methods.py index f573eda..7272539 100755 --- a/bblib/methods.py +++ b/bblib/methods.py @@ -773,6 +773,7 @@ def _run_centering(self, **kwargs) -> tuple: self.config["plots_flag"], ) + if self.config["plots_flag"] and self.centering_converged(center): shift_x = 2 * (center[0] - self.initial_guess[0]) shift_y = 2 * (center[1] - self.initial_guess[1]) From 98b246a0b7968aeccf19f5222f9f867c034b4068 Mon Sep 17 00:00:00 2001 From: Ana Rodrigues Date: Fri, 1 Mar 2024 15:12:27 +0100 Subject: [PATCH 2/9] fix: plots info if not plot --- bblib/methods.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/bblib/methods.py b/bblib/methods.py index 7272539..58a8c47 100755 --- a/bblib/methods.py +++ b/bblib/methods.py @@ -54,6 +54,13 @@ def __init__(self, config: dict, PF8Config: PF8Info, plots_info: dict = None): "From config you want to save plots, please indicate the information to save the plots." ) + if not config["plots_flag"] and not plots_info: + plots_info = { + "file_name": "", + "folder_name": "", + "root_path": "" + } + def _prep_for_centering(self, data: np.ndarray) -> None: self.initial_detector_center = self.PF8Config.get_detector_center() pf8 = PF8(self.PF8Config) @@ -153,6 +160,13 @@ def __init__(self, config: dict, PF8Config: PF8Info, plots_info: dict = None): "From config you want to save plots, please indicate the information to save the plots." ) + if not config["plots_flag"] and not plots_info: + plots_info = { + "file_name": "", + "folder_name": "", + "root_path": "" + } + def _prep_for_centering(self, data: np.ndarray) -> None: self.initial_detector_center = self.PF8Config.get_detector_center() ## Find peaks @@ -286,6 +300,14 @@ def __init__(self, config: dict, PF8Config: PF8Info, plots_info: dict = None): "From config you want to save plots, please indicate the information to save the plots." ) + if not config["plots_flag"] and not plots_info: + plots_info = { + "file_name": "", + "folder_name": "", + "root_path": "" + } + + def _calculate_fwhm(self, coordinate: tuple) -> dict: center_to_radial_average = coordinate try: @@ -561,7 +583,13 @@ def __init__(self, config: dict, PF8Config: PF8Info, plots_info: dict = None): raise ValueError( "From config you want to save plots, please indicate the information to save the plots." ) - + + if not config["plots_flag"] and not plots_info: + plots_info = { + "file_name": "", + "folder_name": "", + "root_path": "" + } def _remove_repeated_pairs(self, pairs_list: list) -> list: x_vector = [] y_vector = [] From 9c7a49f02207cd8844b182ae655754e9b11ef75f Mon Sep 17 00:00:00 2001 From: Ana Rodrigues Date: Fri, 1 Mar 2024 17:02:50 +0100 Subject: [PATCH 3/9] fix: origin plots lower --- bblib/methods.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bblib/methods.py b/bblib/methods.py index 58a8c47..7291c3b 100755 --- a/bblib/methods.py +++ b/bblib/methods.py @@ -836,7 +836,7 @@ def _run_centering(self, **kwargs) -> tuple: label=f"Refined detector center:({np.round(center[0],1)}, {np.round(center[1],1)})", ) ax.set_xlim(200, 900) - ax.set_ylim(900, 200) + ax.set_ylim(200, 900) plt.title("Center refinement: autocorrelation of Friedel pairs") fig.colorbar(pos, shrink=0.6) ax.legend() @@ -907,7 +907,7 @@ def _run_centering(self, **kwargs) -> tuple: label="shift of inverted peaks", ) ax.set_xlim(200, 900) - ax.set_ylim(900, 200) + ax.set_ylim(200, 900) plt.title("Bragg peaks alignement") fig.colorbar(pos, shrink=0.6) ax.legend() From 49b717dcae42217ba4af8025a3e197afb7074e96 Mon Sep 17 00:00:00 2001 From: Ana Rodrigues Date: Mon, 8 Apr 2024 13:22:36 +0200 Subject: [PATCH 4/9] fix: color scale set to log auto --- bblib/methods.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/bblib/methods.py b/bblib/methods.py index 7291c3b..a75d078 100755 --- a/bblib/methods.py +++ b/bblib/methods.py @@ -21,7 +21,7 @@ import multiprocessing import pathlib from scipy.optimize import curve_fit - +from matplotlib.colors import LogNorm class CenteringMethod(ABC): @abstractmethod @@ -120,7 +120,7 @@ def _run_centering(self, **kwargs) -> tuple: fig, ax1 = plt.subplots(1, 1, figsize=(10, 10)) ax1.imshow( self.visual_data * self.mask_for_center_of_mass, - vmax=10, + norm=LogNorm(), cmap="YlGn", origin="lower", ) @@ -259,7 +259,7 @@ def _run_centering(self, **kwargs) -> tuple: fig, ax1 = plt.subplots(1, 1, figsize=(10, 10)) ax1.imshow( self.visual_data * self.mask_for_circle_detection, - vmax=10, + norm=LogNorm(), origin="lower", cmap="YlGn", ) @@ -536,7 +536,7 @@ def _run_centering(self, **kwargs) -> tuple: fig, ax1 = plt.subplots(1, 1, figsize=(10, 10)) ax1.imshow( self.visual_data * self.mask_for_fwhm_min, - vmax=10, + norm=LogNorm(), origin="lower", cmap="YlGn", ) @@ -748,11 +748,13 @@ def _prep_for_centering(self, data: np.ndarray, initial_guess: tuple) -> None: ) peak_list_in_slab = pf8.peak_list_in_slab(peak_list) + #print(peak_list_in_slab) self.peak_list_x_in_frame, self.peak_list_y_in_frame = peak_list_in_slab def _run_centering(self, **kwargs) -> tuple: - peak_list_x_in_frame = self.peak_list_x_in_frame.copy() - peak_list_y_in_frame = self.peak_list_y_in_frame.copy() + + peak_list_y_in_frame = self.peak_list_x_in_frame.copy() + peak_list_x_in_frame = self.peak_list_y_in_frame.copy() peaks = list(zip(peak_list_x_in_frame, peak_list_y_in_frame)) inverted_peaks_x = [-1 * k for k in peak_list_x_in_frame] @@ -806,9 +808,9 @@ def _run_centering(self, **kwargs) -> tuple: shift_x = 2 * (center[0] - self.initial_guess[0]) shift_y = 2 * (center[1] - self.initial_guess[1]) - fig, ax = plt.subplots(1, 1, figsize=(8, 8)) + fig, ax = plt.subplots(1, 1, figsize=(10, 10)) pos = ax.imshow( - self.visual_data, vmin=0, vmax=100, cmap="YlGn", origin="lower" + self.visual_data, norm=LogNorm(), cmap="YlGn", origin="lower" ) ax.scatter( self.initial_detector_center[0], @@ -870,9 +872,9 @@ def _run_centering(self, **kwargs) -> tuple: ] ## Check pairs alignement - fig, ax = plt.subplots(1, 1, figsize=(8, 8)) + fig, ax = plt.subplots(1, 1, figsize=(10, 10)) pos = ax.imshow( - self.visual_data, vmin=0, vmax=100, cmap="YlGn", origin="lower" + self.visual_data, norm=LogNorm(), cmap="YlGn", origin="lower" ) ax.scatter( original_peaks_x, From 051518c6e176e1e521d21f91308091e145a118a8 Mon Sep 17 00:00:00 2001 From: Ana Rodrigues Date: Mon, 8 Apr 2024 13:23:11 +0200 Subject: [PATCH 5/9] fix: center of mass divide by zero handling --- bblib/utils.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bblib/utils.py b/bblib/utils.py index 8baa6ab..da95bd9 100755 --- a/bblib/utils.py +++ b/bblib/utils.py @@ -28,8 +28,12 @@ def center_of_mass(data: np.ndarray, mask: np.ndarray = None) -> list[int]: mask = np.ones_like(data) data = data * mask indexes = np.where(data > 0) - xc = np.sum(data[indexes] * indexes[1]) / np.sum(data[indexes]) - yc = np.sum(data[indexes] * indexes[0]) / np.sum(data[indexes]) + if np.sum(data[indexes])>1e-7: + xc = np.sum(data[indexes] * indexes[1]) / np.sum(data[indexes]) + yc = np.sum(data[indexes] * indexes[0]) / np.sum(data[indexes]) + else: + xc = -1 + yc = -1 if np.isnan(xc) or np.isnan(yc): xc = -1 From 3305c0fb44735fcc287e0871a121fc6760e7c8cf Mon Sep 17 00:00:00 2001 From: Ana Rodrigues Date: Mon, 8 Apr 2024 14:10:41 +0200 Subject: [PATCH 6/9] fix: bug fix inconsistency of signal when shifting the detector --- bblib/methods.py | 8 +++++--- bblib/models.py | 22 ++++++++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/bblib/methods.py b/bblib/methods.py index a75d078..a9ff339 100755 --- a/bblib/methods.py +++ b/bblib/methods.py @@ -753,8 +753,8 @@ def _prep_for_centering(self, data: np.ndarray, initial_guess: tuple) -> None: def _run_centering(self, **kwargs) -> tuple: - peak_list_y_in_frame = self.peak_list_x_in_frame.copy() - peak_list_x_in_frame = self.peak_list_y_in_frame.copy() + peak_list_x_in_frame = self.peak_list_x_in_frame.copy() + peak_list_y_in_frame = self.peak_list_y_in_frame.copy() peaks = list(zip(peak_list_x_in_frame, peak_list_y_in_frame)) inverted_peaks_x = [-1 * k for k in peak_list_x_in_frame] @@ -850,7 +850,7 @@ def _run_centering(self, **kwargs) -> tuple: f'{self.plots_info["root_path"]}/center_refinement/plots/{self.plots_info["folder_name"]}/centered_friedel/{self.plots_info["file_name"]}.png' ) plt.close("all") - + original_peaks_x = [ np.round(k + self.initial_guess[0]) for k in peak_list_x_in_frame ] @@ -886,6 +886,7 @@ def _run_centering(self, **kwargs) -> tuple: linewidth=1.5, label="original peaks", ) + ax.scatter( inverted_non_shifted_peaks_x, inverted_non_shifted_peaks_y, @@ -908,6 +909,7 @@ def _run_centering(self, **kwargs) -> tuple: edgecolor="blue", label="shift of inverted peaks", ) + ax.set_xlim(200, 900) ax.set_ylim(200, 900) plt.title("Bragg peaks alignement") diff --git a/bblib/models.py b/bblib/models.py index 84d1e75..df67fe6 100755 --- a/bblib/models.py +++ b/bblib/models.py @@ -26,6 +26,7 @@ class PF8Info: pixel_maps: TypePixelMaps = None pixel_resolution: float = None _shifted_pixel_maps: bool = False + geometry_txt: list = None def update_pixel_maps(self, detector_shift_x: int, detector_shift_y: int): if not self._shifted_pixel_maps: @@ -33,10 +34,10 @@ def update_pixel_maps(self, detector_shift_x: int, detector_shift_y: int): self._detector_shift_y = detector_shift_y self._shifted_pixel_maps = True self.pixel_maps["x"] = ( - self.pixel_maps["x"].flatten() - detector_shift_x + self.pixel_maps["x"].flatten() + detector_shift_x ).reshape(self._data_shape) self.pixel_maps["y"] = ( - self.pixel_maps["y"].flatten() - detector_shift_y + self.pixel_maps["y"].flatten() + detector_shift_y ).reshape(self._data_shape) self.pixel_maps["radius"] = np.sqrt( np.square(self.pixel_maps["x"]) + np.square(self.pixel_maps["y"]) @@ -46,19 +47,24 @@ def update_pixel_maps(self, detector_shift_x: int, detector_shift_y: int): f"Pixel maps have been moved once before, to avoid errors reset the geometry before moving it again." ) - def set_geometry_from_file(self, geometry_filename: str): - geometry_txt = open(geometry_filename, "r").readlines() + def set_geometry_from_file(self, geometry_filename: str = None): + if geometry_filename: + self.geometry_txt = open(geometry_filename, "r").readlines() + else: + if not self.geometry_txt: + raise ValueError("Please, specify the detector geometry in CrystFEL format.") + self.bad_pixel_map_filename = [ x.split(" = ")[-1][:-1] - for x in geometry_txt + for x in self.geometry_txt if x.split(" = ")[0] == "mask_file" ][0] self.bad_pixel_map_hdf5_path = [ - x.split(" = ")[-1][:-1] for x in geometry_txt if x.split(" = ")[0] == "mask" + x.split(" = ")[-1][:-1] for x in self.geometry_txt if x.split(" = ")[0] == "mask" ][0] geom = GeometryInformation( - geometry_description=geometry_txt, geometry_format="crystfel" + geometry_description=self.geometry_txt, geometry_format="crystfel" ) self.pixel_resolution = 1 / geom.get_pixel_size() self.pixel_maps = geom.get_pixel_maps() @@ -73,7 +79,7 @@ def set_geometry_from_file(self, geometry_filename: str): ) == 1: ## Get single panel transformation matrix from the geometry file ### Warning! Check carefully if the visualized data after reorientation of the panel makes sense, e.g. if it is equal to the real experimental data geometry. - detector, _, _ = _read_crystfel_geometry_from_text(text_lines=geometry_txt) + detector, _, _ = _read_crystfel_geometry_from_text(text_lines=self.geometry_txt) detector_panels = dict(detector["panels"]) panel_name = list(detector_panels.keys())[0] frame_dim_structure = [ From d5fa6b712290c8c1ba3978966291aeadedffec42 Mon Sep 17 00:00:00 2001 From: Ana Rodrigues Date: Mon, 8 Apr 2024 21:15:44 +0200 Subject: [PATCH 7/9] feat: update phi map --- bblib/models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bblib/models.py b/bblib/models.py index df67fe6..f60c62f 100755 --- a/bblib/models.py +++ b/bblib/models.py @@ -42,6 +42,7 @@ def update_pixel_maps(self, detector_shift_x: int, detector_shift_y: int): self.pixel_maps["radius"] = np.sqrt( np.square(self.pixel_maps["x"]) + np.square(self.pixel_maps["y"]) ).reshape(self._data_shape) + self.pixel_maps["phi"] = np.arctan2(self.pixel_maps["y"], self.pixel_maps["x"]) else: raise ValueError( f"Pixel maps have been moved once before, to avoid errors reset the geometry before moving it again." From 39f0cf5abfbf5da4fa015da46d8eaa51c2a5ba9f Mon Sep 17 00:00:00 2001 From: Ana Rodrigues Date: Tue, 9 Apr 2024 13:02:17 +0200 Subject: [PATCH 8/9] fix: visualization maps set to before updating pixel maps --- bblib/methods.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/bblib/methods.py b/bblib/methods.py index a9ff339..50a5279 100755 --- a/bblib/methods.py +++ b/bblib/methods.py @@ -397,16 +397,16 @@ def _calculate_fwhm(self, coordinate: tuple) -> dict: def _prep_for_centering(self, data: np.ndarray, initial_guess: tuple) -> None: self.initial_guess = initial_guess self.initial_detector_center = self.PF8Config.get_detector_center() + non_shifted_pixel_maps_for_visualization = self.PF8Config.pixel_maps.copy() ## Find peaks self.PF8Config.update_pixel_maps( initial_guess[0] - self.initial_detector_center[0], initial_guess[1] - self.initial_detector_center[1], ) pf8 = PF8(self.PF8Config) - # Assemble data and mask - data_visualize = geometry.DataVisualizer(pixel_maps=self.PF8Config.pixel_maps) - + data_visualize = geometry.DataVisualizer(pixel_maps=non_shifted_pixel_maps_for_visualization) + with h5py.File(f"{self.PF8Config.bad_pixel_map_filename}", "r") as f: mask = np.array(f[f"{self.PF8Config.bad_pixel_map_hdf5_path}"]) @@ -678,6 +678,8 @@ def _prep_for_centering(self, data: np.ndarray, initial_guess: tuple) -> None: self.initial_guess = initial_guess self.initial_detector_center = self.PF8Config.get_detector_center() + non_shifted_pixel_maps_for_visualization = self.PF8Config.pixel_maps.copy() + ## Find peaks self.PF8Config.update_pixel_maps( initial_guess[0] - self.initial_detector_center[0], @@ -687,7 +689,7 @@ def _prep_for_centering(self, data: np.ndarray, initial_guess: tuple) -> None: pf8 = PF8(self.PF8Config) # Assemble data and mask - data_visualize = geometry.DataVisualizer(pixel_maps=self.PF8Config.pixel_maps) + data_visualize = geometry.DataVisualizer(pixel_maps=non_shifted_pixel_maps_for_visualization) with h5py.File(f"{self.PF8Config.bad_pixel_map_filename}", "r") as f: mask = np.array(f[f"{self.PF8Config.bad_pixel_map_hdf5_path}"]) From a2b45d58deeb0bd9c84bd403fdb94ae60a5be57f Mon Sep 17 00:00:00 2001 From: Ana Rodrigues Date: Tue, 9 Apr 2024 13:02:46 +0200 Subject: [PATCH 9/9] fix: detector center calculation and update of pixel maps --- bblib/models.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bblib/models.py b/bblib/models.py index f60c62f..6ce08fa 100755 --- a/bblib/models.py +++ b/bblib/models.py @@ -34,10 +34,10 @@ def update_pixel_maps(self, detector_shift_x: int, detector_shift_y: int): self._detector_shift_y = detector_shift_y self._shifted_pixel_maps = True self.pixel_maps["x"] = ( - self.pixel_maps["x"].flatten() + detector_shift_x + self.pixel_maps["x"].flatten() - detector_shift_x ).reshape(self._data_shape) self.pixel_maps["y"] = ( - self.pixel_maps["y"].flatten() + detector_shift_y + self.pixel_maps["y"].flatten() - detector_shift_y ).reshape(self._data_shape) self.pixel_maps["radius"] = np.sqrt( np.square(self.pixel_maps["x"]) + np.square(self.pixel_maps["y"]) @@ -139,6 +139,7 @@ def get(self, parameter: str): def get_detector_center(self) -> list: if not self._shifted_pixel_maps: + if ( self.pf8_detector_info["nasics_x"] * self.pf8_detector_info["nasics_y"] > 1 @@ -174,6 +175,7 @@ def get_detector_center(self) -> list: _img_center_x = int(abs(np.min(self.pixel_maps["x"]))) _img_center_y = int(abs(np.min(self.pixel_maps["y"]))) else: + print("Warning! The detector center was moved by a previous operation, the detector center is not the same as in the geometry file.") _img_center_x = self.detector_center_from_geom[0] + self._detector_shift_x _img_center_y = self.detector_center_from_geom[1] + self._detector_shift_y return [_img_center_x, _img_center_y]