Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 98 additions & 34 deletions src/murfey/client/contexts/spa_metadata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from pathlib import Path
from typing import Optional
from typing import Dict, Optional

import requests
import xmltodict
Expand All @@ -9,13 +9,66 @@
from murfey.client.contexts.spa import _get_source
from murfey.client.instance_environment import MurfeyInstanceEnvironment, SampleInfo
from murfey.util import authorised_requests, capture_post, get_machine_config_client
from murfey.util.spa_metadata import get_grid_square_atlas_positions
from murfey.util.spa_metadata import FoilHoleInfo, get_grid_square_atlas_positions

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

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


def _foil_hole_positions(xml_path: Path, grid_square: int) -> Dict[str, FoilHoleInfo]:
with open(xml_path, "r") as xml:
for_parsing = xml.read()
data = xmltodict.parse(for_parsing)
data = data["GridSquareXml"]
readout_area = data["MicroscopeImage"]["microscopeData"]["acquisition"]["camera"][

Check warning on line 24 in src/murfey/client/contexts/spa_metadata.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/contexts/spa_metadata.py#L20-L24

Added lines #L20 - L24 were not covered by tests
"ReadoutArea"
]
pixel_size = data["MicroscopeImage"]["SpatialScale"]["pixelSize"]["x"][

Check warning on line 27 in src/murfey/client/contexts/spa_metadata.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/contexts/spa_metadata.py#L27

Added line #L27 was not covered by tests
"numericValue"
]
full_size = (int(readout_area["a:width"]), int(readout_area["a:height"]))
serialization_array = data["TargetLocations"]["TargetLocationsEfficient"][

Check warning on line 31 in src/murfey/client/contexts/spa_metadata.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/contexts/spa_metadata.py#L30-L31

Added lines #L30 - L31 were not covered by tests
"a:m_serializationArray"
]
required_key = ""

Check warning on line 34 in src/murfey/client/contexts/spa_metadata.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/contexts/spa_metadata.py#L34

Added line #L34 was not covered by tests
for key in serialization_array.keys():
if key.startswith("b:KeyValuePairOfintTargetLocation"):
required_key = key
break

Check warning on line 38 in src/murfey/client/contexts/spa_metadata.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/contexts/spa_metadata.py#L37-L38

Added lines #L37 - L38 were not covered by tests
if not required_key:
return {}
foil_holes = {}

Check warning on line 41 in src/murfey/client/contexts/spa_metadata.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/contexts/spa_metadata.py#L40-L41

Added lines #L40 - L41 were not covered by tests
for fh_block in serialization_array[required_key]:
if fh_block["b:value"]["IsNearGridBar"] == "false":
image_paths = list(

Check warning on line 44 in src/murfey/client/contexts/spa_metadata.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/contexts/spa_metadata.py#L44

Added line #L44 was not covered by tests
(xml_path.parent.parent).glob(
f"Images-Disc*/GridSquare_{grid_square}/FoilHoles/FoilHole_{fh_block['b:key']}_*.jpg"
)
)
image_paths.sort(key=lambda x: x.stat().st_ctime)
image_path: str = str(image_paths[-1]) if image_paths else ""
pix_loc = fh_block["b:value"]["PixelCenter"]
stage = fh_block["b:value"]["StagePosition"]
diameter = fh_block["b:value"]["PixelWidthHeight"]["c:width"]
foil_holes[fh_block["b:key"]] = FoilHoleInfo(

Check warning on line 54 in src/murfey/client/contexts/spa_metadata.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/contexts/spa_metadata.py#L49-L54

Added lines #L49 - L54 were not covered by tests
id=int(fh_block["b:key"]),
grid_square_id=grid_square,
x_location=int(float(pix_loc["c:x"])),
y_location=int(float(pix_loc["c:y"])),
x_stage_position=float(stage["c:X"]),
y_stage_position=float(stage["c:Y"]),
readout_area_x=full_size[0] if image_path else None,
readout_area_y=full_size[1] if image_path else None,
thumbnail_size_x=None,
thumbnail_size_y=None,
pixel_size=float(pixel_size) if image_path else None,
image=str(image_path),
diameter=int(float(diameter)),
)
return foil_holes

Check warning on line 69 in src/murfey/client/contexts/spa_metadata.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/contexts/spa_metadata.py#L69

Added line #L69 was not covered by tests


