1111)
1212from xchembku_api .models .crystal_well_model import CrystalWellModel
1313
14+ from chimpflow_api .constants import WELL_CENTROID_ALGORITHMS
15+
1416with warnings .catch_warnings ():
1517 warnings .filterwarnings ("ignore" , category = DeprecationWarning )
1618 from xchem_chimp .detector .chimp_detector import ChimpDetector
@@ -47,6 +49,21 @@ def __init__(self, specification: Dict):
4749 "num_classes" ,
4850 )
4951
52+ # Caller specifies the well centroid algorithm they want to use.
53+ # None means don't calculate well centroid.
54+ self .__well_centroid_algorithm = specification .get ("well_centroid_algorithm" )
55+
56+ if self .__well_centroid_algorithm == WELL_CENTROID_ALGORITHMS .TEXRANK_LIKE :
57+ pass
58+ elif self .__well_centroid_algorithm == WELL_CENTROID_ALGORITHMS .MIDDLE_PIXEL :
59+ pass
60+ elif self .__well_centroid_algorithm is None :
61+ pass
62+ else :
63+ raise RuntimeError (
64+ "configuration error: invalid well_centroid_algorithm '{self.__well_centroid_algorithm}'"
65+ )
66+
5067 self .__is_activated = False
5168 self .__detector : Optional [ChimpDetector ] = None
5269
@@ -110,9 +127,10 @@ def detect(
110127 with self .__profiler .context ("coord_generator.extract_coordinates()" ):
111128 coord_generator .extract_coordinates ()
112129
113- # Calculate well centers.
114- with self .__profiler .context ("coord_generator.calculate_well_centres()" ):
115- coord_generator .calculate_well_centres ()
130+ if self .__well_centroid_algorithm == WELL_CENTROID_ALGORITHMS .TEXRANK_LIKE :
131+ # Calculate well centers.
132+ with self .__profiler .context ("coord_generator.calculate_well_centres()" ):
133+ coord_generator .calculate_well_centres ()
116134
117135 # Get the output stucture for the first (only) image.
118136 # TODO: Store the chimp detector output structure as json in the database.
@@ -123,17 +141,36 @@ def detect(
123141 crystal_well_uuid = crystal_well_model .uuid ,
124142 )
125143 model .drop_detected = output_dict ["drop_detected" ]
126- target_position = output_dict ["echo_coordinate" ]
144+ target_position = require (
145+ "coord_generator output_dict" ,
146+ output_dict ,
147+ "echo_coordinate" ,
148+ )
127149 if len (target_position ) > 0 :
128150 # The target position is a list of (np.int64, np.int64), so have to convert to int.
129151 # Coordinate pairs are vertical-first.
130152 # TODO: Change the CrystalWellAutolocationModel to do type checking on field assignment.
131153 model .auto_target_x = int (target_position [0 ][1 ])
132154 model .auto_target_y = int (target_position [0 ][0 ])
133- well_centroid = output_dict ["well_centroid" ]
134- if well_centroid is not None :
155+
156+ if self .__well_centroid_algorithm == WELL_CENTROID_ALGORITHMS .TEXRANK_LIKE :
157+ well_centroid = require (
158+ "coord_generator output_dict" ,
159+ output_dict ,
160+ "well_centroid" ,
161+ )
135162 model .well_centroid_x = int (well_centroid [1 ])
136163 model .well_centroid_y = int (well_centroid [0 ])
164+ # Anything else is assumed MIDDLE_PIXEL.
165+ else :
166+ original_image_shape = require (
167+ "coord_generator output_dict" ,
168+ output_dict ,
169+ "original_image_shape" ,
170+ )
171+ model .well_centroid_x = int (original_image_shape [1 ] / 2.0 )
172+ model .well_centroid_y = int (original_image_shape [0 ] / 2.0 )
173+
137174 model .number_of_crystals = len (output_dict ["xtal_coordinates" ])
138175
139176 # TODO: Store the chimp detected crystal coordinates in the model too.
0 commit comments