diff --git a/common/src/aaredaqlib/find_xtal.py b/common/src/aaredaqlib/find_xtal.py index 5e0bf959..80ad4771 100644 --- a/common/src/aaredaqlib/find_xtal.py +++ b/common/src/aaredaqlib/find_xtal.py @@ -5,7 +5,9 @@ from scipy import ndimage from aaredaqlib.models import CrystalSize from aaredaqlib.raster_grid import RasterGridRequest +from aaredaqlib.logger_config import setup_logger +logger = setup_logger('aareDAQ') def identify_crystal_raster(result, r: RasterGridRequest): images = result.images @@ -23,25 +25,25 @@ def identify_crystal_raster(result, r: RasterGridRequest): and (img.spots_ice / img.spots_low_res) != 1] if indexed_images: - print(f"Find image by maximum number of spots indexed") + logger.debug(f"Find image by maximum number of spots indexed") max_image = max(images, key=lambda img: img.spots_indexed) max_spots = max_image.spots_indexed max_images = [img for img in images if img.spots_indexed == max_spots] max_image = max_images[len(max_images) // 2] - print(f"Image with maximum spots_low_res: {max_image}") - print(f"Maximum spots_indexed value: {max_image.spots_indexed}") - print(f"Maximum spots_low_res value: {max_image.spots_low_res}") + logger.debug(f"Image with maximum spots_low_res: {max_image}") + logger.debug(f"Maximum spots_indexed value: {max_image.spots_indexed}") + logger.debug(f"Maximum spots_low_res value: {max_image.spots_low_res}") else: - print(f"Find image by maximum number of low resolution spots") + logger.debug(f"Find image by maximum number of low resolution spots") max_image = max(images, key=lambda img: img.spots_low_res) - print(f"Image with maximum spots_low_res: {max_image}") - print(f"Maximum spots_low_res value: {max_image.spots_low_res}") + logger.debug(f"Image with maximum spots_low_res: {max_image}") + logger.debug(f"Maximum spots_low_res value: {max_image.spots_low_res}") grid_mm_x = max_image.nx * r.grid_size_mm.x grid_mm_y = max_image.ny * r.grid_size_mm.y - print(f"Grid coordinates in mm: ({grid_mm_x}, {grid_mm_y})") + logger.debug(f"Grid coordinates in mm: ({grid_mm_x}, {grid_mm_y})") return grid_mm_x, grid_mm_y else: @@ -76,6 +78,7 @@ def rebuild_array_from_scan_results(scan_results: List, values.append(float(value)) if not positions: + logger.error("No valid positions found in scan results") raise ValueError("No valid positions found in scan results") # Determine array shape @@ -195,26 +198,28 @@ def get_xtal_size(crystal_size, result_array, r:RasterGridRequest): 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}") + logger.info(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( + logger.info(f"Largest bbox: width={col_max - col_min}, height={row_max - row_min}") + logger.info( 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: + logger.debug("calculating z") crystal_size = CrystalSize(x=crystal_size.x, y=crystal_size.y, z=(col_max - col_min) * r.grid_size_mm.y * 1000) else: + logger.debug("calculating x and y") 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}") + logger.error(f"error calculating xtal size: {e}") crystal_size = CrystalSize(x=0,y=0,z=0) return crystal_size @@ -228,7 +233,7 @@ def get_best_b_factor(result_list: List): default=None) if best_b_factor is None: return None - print(f"Best b: {best_b_factor.b}") + logger.info(f"Best b: {best_b_factor.b}") return best_b_factor.b def get_best_res(result_list: List): @@ -237,7 +242,7 @@ 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.res}") + logger.info(f"Best res: {best_res.res}") return best_res.res def com_nan_check(com): @@ -252,7 +257,7 @@ def get_result_list_from_com(images, com): 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}") + logger.info(f"range x {start_x} {end_x}, y {start_y} {end_y}") result_list = [img for img in images if start_x <= img.nx <= end_x and start_y <= img.ny <= end_y] return result_list @@ -260,7 +265,7 @@ def get_result_list_from_com(images, com): def get_com_image_number(com, images): for image in images: if image.nx == round(com[0]) and image.ny == round(com[1]): - print(f"com found for image: {image.number}") + logger.info(f"com found for image: {image.number}") return def get_grid_mm_from_com(com, r:RasterGridRequest): @@ -276,9 +281,9 @@ def get_grid_mm_from_com(com, r:RasterGridRequest): def raster_centre_of_mass(result_array, r:RasterGridRequest): - print('horizontal scan') + logger.debug('horizontal scan') com = ndimage.center_of_mass(result_array) - print(f"Center of mass: {com}") + logger.info(f"Center of mass: {com}") grid_mm_x, grid_mm_y = get_grid_mm_from_com(com, r) return grid_mm_x, grid_mm_y, com