Skip to content

Commit 405d259

Browse files
committed
Register positions of tomograms on search maps
1 parent 20e74de commit 405d259

File tree

6 files changed

+123
-25
lines changed

6 files changed

+123
-25
lines changed

src/murfey/client/contexts/tomo_metadata.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,9 @@ def post_transfer(
277277
"tag": visitless_source,
278278
"x_stage_position": batch_stage_location_x,
279279
"y_stage_position": batch_stage_location_y,
280-
"search_map": search_map_name,
280+
"x_beamshift": 0,
281+
"y_beamshift": 0,
282+
"search_map_name": search_map_name,
281283
},
282284
)
283285

@@ -298,8 +300,10 @@ def post_transfer(
298300
bp_url,
299301
json={
300302
"tag": visitless_source,
301-
"x_stage_position": beamshift_position_x,
302-
"y_stage_position": beamshift_position_y,
303-
"search_map": search_map_name,
303+
"x_stage_position": batch_stage_location_x,
304+
"y_stage_position": batch_stage_location_y,
305+
"x_beamshift": beamshift_position_x,
306+
"y_beamshift": beamshift_position_y,
307+
"search_map_name": search_map_name,
304308
},
305309
)

src/murfey/server/api/workflow.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,9 @@ def register_completed_tilt_series(
819819
"pixel_size": preproc_params.pixel_size,
820820
"manual_tilt_offset": -tilt_offset,
821821
"node_creator_queue": machine_config.node_creator_queue,
822+
"search_map_id": ts.search_map_id,
823+
"x_location": ts.x_location,
824+
"y_location": ts.y_location,
822825
},
823826
}
824827
if _transport_object:

src/murfey/server/feedback.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,6 +1937,9 @@ def feedback_callback(header: dict, message: dict) -> None:
19371937
"pixel_size": preproc_params.pixel_size,
19381938
"manual_tilt_offset": -tilt_offset,
19391939
"node_creator_queue": machine_config.node_creator_queue,
1940+
"search_map_id": relevant_tilt_series.search_map_id,
1941+
"x_location": relevant_tilt_series.x_location,
1942+
"y_location": relevant_tilt_series.y_location,
19401943
},
19411944
}
19421945
if murfey.server._transport_object:

src/murfey/util/db.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,15 +349,20 @@ class SessionProcessingParameters(SQLModel, table=True): # type: ignore
349349

350350
class TiltSeries(SQLModel, table=True): # type: ignore
351351
id: int = Field(primary_key=True)
352+
ispyb_id: Optional[int] = None
352353
tag: str
353354
rsync_source: str
354355
session_id: int = Field(foreign_key="session.id")
356+
search_map_id: int = Field(foreign_key="searchmap.id")
355357
tilt_series_length: int = -1
356358
processing_requested: bool = False
359+
x_location: Optional[float] = None
360+
y_location: Optional[float] = None
357361
session: Optional[Session] = Relationship(back_populates="tilt_series")
358362
tilts: List["Tilt"] = Relationship(
359363
back_populates="tilt_series", sa_relationship_kwargs={"cascade": "delete"}
360364
)
365+
search_map: Optional["GridSquare"] = Relationship(back_populates="batch_positions")
361366

362367

363368
class Tilt(SQLModel, table=True): # type: ignore
@@ -620,7 +625,12 @@ class SearchMap(SQLModel, table=True): # type: ignore
620625
reference_matrix: Optional[dict[str, float]] = None
621626
stage_correction: Optional[dict[str, float]] = None
622627
image_shift_correction: Optional[dict[str, float]] = None
628+
width: Optional[int] = None
629+
height: Optional[int] = None
623630
session: Optional[Session] = Relationship(back_populates="search_maps")
631+
batch_positions: List["TiltSeries"] = Relationship(
632+
back_populates="search_map", sa_relationship_kwargs={"cascade": "delete"}
633+
)
624634

625635

626636
class Movie(SQLModel, table=True): # type: ignore

src/murfey/util/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,10 @@ class SearchMapParameters:
157157
class BatchPositionParameters:
158158
tag: str
159159
x_stage_position: float
160-
Y_stage_position: float
161-
search_map: str
160+
y_stage_position: float
161+
x_beamshift: float
162+
y_beamshift: float
163+
search_map_name: str
162164

163165

164166
class MultigridWatcherSetup(BaseModel):

src/murfey/util/tomo_metadata.py

Lines changed: 95 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import logging
2+
13
import numpy as np
24
from sqlmodel import Session, select
35

@@ -7,8 +9,11 @@
79
from murfey.util.config import get_machine_config
810
from murfey.util.db import DataCollectionGroup, SearchMap
911
from murfey.util.db import Session as MurfeySession
12+
from murfey.util.db import TiltSeries
1013
from murfey.util.models import BatchPositionParameters, SearchMapParameters
1114

15+
logger = logging.getLogger("murfey.client.util.tomo_metadata")
16+
1217

1318
def 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

Comments
 (0)