|
1 | 1 | from unittest.mock import patch |
2 | 2 | from urllib.parse import urlparse |
3 | 3 |
|
| 4 | +import numpy as np |
| 5 | + |
4 | 6 | from murfey.client.contexts.sxt import SXTContext |
5 | 7 | from murfey.client.instance_environment import MurfeyInstanceEnvironment |
6 | 8 |
|
@@ -36,91 +38,110 @@ def test_sxt_context_xrm(mock_post, tmp_path): |
36 | 38 |
|
37 | 39 |
|
38 | 40 | @patch("requests.post") |
39 | | -@patch("murfey.client.contexts.sxt.Inspector") |
40 | | -@patch("murfey.client.contexts.sxt.open_txrm") |
41 | | -@patch("murfey.client.contexts.sxt.read_stream") |
42 | | -def test_sxt_context_txrm( |
43 | | - mock_read_stream, mock_open_txrm, mock_inspector, mock_post, tmp_path |
44 | | -): |
| 41 | +@patch("murfey.client.contexts.sxt.OleFileIO") |
| 42 | +def test_sxt_context_txrm(mock_ole_file, mock_post, tmp_path): |
45 | 43 | mock_post().status_code = 200 |
46 | | - mock_read_stream.side_effect = [ |
47 | | - [-55, -25, 5, 35, 65], # Angles |
48 | | - [0.01001], # Pixel size |
49 | | - [1024], # Image Width |
50 | | - [2048], # Image Height |
51 | | - [1.5], # Exposure time |
52 | | - [1000], # Mag |
53 | | - [5], # Image count |
| 44 | + mock_ole_file().__enter__().exists.return_value = True |
| 45 | + # Motor position names |
| 46 | + mock_ole_file().__enter__().openstream().read.return_value = ( |
| 47 | + "\x00Val1\x00\x00Energy\x00".encode() |
| 48 | + ) |
| 49 | + # Metadata encoded arrays |
| 50 | + mock_ole_file().__enter__().openstream().getvalue.side_effect = [ |
| 51 | + np.array([-55, -25, 5, 35, 65], dtype=np.float32).tobytes(), # Angles |
| 52 | + np.array([0.01001], dtype=np.float32).tobytes(), # Pixel size |
| 53 | + np.array([1024], dtype=np.int32).tobytes(), # Image Width |
| 54 | + np.array([2048], dtype=np.int32).tobytes(), # Image Height |
| 55 | + np.array([1.5], dtype=np.float32).tobytes(), # Exposure time |
| 56 | + np.array([1000], dtype=np.float32).tobytes(), # Mag |
| 57 | + np.array([5], dtype=np.int32).tobytes(), # Image count |
| 58 | + np.array([0, 519, 2, 3], dtype=np.float32).tobytes(), # Motor Pos (energy) |
54 | 59 | ] |
55 | 60 |
|
56 | 61 | env = MurfeyInstanceEnvironment( |
57 | 62 | url=urlparse("http://localhost:8000"), |
58 | 63 | client_id=0, |
59 | | - sources=[tmp_path], |
60 | | - default_destinations={tmp_path: str(tmp_path / "destination")}, |
| 64 | + sources=[tmp_path / "cm12345-6/grid1"], |
| 65 | + default_destinations={ |
| 66 | + f"{tmp_path}/cm12345-6/grid1": f"{tmp_path}/destination/cm12345-6/grid1" |
| 67 | + }, |
61 | 68 | instrument_name="", |
62 | | - visit="test", |
| 69 | + visit="cm12345-6", |
63 | 70 | murfey_session=1, |
64 | 71 | ) |
65 | | - context = SXTContext("zeiss", tmp_path, {}, "") |
| 72 | + context = SXTContext("zeiss", tmp_path / "cm12345-6/grid1", {}, "") |
66 | 73 | context.post_transfer( |
67 | | - tmp_path / "example.txrm", |
| 74 | + tmp_path / "cm12345-6/grid1/example.txrm", |
68 | 75 | required_position_files=[], |
69 | 76 | required_strings=["fractions"], |
70 | 77 | environment=env, |
71 | 78 | ) |
72 | 79 |
|
73 | | - mock_open_txrm.assert_called_once_with( |
74 | | - tmp_path / "example.txrm", load_images=False, load_reference=False, strict=False |
75 | | - ) |
76 | | - mock_inspector.assert_called_once() |
| 80 | + mock_ole_file.assert_any_call(str(tmp_path / "cm12345-6/grid1/example.txrm")) |
| 81 | + assert mock_ole_file().__enter__().exists.call_count == 10 |
| 82 | + assert mock_ole_file().__enter__().openstream.call_count == 11 # 9 + 2 above |
| 83 | + mock_ole_file().__enter__().exists.assert_any_call("ReferenceData/Image") |
| 84 | + for field_name in [ |
| 85 | + "ImageInfo/Angles", |
| 86 | + "ImageInfo/PixelSize", |
| 87 | + "ImageInfo/ImageWidth", |
| 88 | + "ImageInfo/ImageHeight", |
| 89 | + "ImageInfo/ExpTimes", |
| 90 | + "ImageInfo/XrayMagnification", |
| 91 | + "ImageInfo/ImagesTaken", |
| 92 | + "PositionInfo/AxisNames", |
| 93 | + "PositionInfo/MotorPositions", |
| 94 | + ]: |
| 95 | + mock_ole_file().__enter__().exists.assert_any_call(field_name) |
| 96 | + mock_ole_file().__enter__().openstream.assert_any_call(field_name) |
77 | 97 |
|
78 | 98 | assert mock_post.call_count == 5 |
79 | 99 | mock_post.assert_any_call( |
80 | | - "http://localhost:8000/workflow/visits/test/sessions/1/register_data_collection_group", |
| 100 | + "http://localhost:8000/workflow/visits/cm12345-6/sessions/1/register_data_collection_group", |
81 | 101 | json={ |
82 | 102 | "experiment_type_id": 47, |
83 | | - "tag": str(tmp_path), |
| 103 | + "tag": f"{tmp_path}/cm12345-6/grid1", |
84 | 104 | }, |
85 | 105 | headers={"Authorization": "Bearer "}, |
86 | 106 | ) |
87 | 107 | mock_post.assert_any_call( |
88 | | - "http://localhost:8000/workflow/visits/test/sessions/1/start_data_collection", |
| 108 | + "http://localhost:8000/workflow/visits/cm12345-6/sessions/1/start_data_collection", |
89 | 109 | json={ |
90 | 110 | "experiment_type": "sxt", |
91 | 111 | "file_extension": ".txrm", |
92 | 112 | "acquisition_software": "zeiss", |
93 | | - "image_directory": f"{tmp_path}/destination", |
| 113 | + "image_directory": f"{tmp_path}/destination/cm12345-6/grid1", |
94 | 114 | "data_collection_tag": "example", |
95 | | - "source": str(tmp_path), |
| 115 | + "source": f"{tmp_path}/cm12345-6/grid1", |
96 | 116 | "tag": "example", |
97 | | - "pixel_size_on_image": "100.1", |
| 117 | + "pixel_size_on_image": str(100.1 * 1e-10), |
98 | 118 | "image_size_x": 1024, |
99 | 119 | "image_size_y": 2048, |
100 | 120 | "magnification": 1000, |
| 121 | + "energy": 519, |
101 | 122 | "voltage": 0, |
102 | 123 | }, |
103 | 124 | headers={"Authorization": "Bearer "}, |
104 | 125 | ) |
105 | 126 | mock_post.assert_any_call( |
106 | | - "http://localhost:8000/workflow/visits/test/sessions/1/register_processing_job", |
| 127 | + "http://localhost:8000/workflow/visits/cm12345-6/sessions/1/register_processing_job", |
107 | 128 | json={ |
108 | 129 | "tag": "example", |
109 | | - "source": str(tmp_path), |
| 130 | + "source": f"{tmp_path}/cm12345-6/grid1", |
110 | 131 | "recipe": "sxt-aretomo", |
111 | 132 | "experiment_type": "sxt", |
112 | 133 | }, |
113 | 134 | headers={"Authorization": "Bearer "}, |
114 | 135 | ) |
115 | 136 | mock_post.assert_any_call( |
116 | | - "http://localhost:8000/workflow/sxt/visits/test/sessions/1/sxt_tilt_series", |
| 137 | + "http://localhost:8000/workflow/sxt/visits/cm12345-6/sessions/1/sxt_tilt_series", |
117 | 138 | json={ |
118 | | - "session_id": 1, |
119 | 139 | "tag": "example", |
120 | | - "source": str(tmp_path), |
| 140 | + "source": f"{tmp_path}/cm12345-6/grid1", |
121 | 141 | "pixel_size": 100.1, |
122 | 142 | "tilt_offset": 5, |
123 | | - "txrm": str(tmp_path / "destination/example.txrm"), |
| 143 | + "tilt_series_length": 5, |
| 144 | + "txrm": str(tmp_path / "destination/cm12345-6/grid1/example.txrm"), |
124 | 145 | }, |
125 | 146 | headers={"Authorization": "Bearer "}, |
126 | 147 | ) |
0 commit comments