88from typing import Any
99
1010import numpy as np
11+ import numpy .typing as npt
1112import pytest
1213import scipp as sc
1314from dateutil .parser import parse as parse_datetime
@@ -134,25 +135,17 @@ def experiment_template() -> SqwIXExperiment:
134135def pixel_data () -> sc .DataArray :
135136 rng = np .random .default_rng (9293 )
136137 n_pixels = 76
137- # Chosen numbers can all be represented in float32 to allow exact comparisons.
138- return sc .DataArray (
139- sc .array (
140- dims = ['obs' ],
141- values = rng .uniform (0 , 100 , n_pixels ).astype ('float32' ),
142- variances = rng .uniform (0.1 , 1 , n_pixels ).astype ('float32' ),
143- unit = 'count' ,
144- ),
145- coords = {
146- 'idet' : sc .arange ('obs' , 0 , n_pixels , unit = None ).astype (int ) // sc .index (3 ),
147- 'irun' : sc .arange ('obs' , 0 , n_pixels , unit = None ).astype (int ) // sc .index (2 ),
148- 'ien' : sc .arange ('obs' , 0 , 2 * n_pixels , 2 , unit = None ).astype (int )
149- // sc .index (10 ),
150- 'u1' : sc .arange ('obs' , 0.0 , n_pixels + 0.0 , unit = '1/Å' ),
151- 'u2' : sc .arange ('obs' , 1.0 , n_pixels + 1.0 , unit = '1/Å' ),
152- 'u3' : sc .arange ('obs' , 2.0 , n_pixels + 2.0 , unit = '1/Å' ),
153- 'u4' : sc .arange ('obs' , n_pixels , unit = 'meV' ) * 2 ,
154- },
155- )
138+ return np .c_ [
139+ np .arange (0.0 , n_pixels + 0.0 ), # u1
140+ np .arange (1.0 , n_pixels + 1.0 ), # u2
141+ np .arange (2.0 , n_pixels + 2.0 ), # u3
142+ np .arange (0.0 , n_pixels ), # u4
143+ np .ones (n_pixels ) % 2 + 1 , # irun
144+ np .arange (0 , n_pixels ) % 5 + 1 , # idet
145+ np .arange (0 , n_pixels ) % 3 + 1 , # ien
146+ rng .uniform (0 , 100 , n_pixels ), # values
147+ rng .uniform (0.1 , 1 , n_pixels ), # errors
148+ ].astype (np .float32 )
156149
157150
158151def test_horace_roundtrip_main_header (
@@ -161,7 +154,7 @@ def test_horace_roundtrip_main_header(
161154 null_instrument : SqwIXNullInstrument ,
162155 sample : SqwIXSample ,
163156 experiment_template : SqwIXExperiment ,
164- pixel_data : sc . DataArray ,
157+ pixel_data : npt . NDArray [ np . float32 ] ,
165158 tmp_path : Path ,
166159) -> None :
167160 path = tmp_path / "roundtrip_main_header.sqw"
@@ -191,7 +184,7 @@ def test_horace_roundtrip_null_instruments(
191184 null_instrument : SqwIXNullInstrument ,
192185 sample : SqwIXSample ,
193186 experiment_template : SqwIXExperiment ,
194- pixel_data : sc . DataArray ,
187+ pixel_data : npt . NDArray [ np . float32 ] ,
195188 tmp_path : Path ,
196189) -> None :
197190 path = tmp_path / "roundtrip_null_instrument.sqw"
@@ -211,7 +204,9 @@ def test_horace_roundtrip_null_instruments(
211204 assert loaded .name == null_instrument .name
212205 assert loaded .source .name == null_instrument .source .name
213206 assert loaded .source .target_name == null_instrument .source .target_name
214- assert loaded .source .frequency == null_instrument .source .frequency .value
207+ assert (
208+ loaded .source .frequency == null_instrument .source .frequency .to (unit = 'Hz' ).value
209+ )
215210
216211
217212def test_horace_roundtrip_sample (
@@ -220,7 +215,7 @@ def test_horace_roundtrip_sample(
220215 null_instrument : SqwIXNullInstrument ,
221216 sample : SqwIXSample ,
222217 experiment_template : SqwIXExperiment ,
223- pixel_data : sc . DataArray ,
218+ pixel_data : npt . NDArray [ np . float32 ] ,
224219 tmp_path : Path ,
225220) -> None :
226221 path = tmp_path / "roundtrip_sample.sqw"
@@ -252,7 +247,7 @@ def test_horace_roundtrip_experiment(
252247 null_instrument : SqwIXNullInstrument ,
253248 sample : SqwIXSample ,
254249 experiment_template : SqwIXExperiment ,
255- pixel_data : sc . DataArray ,
250+ pixel_data : npt . NDArray [ np . float32 ] ,
256251 tmp_path : Path ,
257252) -> None :
258253 path = tmp_path / "roundtrip_experiment.sqw"
@@ -291,7 +286,7 @@ def test_horace_roundtrip_experiment_indirect(
291286 dnd_metadata : SqwDndMetadata ,
292287 null_instrument : SqwIXNullInstrument ,
293288 sample : SqwIXSample ,
294- pixel_data : sc . DataArray ,
289+ pixel_data : npt . NDArray [ np . float32 ] ,
295290 tmp_path : Path ,
296291) -> None :
297292 experiment_template = SqwIXExperiment (
@@ -353,7 +348,7 @@ def test_horace_roundtrip_pixels(
353348 null_instrument : SqwIXNullInstrument ,
354349 sample : SqwIXSample ,
355350 experiment_template : SqwIXExperiment ,
356- pixel_data : sc . DataArray ,
351+ pixel_data : npt . NDArray [ np . float32 ] ,
357352 tmp_path : Path ,
358353) -> None :
359354 path = tmp_path / "roundtrip_pixels.sqw"
@@ -362,10 +357,6 @@ def test_horace_roundtrip_pixels(
362357 replace (experiment_template , run_id = 0 , filename = "experiment_1.nxspe" ),
363358 replace (experiment_template , run_id = 1 , filename = "experiment_2.nxspe" ),
364359 ]
365- rng = np .random .default_rng (526 )
366- pixel_data .coords ['irun' ] = sc .array (
367- dims = ['obs' ], values = rng .integers (0 , 2 , len (pixel_data )), unit = None
368- )
369360
370361 (
371362 Sqw .build (path , title = "Pixel test file" )
@@ -377,18 +368,12 @@ def test_horace_roundtrip_pixels(
377368 )
378369
379370 loaded = matlab .read_horace (os .fspath (path ))
380- np .testing .assert_equal (loaded .pix .u1 .squeeze (), pixel_data .coords ['u1' ].values )
381- np .testing .assert_equal (loaded .pix .u2 .squeeze (), pixel_data .coords ['u2' ].values )
382- np .testing .assert_equal (loaded .pix .u3 .squeeze (), pixel_data .coords ['u3' ].values )
383- np .testing .assert_equal (loaded .pix .dE .squeeze (), pixel_data .coords ['u4' ].values )
384- np .testing .assert_equal (
385- loaded .pix .run_idx .squeeze (), pixel_data .coords ['irun' ].values
386- )
387- np .testing .assert_equal (
388- loaded .pix .detector_idx .squeeze (), pixel_data .coords ['idet' ].values
389- )
390- np .testing .assert_equal (
391- loaded .pix .energy_idx .squeeze (), pixel_data .coords ['ien' ].values
392- )
393- np .testing .assert_equal (loaded .pix .signal .squeeze (), pixel_data .values )
394- np .testing .assert_equal (loaded .pix .variance .squeeze (), pixel_data .variances )
371+ np .testing .assert_equal (loaded .pix .u1 .squeeze (), pixel_data [:, 0 ])
372+ np .testing .assert_equal (loaded .pix .u2 .squeeze (), pixel_data [:, 1 ])
373+ np .testing .assert_equal (loaded .pix .u3 .squeeze (), pixel_data [:, 2 ])
374+ np .testing .assert_equal (loaded .pix .dE .squeeze (), pixel_data [:, 3 ])
375+ np .testing .assert_equal (loaded .pix .run_idx .squeeze (), pixel_data [:, 4 ])
376+ np .testing .assert_equal (loaded .pix .detector_idx .squeeze (), pixel_data [:, 5 ])
377+ np .testing .assert_equal (loaded .pix .energy_idx .squeeze (), pixel_data [:, 6 ])
378+ np .testing .assert_equal (loaded .pix .signal .squeeze (), pixel_data [:, 7 ])
379+ np .testing .assert_equal (loaded .pix .variance .squeeze (), pixel_data [:, 8 ])
0 commit comments