DAQ: Changes to automation and debugging statements improved in alc

This commit is contained in:
appleb_m committed 2025-11-20 09:17:11 +01:00
1 parent 1acb69e5f2
commit bdfaf31c93
1 file changed
+50 -41
+50 -41
View File
@@ -63,10 +63,6 @@ class AareDAQ:
self.__bl = bl.value.upper()
self.__aare = AareWrapper(bl)
self.__saved_box = None
self.crystal_size = CrystalSize(x=0,y=0,z=0)
self.last_best_b_factor = None
self.last_best_res = None
self.auto_params = SimpleScanParameters()
@property
def state(self) -> BeamlineStateEnum:
@@ -91,21 +87,19 @@ class AareDAQ:
self.last_time = end - start
@property
def smart_params(self) -> SimpleScanParameters:
return self.auto_params
def spreadsheet_params(self) -> tuple[Optional[SimpleScanParameters], str|None]:
file_prefix = None
@smart_params.setter
def smart_params(self, params: SimpleScanParameters):
self.auto_params = params
def spreadsheet_params(self) -> Optional[SimpleScanParameters]:
if self.status.sample is None:
return None
return None, file_prefix
logger.debug(f"generate params for sample: {self.status.sample}")
aaredb_params = self.status.sample.aaredb_params if hasattr(self.status.sample, "aaredb_params") else None
if aaredb_params is None:
return None
return None, file_prefix
#if aaredb_params.directory:
# file_prefix = aaredb_params.directory
if (
getattr(aaredb_params, 'exposure', None) is None
@@ -127,14 +121,17 @@ class AareDAQ:
if (res := getattr(aaredb_params, 'targetresolution', None)) is not None:
logger.debug(f"resolution: {res}")
logger.debug(f"dtz: {self.diffraction_geometry.calc_dtz_mm(res)}")
params.dtz = self.diffraction_geometry.calc_dtz_mm(res)
logger.debug(f"requested dtz: {self.diffraction_geometry.calc_dtz_mm(res)} ")
new_res = 1 / ((1 / res) + 0.1)
corrected_dtz = self.diffraction_geometry.calc_dtz_mm(new_res)
logger.debug(f"corrected dtz: {corrected_dtz}")
if corrected_dtz < 108:
corrected_dtz = 108
params.dtz = round(corrected_dtz)
osc = getattr(aaredb_params, 'oscillation', None)
total = getattr(aaredb_params, 'totalrange', None)
default_osc = SimpleScanParameters().incr_omega_deg
if osc is not None:
osc = abs(osc)
if osc > 0:
@@ -144,10 +141,14 @@ class AareDAQ:
else:
params.steps = round(360.0 / osc)
elif total is not None and abs(total) > 0:
default_osc = SimpleScanParameters().incr_omega_deg
params.incr_omega_deg = default_osc
params.steps = round(abs(total) / default_osc)
return params
#if file_prefix is not None:
# params.file_prefix = file_prefix
return params, file_prefix
@property
@@ -393,9 +394,9 @@ class AareDAQ:
curr_sample_is_manual = False
logger.info(f"current sample: {curr_sample}")
logger.info(f"new target is: {target}")
self.crystal_size = CrystalSize(x=0, y=0, z=0)
self.last_best_b_factor = None
self.last_best_res = None
self.__cfg.crystal_size = CrystalSize(x=0, y=0, z=0)
self.__cfg.last_best_b_factor = None
self.__cfg.last_best_res = None
self.__cfg.xrf = None
if curr_sample is not None and curr_sample.location is None:
@@ -555,48 +556,43 @@ class AareDAQ:
}
if self.sample is not None and self.sample.db_id is not None:
if r.n_x == 1:
filename=f'{self.sample.db_id}_scan_results_vertical.json'
filename=f'/sls/MX/applications/raster_results/{self.sample.db_id}_scan_results_vertical.json'
else:
filename=f'{self.sample.db_id}_scan_results_horizontal.json'
filename=f'/sls/MX/applications/raster_results/{self.sample.db_id}_scan_results_horizontal.json'
with open(filename, 'w') as f:
json.dump(output_data, f, indent=2)
print('before centre_of_mass')
print("COM by low res spots")
result_array = create_quality_filtered_array(images, 'spots_low_res', min_spots=None,
min_efficiency=1.0, min_background=None, min_low_res_spots=10.0)
self.crystal_size = get_xtal_size(self.crystal_size, result_array, r)
self.__cfg.crystal_size = get_xtal_size(self.__cfg.crystal_size, result_array, r)
grid_mm_x, grid_mm_y, com = raster_centre_of_mass(result_array, r)
if grid_mm_x is None or grid_mm_y is None:
print(f"using old method as COM is none or nan")
if grid_mm_x is None or grid_mm_y is None or com is None:
logger.debug(f"using old method as COM is disabled")
grid_mm_x, grid_mm_y = identify_crystal_raster(result, r)
if grid_mm_x or grid_mm_y:
if r.n_x == 1:
new_delta_mm = self.sample_geometry.smargon_nudge(Coordinate(x=0, y=grid_mm_y))
new_delta_mm = self.sample_geometry.smargon_nudge(Coordinate(x=grid_mm_x, y=grid_mm_y))
else:
result_list = get_result_list_from_com(images, com)
print(result_list)
self.last_best_b_factor = get_best_b_factor(result_list)
self.last_best_res = get_best_res(result_list)
print("b_factor: ", self.last_best_b_factor, " best_res: ", self.last_best_res)
self.__cfg.last_best_b_factor = get_best_b_factor(result_list)
self.__cfg.last_best_res = get_best_res(result_list)
logger.debug(f"b_factor: {self.__cfg.last_best_b_factor}, best_res: {self.__cfg.last_best_res}")
new_delta_mm = self.sample_geometry.smargon_nudge(Coordinate(x=grid_mm_x, y=grid_mm_y))
else:
new_delta_mm = None
print('after centre_of_mass')
if new_delta_mm is not None:
print(f'{time.ctime()}, moving SMARGON to target new delta mm {r.smargon.sh_mm + new_delta_mm} mm')
logger.debug(f'{time.ctime()}, moving SMARGON to target new delta mm {r.smargon.sh_mm + new_delta_mm} mm')
self.__devs.smargon.target = SmargonCoordinate(sh_mm=r.smargon.sh_mm + new_delta_mm,
phi_deg=r.smargon.phi_deg,
chi_deg=r.smargon.chi_deg)
else:
print("Auto finding optimal image failed due to no images found. Using previous position.")
logger.error("Auto finding optimal image failed due to no images found. Using previous position.")
self.__devs.smargon.target = save_smargon_position
return CompletedRasterGridElem(request=copy.deepcopy(r), result=result)
@@ -1114,16 +1110,20 @@ class AareDAQ:
try:
target, cls, classes = self.__ml_loop_centre_box(sample_id, filename)
except:
except Exception as e:
logger.error(f"Error getting ML box for angle {angle}")
logger.error(f"Exception: {e}")
target, cls, classes = None, None, None
if target is None:
logger.debug("no target found")
continue
if classes:
for c in classes:
found_classes_count[int(c)] += 1
logger.debug(f"classes found: {classes}")
logger.debug(f"class found: {cls}")
if cls is not None and cls != 1:
targets_found_this_attempt += 1
found_flag = True
@@ -1135,7 +1135,8 @@ class AareDAQ:
logger.info(f"time to move smargon: {time.perf_counter() - time_to_move_smargon}")
if targets_found_this_attempt == 0:
raise LoopCenteringFailed
logger.error(f"No targets found in this attempt {attempt}")
raise LoopCenteringFailed
else:
if targets_found_this_attempt >= len(base_angles):
@@ -1144,9 +1145,11 @@ class AareDAQ:
if found_flag is not None and found_angle is not None:
logger.debug(f"found a target at angle {found_angle} in attempt {attempt + 1}")
base_angles = (found_angle, found_angle + 45, found_angle + 90)
logger.debug(f"new base angles: {base_angles}")
attempt += 1
logger.debug(f"attempt {attempt} of {max_attempt}")
if attempt >= max_attempt:
logger.error(f"{attempt} exceeds max attempts {max_attempt}")
raise LoopCenteringFailed
#i += 1
@@ -1159,6 +1162,7 @@ class AareDAQ:
except Exception as e:
total_detections = sum(found_classes_count.values())
if total_detections == 0:
logger.error("ALC: No objects found in any zoom")
self.__aare.alc_failed(self.sample, alc_comment="No objects detected")
else:
self.__aare.alc_failed(
@@ -1171,6 +1175,11 @@ class AareDAQ:
f"Pin: {found_classes_count.get(1,0)}"
),
)
logger.error("Failed to centre but detected objects - "
f"Crystal: {found_classes_count.get(2,0)}, "
f"Loop_face: {found_classes_count.get(3,0)}, "
f"Loop_all: {found_classes_count.get(0,0)}, "
f"Pin: {found_classes_count.get(1,0)}")
logger.info(f"Error in loop centering: {e}")
return False