1+ import logging
2+
13import numpy as np
24from sqlmodel import Session , select
35
79from murfey .util .config import get_machine_config
810from murfey .util .db import DataCollectionGroup , SearchMap
911from murfey .util .db import Session as MurfeySession
12+ from murfey .util .db import TiltSeries
1013from murfey .util .models import BatchPositionParameters , SearchMapParameters
1114
15+ logger = logging .getLogger ("murfey.client.util.tomo_metadata" )
16+
1217
1318def register_search_map_in_database (
1419 session_id : MurfeySessionID ,
@@ -127,33 +132,22 @@ def register_search_map_in_database(
127132 ],
128133 ]
129134 )
130- M_corrected = np .matmul (np .linalg .inv (M ), R )
131- vector_pixel = np .matmul (M_corrected , B )
135+ vector_pixel = np .matmul (np .linalg .inv (M ), np .matmul (R , np .matmul (M , B )))
132136
133137 camera = getattr (Camera , machine_config .camera )
134- if camera == Camera .FALCON :
135- vector_pixel = np .matmul (np .array ([[- 1 , 0 ], [0 , - 1 ]]), vector_pixel )
138+ if camera == Camera .FALCON or Camera .K3_FLIPY :
139+ vector_pixel = np .matmul (np .array ([[1 , 0 ], [0 , - 1 ]]), vector_pixel )
140+ elif camera == Camera .K3_FLIPX :
141+ vector_pixel = np .matmul (np .array ([[- 1 , 0 ], [0 , 1 ]]), vector_pixel )
136142
137143 search_map_params .height_on_atlas = int (
138144 search_map .height * search_map .pixel_size / dcg .atlas_pixel_size
139145 )
140146 search_map_params .width_on_atlas = int (
141147 search_map .width * search_map .pixel_size / dcg .atlas_pixel_size
142148 )
143- search_map_params .x_location = (
144- vector_pixel [0 ]
145- / dcg .atlas_binning
146- * search_map .pixel_size
147- / dcg .atlas_pixel_size
148- + 2003
149- )
150- search_map_params .y_location = (
151- vector_pixel [1 ]
152- / dcg .atlas_binning
153- * search_map .pixel_size
154- / dcg .atlas_pixel_size
155- + 2003
156- )
149+ search_map_params .x_location = vector_pixel [0 ] / dcg .atlas_pixel_size + 2003
150+ search_map_params .y_location = vector_pixel [1 ] / dcg .atlas_pixel_size + 2003
157151 search_map .x_location = search_map_params .x_location
158152 search_map .y_location = search_map_params .y_location
159153 if _transport_object :
@@ -169,4 +163,86 @@ def register_batch_position_in_database(
169163 batch_parameters : BatchPositionParameters ,
170164 murfey_db : Session ,
171165):
172- pass
166+ search_map = murfey_db .exec (
167+ select (SearchMap )
168+ .where (SearchMap .name == batch_parameters .search_map_name )
169+ .where (SearchMap .tag == batch_parameters .tag )
170+ .where (SearchMap .session_id == session_id )
171+ ).one ()
172+
173+ try :
174+ tilt_series = murfey_db .exec (
175+ select (TiltSeries )
176+ .where (TiltSeries .tag == batch_name )
177+ .where (TiltSeries .session_id == session_id )
178+ ).one ()
179+ if tilt_series .x_location :
180+ logger .info (f"Already did position analysis for tomogram { batch_name } " )
181+ return
182+ except Exception :
183+ tilt_series = TiltSeries (
184+ tag = batch_name ,
185+ rsync_source = batch_parameters .tag ,
186+ session_id = session_id ,
187+ search_map_id = search_map .id ,
188+ )
189+
190+ # Get the pixel location on the searchmap
191+ M = np .array (
192+ [
193+ [
194+ search_map .reference_matrix ["m11" ],
195+ search_map .reference_matrix ["m12" ],
196+ ],
197+ [
198+ search_map .reference_matrix ["m21" ],
199+ search_map .reference_matrix ["m22" ],
200+ ],
201+ ]
202+ )
203+ R1 = np .array (
204+ [
205+ [
206+ search_map .stage_correction ["m11" ],
207+ search_map .stage_correction ["m12" ],
208+ ],
209+ [
210+ search_map .stage_correction ["m21" ],
211+ search_map .stage_correction ["m22" ],
212+ ],
213+ ]
214+ )
215+ R2 = np .array (
216+ [
217+ [
218+ search_map .image_shift_correction ["m11" ],
219+ search_map .image_shift_correction ["m12" ],
220+ ],
221+ [
222+ search_map .image_shift_correction ["m21" ],
223+ search_map .image_shift_correction ["m22" ],
224+ ],
225+ ]
226+ )
227+
228+ A = np .array ([search_map .x_stage_position , search_map .y_stage_position ])
229+ B = np .array ([batch_parameters .x_stage_position , batch_parameters .y_stage_position ])
230+
231+ vector_pixel = np .matmul (
232+ np .linalg .inv (M ),
233+ np .matmul (np .linalg .inv (R1 ), np .matmul (np .linalg .inv (R2 ), np .matmul (M , B - A ))),
234+ )
235+ centre_batch_pixel = vector_pixel / search_map .pixel_size + [
236+ search_map .width / 2 ,
237+ search_map .height / 2 ,
238+ ]
239+ tilt_series .x_location = (
240+ centre_batch_pixel [0 ]
241+ - BatchPositionParameters .x_beamshift / search_map .pixel_size
242+ )
243+ tilt_series .y_location = (
244+ centre_batch_pixel [1 ]
245+ - BatchPositionParameters .y_beamshift / search_map .pixel_size
246+ )
247+ murfey_db .add (tilt_series )
248+ murfey_db .commit ()
0 commit comments