11
11
)
12
12
from xchembku_api .models .crystal_well_model import CrystalWellModel
13
13
14
+ from chimpflow_api .constants import WELL_CENTROID_ALGORITHMS
15
+
14
16
with warnings .catch_warnings ():
15
17
warnings .filterwarnings ("ignore" , category = DeprecationWarning )
16
18
from xchem_chimp .detector .chimp_detector import ChimpDetector
@@ -47,6 +49,21 @@ def __init__(self, specification: Dict):
47
49
"num_classes" ,
48
50
)
49
51
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
+
50
67
self .__is_activated = False
51
68
self .__detector : Optional [ChimpDetector ] = None
52
69
@@ -110,9 +127,10 @@ def detect(
110
127
with self .__profiler .context ("coord_generator.extract_coordinates()" ):
111
128
coord_generator .extract_coordinates ()
112
129
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 ()
116
134
117
135
# Get the output stucture for the first (only) image.
118
136
# TODO: Store the chimp detector output structure as json in the database.
@@ -123,17 +141,36 @@ def detect(
123
141
crystal_well_uuid = crystal_well_model .uuid ,
124
142
)
125
143
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
+ )
127
149
if len (target_position ) > 0 :
128
150
# The target position is a list of (np.int64, np.int64), so have to convert to int.
129
151
# Coordinate pairs are vertical-first.
130
152
# TODO: Change the CrystalWellAutolocationModel to do type checking on field assignment.
131
153
model .auto_target_x = int (target_position [0 ][1 ])
132
154
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
+ )
135
162
model .well_centroid_x = int (well_centroid [1 ])
136
163
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
+
137
174
model .number_of_crystals = len (output_dict ["xtal_coordinates" ])
138
175
139
176
# TODO: Store the chimp detected crystal coordinates in the model too.
0 commit comments