Skip to content
14 changes: 6 additions & 8 deletions src/murfey/client/contexts/atlas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@
from pathlib import Path
from typing import Optional

import requests

from murfey.client.context import Context
from murfey.client.contexts.spa import _get_source
from murfey.client.contexts.spa_metadata import _atlas_destination
from murfey.client.instance_environment import MurfeyInstanceEnvironment
from murfey.util.api import url_path_for
from murfey.util.client import authorised_requests, capture_post
from murfey.util.client import capture_post

logger = logging.getLogger("murfey.client.contexts.atlas")

requests.get, requests.post, requests.put, requests.delete = authorised_requests()


class AtlasContext(Context):
def __init__(self, acquisition_software: str, basepath: Path):
Expand Down Expand Up @@ -44,8 +39,11 @@ def post_transfer(
environment, source, transferred_file
) / transferred_file.relative_to(source.parent)
capture_post(
f"{str(environment.url.geturl())}{url_path_for('session_control.spa_router', 'make_atlas_jpg', session_id=environment.murfey_session)}",
json={"path": str(transferred_atlas_name)},
base_url=str(environment.url.geturl()),
router_name="session_control.spa_router",
function_name="make_atlas_jpg",
session_id=environment.murfey_session,
data={"path": str(transferred_atlas_name)},
)
logger.info(
f"Submitted request to create JPG image of atlas {str(transferred_atlas_name)!r}"
Expand Down
74 changes: 28 additions & 46 deletions src/murfey/client/contexts/clem.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

from murfey.client.context import Context
from murfey.client.instance_environment import MurfeyInstanceEnvironment
from murfey.util.api import url_path_for
from murfey.util.client import capture_post, get_machine_config_client

# Create logger object
Expand Down Expand Up @@ -353,19 +352,14 @@ def register_lif_file(
register the LIF file in the database correctly as part of the CLEM workflow.
"""
try:
# Construct URL to post to post the request to
url = f"{environment.url.geturl()}{url_path_for('clem.router', 'register_lif_file', session_id=environment.murfey_session)}?lif_file={quote(str(lif_file), safe='')}"
# Validate
if not url:
logger.error(
"URL could not be constructed from the environment and file path"
)
return False

# Send the message
capture_post(url)
capture_post(
base_url=str(environment.url.geturl()),
router_name="clem.router",
function_name="register_lif_file",
session_id=environment.murfey_session,
data={"lif_file": quote(str(lif_file), safe="")},
)
return True

except Exception as e:
logger.error(
f"Error encountered when registering the LIF file in the database: {e}"
Expand All @@ -383,19 +377,14 @@ def process_lif_file(
"""

try:
# Construct the URL to post the request to
url = f"{environment.url.geturl()}{url_path_for('clem.router', 'process_raw_lifs', session_id=environment.murfey_session)}?lif_file={quote(str(lif_file), safe='')}"
# Validate
if not url:
logger.error(
"URL could not be constructed from the environment and file path"
)
return False

# Send the message
capture_post(url)
capture_post(
base_url=str(environment.url.geturl()),
router_name="clem.router",
function_name="process_raw_lifs",
session_id=environment.murfey_session,
data={"lif_file": quote(str(lif_file), safe="")},
)
return True

except Exception as e:
logger.error(f"Error encountered processing LIF file: {e}")
return False
Expand All @@ -411,17 +400,14 @@ def register_tiff_file(
"""

try:
url = f"{environment.url.geturl()}{url_path_for('clem.router', 'register_tiff_file', session_id=environment.murfey_session)}?tiff_file={quote(str(tiff_file), safe='')}"
if not url:
logger.error(
"URL could not be constructed from the environment and file path"
)
return False

# Send the message
capture_post(url)
capture_post(
base_url=str(environment.url.geturl()),
router_name="clem.router",
function_name="register_tiff_file",
session_id=environment.murfey_session,
data={"tiff_file": quote(str(tiff_file), safe="")},
)
return True

except Exception as e:
logger.error(
f"Error encountered when registering the TIFF file in the database: {e}"
Expand All @@ -439,18 +425,14 @@ def process_tiff_series(
"""

try:
# Construct URL for Murfey server to communicate with
url = f"{environment.url.geturl()}{url_path_for('clem.router', 'process_raw_tiffs', session_id=environment.murfey_session)}"
if not url:
logger.error(
"URL could not be constructed from the environment and file path"
)
return False

# Send the message
capture_post(url, json=tiff_dataset)
capture_post(
base_url=str(environment.url.geturl()),
router_name="clem.router",
function_name="process_raw_tiffs",
session_id=environment.murfey_session,
data=tiff_dataset,
)
return True

except Exception as e:
logger.error(f"Error encountered processing the TIFF series: {e}")
return False
17 changes: 9 additions & 8 deletions src/murfey/client/contexts/fib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
from pathlib import Path
from typing import Dict, List, NamedTuple, Optional

import requests
import xmltodict

from murfey.client.context import Context
from murfey.client.instance_environment import MurfeyInstanceEnvironment
from murfey.util.api import url_path_for
from murfey.util.client import authorised_requests
from murfey.util.client import capture_post

logger = logging.getLogger("murfey.client.contexts.fib")

requests.get, requests.post, requests.put, requests.delete = authorised_requests()


class Lamella(NamedTuple):
name: str
Expand Down Expand Up @@ -95,9 +91,14 @@ def post_transfer(
environment.default_destinations[self._basepath]
).name
# post gif list to gif making API call
requests.post(
f"{environment.url.geturl()}{url_path_for('workflow.correlative_router', 'make_gif', year=datetime.now().year, visit_name=environment.visit, session_id=environment.murfey_session)}",
json={
capture_post(
base_url=str(environment.url.geturl()),
router_name="workflow.correlative_router",
function_name="make_gif",
year=datetime.now().year,
visit_name=environment.visit,
session_id=environment.murfey_session,
data={
"lamella_number": lamella_number,
"images": gif_list,
"raw_directory": raw_directory,
Expand Down
70 changes: 43 additions & 27 deletions src/murfey/client/contexts/spa.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from pathlib import Path
from typing import Any, Dict, List, Optional, OrderedDict, Tuple

import requests
import xmltodict

from murfey.client.context import Context, ProcessingParameter
Expand All @@ -14,13 +13,7 @@
MurfeyID,
MurfeyInstanceEnvironment,
)
from murfey.util.api import url_path_for
from murfey.util.client import (
authorised_requests,
capture_get,
capture_post,
get_machine_config_client,
)
from murfey.util.client import capture_get, capture_post, get_machine_config_client
from murfey.util.spa_metadata import (
foil_hole_data,
foil_hole_from_file,
Expand All @@ -31,8 +24,6 @@

logger = logging.getLogger("murfey.client.contexts.spa")

requests.get, requests.post, requests.put, requests.delete = authorised_requests()


def _file_transferred_to(
environment: MurfeyInstanceEnvironment, source: Path, file_path: Path
Expand Down Expand Up @@ -235,7 +226,10 @@ def gather_metadata(
binning_factor = 1
if environment:
server_config_response = capture_get(
f"{str(environment.url.geturl())}{url_path_for('session_control.router', 'machine_info_by_instrument', instrument_name=environment.instrument_name)}"
base_url=str(environment.url.geturl()),
router_name="session_control.router",
function_name="machine_info_by_instrument",
instrument_name=environment.instrument_name,
)
if server_config_response is None:
return None
Expand Down Expand Up @@ -304,8 +298,11 @@ def _position_analysis(
Optional[float],
] = (None, None, None, None, None, None, None)
data_collection_group = (
requests.get(
f"{environment.url.geturl()}{url_path_for('session_info.router', 'get_dc_groups', session_id=environment.murfey_session)}"
capture_get(
base_url=str(environment.url.geturl()),
router_name="session_info.router",
function_name="get_dc_groups",
session_id=environment.murfey_session,
)
.json()
.get(str(source), {})
Expand All @@ -327,7 +324,6 @@ def _position_analysis(
local_atlas_path,
grid_square=str(grid_square),
)[str(grid_square)]
gs_url = f"{str(environment.url.geturl())}{url_path_for('session_control.spa_router', 'register_grid_square', session_id=environment.murfey_session, gsid=grid_square)}"
gs = grid_square_data(
grid_square_metadata_file,
grid_square,
Expand All @@ -348,8 +344,12 @@ def _position_analysis(
else ""
)
capture_post(
gs_url,
json={
base_url=str(environment.url.geturl()),
router_name="session_control.spa_router",
function_name="register_grid_square",
session_id=environment.murfey_session,
gsid=grid_square,
data={
"tag": str(source),
"readout_area_x": gs.readout_area_x,
"readout_area_y": gs.readout_area_y,
Expand All @@ -368,7 +368,6 @@ def _position_analysis(
)
foil_hole = foil_hole_from_file(transferred_file)
if foil_hole not in self._foil_holes[grid_square]:
fh_url = f"{str(environment.url.geturl())}{url_path_for('session_control.spa_router', 'register_foil_hole', session_id=environment.murfey_session, gs_name=grid_square)}"
if environment.murfey_session is not None:
fh = foil_hole_data(
grid_square_metadata_file,
Expand All @@ -391,8 +390,12 @@ def _position_analysis(
else ""
)
capture_post(
fh_url,
json={
base_url=str(environment.url.geturl()),
router_name="session_control.spa_router",
function_name="register_foil_hole",
session_id=environment.murfey_session,
gs_name=grid_square,
data={
"name": foil_hole,
"x_location": fh.x_location,
"y_location": fh.y_location,
Expand All @@ -410,8 +413,12 @@ def _position_analysis(
)
else:
capture_post(
fh_url,
json={
base_url=str(environment.url.geturl()),
router_name="session_control.spa_router",
function_name="register_foil_hole",
session_id=environment.murfey_session,
gs_name=grid_square,
data={
"name": foil_hole,
"tag": str(source),
},
Expand Down Expand Up @@ -467,7 +474,9 @@ def post_transfer(
)
if not environment.movie_counters.get(str(source)):
movie_counts_get = capture_get(
f"{environment.url.geturl()}{url_path_for('session_control.router', 'count_number_of_movies')}",
base_url=str(environment.url.geturl()),
router_name="session_control.router",
function_name="count_number_of_movies",
)
if movie_counts_get is not None:
environment.movie_counters[str(source)] = count(
Expand All @@ -481,8 +490,12 @@ def post_transfer(
eer_fractionation_file = None
if file_transferred_to.suffix == ".eer":
response = capture_post(
f"{str(environment.url.geturl())}{url_path_for('file_io_instrument.router', 'write_eer_fractionation_file', visit_name=environment.visit, session_id=environment.murfey_session)}",
json={
base_url=str(environment.url.geturl()),
router_name="file_io_instrument.router",
function_name="write_eer_fractionation_file",
visit_name=environment.visit,
session_id=environment.murfey_session,
data={
"eer_path": str(file_transferred_to),
"fractionation": self.data_collection_parameters[
"eer_fractionation"
Expand Down Expand Up @@ -511,7 +524,6 @@ def post_transfer(
)
foil_hole = None

preproc_url = f"{str(environment.url.geturl())}{url_path_for('workflow.spa_router', 'request_spa_preprocessing', visit_name=environment.visit, session_id=environment.murfey_session)}"
preproc_data = {
"path": str(file_transferred_to),
"description": "",
Expand All @@ -537,8 +549,12 @@ def post_transfer(
"foil_hole_id": foil_hole,
}
capture_post(
preproc_url,
json={
base_url=str(environment.url.geturl()),
router_name="workflow.spa_router",
function_name="request_spa_preprocessing",
visit_name=environment.visit,
session_id=environment.murfey_session,
data={
k: None if v == "None" else v
for k, v in preproc_data.items()
},
Expand Down
Loading