Skip to content

Commit 5797c25

Browse files
committed
Update sqw tests
1 parent 8b05819 commit 5797c25

File tree

4 files changed

+87
-206
lines changed

4 files changed

+87
-206
lines changed

src/scippneutron/io/sqw/_sqw.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,18 @@ def g(n: str) -> Any:
435435
(e,) = candidate_efix
436436
efix = sc.scalar(e.value, unit="meV")
437437

438+
emode = EnergyMode(g("emode"))
439+
438440
raw_en = _get_struct_field(struct, "en").data
439-
if isinstance(raw_en, np.ndarray):
440-
# en = raw_en.squeeze()
441-
en = raw_en # TODO ?! no squeeze in indirect mode?
441+
raw_en = (
442+
raw_en
443+
if isinstance(raw_en, np.ndarray)
444+
else np.array([e.value for e in raw_en])
445+
)
446+
if emode == EnergyMode.indirect:
447+
en = sc.array(dims=["detector", "energy_transfer"], values=raw_en, unit="meV")
442448
else:
443-
en = [e.value for e in raw_en]
449+
en = sc.array(dims=["energy_transfer"], values=raw_en.squeeze(), unit="meV")
444450

445451
angle_unit = sc.Unit("deg" if g("angular_is_degree") else "rad")
446452

@@ -449,9 +455,8 @@ def g(n: str) -> Any:
449455
filepath=g("filepath"),
450456
run_id=int(g("run_id")) - 1,
451457
efix=efix,
452-
emode=EnergyMode(g("emode")),
453-
# TODO indirect mode
454-
en=sc.array(dims=["detector", "energy_transfer"], values=en, unit="meV"),
458+
emode=emode,
459+
en=en,
455460
psi=sc.scalar(g("psi"), unit=angle_unit),
456461
u=sc.vector(_get_struct_field(struct, "u").data, unit="1/angstrom"),
457462
v=sc.vector(_get_struct_field(struct, "v").data, unit="1/angstrom"),

tests/io/sqw/sqw_horace_test.py

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from typing import Any
99

1010
import numpy as np
11+
import numpy.typing as npt
1112
import pytest
1213
import scipp as sc
1314
from dateutil.parser import parse as parse_datetime
@@ -134,25 +135,17 @@ def experiment_template() -> SqwIXExperiment:
134135
def 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

158151
def 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

217212
def 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])

tests/io/sqw/sqw_read_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ def test_read_expdata(intact_v4_sqw: Path | BytesIO) -> None:
159159
)
160160
sc.testing.assert_identical(
161161
expdata[0].u,
162-
sc.vector([0.9957987070083618, 0.002324522938579321, -0.004358167294412851]),
162+
sc.vector(
163+
[0.9957987070083618, 0.002324522938579321, -0.004358167294412851],
164+
unit='1/Å',
165+
),
163166
)
164167

165168

0 commit comments

Comments
 (0)