|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import re |
4 | | -import sys |
5 | 4 | import traceback |
| 5 | +from importlib.metadata import EntryPoint # type hinting only |
6 | 6 | from logging import getLogger |
7 | 7 | from pathlib import Path |
8 | 8 | from typing import Optional, Type, Union |
9 | 9 |
|
| 10 | +from backports.entry_points_selectable import entry_points |
10 | 11 | from fastapi import APIRouter |
11 | 12 | from sqlalchemy.exc import NoResultFound |
12 | 13 | from sqlmodel import Session, select |
|
22 | 23 | CLEMTIFFFile, |
23 | 24 | ) |
24 | 25 | from murfey.util.db import Session as MurfeySession |
25 | | -from murfey.util.models import TiffSeriesInfo |
26 | | - |
27 | | -# Use backport from importlib_metadata for Python <3.10 |
28 | | -if sys.version_info.major == 3 and sys.version_info.minor < 10: |
29 | | - from importlib_metadata import EntryPoint, entry_points |
30 | | -else: |
31 | | - from importlib.metadata import EntryPoint, entry_points |
| 26 | +from murfey.util.models import TIFFSeriesInfo |
32 | 27 |
|
33 | 28 | # Set up logger |
34 | 29 | logger = getLogger("murfey.server.api.clem") |
@@ -81,23 +76,15 @@ def validate_and_sanitise( |
81 | 76 | machine_config = get_machine_config(instrument_name=instrument_name)[ |
82 | 77 | instrument_name |
83 | 78 | ] |
84 | | - rsync_basepath = machine_config.rsync_basepath |
85 | | - try: |
86 | | - base_path = list(rsync_basepath.parents)[-2].as_posix() |
87 | | - except IndexError: |
88 | | - # Print to troubleshoot |
89 | | - logger.warning(f"Base path {rsync_basepath!r} is too short") |
90 | | - base_path = rsync_basepath.as_posix() |
91 | | - except Exception: |
92 | | - raise Exception("Unexpected exception occurred when loading the file base path") |
| 79 | + base_path = machine_config.rsync_basepath.as_posix() |
93 | 80 |
|
94 | 81 | # Check that full file path doesn't contain unallowed characters |
95 | | - # Currently allows only: |
96 | | - # - words (alphanumerics and "_"; \w), |
97 | | - # - spaces (\s), |
98 | | - # - periods, |
99 | | - # - dashes, |
100 | | - # - forward slashes ("/") |
| 82 | + # Currently allows only: |
| 83 | + # - words (alphanumerics and "_"; \w), |
| 84 | + # - spaces (\s), |
| 85 | + # - periods, |
| 86 | + # - dashes, |
| 87 | + # - forward slashes ("/") |
101 | 88 | if bool(re.fullmatch(r"^[\w\s\.\-/]+$", str(full_path))) is False: |
102 | 89 | raise ValueError(f"Unallowed characters present in {file}") |
103 | 90 |
|
@@ -631,51 +618,68 @@ def register_image_stack( |
631 | 618 | """ |
632 | 619 |
|
633 | 620 |
|
634 | | -@router.post("/sessions/{session_id}/lif_to_stack") # API posts to this URL |
635 | | -def lif_to_stack( |
| 621 | +@router.post( |
| 622 | + "/sessions/{session_id}/clem/preprocessing/process_raw_lifs" |
| 623 | +) # API posts to this URL |
| 624 | +def process_raw_lifs( |
636 | 625 | session_id: int, # Used by the decorator |
637 | 626 | lif_file: Path, |
| 627 | + db: Session = murfey_db, |
638 | 628 | ): |
639 | | - # Get command line entry point |
640 | | - murfey_workflows = entry_points().select( |
641 | | - group="murfey.workflows", name="lif_to_stack" |
642 | | - ) |
643 | | - |
644 | | - # Use entry point if found |
645 | | - if len(murfey_workflows) == 1: |
646 | | - workflow: EntryPoint = list(murfey_workflows)[0] |
647 | | - workflow.load()( |
648 | | - # Match the arguments found in murfey.workflows.lif_to_stack |
649 | | - file=lif_file, |
650 | | - root_folder="images", |
651 | | - messenger=_transport_object, |
652 | | - ) |
653 | | - return True |
654 | | - # Raise error if Murfey workflow not found |
655 | | - else: |
| 629 | + try: |
| 630 | + # Try and load relevant Murfey workflow |
| 631 | + workflow: EntryPoint = list( |
| 632 | + entry_points().select(group="murfey.workflows", name="process_raw_lifs") |
| 633 | + )[0] |
| 634 | + except IndexError: |
656 | 635 | raise RuntimeError("The relevant Murfey workflow was not found") |
657 | 636 |
|
| 637 | + # Get instrument name from the database to load the correct config file |
| 638 | + session_row: MurfeySession = db.exec( |
| 639 | + select(MurfeySession).where(MurfeySession.id == session_id) |
| 640 | + ).one() |
| 641 | + instrument_name = session_row.instrument_name |
| 642 | + |
| 643 | + # Pass arguments along to the correct workflow |
| 644 | + workflow.load()( |
| 645 | + # Match the arguments found in murfey.workflows.clem.process_raw_lifs |
| 646 | + file=lif_file, |
| 647 | + root_folder="images", |
| 648 | + session_id=session_id, |
| 649 | + instrument_name=instrument_name, |
| 650 | + messenger=_transport_object, |
| 651 | + ) |
| 652 | + return True |
| 653 | + |
658 | 654 |
|
659 | | -@router.post("/sessions/{session_id}/tiff_to_stack") |
660 | | -def tiff_to_stack( |
| 655 | +@router.post("/sessions/{session_id}/clem/preprocessing/process_raw_tiffs") |
| 656 | +def process_raw_tiffs( |
661 | 657 | session_id: int, # Used by the decorator |
662 | | - tiff_info: TiffSeriesInfo, |
| 658 | + tiff_info: TIFFSeriesInfo, |
| 659 | + db: Session = murfey_db, |
663 | 660 | ): |
664 | | - # Get command line entry point |
665 | | - murfey_workflows = entry_points().select( |
666 | | - group="murfey.workflows", name="tiff_to_stack" |
667 | | - ) |
668 | | - |
669 | | - # Use entry point if found |
670 | | - if murfey_workflows: |
671 | | - workflow: EntryPoint = list(murfey_workflows)[0] |
672 | | - workflow.load()( |
673 | | - # Match the arguments found in murfey.workflows.tiff_to_stack |
674 | | - file=tiff_info.tiff_files[0], # Pass it only one file from the list |
675 | | - root_folder="images", |
676 | | - metadata=tiff_info.series_metadata, |
677 | | - messenger=_transport_object, |
678 | | - ) |
679 | | - # Raise error if Murfey workflow not found |
680 | | - else: |
| 661 | + try: |
| 662 | + # Try and load relevant Murfey workflow |
| 663 | + workflow: EntryPoint = list( |
| 664 | + entry_points().select(group="murfey.workflows", name="process_raw_tiffs") |
| 665 | + )[0] |
| 666 | + except IndexError: |
681 | 667 | raise RuntimeError("The relevant Murfey workflow was not found") |
| 668 | + |
| 669 | + # Get instrument name from the database to load the correct config file |
| 670 | + session_row: MurfeySession = db.exec( |
| 671 | + select(MurfeySession).where(MurfeySession.id == session_id) |
| 672 | + ).one() |
| 673 | + instrument_name = session_row.instrument_name |
| 674 | + |
| 675 | + # Pass arguments to correct workflow |
| 676 | + workflow.load()( |
| 677 | + # Match the arguments found in murfey.workflows.clem.process_raw_tiffs |
| 678 | + tiff_list=tiff_info.tiff_files, |
| 679 | + root_folder="images", |
| 680 | + session_id=session_id, |
| 681 | + instrument_name=instrument_name, |
| 682 | + metadata=tiff_info.series_metadata, |
| 683 | + messenger=_transport_object, |
| 684 | + ) |
| 685 | + return True |
0 commit comments