|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from unittest.mock import patch |
| 5 | +from urllib.parse import urlparse |
| 6 | + |
| 7 | +from murfey.client.contexts.tomo import TomographyContext |
| 8 | +from murfey.client.instance_environment import MurfeyInstanceEnvironment |
| 9 | + |
| 10 | + |
| 11 | +def test_tomography_context_initialisation_for_tomo(tmp_path): |
| 12 | + context = TomographyContext("tomo", tmp_path, "") |
| 13 | + assert not context._completed_tilt_series |
| 14 | + assert context._acquisition_software == "tomo" |
| 15 | + |
| 16 | + |
| 17 | +@patch("requests.get") |
| 18 | +@patch("requests.post") |
| 19 | +def test_tomography_context_add_tomo_tilt(mock_post, mock_get, tmp_path): |
| 20 | + mock_post().status_code = 200 |
| 21 | + |
| 22 | + env = MurfeyInstanceEnvironment( |
| 23 | + url=urlparse("http://localhost:8000"), |
| 24 | + client_id=0, |
| 25 | + sources=[tmp_path], |
| 26 | + default_destinations={tmp_path: str(tmp_path)}, |
| 27 | + instrument_name="", |
| 28 | + visit="test", |
| 29 | + murfey_session=1, |
| 30 | + ) |
| 31 | + context = TomographyContext("tomo", tmp_path, "") |
| 32 | + (tmp_path / "Position_1_001_[30.0]_date_time_fractions.tiff").touch() |
| 33 | + context.post_transfer( |
| 34 | + tmp_path / "Position_1_001_[30.0]_date_time_fractions.tiff", |
| 35 | + required_position_files=[], |
| 36 | + required_strings=["fractions"], |
| 37 | + environment=env, |
| 38 | + ) |
| 39 | + assert context._tilt_series == { |
| 40 | + "Position_1": [tmp_path / "Position_1_001_[30.0]_date_time_fractions.tiff"] |
| 41 | + } |
| 42 | + (tmp_path / "Position_1_002_[-30.0]_date_time_fractions.tiff").touch() |
| 43 | + context.post_transfer( |
| 44 | + tmp_path / "Position_1_002_[-30.0]_date_time_fractions.tiff", |
| 45 | + required_position_files=[], |
| 46 | + required_strings=["fractions"], |
| 47 | + environment=env, |
| 48 | + ) |
| 49 | + assert not context._completed_tilt_series |
| 50 | + |
| 51 | + # Add Position_1.mdoc, which completes this position |
| 52 | + with open(tmp_path / "Position_1.mdoc", "w") as mdoc: |
| 53 | + mdoc.write("[ZValue = 0]\n[ZValue = 1]\n") |
| 54 | + context.post_transfer( |
| 55 | + tmp_path / "Position_1.mdoc", |
| 56 | + required_position_files=[], |
| 57 | + required_strings=["fractions"], |
| 58 | + environment=env, |
| 59 | + ) |
| 60 | + assert context._completed_tilt_series == ["Position_1"] |
| 61 | + |
| 62 | + # Start Position_2, this is not complete |
| 63 | + (tmp_path / "Position_2_002_[30.0]_date_time_fractions.tiff").touch() |
| 64 | + context.post_transfer( |
| 65 | + tmp_path / "Position_2_002_[30.0]_date_time_fractions.tiff", |
| 66 | + required_position_files=[], |
| 67 | + required_strings=["fractions"], |
| 68 | + environment=env, |
| 69 | + ) |
| 70 | + assert len(context._tilt_series.values()) == 2 |
| 71 | + assert context._completed_tilt_series == ["Position_1"] |
| 72 | + |
| 73 | + |
| 74 | +@patch("requests.get") |
| 75 | +@patch("requests.post") |
| 76 | +def test_tomography_context_add_tomo_tilt_out_of_order(mock_post, mock_get, tmp_path): |
| 77 | + mock_post().status_code = 200 |
| 78 | + |
| 79 | + env = MurfeyInstanceEnvironment( |
| 80 | + url=urlparse("http://localhost:8000"), |
| 81 | + client_id=0, |
| 82 | + sources=[tmp_path], |
| 83 | + default_destinations={tmp_path: str(tmp_path)}, |
| 84 | + instrument_name="", |
| 85 | + visit="test", |
| 86 | + murfey_session=1, |
| 87 | + ) |
| 88 | + context = TomographyContext("tomo", tmp_path, "") |
| 89 | + (tmp_path / "Position_1_001_[30.0]_date_time_fractions.tiff").touch() |
| 90 | + context.post_transfer( |
| 91 | + tmp_path / "Position_1_001_[30.0]_date_time_fractions.tiff", |
| 92 | + required_position_files=[], |
| 93 | + required_strings=["fractions"], |
| 94 | + environment=env, |
| 95 | + ) |
| 96 | + assert context._tilt_series == { |
| 97 | + "Position_1": [tmp_path / "Position_1_001_[30.0]_date_time_fractions.tiff"] |
| 98 | + } |
| 99 | + (tmp_path / "Position_1_002_[-30.0]_date_time_fractions.tiff").touch() |
| 100 | + context.post_transfer( |
| 101 | + tmp_path / "Position_1_002_[-30.0]_date_time_fractions.tiff", |
| 102 | + required_position_files=[], |
| 103 | + required_strings=["fractions"], |
| 104 | + environment=env, |
| 105 | + ) |
| 106 | + assert not context._completed_tilt_series |
| 107 | + (tmp_path / "Position_2_002_[-30.0]_date_time_fractions.tiff").touch() |
| 108 | + context.post_transfer( |
| 109 | + tmp_path / "Position_2_002_[-30.0]_date_time_fractions.tiff", |
| 110 | + required_position_files=[], |
| 111 | + required_strings=["fractions"], |
| 112 | + environment=env, |
| 113 | + ) |
| 114 | + assert len(context._tilt_series.values()) == 2 |
| 115 | + assert not context._completed_tilt_series |
| 116 | + (tmp_path / "Position_2_001_[30.0]_date_time_fractions.tiff").touch() |
| 117 | + context.post_transfer( |
| 118 | + tmp_path / "Position_2_001_[30.0]_date_time_fractions.tiff", |
| 119 | + required_position_files=[], |
| 120 | + required_strings=["fractions"], |
| 121 | + environment=env, |
| 122 | + ) |
| 123 | + assert len(context._tilt_series.values()) == 2 |
| 124 | + assert not context._completed_tilt_series |
| 125 | + (tmp_path / "Position_3_001_[30.0]_date_time_fractions.tiff").touch() |
| 126 | + (tmp_path / "Position_3_002_[-30.0]_date_time_fractions.tiff").touch() |
| 127 | + context.post_transfer( |
| 128 | + tmp_path / "Position_3_002_[-30.0]_date_time_fractions.tiff", |
| 129 | + required_position_files=[], |
| 130 | + required_strings=["fractions"], |
| 131 | + environment=env, |
| 132 | + ) |
| 133 | + assert len(context._tilt_series.values()) == 3 |
| 134 | + assert not context._completed_tilt_series |
| 135 | + |
| 136 | + # Add Position_1.mdoc, which completes this position |
| 137 | + with open(tmp_path / "Position_1.mdoc", "w") as mdoc: |
| 138 | + mdoc.write("[ZValue = 0]\n[ZValue = 1]\n") |
| 139 | + context.post_transfer( |
| 140 | + tmp_path / "Position_1.mdoc", |
| 141 | + required_position_files=[], |
| 142 | + required_strings=["fractions"], |
| 143 | + environment=env, |
| 144 | + ) |
| 145 | + assert context._completed_tilt_series == ["Position_1"] |
| 146 | + |
| 147 | + # Add Position_2.mdoc, which completes this position |
| 148 | + with open(tmp_path / "Position_2.mdoc", "w") as mdoc: |
| 149 | + mdoc.write("[ZValue = 0]\n[ZValue = 1]\n") |
| 150 | + context.post_transfer( |
| 151 | + tmp_path / "Position_2.mdoc", |
| 152 | + required_position_files=[], |
| 153 | + required_strings=["fractions"], |
| 154 | + environment=env, |
| 155 | + ) |
| 156 | + assert context._completed_tilt_series == ["Position_1", "Position_2"] |
| 157 | + |
| 158 | + |
| 159 | +@patch("requests.get") |
| 160 | +@patch("requests.post") |
| 161 | +def test_tomography_context_add_tomo_tilt_delayed_tilt(mock_post, mock_get, tmp_path): |
| 162 | + mock_post().status_code = 200 |
| 163 | + |
| 164 | + env = MurfeyInstanceEnvironment( |
| 165 | + url=urlparse("http://localhost:8000"), |
| 166 | + client_id=0, |
| 167 | + sources=[tmp_path], |
| 168 | + default_destinations={tmp_path: str(tmp_path)}, |
| 169 | + instrument_name="", |
| 170 | + visit="test", |
| 171 | + murfey_session=1, |
| 172 | + ) |
| 173 | + context = TomographyContext("tomo", tmp_path, "") |
| 174 | + (tmp_path / "Position_1_001_[30.0]_date_time_fractions.tiff").touch() |
| 175 | + context.post_transfer( |
| 176 | + tmp_path / "Position_1_001_[30.0]_date_time_fractions.tiff", |
| 177 | + required_position_files=[], |
| 178 | + required_strings=["fractions"], |
| 179 | + environment=env, |
| 180 | + ) |
| 181 | + assert context._tilt_series == { |
| 182 | + "Position_1": [tmp_path / "Position_1_001_[30.0]_date_time_fractions.tiff"] |
| 183 | + } |
| 184 | + (tmp_path / "Position_1_002_[-30.0]_date_time_fractions.tiff").touch() |
| 185 | + context.post_transfer( |
| 186 | + tmp_path / "Position_1_002_[-30.0]_date_time_fractions.tiff", |
| 187 | + required_position_files=[], |
| 188 | + required_strings=["fractions"], |
| 189 | + environment=env, |
| 190 | + ) |
| 191 | + assert not context._completed_tilt_series |
| 192 | + |
| 193 | + # Add Position_1.mdoc, with more tilts than have been seen so far |
| 194 | + with open(tmp_path / "Position_1.mdoc", "w") as mdoc: |
| 195 | + mdoc.write("[ZValue = 0]\n[ZValue = 1]\n[ZValue = 2]\n") |
| 196 | + context.post_transfer( |
| 197 | + tmp_path / "Position_1.mdoc", |
| 198 | + required_position_files=[], |
| 199 | + required_strings=["fractions"], |
| 200 | + environment=env, |
| 201 | + ) |
| 202 | + assert not context._completed_tilt_series |
| 203 | + |
| 204 | + # Now add the tilt which completes the series |
| 205 | + (tmp_path / "Position_1_003_[60.0]_data_time_fractions.tiff").touch() |
| 206 | + new_series = context.post_transfer( |
| 207 | + tmp_path / "Position_1_003_[60.0]_data_time_fractions.tiff", |
| 208 | + required_position_files=[], |
| 209 | + required_strings=["fractions"], |
| 210 | + environment=env, |
| 211 | + ) |
| 212 | + assert context._completed_tilt_series == ["Position_1"] |
| 213 | + assert new_series == ["Position_1"] |
| 214 | + |
| 215 | + |
| 216 | +def test_tomography_context_initialisation_for_serialem(tmp_path): |
| 217 | + context = TomographyContext("serialem", tmp_path, "") |
| 218 | + assert not context._completed_tilt_series |
| 219 | + assert context._acquisition_software == "serialem" |
| 220 | + |
| 221 | + |
| 222 | +@patch("requests.get") |
| 223 | +@patch("requests.post") |
| 224 | +def test_setting_tilt_series_size_and_completion_from_mdoc_parsing( |
| 225 | + mock_post, mock_get, tmp_path |
| 226 | +): |
| 227 | + mock_post().status_code = 200 |
| 228 | + |
| 229 | + env = MurfeyInstanceEnvironment( |
| 230 | + url=urlparse("http://localhost:8000"), |
| 231 | + client_id=0, |
| 232 | + sources=[tmp_path], |
| 233 | + default_destinations={tmp_path: str(tmp_path)}, |
| 234 | + instrument_name="", |
| 235 | + visit="test", |
| 236 | + murfey_session=1, |
| 237 | + ) |
| 238 | + context = TomographyContext("tomo", tmp_path, "") |
| 239 | + assert len(context._tilt_series_sizes) == 0 |
| 240 | + context.post_transfer( |
| 241 | + Path(__file__).parent.parent / "util" / "test_1.mdoc", |
| 242 | + environment=env, |
| 243 | + required_strings=["fractions"], |
| 244 | + ) |
| 245 | + assert len(context._tilt_series_sizes) == 1 |
| 246 | + assert context._tilt_series_sizes == {"test_1": 11} |
| 247 | + (tmp_path / "test_1.mdoc").touch() |
| 248 | + tilt = -50 |
| 249 | + (tmp_path / f"test_1_001_[{tilt:.1f}]_data_time_fractions.tiff").touch() |
| 250 | + context.post_transfer( |
| 251 | + tmp_path / f"test_1_001_[{tilt:.1f}]_data_time_fractions.tiff", |
| 252 | + environment=env, |
| 253 | + required_strings=["fractions"], |
| 254 | + ) |
| 255 | + assert context._tilt_series == { |
| 256 | + "test_1": [tmp_path / f"test_1_001_[{tilt:.1f}]_data_time_fractions.tiff"] |
| 257 | + } |
| 258 | + for i, t in enumerate(range(-40, 60, 10)): |
| 259 | + assert not context._completed_tilt_series |
| 260 | + (tmp_path / f"test_1_{i:03}_[{t:.1f}]_data_time_fractions.tiff").touch() |
| 261 | + context.post_transfer( |
| 262 | + tmp_path / f"test_1_{i:03}_[{t:.1f}]_data_time_fractions.tiff", |
| 263 | + environment=env, |
| 264 | + required_strings=["fractions"], |
| 265 | + ) |
| 266 | + assert len(context._tilt_series["test_1"]) == 11 |
| 267 | + assert context._completed_tilt_series == ["test_1"] |
0 commit comments