Skip to content

Commit

Permalink
Save flat and edge images (#53)
Browse files Browse the repository at this point in the history
* plotting is faster

* tests

* more tests

* plot tests

* bump version
  • Loading branch information
fhernandezvivanco authored Jan 21, 2025
1 parent e159e49 commit f68ed69
Show file tree
Hide file tree
Showing 7 changed files with 787 additions and 30 deletions.
33 changes: 16 additions & 17 deletions mx3_beamline_library/plans/optical_centering.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pickle
from io import BytesIO
from os import getcwd, mkdir, path
from typing import Generator, Union
from os import getcwd, makedirs, path
from typing import Generator

import matplotlib.pyplot as plt
import numpy as np
Expand Down Expand Up @@ -57,11 +57,11 @@ def __init__(
self,
sample_id: str,
beam_position: tuple[int, int],
grid_step: Union[tuple[float, float], None] = None,
grid_step: tuple[float, float] | None = None,
calibrated_alignment_z: float = 0.634,
plot: bool = False,
top_camera_background_img_array: npt.NDArray = None,
output_directory: Union[str, None] = None,
output_directory: str | None = None,
use_top_camera_camera: bool = True,
manual_mode: bool = False,
extra_config: OpticalCenteringExtraConfig | None = None,
Expand All @@ -73,7 +73,7 @@ def __init__(
Sample id
beam_position : tuple[int, int]
Position of the beam
grid_step : Union[tuple[float, float], None]
grid_step : tuple[float, float] | None
The step of the grid (x,y) in micrometers. Can also be None
only if manual_mode=True
calibrated_alignment_z : float, optional.
Expand All @@ -87,7 +87,7 @@ def __init__(
Top camera background image array used to determine if there is a pin.
If top_camera_background_img_array is None, we use the default background image from
the mx3-beamline-library
output_directory : Union[str, None], optional
output_directory : str | None, optional
The directory where all diagnostic plots are saved if self.plot=True.
If output_directory=None, we use the current working directory,
by default None
Expand Down Expand Up @@ -139,16 +139,15 @@ def __init__(
self.sample_path = path.join(self.output_directory, self.sample_id)
if self.plot:
try:
mkdir(self.sample_path)
makedirs(self.sample_path)
except FileExistsError:
pass
self.use_top_camera_camera = use_top_camera_camera
self.manual_mode = manual_mode

if not self.manual_mode:
assert (
self.grid_step is not None
), "grid_step can only be None if manual_mode=True"
if grid_step is None:
raise ValueError("grid_step can only be None if manual_mode=True")

def _check_top_camera_config(self) -> None:
"""
Expand Down Expand Up @@ -628,7 +627,7 @@ def find_loop_edge_coordinates(self) -> tuple[float, float]:
Returns
-------
tuple[float, float]
The x and y pixel coordinates of the edge of the loop,
The x and y pixel coordinates of the edge of the loop
"""
x_coord_list = []
y_coord_list = []
Expand Down Expand Up @@ -787,12 +786,12 @@ def find_edge_and_flat_angles(self) -> Generator[Msg, None, None]:
plt.figure()
plt.plot(x_new, y_new, label="Curve fit")
plt.scatter(np.radians(omega_list), np.array(area_list), label="Data")
plt.xlabel("$\omega$ [radians]", fontsize=18)
plt.ylabel("Area [pixels$^2$]", fontsize=18)
plt.xlabel("omega [radians]")
plt.ylabel("Area [pixels^2]")
plt.legend(fontsize=15)
plt.tight_layout()
# plt.tight_layout()
filename = path.join(self.sample_path, f"{self.sample_id}_area_curve_fit")
plt.savefig(filename)
plt.savefig(filename, dpi=70)
plt.close()
return successful_centering

Expand Down Expand Up @@ -1006,8 +1005,8 @@ def save_image(
c="r",
marker="x",
)
plt.title(f"$\omega={round(md3.omega.position)}^\circ$", fontsize=18)
plt.savefig(filename)
plt.title(f"omega={round(md3.omega.position)}", fontsize=18)
plt.savefig(filename, dpi=70)
plt.close()

def prepare_raster_grid(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,5 @@ def plot_raster_grid(
)
x = rectangle_coordinates.top_left[0] * np.ones(len(x))
plt.plot(x, z, color="red", linestyle="--")
plt.savefig(filename)
plt.savefig(filename, dpi=70)
plt.close()
Loading

0 comments on commit f68ed69

Please sign in to comment.