From a15c6c9c4706876e03e761ae9a8b384b186926ba Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Fri, 10 Oct 2025 17:19:29 +0200 Subject: [PATCH] DAQ: moved raster calculations to find_xtal.py --- common/src/aaredaqlib/find_xtal.py | 80 ++++++++++++++---------------- 1 file changed, 38 insertions(+), 42 deletions(-) diff --git a/common/src/aaredaqlib/find_xtal.py b/common/src/aaredaqlib/find_xtal.py index a40ecec2..1eb493d9 100644 --- a/common/src/aaredaqlib/find_xtal.py +++ b/common/src/aaredaqlib/find_xtal.py @@ -7,7 +7,7 @@ from aaredaqlib.models import CrystalSize from aaredaqlib.raster_grid import RasterGridRequest -def identify_crystal_raster(self, result, r: RasterGridRequest): +def identify_crystal_raster(result, r: RasterGridRequest): images = result.images if images and any(img.spots > 0 for img in images): @@ -47,8 +47,7 @@ def identify_crystal_raster(self, result, r: RasterGridRequest): else: return None, None -def rebuild_array_from_scan_results(self, - scan_results: List, +def rebuild_array_from_scan_results(scan_results: List, value_field: str, array_shape: Optional[tuple] = None, nx_field: str = 'nx', @@ -127,25 +126,25 @@ def rebuild_array_from_scan_results(self, return result_array -def create_quality_filtered_array(self, - scan_results: List, +def create_quality_filtered_array(scan_results: List, value_field: str, min_spots: Optional[int] = None, min_efficiency: Optional[float] = 1.0, min_background: Optional[float] = None, exclude_ice: Optional[bool] = True, + min_low_res_spots: Optional[float] = 10.0, **kwargs ) -> np.ndarray: """ Create array with comprehensive quality filtering """ - def quality_condition(result, min_bkg, min_spots, min_efficiency): + def quality_condition(result, min_bkg, min_spots, min_efficiency, min_res_spots = 10.0): if exclude_ice and (result.spots_ice / max(result.spots_low_res, 1.0)) == 1.0: # print(f"all ice for {result.number}") return False - # if (result.spots_ice / result.spots_low_res) > 5.0: - # return False + if result.spots_low_res < min_res_spots: + return False if exclude_ice and result.spots_ice > result.spots * 0.8: # More than 50% ice # print(f"more than 80% ice for {result.number}") return False @@ -176,7 +175,7 @@ def create_quality_filtered_array(self, for result in scan_results: if result.nx is not None and result.ny is not None: if quality_condition(result, min_bkg=min_background, min_spots=min_spots, - min_efficiency=min_efficiency): + min_efficiency=min_efficiency, min_res_spots=min_low_res_spots): filtered_results.append(result) else: # Create a copy with zero value for filtered positions @@ -185,40 +184,35 @@ def create_quality_filtered_array(self, setattr(zero_result, value_field, 0) filtered_results.append(zero_result) - return self.rebuild_array_from_scan_results(filtered_results, value_field, **kwargs) + return rebuild_array_from_scan_results(filtered_results, value_field, **kwargs) def get_xtal_size(crystal_size, result_array, r:RasterGridRequest): # Optional: get bounding box of the largest object - labeled_array, num_objects = ndimage.label(result_array) - areas = ndimage.sum(np.ones_like(result_array, dtype=np.int32), labeled_array, - index=range(1, num_objects + 1)) - largest_idx = int(np.argmax(areas)) + 1 # +1 because labels start at 1 - largest_area = int(areas[largest_idx - 1]) - print(f"Largest object label: {largest_idx}, area (px): {largest_area}") - - object_mask = labeled_array == largest_idx - rows = np.any(object_mask, axis=1) - cols = np.any(object_mask, axis=0) - row_min, row_max = np.where(rows)[0][[0, -1]] - col_min, col_max = np.where(cols)[0][[0, -1]] - print(f"Largest bbox: width={col_max - col_min}, height={row_max - row_min}") - print( - f"Largest bbox: width={(col_max - col_min) * r.grid_size_mm.x}, y={(row_max - row_min) * r.grid_size_mm.y}") try: - if r.n_x == 1: + labeled_array, num_objects = ndimage.label(result_array) + areas = ndimage.sum(np.ones_like(result_array, dtype=np.int32), labeled_array, + index=range(1, num_objects + 1)) + largest_idx = int(np.argmax(areas)) + 1 # +1 because labels start at 1 + largest_area = int(areas[largest_idx - 1]) + print(f"Largest object label: {largest_idx}, area (px): {largest_area}") + object_mask = labeled_array == largest_idx + rows = np.any(object_mask, axis=1) + cols = np.any(object_mask, axis=0) + row_min, row_max = np.where(rows)[0][[0, -1]] + col_min, col_max = np.where(cols)[0][[0, -1]] + print(f"Largest bbox: width={col_max - col_min}, height={row_max - row_min}") + print( + f"Largest bbox: width={(col_max - col_min) * r.grid_size_mm.x}, y={(row_max - row_min) * r.grid_size_mm.y}") + if r.n_x == 1: + crystal_size = CrystalSize(x=crystal_size.x, y=crystal_size.y, + z=(col_max - col_min) * r.grid_size_mm.y * 1000) - - crystal_size = crystal_size - - crystal_size = CrystalSize(x=crystal_size.x, y=crystal_size.y, - z=(col_max - col_min) * r.grid_size_mm.y * 1000) - - else: - crystal_size = CrystalSize(x=(row_max - row_min) * r.grid_size_mm.x * 1000, - y=(col_max - col_min) * r.grid_size_mm.y * 1000, - z=crystal_size.z) + else: + crystal_size = CrystalSize(x=(row_max - row_min) * r.grid_size_mm.x * 1000, + y=(col_max - col_min) * r.grid_size_mm.y * 1000, + z=crystal_size.z) except ValueError as e: print(f"error calculating xtal size: {e}") crystal_size = CrystalSize(x=0,y=0,z=0) @@ -232,8 +226,10 @@ def get_best_b_factor(result_list: List): best_b_factor = min((img for img in result_list if img.b is not None), key=lambda img: img.b, default=None) + if best_b_factor is None: + return None print(f"Best b: {best_b_factor.b}") - return best_b_factor + return best_b_factor.b def get_best_res(result_list: List): if not result_list: @@ -241,8 +237,8 @@ def get_best_res(result_list: List): best_res = min((img for img in result_list if img.res is not None), key=lambda img: img.res, default=None) - print(f"Best res: {best_res}") - return best_res + print(f"Best res: {best_res.res}") + return best_res.res def com_nan_check(com): if np.isnan(com[0]) or np.isnan(com[1]): @@ -253,7 +249,7 @@ def com_nan_check(com): def get_result_list_from_com(images, com): if not com_nan_check(com): return None - cx, cy = com[::-1] + cy, cx = com[::-1] start_x, end_x = round(cx - 1), round(cx + 1) start_y, end_y = round(cy - 1), round(cy + 1) print(f"range x {start_x} {end_x}, y {start_y} {end_y}") @@ -279,10 +275,10 @@ def get_grid_mm_from_com(com, r:RasterGridRequest): return grid_mm_x, grid_mm_y -def raster_centre_of_mass(self, result_array, r:RasterGridRequest): +def raster_centre_of_mass(result_array, r:RasterGridRequest): print('horizontal scan') com = ndimage.center_of_mass(result_array) print(f"Center of mass: {com}") - grid_mm_x, grid_mm_y = self.get_grid_mm_from_com(com, r) + grid_mm_x, grid_mm_y = get_grid_mm_from_com(com, r) return grid_mm_x, grid_mm_y, com