def _atlas_destination(
environment: MurfeyInstanceEnvironment, source: Path, file_path: Path
) -> Path:
Expand Down Expand Up @@ -109,36 +162,47 @@
"atlas_pixel_size": atlas_pixel_size,
}
capture_post(url, json=dcg_data)
registered_grid_squares = (
requests.get(
f"{str(environment.url.geturl())}/sessions/{environment.murfey_session}/grid_squares"
)
.json()
.get(str(source), [])
gs_pix_positions = get_grid_square_atlas_positions(

Check warning on line 165 in src/murfey/client/contexts/spa_metadata.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/contexts/spa_metadata.py#L165

Added line #L165 was not covered by tests
_atlas_destination(environment, source, transferred_file)
/ environment.samples[source].atlas
)
for gs, pos_data in gs_pix_positions.items():
if pos_data:
capture_post(

Check warning on line 171 in src/murfey/client/contexts/spa_metadata.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/contexts/spa_metadata.py#L171

Added line #L171 was not covered by tests
f"{str(environment.url.geturl())}/sessions/{environment.murfey_session}/grid_square/{gs}",
json={
"tag": str(source),
"x_location": pos_data[0],
"y_location": pos_data[1],
"x_stage_position": pos_data[2],
"y_stage_position": pos_data[3],
"width": pos_data[4],
"height": pos_data[5],
"angle": pos_data[6],
},
)

elif transferred_file.suffix == ".dm" and environment:
gs_name = transferred_file.name.split("_")[1]
fh_positions = _foil_hole_positions(transferred_file, int(gs_name))
source = _get_source(transferred_file, environment=environment)
visitless_source = str(source).replace(f"/{environment.visit}", "")

Check warning on line 189 in src/murfey/client/contexts/spa_metadata.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/contexts/spa_metadata.py#L186-L189

Added lines #L186 - L189 were not covered by tests
for fh, fh_data in fh_positions.items():
capture_post(

Check warning on line 191 in src/murfey/client/contexts/spa_metadata.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/client/contexts/spa_metadata.py#L191

Added line #L191 was not covered by tests
f"{str(environment.url.geturl())}/sessions/{environment.murfey_session}/grid_square/{gs_name}/foil_hole",
json={
"name": fh,
"x_location": fh_data.x_location,
"y_location": fh_data.y_location,
"x_stage_position": fh_data.x_stage_position,
"y_stage_position": fh_data.y_stage_position,
"readout_area_x": fh_data.readout_area_x,
"readout_area_y": fh_data.readout_area_y,
"thumbnail_size_x": fh_data.thumbnail_size_x,
"thumbnail_size_y": fh_data.thumbnail_size_y,
"pixel_size": fh_data.pixel_size,
"diameter": fh_data.diameter,
"tag": visitless_source,
"image": fh_data.image,
},
)
if registered_grid_squares:
gs_pix_positions = get_grid_square_atlas_positions(
source_visit_dir / partial_path
)
for gs in registered_grid_squares:
pos_data = gs_pix_positions.get(str(gs["name"]))
if pos_data:
capture_post(
f"{str(environment.url.geturl())}/sessions/{environment.murfey_session}/grid_square/{gs['name']}",
json={
"tag": gs["tag"],
"readout_area_x": gs["readout_area_x"],
"readout_area_y": gs["readout_area_y"],
"thumbnail_size_x": gs["thumbnail_size_x"],
"thumbnail_size_y": gs["thumbnail_size_y"],
"pixel_size": gs["pixel_size"],
"image": gs["image"],
"x_location": pos_data[0],
"y_location": pos_data[1],
"x_stage_position": pos_data[2],
"y_stage_position": pos_data[3],
"width": pos_data[4],
"height": pos_data[5],
"angle": pos_data[6],
},
)
4 changes: 2 additions & 2 deletions src/murfey/util/spa_metadata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from pathlib import Path
from typing import Dict, NamedTuple, Optional, Tuple
from typing import Dict, NamedTuple, Optional, Tuple, Union

import xmltodict

Expand Down Expand Up @@ -167,7 +167,7 @@
)
)
image_paths.sort(key=lambda x: x.stat().st_ctime)
image_path: Path | str = image_paths[-1] if image_paths else ""
image_path: Union[Path, str] = image_paths[-1] if image_paths else ""

Check warning on line 170 in src/murfey/util/spa_metadata.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/util/spa_metadata.py#L170

Added line #L170 was not covered by tests
if image_path:
with open(Path(image_path).with_suffix(".xml")) as fh_xml:
fh_xml_data = xmltodict.parse(fh_xml.read())
Expand Down
Loading