diff --git a/README.md b/README.md index 69c55c5..722c499 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,8 @@ plots_info = { "xlim_max": ..., "ylim_min": ..., "ylim_max": ..., - "color_map": ... + "color_map": ..., + "marker_size": ... } ``` To calculate the refined detector center of raw data frame as a numpy array using the following methods: diff --git a/bblib/methods.py b/bblib/methods.py index f826d84..f20f65a 100755 --- a/bblib/methods.py +++ b/bblib/methods.py @@ -21,6 +21,8 @@ import pathlib from scipy.optimize import curve_fit from matplotlib.colors import LogNorm +import copy +import matplotlib class CenteringMethod(ABC): @@ -114,40 +116,57 @@ def _run_centering(self, **kwargs) -> tuple: if self.config["plots_flag"]: visual_img = self.visual_data * self.mask_for_center_of_mass - visual_img[np.where(visual_img<1)] = 1 # For better visualization of plots in logarithmic scale fig, ax1 = plt.subplots(1, 1, figsize=(10, 10)) if self.plots_info["value_auto"]: + color_map = copy.copy( + matplotlib.colormaps[self.plots_info["color_map"]] + ) + color_map.set_bad(color_map(0)) pos = ax1.imshow( - visual_img, - norm=LogNorm(), - cmap=self.plots_info["color_map"], - origin="lower", + visual_img, norm=LogNorm(), cmap=color_map, origin="lower" ) else: + + color_map = copy.copy( + matplotlib.colormaps[self.plots_info["color_map"]] + ) + color_map.set_bad(color_map.colors(0)) pos = ax1.imshow( visual_img, norm=LogNorm( self.plots_info["value_min"], self.plots_info["value_max"] ), - cmap=self.plots_info["color_map"], + cmap=color_map, origin="lower", ) ax1.scatter( self.initial_detector_center[0], self.initial_detector_center[1], + s=self.plots_info["marker_size"], color="blue", marker="o", + edgecolor="black", + linewidth=0.5, label=f"Initial detector center: ({np.round(self.initial_detector_center[0])}, {np.round(self.initial_detector_center[1])})", ) ax1.scatter( center[0], center[1], + s=self.plots_info["marker_size"], color="r", - marker="o", + marker="^", + edgecolor="black", + linewidth=0.5, label=f"Refined detector center: ({center[0]}, {center[1]})", ) - ax1.legend() - fig.colorbar(pos, ax=ax1, shrink=0.6) + ax1.legend(fontsize=14, loc=1, markerscale=1) + plt.tick_params(axis="both", which="major", labelsize=16) + ax1.set_xlabel("x (pixel)", fontsize=20) + ax1.set_ylabel("y (pixel)", fontsize=20) + + cbar = fig.colorbar(pos, ax=ax1, shrink=0.6) + cbar.ax.tick_params(labelsize=20) + path = pathlib.Path( f'{self.plots_info["root_path"]}/center_refinement/plots/{self.plots_info["folder_name"]}/center_of_mass/' ) @@ -236,8 +255,15 @@ def _run_centering(self, **kwargs) -> tuple: if self.config["plots_flag"]: fig, ax1 = plt.subplots(1, 1, figsize=(10, 10)) - pos = ax1.imshow(edges, origin="lower", cmap=self.plots_info["color_map"]) - fig.colorbar(pos, ax=ax1, shrink=0.6) + color_map = copy.copy(matplotlib.colormaps[self.plots_info["color_map"]]) + color_map.set_bad(color_map(0)) + pos = ax1.imshow(edges, origin="lower", cmap=color_map) + plt.tick_params(axis="both", which="major", labelsize=16) + ax1.set_xlabel("x (pixel)", fontsize=20) + ax1.set_ylabel("y (pixel)", fontsize=20) + cbar = fig.colorbar(pos, ax=ax1, shrink=0.6) + cbar.ax.tick_params(labelsize=20) + path = pathlib.Path( f'{self.plots_info["root_path"]}/center_refinement/plots/{self.plots_info["folder_name"]}/edges/' ) @@ -266,44 +292,62 @@ def _run_centering(self, **kwargs) -> tuple: center = [xc, yc] if self.config["plots_flag"]: visual_img = self.visual_data * self.mask_for_circle_detection - visual_img[np.where(visual_img<1)] = 1 # For better visualization of plots in logarithmic scale fig, ax1 = plt.subplots(1, 1, figsize=(10, 10)) if self.plots_info["value_auto"]: + color_map = copy.copy( + matplotlib.colormaps[self.plots_info["color_map"]] + ) + color_map.set_bad(color_map(0)) pos = ax1.imshow( visual_img, norm=LogNorm(), origin="lower", - cmap=self.plots_info["color_map"], + cmap=color_map, ) else: + color_map = copy.copy( + matplotlib.colormaps[self.plots_info["color_map"]] + ) + color_map.set_bad(color_map(0)) pos = ax1.imshow( visual_img, norm=LogNorm( self.plots_info["value_min"], self.plots_info["value_max"] ), origin="lower", - cmap=self.plots_info["color_map"], + cmap=color_map, ) ax1.scatter( self.initial_detector_center[0], self.initial_detector_center[1], + s=self.plots_info["marker_size"], color="blue", marker="o", + edgecolor="black", + linewidth=0.5, label=f"Initial detector center: ({np.round(self.initial_detector_center[0])}, {np.round(self.initial_detector_center[1])})", ) ax1.scatter( center[0], center[1], + s=self.plots_info["marker_size"], color="r", - marker="o", + marker="^", + edgecolor="black", + linewidth=0.5, label=f"Refined detector center: ({center[0]}, {center[1]})", ) path = pathlib.Path( f'{self.plots_info["root_path"]}/center_refinement/plots/{self.plots_info["folder_name"]}/center_circle_detection/' ) path.mkdir(parents=True, exist_ok=True) - ax1.legend() - fig.colorbar(pos, ax=ax1, shrink=0.6) + ax1.legend(fontsize=14, loc=1, markerscale=1) + cbar = fig.colorbar(pos, ax=ax1, shrink=0.6) + cbar.ax.tick_params(labelsize=20) + plt.tick_params(axis="both", which="major", labelsize=16) + ax1.set_xlabel("x (pixel)", fontsize=20) + ax1.set_ylabel("y (pixel)", fontsize=20) + if not self.plots_info["axis_lim_auto"]: ax1.set_xlim(self.plots_info["xlim_min"], self.plots_info["xlim_max"]) ax1.set_ylim(self.plots_info["ylim_min"], self.plots_info["ylim_max"]) @@ -345,8 +389,11 @@ def _calculate_fwhm(self, coordinate: tuple) -> dict: } if self.plot_fwhm_flag: - fig, ax1 = plt.subplots(1, 1, figsize=(5, 5)) + fig, ax1 = plt.subplots(1, 1, figsize=(8, 8)) plt.plot(x_all, y_all) + ax1.set_xlabel("Radial distance (pixel)", fontsize=20) + ax1.set_ylabel("Intensity (ADU)", fontsize=20) + plt.tick_params(axis="both", which="major", labelsize=16) ## Define background peak region x_min = self.config["peak_region"]["min"] @@ -394,10 +441,7 @@ def _calculate_fwhm(self, coordinate: tuple) -> dict: label=f"gaussian fit \n a:{round(popt[0],2)} \n x0:{round(popt[1],2)} \n sigma:{round(popt[2],2)} \n R² {round(r_squared, 4)}\n FWHM : {round(fwhm,3)}", ) - plt.title("Azimuthal integration") - # plt.xlim(0, 500) - # plt.ylim(0, 5) - plt.legend() + plt.legend(fontsize=14, loc=1, markerscale=1) path = pathlib.Path( f'{self.plots_info["root_path"]}/center_refinement/plots/{self.plots_info["folder_name"]}/radial_average/' ) @@ -556,51 +600,64 @@ def _run_centering(self, **kwargs) -> tuple: if self.config["plots_flag"]: visual_img = self.visual_data * self.mask_for_fwhm_min - visual_img[np.where(visual_img<1)] = 1 # For better visualization of plots in logarithmic scale fig, ax1 = plt.subplots(1, 1, figsize=(10, 10)) if self.plots_info["value_auto"]: + color_map = copy.copy( + matplotlib.colormaps[self.plots_info["color_map"]] + ) + color_map.set_bad(color_map(0)) pos = ax1.imshow( visual_img, norm=LogNorm(), origin="lower", - cmap=self.plots_info["color_map"], + cmap=color_map, ) else: + color_map = copy.copy( + matplotlib.colormaps[self.plots_info["color_map"]] + ) + color_map.set_bad(color_map(0)) pos = ax1.imshow( visual_img, norm=LogNorm( self.plots_info["value_min"], self.plots_info["value_max"] ), origin="lower", - cmap=self.plots_info["color_map"], + cmap=color_map, ) + ax1.scatter( - self.initial_detector_center[0], - self.initial_detector_center[1], - color="blue", - marker="o", - label=f"Initial detector center: ({np.round(self.initial_detector_center[0])}, {np.round(self.initial_detector_center[1])})", - ) - ax1.scatter( - self.initial_detector_center[0], - self.initial_detector_center[1], + self.initial_guess[0], + self.initial_guess[1], + s=self.plots_info["marker_size"], color="blue", marker="o", + edgecolor="black", + linewidth=0.5, label=f"Initial guess: ({np.round(self.initial_guess[0])}, {np.round(self.initial_guess[1])})", ) ax1.scatter( center[0], center[1], + s=self.plots_info["marker_size"], color="r", - marker="o", + marker="^", + edgecolor="black", + linewidth=0.5, label=f"Refined detector center: ({center[0]}, {center[1]})", ) path = pathlib.Path( f'{self.plots_info["root_path"]}/center_refinement/plots/{self.plots_info["folder_name"]}/center_fwhm_minimization/' ) path.mkdir(parents=True, exist_ok=True) - ax1.legend() - fig.colorbar(pos, ax=ax1, shrink=0.6) + ax1.legend(fontsize=14, loc=1, markerscale=1) + plt.tick_params(axis="both", which="major", labelsize=16) + ax1.set_xlabel("x (pixel)", fontsize=20) + ax1.set_ylabel("y (pixel)", fontsize=20) + + cbar = fig.colorbar(pos, ax=ax1, shrink=0.6) + cbar.ax.tick_params(labelsize=20) + if not self.plots_info["axis_lim_auto"]: ax1.set_xlim(self.plots_info["xlim_min"], self.plots_info["xlim_max"]) ax1.set_ylim(self.plots_info["ylim_min"], self.plots_info["ylim_max"]) @@ -714,14 +771,14 @@ def _prep_for_centering(self, data: np.ndarray, initial_guess: tuple) -> None: > 1 ): self.visual_data = data_visualize.visualize_data(data=data * mask) - visual_mask = data_visualize.visualize_data(data=mask).astype(int) + self.visual_mask = data_visualize.visualize_data(data=mask).astype(int) else: self.visual_data = visualize_single_panel( data, self.PF8Config.transformation_matrix, self.PF8Config.ss_in_rows, ) - visual_mask = visualize_single_panel( + self.visual_mask = visualize_single_panel( mask, self.PF8Config.transformation_matrix, self.PF8Config.ss_in_rows, @@ -748,14 +805,14 @@ def _prep_for_centering(self, data: np.ndarray, initial_guess: tuple) -> None: self.visual_data = data_visualize.visualize_data( data=pol_corrected_data * mask ) - visual_mask = data_visualize.visualize_data(data=mask).astype(int) + self.visual_mask = data_visualize.visualize_data(data=mask).astype(int) else: self.visual_data = visualize_single_panel( pol_corrected_data, self.PF8Config.transformation_matrix, self.PF8Config.ss_in_rows, ) - visual_mask = visualize_single_panel( + self.visual_mask = visualize_single_panel( mask, self.PF8Config.transformation_matrix, self.PF8Config.ss_in_rows, @@ -817,7 +874,7 @@ def _run_centering(self, **kwargs) -> tuple: print(peaks_list_after_correction) print(f"-- End --") - center[0] += self.config["offset"]["x"] + center[0] += self.config["offset"]["x"] center[1] += self.config["offset"]["y"] else: center = [-1, -1] @@ -826,21 +883,30 @@ def _run_centering(self, **kwargs) -> tuple: fig, ax1 = plt.subplots(1, 1, figsize=(10, 10)) if self.plots_info["value_auto"]: + color_map = copy.copy( + matplotlib.colormaps[self.plots_info["color_map"]] + ) + color_map.set_bad(color_map(0)) pos = ax1.imshow( - self.visual_data, + self.visual_data * self.visual_mask, norm=LogNorm(), - cmap=self.plots_info["color_map"], + cmap=color_map, origin="lower", ) else: + color_map = copy.copy( + matplotlib.colormaps[self.plots_info["color_map"]] + ) + color_map.set_bad(color_map(0)) pos = ax1.imshow( - self.visual_data, + self.visual_data * self.visual_mask, norm=LogNorm( self.plots_info["value_min"], self.plots_info["value_max"] ), - cmap=self.plots_info["color_map"], + cmap=color_map, origin="lower", ) + """ ax1.scatter( self.initial_detector_center[0], self.initial_detector_center[1], @@ -849,12 +915,15 @@ def _run_centering(self, **kwargs) -> tuple: s=25, label=f"Initial detector center:({np.round(self.initial_detector_center[0],1)},{np.round(self.initial_detector_center[1], 1)})", ) + """ ax1.scatter( self.initial_guess[0], self.initial_guess[1], - color="lime", - marker="+", - s=150, + color="b", + marker="o", + edgecolor="black", + linewidth=0.5, + s=self.plots_info["marker_size"], label=f"Initial guess:({np.round(self.initial_guess[0],1)},{np.round(self.initial_guess[1], 1)})", ) @@ -862,14 +931,20 @@ def _run_centering(self, **kwargs) -> tuple: center[0], center[1], color="r", - marker="o", - s=25, + marker="^", + edgecolor="black", + linewidth=0.5, + s=self.plots_info["marker_size"], label=f"Refined detector center:({np.round(center[0],1)}, {np.round(center[1],1)})", ) - plt.title("Center refinement: autocorrelation of Friedel pairs") - fig.colorbar(pos, ax=ax1, shrink=0.6) - ax1.legend() + cbar = fig.colorbar(pos, ax=ax1, shrink=0.6) + cbar.ax.tick_params(labelsize=20) + ax1.legend(fontsize=14, loc=1, markerscale=1) + plt.tick_params(axis="both", which="major", labelsize=16) + ax1.set_xlabel("x (pixel)", fontsize=20) + ax1.set_ylabel("y (pixel)", fontsize=20) + if not self.plots_info["axis_lim_auto"]: ax1.set_xlim(self.plots_info["xlim_min"], self.plots_info["xlim_max"]) ax1.set_ylim(self.plots_info["ylim_min"], self.plots_info["ylim_max"]) @@ -908,19 +983,27 @@ def _run_centering(self, **kwargs) -> tuple: ## Check pairs alignement fig, ax1 = plt.subplots(1, 1, figsize=(10, 10)) if self.plots_info["value_auto"]: + color_map = copy.copy( + matplotlib.colormaps[self.plots_info["color_map"]] + ) + color_map.set_bad(color_map(0)) pos = ax1.imshow( - self.visual_data, + self.visual_data * self.visual_mask, norm=LogNorm(), - cmap=self.plots_info["color_map"], + cmap=color_map, origin="lower", ) else: + color_map = copy.copy( + matplotlib.colormaps[self.plots_info["color_map"]] + ) + color_map.set_bad(color_map(0)) pos = ax1.imshow( - self.visual_data, + self.visual_data * self.visual_mask, norm=LogNorm( self.plots_info["value_min"], self.plots_info["value_max"] ), - cmap=self.plots_info["color_map"], + cmap=color_map, origin="lower", ) @@ -928,43 +1011,46 @@ def _run_centering(self, **kwargs) -> tuple: original_peaks_x, original_peaks_y, facecolor="none", - s=80, - marker="s", + s=0.8 * self.plots_info["marker_size"], + marker="o", edgecolor="red", - linewidth=1.5, + linewidth=1.8, label="original peaks", ) ax1.scatter( inverted_non_shifted_peaks_x, inverted_non_shifted_peaks_y, - s=80, + s=1.2 * self.plots_info["marker_size"], facecolor="none", marker="s", - edgecolor="tab:orange", - linewidth=1.5, + edgecolor="blue", + linewidth=1.8, label="inverted peaks", - alpha=0.8, + alpha=1, ) ax1.scatter( inverted_shifted_peaks_x, inverted_shifted_peaks_y, facecolor="none", - s=120, + s=1.2 * self.plots_info["marker_size"], marker="D", linewidth=1.8, - alpha=0.8, - edgecolor="blue", - label="shift of inverted peaks", + alpha=1, + edgecolor="green", + label="inverted peaks shifted", ) if not self.plots_info["axis_lim_auto"]: ax1.set_xlim(self.plots_info["xlim_min"], self.plots_info["xlim_max"]) ax1.set_ylim(self.plots_info["ylim_min"], self.plots_info["ylim_max"]) - plt.title("Bragg peaks alignement") - fig.colorbar(pos, ax=ax1, shrink=0.6) - ax1.legend() + cbar = fig.colorbar(pos, ax=ax1, shrink=0.6) + cbar.ax.tick_params(labelsize=20) + ax1.legend(fontsize=14, loc=1, markerscale=1) + plt.tick_params(axis="both", which="major", labelsize=16) + ax1.set_xlabel("x (pixel)", fontsize=20) + ax1.set_ylabel("y (pixel)", fontsize=20) path = pathlib.Path( f'{self.plots_info["root_path"]}/center_refinement/plots/{self.plots_info["folder_name"]}/peaks/' ) diff --git a/bblib/models.py b/bblib/models.py index 1e7c475..94f9fa2 100755 --- a/bblib/models.py +++ b/bblib/models.py @@ -8,7 +8,6 @@ TypeDetectorLayoutInformation, GeometryInformation, _read_crystfel_geometry_from_text, - _parse_direction ) @@ -121,7 +120,7 @@ def set_geometry_from_file(self, geometry_filename: str = None): self.ss_in_rows = True else: self.ss_in_rows = False - + fs_string = [ x.split(" = ")[-1][:-1] for x in self.geometry_txt @@ -134,15 +133,15 @@ def set_geometry_from_file(self, geometry_filename: str = None): if (x.split(" = ")[0]).split("/")[-1] == "ss" ][0] pattern = r"([-+]?\d*\.?\d+)(?=[xyz])" - - try: + + try: fsx, fsy, fsz = re.findall(pattern, fs_string) - except(ValueError): + except ValueError: fsx, fsy = re.findall(pattern, fs_string) - try: + try: ssx, ssy, ssz = re.findall(pattern, ss_string) - except(ValueError): + except ValueError: ssx, ssy = re.findall(pattern, ss_string) ## The transformation matrix here is only for visualization purposes. Small stretching factors won't have an impact on the visualization of the images (slabby data). diff --git a/bblib/utils.py b/bblib/utils.py index b6fc15f..876a2d3 100755 --- a/bblib/utils.py +++ b/bblib/utils.py @@ -360,6 +360,7 @@ def get_fwhm_map_global_min( plt.close() return [np.round(xc, 0), np.round(yc, 0)] + def circle_mask(data: np.ndarray, center: tuple, radius: int) -> np.ndarray: """ Make a ring mask for the data