|
31 | 31 | import murfey.server.ispyb |
32 | 32 | import murfey.server.prometheus as prom |
33 | 33 | import murfey.server.websocket as ws |
34 | | -import murfey.util.eer |
35 | 34 | from murfey.server import ( |
36 | 35 | _murfey_id, |
37 | 36 | _transport_object, |
|
48 | 47 | ) |
49 | 48 | from murfey.server.api.auth import MurfeySessionID, validate_token |
50 | 49 | from murfey.server.api.spa import _cryolo_model_path |
51 | | -from murfey.server.gain import Camera, prepare_eer_gain, prepare_gain |
52 | 50 | from murfey.server.murfey_db import murfey_db |
53 | | -from murfey.util import safe_run, secure_path |
| 51 | +from murfey.util import safe_run |
54 | 52 | from murfey.util.config import MachineConfig, from_file, settings |
55 | 53 | from murfey.util.db import ( |
56 | 54 | AutoProcProgram, |
|
78 | 76 | ClientInfo, |
79 | 77 | CurrentGainRef, |
80 | 78 | FoilHoleParameters, |
81 | | - FractionationParameters, |
82 | | - GainReference, |
83 | 79 | GridSquareParameters, |
84 | 80 | MillingParameters, |
85 | 81 | PostInfo, |
|
91 | 87 | Sample, |
92 | 88 | SessionInfo, |
93 | 89 | SPAProcessFile, |
94 | | - SuggestedPathParameters, |
95 | 90 | TiltInfo, |
96 | 91 | TiltSeriesGroupInfo, |
97 | 92 | TiltSeriesInfo, |
@@ -1179,155 +1174,6 @@ async def request_tomography_preprocessing( |
1179 | 1174 | return proc_file |
1180 | 1175 |
|
1181 | 1176 |
|
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 | | - |
1331 | 1177 | @router.delete("/sessions/{session_id}") |
1332 | 1178 | def remove_session_by_id(session_id: MurfeySessionID, db=murfey_db): |
1333 | 1179 | 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): |
1392 | 1238 | return |
1393 | 1239 |
|
1394 | 1240 |
|
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 | | - |
1457 | 1241 | @router.post("/visits/{year}/{visit_name}/{session_id}/make_milling_gif") |
1458 | 1242 | async def make_gif( |
1459 | 1243 | year: int, |
|
0 commit comments