Skip to content

Commit aa8e9ae

Browse files
committed
Find xrm reference if no internal one
1 parent 15be45a commit aa8e9ae

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

src/murfey/client/contexts/sxt.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from datetime import datetime
23
from pathlib import Path
34
from typing import Any
45

@@ -13,11 +14,50 @@
1314
)
1415
from murfey.client.instance_environment import MurfeyInstanceEnvironment
1516
from murfey.util.client import capture_post
17+
from murfey.util.models import File
1618
from murfey.util.tomo import midpoint
1719

1820
logger = logging.getLogger("murfey.client.contexts.sxt")
1921

2022

23+
def _find_reference(txrm_file: Path) -> Path | None:
24+
"""Find a suitable reference to apply to the given txrm file"""
25+
# Look for xrm files in the txrm folder, reverse sorted by time
26+
candidates = []
27+
for gf in txrm_file.parent.glob("*.xrm"):
28+
candidates.append(
29+
File(
30+
name=gf.name,
31+
description="",
32+
size=gf.stat().st_size / 1e6,
33+
timestamp=datetime.fromtimestamp(gf.stat().st_mtime),
34+
full_path=str(gf),
35+
)
36+
)
37+
candidates.sort(key=lambda x: x.timestamp, reverse=True)
38+
for ref_option in candidates:
39+
mosaic_size = 1
40+
with OleFileIO(str(ref_option)) as xrm_ole:
41+
# Find images which are not mosaics (txrm spec typos this as mosiac)
42+
if xrm_ole.exists("ImageInfo/MosiacRows") and xrm_ole.exists(
43+
"ImageInfo/MosiacColumns"
44+
):
45+
mosaic_size = int(
46+
np.frombuffer(
47+
xrm_ole.openstream("ImageInfo/MosiacRows").getvalue(), np.int32
48+
)[0]
49+
* np.frombuffer(
50+
xrm_ole.openstream("ImageInfo/MosiacColumns").getvalue(),
51+
np.int32,
52+
)[0]
53+
)
54+
if mosaic_size == 0:
55+
logger.info(f"Found reference {ref_option}")
56+
return ref_option
57+
logger.warning(f"No reference found for {txrm_file}")
58+
return None
59+
60+
2161
class SXTContext(Context):
2262
def __init__(
2363
self,
@@ -206,9 +246,17 @@ def post_transfer(
206246
)
207247
metadata["energy"] = int(round(axis_values[energy_index]))
208248

209-
if not metadata.get("has_reference", False):
249+
if (
250+
not metadata.get("has_reference", False)
251+
and metadata.get("tilt_series_length", len(angles)) < 20
252+
):
253+
# References are collected with only 10 frames
210254
logger.debug(f"Reference image {transferred_file} not processed")
211255
return True
256+
elif not metadata.get("has_reference", False):
257+
reference_file = _find_reference(transferred_file)
258+
else:
259+
reference_file = None
212260

213261
tilt_series_tag = "_".join(
214262
transferred_file.stem.split("@")[0].split("_")[:-1]
@@ -260,6 +308,7 @@ def post_transfer(
260308
"tilt_series_length", len(angles)
261309
),
262310
"txrm": str(file_transferred_to),
311+
"xrm_reference": str(reference_file) if reference_file else None,
263312
},
264313
)
265314
return True

src/murfey/workflows/sxt/process_sxt_tilt_series.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class SXTTiltSeriesInfo(BaseModel):
2828
tilt_series_length: int
2929
pixel_size: float
3030
tilt_offset: int
31+
xrm_reference: str | None
3132

3233

3334
def process_sxt_tilt_series_workflow(
@@ -98,6 +99,7 @@ def process_sxt_tilt_series_workflow(
9899
"recipes": ["sxt-aretomo"],
99100
"parameters": {
100101
"txrm_file": tilt_series_info.txrm,
102+
"xrm_reference": tilt_series_info.xrm_reference or "",
101103
"dcid": collected_ids[1].id,
102104
"appid": collected_ids[3].id,
103105
"stack_file": str(stack_file),

0 commit comments

Comments
 (0)