scipp / essnmx

Data reduction for NMX at the European Spallation Source
https://scipp.github.io/essnmx/
BSD 3-Clause "New" or "Revised" License
0 stars 1 forks source link

Fix McStas loader to support McStas 3 results. #35

Closed YooSunYoung closed 6 months ago

YooSunYoung commented 7 months ago

Continuing from #33

Fixes #31 Fixes #26

Main Changes

YooSunYoung commented 7 months ago

Are there tests for this?

Yes...! Here: https://github.com/scipp/essnmx/blob/mcstas3-loader/tests/loader_test.py

jl-wynen commented 6 months ago

Are there tests for this?

Yes...! Here: https://github.com/scipp/essnmx/blob/mcstas3-loader/tests/loader_test.py

Do they test both McStas 2 and 3 files?

YooSunYoung commented 6 months ago

Are there tests for this?

Yes...! Here: https://github.com/scipp/essnmx/blob/mcstas3-loader/tests/loader_test.py

Do they test both McStas 2 and 3 files?

Good point... I'll add a test for 3 file...

YooSunYoung commented 6 months ago

Here is the another script to make a subset of McStas 3 file for testing. It is slightly different from the previous one since it now has 3 data banks.

from pathlib import Path
import h5py
import os

file_path =  Path('mccode.h5')
copied_file_path = Path('mccode.h5'.replace('.h5', '_subset.h5'))

print(f"Copy subset of {file_path} to {copied_file_path}")

# Remove file path if exists.
if os.path.exists(copied_file_path):
    os.remove(copied_file_path)

# Copy all fields and subset of event data field.
# If you copy a file and remove a field, the file size does not change.
with h5py.File(file_path, 'r') as file:
    entry = file['entry1']
    dataset = entry['data']
    event_paths = [f'bank0{i}_events_dat_list_p_x_y_n_id_t' for i in range(1, 4)]
    event_map = {
        event_path: dataset[event_path]['events'][()] for event_path in event_paths
    }

    print("Original Shape: ", [events.shape for events in event_map.values()])

    with h5py.File(copied_file_path, 'w') as copied_file:
        copied_entry = copied_file.create_group('entry1')
        copied_data = copied_entry.create_group('data')

        # Copy all non-data fields.
        for key in entry.keys():
            if key == 'data':
               continue
            else:
                entry.copy(key, copied_entry, key)

        # Copy all non-event data fields.
        for data_key in copied_entry['data'].keys():
            if data_key not in event_paths:
                dataset.copy(data_key, copied_data, data_key)

        # Copy subset of events.
        for event_path, events in event_map.items():
            copied_data.create_group(event_path)
            copied_data[event_path].create_dataset('events', data=events)
            print(event_path, " subset Shape: ", events.shape)
YooSunYoung commented 6 months ago

Are there tests for this?

Yes...! Here: https://github.com/scipp/essnmx/blob/mcstas3-loader/tests/loader_test.py

Do they test both McStas 2 and 3 files?

I added tests with McStas 3 file as well...!