Skip to content

Commit 3e13879

Browse files
committed
Updated code to reflect type changes of parameters from str to Path and making keys Optional in MachineConfig
1 parent c53b099 commit 3e13879

File tree

4 files changed

+56
-27
lines changed

4 files changed

+56
-27
lines changed

src/murfey/server/api/__init__.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
from murfey.server.gain import Camera, prepare_eer_gain, prepare_gain
5252
from murfey.server.murfey_db import murfey_db
5353
from murfey.util import secure_path
54-
from murfey.util.config import MachineConfig, from_file, settings
54+
from murfey.util.config import MachineConfig, machine_config_from_file, settings
5555
from murfey.util.db import (
5656
AutoProcProgram,
5757
ClientEnvironment,
@@ -146,19 +146,19 @@ def connections_check():
146146
def machine_info() -> Optional[MachineConfig]:
147147
instrument_name = os.getenv("BEAMLINE")
148148
if settings.murfey_machine_configuration and instrument_name:
149-
return from_file(Path(settings.murfey_machine_configuration), instrument_name)[
150-
instrument_name
151-
]
149+
return machine_config_from_file(
150+
Path(settings.murfey_machine_configuration), instrument_name
151+
)[instrument_name]
152152
return None
153153

154154

155155
@lru_cache(maxsize=5)
156156
@router.get("/instruments/{instrument_name}/machine")
157157
def machine_info_by_name(instrument_name: str) -> Optional[MachineConfig]:
158158
if settings.murfey_machine_configuration:
159-
return from_file(Path(settings.murfey_machine_configuration), instrument_name)[
160-
instrument_name
161-
]
159+
return machine_config_from_file(
160+
Path(settings.murfey_machine_configuration), instrument_name
161+
)[instrument_name]
162162
return None
163163

164164

@@ -1218,6 +1218,10 @@ async def request_tomography_preprocessing(
12181218
murfey_ids = _murfey_id(appid, db, number=1, close=False)
12191219
if not mrc_out.parent.exists():
12201220
mrc_out.parent.mkdir(parents=True, exist_ok=True)
1221+
# Handle case when gain reference file is None
1222+
if not proc_file.gain_ref:
1223+
log.error("No gain reference file was provided in the ProcessFile object")
1224+
return proc_file
12211225
zocalo_message: dict = {
12221226
"recipes": ["em-tomo-preprocess"],
12231227
"parameters": {
@@ -1236,7 +1240,9 @@ async def request_tomography_preprocessing(
12361240
"fm_dose": proc_file.dose_per_frame,
12371241
"gain_ref": (
12381242
str(machine_config.rsync_basepath / proc_file.gain_ref)
1239-
if proc_file.gain_ref and machine_config.data_transfer_enabled
1243+
if proc_file.gain_ref
1244+
and machine_config.data_transfer_enabled
1245+
and machine_config.rsync_basepath
12401246
else proc_file.gain_ref
12411247
),
12421248
"fm_int_file": proc_file.eer_fractionation_file,
@@ -1249,7 +1255,7 @@ async def request_tomography_preprocessing(
12491255
_transport_object.send("processing_recipe", zocalo_message)
12501256
else:
12511257
log.error(
1252-
f"Pe-processing was requested for {sanitise(ppath.name)} but no Zocalo transport object was found"
1258+
f"Preprocessing was requested for {sanitise(ppath.name)} but no Zocalo transport object was found"
12531259
)
12541260
return proc_file
12551261
else:
@@ -1285,6 +1291,8 @@ def suggest_path(
12851291
raise ValueError(
12861292
"No machine configuration set when suggesting destination path"
12871293
)
1294+
if not machine_config.rsync_basepath:
1295+
raise ValueError("No rsync basepath set")
12881296
check_path = machine_config.rsync_basepath / base_path
12891297
check_path_name = check_path.name
12901298
while check_path.exists():
@@ -1366,6 +1374,8 @@ def start_dc(
13661374
machine_config = get_machine_config(instrument_name=instrument_name)[
13671375
instrument_name
13681376
]
1377+
if not machine_config.rsync_basepath:
1378+
raise ValueError("No rsync basepath set")
13691379
log.info(
13701380
f"Starting data collection on microscope {get_microscope(machine_config=machine_config)} "
13711381
f"with basepath {sanitise(str(machine_config.rsync_basepath))} and directory {sanitise(dc_params.image_directory)}"
@@ -1453,8 +1463,10 @@ async def process_gain(
14531463
executables = machine_config.external_executables
14541464
env = machine_config.external_environment
14551465
safe_path_name = secure_filename(gain_reference_params.gain_ref.name)
1466+
if not machine_config.rsync_basepath:
1467+
raise ValueError("No rsync basepath set")
14561468
filepath = (
1457-
Path(machine_config.rsync_basepath)
1469+
machine_config.rsync_basepath
14581470
/ (machine_config.rsync_module or "data")
14591471
/ str(datetime.datetime.now().year)
14601472
/ secure_filename(visit_name)
@@ -1521,8 +1533,10 @@ async def write_eer_fractionation_file(
15211533
)
15221534
) / secure_filename(fractionation_params.fractionation_file_name)
15231535
else:
1536+
if not machine_config.rsync_basepath:
1537+
raise ValueError("rsync basepath not set")
15241538
file_path = (
1525-
Path(machine_config.rsync_basepath)
1539+
machine_config.rsync_basepath
15261540
/ (machine_config.rsync_module or "data")
15271541
/ str(datetime.datetime.now().year)
15281542
/ secure_filename(visit_name)
@@ -1566,8 +1580,10 @@ async def make_gif(
15661580
machine_config = get_machine_config(instrument_name=instrument_name)[
15671581
instrument_name
15681582
]
1583+
if not machine_config.rsync_basepath:
1584+
raise ValueError("rsync basepath not set")
15691585
output_dir = (
1570-
Path(machine_config.rsync_basepath)
1586+
machine_config.rsync_basepath
15711587
/ (machine_config.rsync_module or "data")
15721588
/ secure_filename(year)
15731589
/ secure_filename(visit_name)

src/murfey/server/api/spa.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ def _cryolo_model_path(visit: str, instrument_name: str) -> Path:
2020
machine_config = get_machine_config(instrument_name=instrument_name)[
2121
instrument_name
2222
]
23+
# Raise error if relevant keys weren't set in MachineConfig
24+
if not machine_config.rsync_basepath:
25+
raise ValueError("Unable to find crYOLO model; rsync_basepath was not set")
26+
if not machine_config.default_model:
27+
raise ValueError("No default crYOLO model was set")
28+
29+
# Find user-provided crYOLO model
2330
if machine_config.model_search_directory:
2431
visit_directory = (
2532
machine_config.rsync_basepath
@@ -32,10 +39,14 @@ def _cryolo_model_path(visit: str, instrument_name: str) -> Path:
3239
)
3340
if possible_models:
3441
return sorted(possible_models, key=lambda x: x.stat().st_ctime)[-1]
42+
43+
# Return default crYOLO model otherwise
3544
return machine_config.default_model
3645

3746

3847
@router.get("/sessions/{session_id}/cryolo_model")
3948
def get_cryolo_model_path(session_id: int, db=murfey_db):
4049
session = db.exec(select(MurfeySession).where(MurfeySession.id == session_id)).one()
41-
return {"model_path": _cryolo_model_path(session.visit, session.instrment_name)}
50+
return {
51+
"model_path": str(_cryolo_model_path(session.visit, session.instrment_name))
52+
}

src/murfey/server/demo_api.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
from murfey.server.api import MurfeySessionID
4141
from murfey.server.api.auth import validate_token
4242
from murfey.server.murfey_db import murfey_db
43-
from murfey.util.config import MachineConfig, from_file
43+
from murfey.util.config import MachineConfig, machine_config_from_file
4444
from murfey.util.db import (
4545
AutoProcProgram,
4646
ClientEnvironment,
@@ -113,7 +113,9 @@ class Settings(BaseSettings):
113113
machine_config: dict = {}
114114
if settings.murfey_machine_configuration:
115115
microscope = get_microscope()
116-
machine_config = from_file(Path(settings.murfey_machine_configuration), microscope)
116+
machine_config = machine_config_from_file(
117+
Path(settings.murfey_machine_configuration), microscope
118+
)
117119

118120

119121
# This will be the homepage for a given microscope.
@@ -134,19 +136,19 @@ async def root(request: Request):
134136
def machine_info() -> Optional[MachineConfig]:
135137
instrument_name = os.getenv("BEAMLINE")
136138
if settings.murfey_machine_configuration and instrument_name:
137-
return from_file(Path(settings.murfey_machine_configuration), instrument_name)[
138-
instrument_name
139-
]
139+
return machine_config_from_file(
140+
Path(settings.murfey_machine_configuration), instrument_name
141+
)[instrument_name]
140142
return None
141143

142144

143145
@lru_cache(maxsize=5)
144146
@router.get("/instruments/{instrument_name}/machine")
145147
def machine_info_by_name(instrument_name: str) -> Optional[MachineConfig]:
146148
if settings.murfey_machine_configuration:
147-
return from_file(Path(settings.murfey_machine_configuration), instrument_name)[
148-
instrument_name
149-
]
149+
return machine_config_from_file(
150+
Path(settings.murfey_machine_configuration), instrument_name
151+
)[instrument_name]
150152
return None
151153

152154

src/murfey/server/gain.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def _sanitise(gain_path: Path) -> Path:
2424
async def prepare_gain(
2525
camera: int,
2626
gain_path: Path,
27-
executables: Dict[str, str],
27+
executables: Dict[str, Path],
2828
env: Dict[str, str],
2929
rescale: bool = True,
3030
tag: str = "",
@@ -57,15 +57,15 @@ async def prepare_gain(
5757
gain_path_mrc = gain_path.with_suffix(".mrc")
5858
gain_path_superres = gain_path.parent / (gain_path.name + "_superres.mrc")
5959
dm4_proc = await asyncio.create_subprocess_shell(
60-
f"{executables['dm2mrc']} {gain_path} {gain_path_mrc}",
60+
f"{str(executables['dm2mrc'])} {gain_path} {gain_path_mrc}",
6161
stdout=asyncio.subprocess.PIPE,
6262
stderr=asyncio.subprocess.PIPE,
6363
)
6464
stdout, stderr = await dm4_proc.communicate()
6565
if dm4_proc.returncode:
6666
return None, None
6767
clip_proc = await asyncio.create_subprocess_shell(
68-
f"{executables['clip']} {flip} {secure_path(gain_path_mrc)} {secure_path(gain_path_superres) if rescale else secure_path(gain_out)}",
68+
f"{str(executables['clip'])} {flip} {secure_path(gain_path_mrc)} {secure_path(gain_path_superres) if rescale else secure_path(gain_out)}",
6969
stdout=asyncio.subprocess.PIPE,
7070
stderr=asyncio.subprocess.PIPE,
7171
)
@@ -74,7 +74,7 @@ async def prepare_gain(
7474
return None, None
7575
if rescale:
7676
newstack_proc = await asyncio.create_subprocess_shell(
77-
f"{executables['newstack']} -bin 2 {secure_path(gain_path_superres)} {secure_path(gain_out)}",
77+
f"{str(executables['newstack'])} -bin 2 {secure_path(gain_path_superres)} {secure_path(gain_out)}",
7878
stdout=asyncio.subprocess.PIPE,
7979
stderr=asyncio.subprocess.PIPE,
8080
)
@@ -88,7 +88,7 @@ async def prepare_gain(
8888

8989

9090
async def prepare_eer_gain(
91-
gain_path: Path, executables: Dict[str, str], env: Dict[str, str], tag: str = ""
91+
gain_path: Path, executables: Dict[str, Path], env: Dict[str, str], tag: str = ""
9292
) -> Tuple[Path | None, Path | None]:
9393
if not executables.get("tif2mrc"):
9494
return None, None
@@ -98,7 +98,7 @@ async def prepare_eer_gain(
9898
for k, v in env.items():
9999
os.environ[k] = v
100100
mrc_convert = await asyncio.create_subprocess_shell(
101-
f"{executables['tif2mrc']} {secure_path(gain_path)} {secure_path(gain_out)}"
101+
f"{str(executables['tif2mrc'])} {secure_path(gain_path)} {secure_path(gain_out)}"
102102
)
103103
await mrc_convert.communicate()
104104
if mrc_convert.returncode:

0 commit comments

Comments
 (0)