From cb8c23486ec73653b5b9b261bf9412d41b836f83 Mon Sep 17 00:00:00 2001 From: celinedurniak Date: Tue, 2 Apr 2024 13:23:32 +0200 Subject: [PATCH 1/5] Load DREAM SANS detector from csv --- src/ess/dream/io/geant4.py | 11 ++++++++--- tests/dream/io/geant4_test.py | 17 ++++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/ess/dream/io/geant4.py b/src/ess/dream/io/geant4.py index a20a1701..eb8b8f7a 100644 --- a/src/ess/dream/io/geant4.py +++ b/src/ess/dream/io/geant4.py @@ -20,6 +20,7 @@ MANTLE_DETECTOR_ID = sc.index(7) HIGH_RES_DETECTOR_ID = sc.index(8) +SANS_DETECTOR_ID = sc.index(9) ENDCAPS_DETECTOR_IDS = tuple(map(sc.index, (3, 4, 5, 6))) @@ -96,8 +97,8 @@ def _group(detectors: Dict[str, sc.DataArray]) -> Dict[str, sc.DataGroup]: elements = ('module', 'segment', 'counter', 'wire', 'strip') def group(key: str, da: sc.DataArray) -> sc.DataArray: - if key == 'high_resolution': - # Only the HR detector has sectors. + if key in ['high_resolution', 'sans']: + # Only the HR and SANS detectors have sectors. return da.group('sector', *elements) res = da.group(*elements) res.bins.coords.pop('sector', None) @@ -111,7 +112,7 @@ def _split_detectors( ) -> Dict[str, sc.DataArray]: groups = data.group( sc.concat( - [MANTLE_DETECTOR_ID, HIGH_RES_DETECTOR_ID, *ENDCAPS_DETECTOR_IDS], + [MANTLE_DETECTOR_ID, HIGH_RES_DETECTOR_ID, SANS_DETECTOR_ID, *ENDCAPS_DETECTOR_IDS], dim=detector_id_name, ) ) @@ -124,6 +125,10 @@ def _split_detectors( high_res := _extract_detector(groups, detector_id_name, HIGH_RES_DETECTOR_ID) ) is not None: detectors['high_resolution'] = high_res.copy() + if ( + sans := _extract_detector(groups, detector_id_name, SANS_DETECTOR_ID) + ) is not None: + detectors['sans'] = sans.copy() endcaps_list = [ det diff --git a/tests/dream/io/geant4_test.py b/tests/dream/io/geant4_test.py index 44549c5c..7199af30 100644 --- a/tests/dream/io/geant4_test.py +++ b/tests/dream/io/geant4_test.py @@ -52,13 +52,14 @@ def test_load_geant4_csv_loads_expected_structure(file): assert instrument.keys() == { 'mantle', 'high_resolution', + 'sans', 'endcap_forward', 'endcap_backward', } @pytest.mark.parametrize( - 'key', ('mantle', 'high_resolution', 'endcap_forward', 'endcap_backward') + 'key', ('mantle', 'high_resolution', 'sans', 'endcap_forward', 'endcap_backward') ) def test_load_gean4_csv_set_weights_to_one(file, key): detector = load_geant4_csv(file)['instrument'][key]['events'] @@ -128,6 +129,20 @@ def test_load_geant4_csv_high_resolution_has_expected_coords(file): assert 'position' in hr.bins.coords +def test_load_geant4_csv_sans_has_expected_coords(file): + sans = load_geant4_csv(file)['instrument']['sans']['events'] + assert_index_coord(sans.coords['module']) + assert_index_coord(sans.coords['segment']) + assert_index_coord(sans.coords['counter']) + assert_index_coord(sans.coords['wire'], values=set(range(1, 17))) + assert_index_coord(sans.coords['strip'], values=set(range(1, 33))) + assert_index_coord(sans.coords['sector'], values=set(range(1, 5))) + + assert 'tof' in sans.bins.coords + assert 'wavelength' in sans.bins.coords + assert 'position' in sans.bins.coords + + def test_geant4_in_pipeline(file_path, file): from ess.dream.io.geant4 import providers From ca5c2eb10d3d90faba9ce9d9ce9dc0eea30b42d6 Mon Sep 17 00:00:00 2001 From: celinedurniak Date: Tue, 2 Apr 2024 15:50:41 +0200 Subject: [PATCH 2/5] Add check for file without SANS data --- tests/dream/io/geant4_test.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/dream/io/geant4_test.py b/tests/dream/io/geant4_test.py index 7199af30..6b867b7b 100644 --- a/tests/dream/io/geant4_test.py +++ b/tests/dream/io/geant4_test.py @@ -134,9 +134,12 @@ def test_load_geant4_csv_sans_has_expected_coords(file): assert_index_coord(sans.coords['module']) assert_index_coord(sans.coords['segment']) assert_index_coord(sans.coords['counter']) - assert_index_coord(sans.coords['wire'], values=set(range(1, 17))) - assert_index_coord(sans.coords['strip'], values=set(range(1, 33))) - assert_index_coord(sans.coords['sector'], values=set(range(1, 5))) + + # check ranges only if csv file contains events from SANS detectors + if len(sans.coords['module'].values) > 0: + assert_index_coord(sans.coords['wire'], values=set(range(1, 17))) + assert_index_coord(sans.coords['strip'], values=set(range(1, 33))) + assert_index_coord(sans.coords['sector'], values=set(range(1, 5))) assert 'tof' in sans.bins.coords assert 'wavelength' in sans.bins.coords From 0e30bd7f076e855e56a93cd1792637b2c3872bc1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Tue, 2 Apr 2024 13:53:48 +0000 Subject: [PATCH 3/5] Apply automatic formatting --- src/ess/dream/io/geant4.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ess/dream/io/geant4.py b/src/ess/dream/io/geant4.py index eb8b8f7a..7be50b60 100644 --- a/src/ess/dream/io/geant4.py +++ b/src/ess/dream/io/geant4.py @@ -112,7 +112,12 @@ def _split_detectors( ) -> Dict[str, sc.DataArray]: groups = data.group( sc.concat( - [MANTLE_DETECTOR_ID, HIGH_RES_DETECTOR_ID, SANS_DETECTOR_ID, *ENDCAPS_DETECTOR_IDS], + [ + MANTLE_DETECTOR_ID, + HIGH_RES_DETECTOR_ID, + SANS_DETECTOR_ID, + *ENDCAPS_DETECTOR_IDS, + ], dim=detector_id_name, ) ) @@ -126,7 +131,7 @@ def _split_detectors( ) is not None: detectors['high_resolution'] = high_res.copy() if ( - sans := _extract_detector(groups, detector_id_name, SANS_DETECTOR_ID) + sans := _extract_detector(groups, detector_id_name, SANS_DETECTOR_ID) ) is not None: detectors['sans'] = sans.copy() From 1e4e5782be5806aa3b63d75cee0ce88780ae9abb Mon Sep 17 00:00:00 2001 From: Jan-Lukas Wynen Date: Wed, 3 Apr 2024 13:16:32 +0200 Subject: [PATCH 4/5] Add dream file with sans detector --- src/ess/dream/data.py | 1 + tests/dream/io/geant4_test.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/ess/dream/data.py b/src/ess/dream/data.py index 2a4f5e27..fe1b694d 100644 --- a/src/ess/dream/data.py +++ b/src/ess/dream/data.py @@ -19,6 +19,7 @@ def _make_pooch(): version=_version, registry={ 'data_dream_with_sectors.csv.zip': 'md5:52ae6eb3705e5e54306a001bc0ae85d8', + 'data_dream0_new_hkl_Si_pwd.csv.zip': 'md5:d0ae518dc1b943bb817b3d18c354ed01', # noqa: E501 'DREAM_nexus_sorted-2023-12-07.nxs': 'md5:22824e14f6eb950d24a720b2a0e2cb66', 'DREAM_simple_pwd_workflow/data_dream_diamond_vana_container_sample_union.csv.zip': 'md5:33302d0506b36aab74003b8aed4664cc', # noqa: E501 'DREAM_simple_pwd_workflow/data_dream_diamond_vana_container_sample_union_run2.csv.zip': 'md5:c7758682f978d162dcb91e47c79abb83', # noqa: E501 diff --git a/tests/dream/io/geant4_test.py b/tests/dream/io/geant4_test.py index 6b867b7b..204cb0ec 100644 --- a/tests/dream/io/geant4_test.py +++ b/tests/dream/io/geant4_test.py @@ -17,6 +17,11 @@ @pytest.fixture(scope='module') def file_path(): + return data.get_path('data_dream0_new_hkl_Si_pwd.csv.zip') + + +@pytest.fixture(scope='module') +def file_path_without_sans(): return data.get_path('data_dream_with_sectors.csv.zip') @@ -27,11 +32,23 @@ def load_file(file_path): return archive.read(archive.namelist()[0]) +# Load file into memory only once +@pytest.fixture(scope='module') +def load_file_without_sans(file_path_without_sans): + with zipfile.ZipFile(file_path_without_sans, 'r') as archive: + return archive.read(archive.namelist()[0]) + + @pytest.fixture(scope='function') def file(load_file): return BytesIO(load_file) +@pytest.fixture(scope='function') +def file_without_sans(load_file_without_sans): + return BytesIO(load_file_without_sans) + + def assert_index_coord( coord: sc.Variable, *, values: Optional[Set[int]] = None ) -> None: @@ -58,6 +75,21 @@ def test_load_geant4_csv_loads_expected_structure(file): } +def test_load_geant4_csv_loads_expected_structure_without_sans(file_without_sans): + loaded = load_geant4_csv(file_without_sans) + assert isinstance(loaded, sc.DataGroup) + assert loaded.keys() == {'instrument'} + + instrument = loaded['instrument'] + assert isinstance(instrument, sc.DataGroup) + assert instrument.keys() == { + 'mantle', + 'high_resolution', + 'endcap_forward', + 'endcap_backward', + } + + @pytest.mark.parametrize( 'key', ('mantle', 'high_resolution', 'sans', 'endcap_forward', 'endcap_backward') ) From 33df5e110a51da5469a7e37c21d46aadba11e933 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Wynen Date: Wed, 3 Apr 2024 13:18:31 +0200 Subject: [PATCH 5/5] Use data with sans in docs --- docs/user-guide/dream/dream-instrument-view.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/user-guide/dream/dream-instrument-view.ipynb b/docs/user-guide/dream/dream-instrument-view.ipynb index 1c69fa7e..3a3765a2 100644 --- a/docs/user-guide/dream/dream-instrument-view.ipynb +++ b/docs/user-guide/dream/dream-instrument-view.ipynb @@ -44,7 +44,7 @@ "outputs": [], "source": [ "dg = dream.io.load_geant4_csv(\n", - " dream.data.get_path('data_dream_with_sectors.csv.zip')\n", + " dream.data.get_path('data_dream0_new_hkl_Si_pwd.csv.zip')\n", ")\n", "dg = dg['instrument'] # Extract the instrument data\n", "\n",