Skip to content
Open
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
50 changes: 50 additions & 0 deletions src/murfey/client/contexts/atlas.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from murfey.client.context import Context, _atlas_destination, _get_source
from murfey.client.instance_environment import MurfeyInstanceEnvironment
from murfey.util.client import capture_post
from murfey.util.spa_metadata import get_grid_square_atlas_positions

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

Expand Down Expand Up @@ -158,3 +159,52 @@ def post_transfer_epu(
logger.info(
f"Registered data collection group for atlas {str(transferred_atlas_jpg)!r}"
)

elif environment and transferred_file.name == "Atlas.dm":
# Register all grid squares on this atlas
gs_pix_positions = get_grid_square_atlas_positions(transferred_file)
for gs, pos_data in gs_pix_positions.items():
if pos_data:
capture_post(
base_url=str(environment.url.geturl()),
router_name="session_control.spa_router",
function_name="register_grid_square",
token=self._token,
instrument_name=environment.instrument_name,
session_id=environment.murfey_session,
gsid=int(gs),
data={
"tag": str(transferred_file.parent),
"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],
},
)
if gs_pix_positions:
for p in transferred_file.parts:
if p.startswith("Sample"):
sample = int(p.replace("Sample", ""))
break
else:
logger.warning(
f"Sample could not be identified for {transferred_file}"
)
return
capture_post(
base_url=str(environment.url.geturl()),
router_name="session_control.spa_router",
function_name="register_atlas",
token=self._token,
instrument_name=environment.instrument_name,
session_id=environment.murfey_session,
data={
"name": f"{environment.visit}-sample-{sample}",
"acquisition_uuid": environment.acquisition_uuid,
"register_grid": True,
"tag": str(transferred_file.parent),
},
)
2 changes: 1 addition & 1 deletion src/murfey/client/contexts/spa_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def post_transfer(
"angle": pos_data[6],
},
)
if pos_data:
if gs_pix_positions:
capture_post(
base_url=str(environment.url.geturl()),
router_name="session_control.spa_router",
Expand Down
8 changes: 8 additions & 0 deletions src/murfey/server/api/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def register_dc_group(
)
).all():
# Case where we switch from atlas to processing
original_tag = dcg_murfey[0].tag
dcg_murfey[0].tag = dcg_params.tag or dcg_murfey[0].tag
if _transport_object:
_transport_object.send(
Expand All @@ -243,6 +244,13 @@ def register_dc_group(
},
)
db.add(dcg_murfey[0])
for grid_square in db.exec(
select(GridSquare)
.where(GridSquare.tag == original_tag)
.where(GridSquare.session_id == session_id)
).all():
grid_square.tag = dcg_params.tag or original_tag
db.add(grid_square)
db.commit()
else:
dcg_parameters = {
Expand Down
94 changes: 86 additions & 8 deletions tests/client/contexts/test_atlas.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ def test_atlas_context_mrc(mock_capture_post, tmp_path):
atlas_mrc.parent.mkdir(parents=True)
atlas_mrc.touch()

context.post_transfer(
atlas_mrc,
environment=env,
)
context.post_transfer(atlas_mrc, environment=env)
mock_capture_post.assert_called_once_with(
base_url="http://localhost:8000",
router_name="session_control.spa_router",
Expand Down Expand Up @@ -72,10 +69,7 @@ def test_atlas_context_xml(mock_capture_post, tmp_path):
"</numericValue></x></pixelSize></SpatialScale></MicroscopeImage>"
)

context.post_transfer(
atlas_xml,
environment=env,
)
context.post_transfer(atlas_xml, environment=env)
dcg_data = {
"experiment_type_id": 44, # Atlas
"tag": str(atlas_xml.parent),
Expand All @@ -95,3 +89,87 @@ def test_atlas_context_xml(mock_capture_post, tmp_path):
session_id=1,
data=dcg_data,
)


@patch("murfey.client.contexts.atlas.capture_post")
def test_atlas_context_dm(mock_capture_post, tmp_path):
env = MurfeyInstanceEnvironment(
url=urlparse("http://localhost:8000"),
client_id=0,
sources=[tmp_path / "cm12345-6"],
default_destinations={
tmp_path / "cm12345-6": f"{tmp_path}/destination/cm12345-6"
},
instrument_name="m01",
visit="cm12345-6",
murfey_session=1,
acquisition_uuid="uuid1",
)

# Write sample dm file
atlas_dm = tmp_path / "cm12345-6/Supervisor_atlas/Sample2/Atlas/Atlas.dm"
atlas_dm.parent.mkdir(parents=True)
grid_square_values = (
"<value><b:PositionOnTheAtlas>"
"<c:Center><d:x>1200</d:x><d:y>1500</d:y></c:Center>"
"<c:Physical><d:x>2</d:x><d:y>3</d:y></c:Physical>"
"<c:Size><d:width>130</d:width><d:height>560</d:height></c:Size>"
"<c:Rotation>0.14</c:Rotation>"
"</b:PositionOnTheAtlas></value>"
)
with open(atlas_dm, "w") as new_xml:
new_xml.write(
"<AtlasSessionXml><Atlas><TilesEfficient><_items>"
# First tile with two grid squares
"<TileXml><Nodes><KeyValuePairs><KeyValuePairOfintNodeXml1>"
f"<key>101</key>{grid_square_values}"
"</KeyValuePairOfintNodeXml1><KeyValuePairOfintNodeXml1>"
f"<key>102</key>{grid_square_values}"
"</KeyValuePairOfintNodeXml1></KeyValuePairs></Nodes></TileXml>"
# Second tile with two grid squares
"<TileXml><Nodes><KeyValuePairs><KeyValuePairOfintNodeXml1>"
f"<key>103</key>{grid_square_values}"
"</KeyValuePairOfintNodeXml1><KeyValuePairOfintNodeXml1>"
f"<key>104</key>{grid_square_values}"
"</KeyValuePairOfintNodeXml1></KeyValuePairs></Nodes></TileXml>"
# Close all
"</_items></TilesEfficient></Atlas></AtlasSessionXml>"
)

context = AtlasContext("tomo", tmp_path, {}, "token")
context.post_transfer(atlas_dm, environment=env)

assert mock_capture_post.call_count == 5
mock_capture_post.assert_any_call(
base_url="http://localhost:8000",
router_name="session_control.spa_router",
function_name="register_grid_square",
token="token",
instrument_name="m01",
session_id=1,
gsid=101,
data={
"tag": str(atlas_dm.parent),
"x_location": 1200,
"y_location": 1500,
"x_stage_position": 2e9,
"y_stage_position": 3e9,
"width": 130,
"height": 560,
"angle": 0.14,
},
)
mock_capture_post.assert_any_call(
base_url="http://localhost:8000",
router_name="session_control.spa_router",
function_name="register_atlas",
token="token",
instrument_name="m01",
session_id=1,
data={
"name": "cm12345-6-sample-2",
"acquisition_uuid": "uuid1",
"register_grid": True,
"tag": str(atlas_dm.parent),
},
)
Loading