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
5 changes: 1 addition & 4 deletions src/murfey/client/contexts/spa.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,7 @@ 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())}/sessions/{environment.murfey_session}/grid_square/{grid_square}/foil_hole"
if (
grid_square_metadata_file.is_file()
and environment.murfey_session is not None
):
if environment.murfey_session is not None:
fh = foil_hole_data(
grid_square_metadata_file,
foil_hole,
Expand Down
28 changes: 10 additions & 18 deletions src/murfey/client/contexts/spa_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@
for_parsing = xml.read()
data = xmltodict.parse(for_parsing)
data = data["GridSquareXml"]
readout_area = data["MicroscopeImage"]["microscopeData"]["acquisition"]["camera"][
"ReadoutArea"
]
pixel_size = data["MicroscopeImage"]["SpatialScale"]["pixelSize"]["x"][
"numericValue"
]
full_size = (int(readout_area["a:width"]), int(readout_area["a:height"]))
serialization_array = data["TargetLocations"]["TargetLocationsEfficient"][
"a:m_serializationArray"
]
Expand Down Expand Up @@ -58,11 +51,6 @@
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)),
)
Expand Down Expand Up @@ -128,17 +116,21 @@
source_visit_dir = source.parent

logger.info(
f"Looking for atlas XML file in metadata directory {str((source_visit_dir / partial_path).parent)}"
f"Looking for atlas XML file in metadata directory {str((source_visit_dir / environment.visit / partial_path).parent)}"
)
atlas_xml_path = list(
(source_visit_dir / partial_path).parent.glob("Atlas_*.xml")
(source_visit_dir / environment.visit / partial_path).parent.glob(
"Atlas_*.xml"
)
)[0]
logger.info(f"Atlas XML path {str(atlas_xml_path)} found")
with open(atlas_xml_path, "rb") as atlas_xml:
atlas_xml_data = xmltodict.parse(atlas_xml)
atlas_original_pixel_size = atlas_xml_data["MicroscopeImage"][
"SpatialScale"
]["pixelSize"]["x"]["numericValue"]
atlas_original_pixel_size = float(

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L129 was not covered by tests
atlas_xml_data["MicroscopeImage"]["SpatialScale"]["pixelSize"]["x"][
"numericValue"
]
)

# need to calculate the pixel size of the downscaled image
atlas_pixel_size = atlas_original_pixel_size * 7.8
Expand Down Expand Up @@ -190,7 +182,7 @@
)

