Skip to content

Commit a9c50db

Browse files
committed
Still moving endpoints under their own routers
1 parent 76b43b6 commit a9c50db

File tree

5 files changed

+261
-407
lines changed

5 files changed

+261
-407
lines changed

src/murfey/server/api/__init__.py

Lines changed: 1 addition & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import murfey.server.ispyb
3232
import murfey.server.prometheus as prom
3333
import murfey.server.websocket as ws
34-
import murfey.util.eer
3534
from murfey.server import (
3635
_murfey_id,
3736
_transport_object,
@@ -48,9 +47,8 @@
4847
)
4948
from murfey.server.api.auth import MurfeySessionID, validate_token
5049
from murfey.server.api.spa import _cryolo_model_path
51-
from murfey.server.gain import Camera, prepare_eer_gain, prepare_gain
5250
from murfey.server.murfey_db import murfey_db
53-
from murfey.util import safe_run, secure_path
51+
from murfey.util import safe_run
5452
from murfey.util.config import MachineConfig, from_file, settings
5553
from murfey.util.db import (
5654
AutoProcProgram,
@@ -78,8 +76,6 @@
7876
ClientInfo,
7977
CurrentGainRef,
8078
FoilHoleParameters,
81-
FractionationParameters,
82-
GainReference,
8379
GridSquareParameters,
8480
MillingParameters,
8581
PostInfo,
@@ -91,7 +87,6 @@
9187
Sample,
9288
SessionInfo,
9389
SPAProcessFile,
94-
SuggestedPathParameters,
9590
TiltInfo,
9691
TiltSeriesGroupInfo,
9792
TiltSeriesInfo,
@@ -1179,155 +1174,6 @@ async def request_tomography_preprocessing(
11791174
return proc_file
11801175

11811176

1182-
@router.post("/visits/{visit_name}/{session_id}/suggested_path")
1183-
def suggest_path(
1184-
visit_name: str, session_id: int, params: SuggestedPathParameters, db=murfey_db
1185-
):
1186-
count: int | None = None
1187-
secure_path_parts = [secure_filename(p) for p in params.base_path.parts]
1188-
base_path = "/".join(secure_path_parts)
1189-
instrument_name = (
1190-
db.exec(select(Session).where(Session.id == session_id)).one().instrument_name
1191-
)
1192-
machine_config = get_machine_config(instrument_name=instrument_name)[
1193-
instrument_name
1194-
]
1195-
if not machine_config:
1196-
raise ValueError(
1197-
"No machine configuration set when suggesting destination path"
1198-
)
1199-
1200-
# Construct the full path to where the dataset is to be saved
1201-
check_path = machine_config.rsync_basepath / base_path
1202-
1203-
# Check previous year to account for the year rolling over during data collection
1204-
if not check_path.parent.exists():
1205-
base_path_parts = base_path.split("/")
1206-
for part in base_path_parts:
1207-
# Find the path part corresponding to the year
1208-
if len(part) == 4 and part.isdigit():
1209-
year_idx = base_path_parts.index(part)
1210-
base_path_parts[year_idx] = str(int(part) - 1)
1211-
base_path = "/".join(base_path_parts)
1212-
check_path_prev = check_path
1213-
check_path = machine_config.rsync_basepath / base_path
1214-
1215-
# If it's not in the previous year either, it's a genuine error
1216-
if not check_path.parent.exists():
1217-
log_message = (
1218-
"Unable to find current visit folder under "
1219-
f"{str(check_path_prev.parent)!r} or {str(check_path.parent)!r}"
1220-
)
1221-
log.error(log_message)
1222-
raise FileNotFoundError(log_message)
1223-
1224-
check_path_name = check_path.name
1225-
while check_path.exists():
1226-
count = count + 1 if count else 2
1227-
check_path = check_path.parent / f"{check_path_name}{count}"
1228-
if params.touch:
1229-
check_path.mkdir(mode=0o750)
1230-
if params.extra_directory:
1231-
(check_path / secure_filename(params.extra_directory)).mkdir(mode=0o750)
1232-
return {"suggested_path": check_path.relative_to(machine_config.rsync_basepath)}
1233-
1234-
1235-
class Dest(BaseModel):
1236-
destination: Path
1237-
1238-
1239-
@router.post("/sessions/{session_id}/make_rsyncer_destination")
1240-
def make_rsyncer_destination(session_id: int, destination: Dest, db=murfey_db):
1241-
secure_path_parts = [secure_filename(p) for p in destination.destination.parts]
1242-
destination_path = "/".join(secure_path_parts)
1243-
instrument_name = (
1244-
db.exec(select(Session).where(Session.id == session_id)).one().instrument_name
1245-
)
1246-
machine_config = get_machine_config(instrument_name=instrument_name)[
1247-
instrument_name
1248-
]
1249-
if not machine_config:
1250-
raise ValueError("No machine configuration set when making rsyncer destination")
1251-
full_destination_path = machine_config.rsync_basepath / destination_path
1252-
for parent_path in full_destination_path.parents:
1253-
parent_path.mkdir(mode=0o750, exist_ok=True)
1254-
return destination
1255-
1256-
1257-
@router.post("/sessions/{session_id}/process_gain")
1258-
async def process_gain(
1259-
session_id: MurfeySessionID, gain_reference_params: GainReference, db=murfey_db
1260-
):
1261-
murfey_session = db.exec(select(Session).where(Session.id == session_id)).one()
1262-
visit_name = murfey_session.visit
1263-
instrument_name = murfey_session.instrument_name
1264-
machine_config = get_machine_config(instrument_name=instrument_name)[
1265-
instrument_name
1266-
]
1267-
camera = getattr(Camera, machine_config.camera)
1268-
if gain_reference_params.eer:
1269-
executables = machine_config.external_executables_eer
1270-
else:
1271-
executables = machine_config.external_executables
1272-
env = machine_config.external_environment
1273-
safe_path_name = secure_filename(gain_reference_params.gain_ref.name)
1274-
filepath = (
1275-
Path(machine_config.rsync_basepath)
1276-
/ str(datetime.datetime.now().year)
1277-
/ secure_filename(visit_name)
1278-
/ machine_config.gain_directory_name
1279-
)
1280-
1281-
# Check under previous year if the folder doesn't exist
1282-
if not filepath.exists():
1283-
filepath_prev = filepath
1284-
filepath = (
1285-
Path(machine_config.rsync_basepath)
1286-
/ str(datetime.datetime.now().year - 1)
1287-
/ secure_filename(visit_name)
1288-
/ machine_config.gain_directory_name
1289-
)
1290-
# If it's not in the previous year, it's a genuine error
1291-
if not filepath.exists():
1292-
log_message = (
1293-
"Unable to find gain reference directory under "
1294-
f"{str(filepath_prev)!r} or {str(filepath)}"
1295-
)
1296-
log.error(log_message)
1297-
raise FileNotFoundError(log_message)
1298-
1299-
if gain_reference_params.eer:
1300-
new_gain_ref, new_gain_ref_superres = await prepare_eer_gain(
1301-
filepath / safe_path_name,
1302-
executables,
1303-
env,
1304-
tag=gain_reference_params.tag,
1305-
)
1306-
else:
1307-
new_gain_ref, new_gain_ref_superres = await prepare_gain(
1308-
camera,
1309-
filepath / safe_path_name,
1310-
executables,
1311-
env,
1312-
rescale=gain_reference_params.rescale,
1313-
tag=gain_reference_params.tag,
1314-
)
1315-
if new_gain_ref and new_gain_ref_superres:
1316-
return {
1317-
"gain_ref": new_gain_ref.relative_to(Path(machine_config.rsync_basepath)),
1318-
"gain_ref_superres": new_gain_ref_superres.relative_to(
1319-
Path(machine_config.rsync_basepath)
1320-
),
1321-
}
1322-
elif new_gain_ref:
1323-
return {
1324-
"gain_ref": new_gain_ref.relative_to(Path(machine_config.rsync_basepath)),
1325-
"gain_ref_superres": None,
1326-
}
1327-
else:
1328-
return {"gain_ref": str(filepath / safe_path_name), "gain_ref_superres": None}
1329-
1330-
13311177
@router.delete("/sessions/{session_id}")
13321178
def remove_session_by_id(session_id: MurfeySessionID, db=murfey_db):
13331179
session = db.exec(select(Session).where(Session.id == session_id)).one()
@@ -1392,68 +1238,6 @@ def remove_session_by_id(session_id: MurfeySessionID, db=murfey_db):
13921238
return
13931239

13941240

1395-
@router.post("/visits/{visit_name}/{session_id}/eer_fractionation_file")
1396-
async def write_eer_fractionation_file(
1397-
visit_name: str,
1398-
session_id: int,
1399-
fractionation_params: FractionationParameters,
1400-
db=murfey_db,
1401-
) -> dict:
1402-
instrument_name = (
1403-
db.exec(select(Session).where(Session.id == session_id)).one().instrument_name
1404-
)
1405-
machine_config = get_machine_config(instrument_name=instrument_name)[
1406-
instrument_name
1407-
]
1408-
if machine_config.eer_fractionation_file_template:
1409-
file_path = Path(
1410-
machine_config.eer_fractionation_file_template.format(
1411-
visit=secure_filename(visit_name),
1412-
year=str(datetime.datetime.now().year),
1413-
)
1414-
) / secure_filename(fractionation_params.fractionation_file_name)
1415-
else:
1416-
file_path = (
1417-
Path(machine_config.rsync_basepath)
1418-
/ str(datetime.datetime.now().year)
1419-
/ secure_filename(visit_name)
1420-
/ machine_config.gain_directory_name
1421-
/ secure_filename(fractionation_params.fractionation_file_name)
1422-
)
1423-
1424-
session_parameters = db.exec(
1425-
select(SessionProcessingParameters).where(
1426-
SessionProcessingParameters.session_id == session_id
1427-
)
1428-
).all()
1429-
if session_parameters:
1430-
session_parameters[0].eer_fractionation_file = str(file_path)
1431-
db.add(session_parameters[0])
1432-
db.commit()
1433-
1434-
if file_path.is_file():
1435-
return {"eer_fractionation_file": str(file_path)}
1436-
1437-
if fractionation_params.num_frames:
1438-
num_eer_frames = fractionation_params.num_frames
1439-
elif (
1440-
fractionation_params.eer_path
1441-
and secure_path(Path(fractionation_params.eer_path)).is_file()
1442-
):
1443-
num_eer_frames = murfey.util.eer.num_frames(Path(fractionation_params.eer_path))
1444-
else:
1445-
log.warning(
1446-
f"EER fractionation unable to find {secure_path(Path(fractionation_params.eer_path)) if fractionation_params.eer_path else None} "
1447-
f"or use {int(sanitise(str(fractionation_params.num_frames)))} frames"
1448-
)
1449-
return {"eer_fractionation_file": None}
1450-
with open(file_path, "w") as frac_file:
1451-
frac_file.write(
1452-
f"{num_eer_frames} {fractionation_params.fractionation} {fractionation_params.dose_per_frame / fractionation_params.fractionation}"
1453-
)
1454-
return {"eer_fractionation_file": str(file_path)}
1455-
1456-
14571241
@router.post("/visits/{year}/{visit_name}/{session_id}/make_milling_gif")
14581242
async def make_gif(
14591243
year: int,

0 commit comments

Comments
 (0)