-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworkflow_clem.py
More file actions
169 lines (145 loc) · 5.28 KB
/
Copy pathworkflow_clem.py
File metadata and controls
169 lines (145 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from __future__ import annotations
from ast import literal_eval
from importlib.metadata import (
EntryPoint, # type hinting only
entry_points,
)
from logging import getLogger
from pathlib import Path
from typing import Literal, Optional
from fastapi import APIRouter
from pydantic import BaseModel, field_validator
from sqlmodel import Session, select
import murfey.util.db as MurfeyDB
from murfey.server import _transport_object
from murfey.server.murfey_db import murfey_db
# Set up logger
logger = getLogger("murfey.server.api.clem")
# Create APIRouter class object
router = APIRouter(
prefix="/workflow/clem",
tags=["Workflows: CLEM"],
)
class LifInfo(BaseModel):
lif_file: Path
@router.post("/sessions/{session_id}/process_raw_lifs") # API posts to this URL
def process_raw_lifs(
session_id: int,
lif_file: LifInfo,
db: Session = murfey_db,
):
try:
# Try and load relevant Murfey workflow
workflow: EntryPoint = list(
entry_points(group="murfey.workflows", name="clem.process_raw_lifs")
)[0]
except IndexError:
raise RuntimeError("The relevant Murfey workflow was not found")
# Get instrument name from the database to load the correct config file
session_row: MurfeyDB.Session = db.exec(
select(MurfeyDB.Session).where(MurfeyDB.Session.id == session_id)
).one()
instrument_name = session_row.instrument_name
# Pass arguments along to the correct workflow
workflow.load()(
# Match the arguments found in murfey.workflows.clem.process_raw_lifs
file=lif_file.lif_file,
root_folder="images",
session_id=session_id,
instrument_name=instrument_name,
messenger=_transport_object,
)
return True
class TIFFSeriesInfo(BaseModel):
series_name: str
tiff_files: list[Path]
series_metadata: Path
@router.post("/sessions/{session_id}/process_raw_tiffs")
def process_raw_tiffs(
session_id: int,
tiff_info: TIFFSeriesInfo,
db: Session = murfey_db,
):
try:
# Try and load relevant Murfey workflow
workflow: EntryPoint = list(
entry_points(group="murfey.workflows", name="clem.process_raw_tiffs")
)[0]
except IndexError:
raise RuntimeError("The relevant Murfey workflow was not found")
# Get instrument name from the database to load the correct config file
session_row: MurfeyDB.Session = db.exec(
select(MurfeyDB.Session).where(MurfeyDB.Session.id == session_id)
).one()
instrument_name = session_row.instrument_name
# Pass arguments to correct workflow
workflow.load()(
# Match the arguments found in murfey.workflows.clem.process_raw_tiffs
tiff_list=tiff_info.tiff_files,
root_folder="images",
session_id=session_id,
instrument_name=instrument_name,
metadata=tiff_info.series_metadata,
messenger=_transport_object,
)
return True
class AlignAndMergeParams(BaseModel):
# Processing parameters
series_name: str
images: list[Path]
metadata: Path
# Optional processing parameters
crop_to_n_frames: Optional[int] = None
align_self: Literal["enabled", ""] = ""
flatten: Literal["mean", "min", "max", ""] = ""
align_across: Literal["enabled", ""] = ""
@field_validator("images", mode="before")
@classmethod
def parse_stringified_list(cls, value):
if isinstance(value, str):
try:
eval_result = literal_eval(value)
if isinstance(eval_result, list):
parent_tiffs = [Path(p) for p in eval_result]
return parent_tiffs
except (SyntaxError, ValueError):
raise ValueError("Unable to parse input")
# Return value as-is; if it fails, it fails
return value
@router.post("/sessions/{session_id}/align_and_merge_stacks")
def align_and_merge_stacks(
session_id: int,
align_and_merge_params: AlignAndMergeParams,
db: Session = murfey_db,
):
try:
# Try and load relevant Murfey workflow
workflow: EntryPoint = list(
entry_points(group="murfey.workflows", name="clem.align_and_merge")
)[0]
except IndexError:
raise RuntimeError("The relevant Murfey workflow was not found")
# Get instrument name from the database to load the correct config file
session_row: MurfeyDB.Session = db.exec(
select(MurfeyDB.Session).where(MurfeyDB.Session.id == session_id)
).one()
instrument_name = session_row.instrument_name
# Pass arguments to correct workflow
workflow.load()(
# Match the arguments found in murfey.workflows.clem.align_and_merge
# Session parameters
session_id=session_id,
instrument_name=instrument_name,
# Processing parameters
series_name=align_and_merge_params.series_name,
images=align_and_merge_params.images,
metadata=align_and_merge_params.metadata,
# Optional processing parameters
crop_to_n_frames=align_and_merge_params.crop_to_n_frames,
align_self=align_and_merge_params.align_self,
flatten=align_and_merge_params.flatten,
align_across=align_and_merge_params.align_across,
# Optional session parameters
messenger=_transport_object,
)
return True