elif transferred_file.suffix == ".dm" and environment:
gs_name = transferred_file.name.split("_")[1]
gs_name = transferred_file.stem.split("_")[1]

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L185 was not covered by tests
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}", "")
Expand Down
33 changes: 17 additions & 16 deletions src/murfey/server/ispyb.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@
def do_insert_foil_hole(
self,
grid_square_id: int,
scale_factor: float,
scale_factor: Optional[float],
foil_hole_parameters: FoilHoleParameters,
):
if (
Expand All @@ -265,21 +265,22 @@
foil_hole_parameters.readout_area_x
/ foil_hole_parameters.thumbnail_size_x
)
foil_hole_parameters.diameter = (
int(foil_hole_parameters.diameter * scale_factor)
if foil_hole_parameters.diameter
else None
)
foil_hole_parameters.x_location = (
int(foil_hole_parameters.x_location * scale_factor)
if foil_hole_parameters.x_location
else None
)
foil_hole_parameters.y_location = (
int(foil_hole_parameters.y_location * scale_factor)
if foil_hole_parameters.y_location
else None
)
if scale_factor:
foil_hole_parameters.diameter = (

Check warning on line 269 in src/murfey/server/ispyb.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/ispyb.py#L269

Added line #L269 was not covered by tests
int(foil_hole_parameters.diameter * scale_factor)
if foil_hole_parameters.diameter
else None
)
foil_hole_parameters.x_location = (

Check warning on line 274 in src/murfey/server/ispyb.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/ispyb.py#L274

Added line #L274 was not covered by tests
int(foil_hole_parameters.x_location * scale_factor)
if foil_hole_parameters.x_location
else None
)
foil_hole_parameters.y_location = (

Check warning on line 279 in src/murfey/server/ispyb.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/ispyb.py#L279

Added line #L279 was not covered by tests
int(foil_hole_parameters.y_location * scale_factor)
if foil_hole_parameters.y_location
else None
)
record = FoilHole(
gridSquareId=grid_square_id,
foilHoleLabel=foil_hole_parameters.name,
Expand Down
68 changes: 40 additions & 28 deletions src/murfey/util/spa_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,36 +148,37 @@


def foil_hole_data(xml_path: Path, foil_hole: int, grid_square: int) -> FoilHoleInfo:
with open(xml_path, "r") as xml:
for_parsing = xml.read()
data = xmltodict.parse(for_parsing)
data = data["GridSquareXml"]
serialization_array = data["TargetLocations"]["TargetLocationsEfficient"][
"a:m_serializationArray"
]
required_key = ""
for key in serialization_array.keys():
if key.startswith("b:KeyValuePairOfintTargetLocation"):
required_key = key
break
if required_key:
image_paths = list(
(xml_path.parent.parent).glob(
f"Images-Disc*/GridSquare_{grid_square}/FoilHoles/FoilHole_{foil_hole}_*.jpg"
)
if xml_path.is_file():
with open(xml_path, "r") as xml:
for_parsing = xml.read()
data = xmltodict.parse(for_parsing)
data = data["GridSquareXml"]
serialization_array = data["TargetLocations"]["TargetLocationsEfficient"][

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

View check run for this annotation

Codecov / codecov/patch

src/murfey/util/spa_metadata.py#L153-L157

Added lines #L153 - L157 were not covered by tests
"a:m_serializationArray"
]
for key in serialization_array.keys():
if key.startswith("b:KeyValuePairOfintTargetLocation"):
required_key = key
break
image_paths = list(

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

View check run for this annotation

Codecov / codecov/patch

src/murfey/util/spa_metadata.py#L162-L164

Added lines #L162 - L164 were not covered by tests
(xml_path.parent.parent).glob(
f"Images-Disc*/GridSquare_{grid_square}/FoilHoles/FoilHole_{foil_hole}_*.jpg"
)
image_paths.sort(key=lambda x: x.stat().st_ctime)
image_path: Union[Path, str] = image_paths[-1] if image_paths else ""
if image_path:
with open(Path(image_path).with_suffix(".xml")) as fh_xml:
fh_xml_data = xmltodict.parse(fh_xml.read())
readout_area = fh_xml_data["MicroscopeImage"]["microscopeData"][
"acquisition"
]["camera"]["ReadoutArea"]
pixel_size = fh_xml_data["MicroscopeImage"]["SpatialScale"]["pixelSize"][
"x"
]["numericValue"]
full_size = (int(readout_area["a:width"]), int(readout_area["a:height"]))
)
image_paths.sort(key=lambda x: x.stat().st_ctime)
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#L169-L170

Added lines #L169 - L170 were 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())
readout_area = fh_xml_data["MicroscopeImage"]["microscopeData"]["acquisition"][

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

View check run for this annotation

Codecov / codecov/patch

src/murfey/util/spa_metadata.py#L172-L174

Added lines #L172 - L174 were not covered by tests
"camera"
]["ReadoutArea"]
pixel_size = fh_xml_data["MicroscopeImage"]["SpatialScale"]["pixelSize"]["x"][

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

View check run for this annotation

Codecov / codecov/patch

src/murfey/util/spa_metadata.py#L177

Added line #L177 was not covered by tests
"numericValue"
]
full_size = (int(readout_area["a:width"]), int(readout_area["a:height"]))

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

View check run for this annotation

Codecov / codecov/patch

src/murfey/util/spa_metadata.py#L180

Added line #L180 was not covered by tests
if required_key:
for fh_block in serialization_array[required_key]:
pix = fh_block["b:value"]["PixelCenter"]
stage = fh_block["b:value"]["StagePosition"]
Expand All @@ -198,6 +199,17 @@
image=str(image_path),
diameter=diameter,
)
elif image_path:
return FoilHoleInfo(

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

View check run for this annotation

Codecov / codecov/patch

src/murfey/util/spa_metadata.py#L203

Added line #L203 was not covered by tests
id=foil_hole,
grid_square_id=grid_square,
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),
)
logger.warning(
f"Foil hole positions could not be determined from metadata file {xml_path} for foil hole {foil_hole}"
)
Expand Down
51 changes: 35 additions & 16 deletions src/murfey/workflows/spa/flush_spa_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@
.where(GridSquare.tag == grid_square_params.tag)
.where(GridSquare.session_id == session_id)
).one()
grid_square.x_location = grid_square_params.x_location
grid_square.y_location = grid_square_params.y_location
grid_square.x_stage_position = grid_square_params.x_stage_position
grid_square.y_stage_position = grid_square_params.y_stage_position
grid_square.x_location = grid_square_params.x_location or grid_square.x_location
grid_square.y_location = grid_square_params.y_location or grid_square.y_location
grid_square.x_stage_position = (

Check warning on line 44 in src/murfey/workflows/spa/flush_spa_preprocess.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/workflows/spa/flush_spa_preprocess.py#L42-L44

Added lines #L42 - L44 were not covered by tests
grid_square_params.x_stage_position or grid_square.x_stage_position
)
grid_square.y_stage_position = (

Check warning on line 47 in src/murfey/workflows/spa/flush_spa_preprocess.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/workflows/spa/flush_spa_preprocess.py#L47

Added line #L47 was not covered by tests
grid_square_params.y_stage_position or grid_square.y_stage_position
)
if _transport_object:
_transport_object.do_update_grid_square(grid_square.id, grid_square_params)
except Exception:
Expand Down Expand Up @@ -122,23 +126,37 @@
.where(FoilHole.grid_square_id == gsid)
.where(FoilHole.session_id == session_id)
).one()
foil_hole.x_location = foil_hole_params.x_location
foil_hole.y_location = foil_hole_params.y_location
foil_hole.x_stage_position = foil_hole_params.x_stage_position
foil_hole.y_stage_position = foil_hole_params.y_stage_position
foil_hole.readout_area_x = foil_hole_params.readout_area_x
foil_hole.readout_area_y = foil_hole_params.readout_area_y
foil_hole.thumbnail_size_x = foil_hole_params.thumbnail_size_x or jpeg_size[0]
foil_hole.thumbnail_size_y = foil_hole_params.thumbnail_size_y or jpeg_size[1]
foil_hole.pixel_size = foil_hole_params.pixel_size
if _transport_object:
foil_hole.x_location = foil_hole_params.x_location or foil_hole.x_location
foil_hole.y_location = foil_hole_params.y_location or foil_hole.y_location
foil_hole.x_stage_position = (

Check warning on line 131 in src/murfey/workflows/spa/flush_spa_preprocess.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/workflows/spa/flush_spa_preprocess.py#L129-L131

Added lines #L129 - L131 were not covered by tests
foil_hole_params.x_stage_position or foil_hole.x_stage_position
)
foil_hole.y_stage_position = (

Check warning on line 134 in src/murfey/workflows/spa/flush_spa_preprocess.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/workflows/spa/flush_spa_preprocess.py#L134

Added line #L134 was not covered by tests
foil_hole_params.y_stage_position or foil_hole.y_stage_position
)
foil_hole.readout_area_x = (

Check warning on line 137 in src/murfey/workflows/spa/flush_spa_preprocess.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/workflows/spa/flush_spa_preprocess.py#L137

Added line #L137 was not covered by tests
foil_hole_params.readout_area_x or foil_hole.readout_area_x
)
foil_hole.readout_area_y = (

Check warning on line 140 in src/murfey/workflows/spa/flush_spa_preprocess.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/workflows/spa/flush_spa_preprocess.py#L140

Added line #L140 was not covered by tests
foil_hole_params.readout_area_y or foil_hole.readout_area_y
)
foil_hole.thumbnail_size_x = (

Check warning on line 143 in src/murfey/workflows/spa/flush_spa_preprocess.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/workflows/spa/flush_spa_preprocess.py#L143

Added line #L143 was not covered by tests
foil_hole_params.thumbnail_size_x or foil_hole.thumbnail_size_x
) or jpeg_size[0]
foil_hole.thumbnail_size_y = (

Check warning on line 146 in src/murfey/workflows/spa/flush_spa_preprocess.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/workflows/spa/flush_spa_preprocess.py#L146

Added line #L146 was not covered by tests
foil_hole_params.thumbnail_size_y or foil_hole.thumbnail_size_y
) or jpeg_size[1]
foil_hole.pixel_size = foil_hole_params.pixel_size or foil_hole.pixel_size

Check warning on line 149 in src/murfey/workflows/spa/flush_spa_preprocess.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/workflows/spa/flush_spa_preprocess.py#L149

Added line #L149 was not covered by tests
if _transport_object and gs.readout_area_x:
_transport_object.do_update_foil_hole(
foil_hole.id, gs.thumbnail_size_x / gs.readout_area_x, foil_hole_params
)
except Exception:
if _transport_object:
fh_ispyb_response = _transport_object.do_insert_foil_hole(
gs.id, gs.thumbnail_size_x / gs.readout_area_x, foil_hole_params
gs.id,
gs.thumbnail_size_x / gs.readout_area_x if gs.readout_area_x else None,
foil_hole_params,
)
else:
fh_ispyb_response = {"success": False, "return_value": None}
Expand Down Expand Up @@ -334,7 +352,8 @@
)
except Exception as e:
logger.error(
f"Flushing position analysis for {f.file_path} caused exception {e}", exc_info=True
f"Flushing position analysis for {f.file_path} caused exception {e}",
exc_info=True,
)
foil_hole_id = None

Expand Down
Loading