|
| 1 | +# SPDX-License-Identifier: BSD-3-Clause |
| 2 | +# Copyright (c) 2023 Scipp contributors (https://github.com/scipp) |
| 3 | + |
| 4 | +import zipfile |
| 5 | +from io import BytesIO |
| 6 | +from typing import Optional, Set |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import pytest |
| 10 | +import scipp as sc |
| 11 | +import scipp.testing |
| 12 | + |
| 13 | +from ess.dream import data, load_geant4_csv |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture(scope='module') |
| 17 | +def file_path(): |
| 18 | + return data.get_path('data_dream_with_sectors.csv.zip') |
| 19 | + |
| 20 | + |
| 21 | +# Load file into memory only once |
| 22 | +@pytest.fixture(scope='module') |
| 23 | +def load_file(file_path): |
| 24 | + with zipfile.ZipFile(file_path, 'r') as archive: |
| 25 | + return archive.read(archive.namelist()[0]) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(scope='function') |
| 29 | +def file(load_file): |
| 30 | + return BytesIO(load_file) |
| 31 | + |
| 32 | + |
| 33 | +def assert_index_coord( |
| 34 | + coord: sc.Variable, *, values: Optional[Set[int]] = None |
| 35 | +) -> None: |
| 36 | + assert coord.ndim == 1 |
| 37 | + assert coord.unit is None |
| 38 | + assert coord.dtype == 'int64' |
| 39 | + if values is not None: |
| 40 | + assert set(np.unique(coord.values)) == values |
| 41 | + |
| 42 | + |
| 43 | +def test_load_geant4_csv_loads_expected_structure(file): |
| 44 | + loaded = load_geant4_csv(file) |
| 45 | + assert isinstance(loaded, sc.DataGroup) |
| 46 | + assert loaded.keys() == {'instrument'} |
| 47 | + |
| 48 | + instrument = loaded['instrument'] |
| 49 | + assert isinstance(instrument, sc.DataGroup) |
| 50 | + assert instrument.keys() == { |
| 51 | + 'mantle', |
| 52 | + 'high_resolution', |
| 53 | + 'endcap_forward', |
| 54 | + 'endcap_backward', |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.parametrize( |
| 59 | + 'key', ('mantle', 'high_resolution', 'endcap_forward', 'endcap_backward') |
| 60 | +) |
| 61 | +def test_load_gean4_csv_set_weights_to_one(file, key): |
| 62 | + detector = load_geant4_csv(file)['instrument'][key] |
| 63 | + events = detector.bins.constituents['data'].data |
| 64 | + sc.testing.assert_identical( |
| 65 | + events, sc.ones(sizes=events.sizes, with_variances=True, unit='counts') |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +def test_load_geant4_csv_mantle_has_expected_coords(file): |
| 70 | + # Only testing ranges that will not change in the future |
| 71 | + mantle = load_geant4_csv(file)['instrument']['mantle'] |
| 72 | + assert_index_coord(mantle.coords['module']) |
| 73 | + assert_index_coord(mantle.coords['segment']) |
| 74 | + assert_index_coord(mantle.coords['counter']) |
| 75 | + assert_index_coord(mantle.coords['wire'], values=set(range(1, 33))) |
| 76 | + assert_index_coord(mantle.coords['strip'], values=set(range(1, 257))) |
| 77 | + assert 'sector' not in mantle.coords |
| 78 | + |
| 79 | + assert 'sector' not in mantle.bins.coords |
| 80 | + assert 'tof' in mantle.bins.coords |
| 81 | + assert 'wavelength' in mantle.bins.coords |
| 82 | + assert 'position' in mantle.bins.coords |
| 83 | + |
| 84 | + |
| 85 | +def test_load_geant4_csv_endcap_backward_has_expected_coords(file): |
| 86 | + endcap = load_geant4_csv(file)['instrument']['endcap_backward'] |
| 87 | + assert_index_coord(endcap.coords['module']) |
| 88 | + assert_index_coord(endcap.coords['segment']) |
| 89 | + assert_index_coord(endcap.coords['counter']) |
| 90 | + assert_index_coord(endcap.coords['wire'], values=set(range(1, 17))) |
| 91 | + assert_index_coord(endcap.coords['strip'], values=set(range(1, 17))) |
| 92 | + assert 'sector' not in endcap.coords |
| 93 | + |
| 94 | + assert 'sector' not in endcap.bins.coords |
| 95 | + assert 'tof' in endcap.bins.coords |
| 96 | + assert 'wavelength' in endcap.bins.coords |
| 97 | + assert 'position' in endcap.bins.coords |
| 98 | + |
| 99 | + |
| 100 | +def test_load_geant4_csv_endcap_forward_has_expected_coords(file): |
| 101 | + endcap = load_geant4_csv(file)['instrument']['endcap_forward'] |
| 102 | + assert_index_coord(endcap.coords['module']) |
| 103 | + assert_index_coord(endcap.coords['segment']) |
| 104 | + assert_index_coord(endcap.coords['counter']) |
| 105 | + assert_index_coord(endcap.coords['wire'], values=set(range(1, 17))) |
| 106 | + assert_index_coord(endcap.coords['strip'], values=set(range(1, 17))) |
| 107 | + assert 'sector' not in endcap.coords |
| 108 | + |
| 109 | + assert 'sector' not in endcap.bins.coords |
| 110 | + assert 'tof' in endcap.bins.coords |
| 111 | + assert 'wavelength' in endcap.bins.coords |
| 112 | + assert 'position' in endcap.bins.coords |
| 113 | + |
| 114 | + |
| 115 | +def test_load_geant4_csv_high_resolution_has_expected_coords(file): |
| 116 | + hr = load_geant4_csv(file)['instrument']['high_resolution'] |
| 117 | + assert_index_coord(hr.coords['module']) |
| 118 | + assert_index_coord(hr.coords['segment']) |
| 119 | + assert_index_coord(hr.coords['counter']) |
| 120 | + assert_index_coord(hr.coords['wire'], values=set(range(1, 17))) |
| 121 | + assert_index_coord(hr.coords['strip'], values=set(range(1, 33))) |
| 122 | + assert_index_coord(hr.coords['sector'], values=set(range(1, 5))) |
| 123 | + |
| 124 | + assert 'tof' in hr.bins.coords |
| 125 | + assert 'wavelength' in hr.bins.coords |
| 126 | + assert 'position' in hr.bins.coords |
0 commit comments