MuonDataLib Example: Slicing data

In this example we have a simple code for splitting event data into N_chunks of non-overlapping data.

[1]:
from MuonDataLib.data.loader.load_events import load_events
import os

file_name = 'HIFI00195790.nxs'
input_file = os.path.join('..', '..', '..', '..', 'test', 'data_files', file_name)

data = load_events(input_file, 64)
frame_start_times = data.get_frame_start_times()

# approx length of 1 frame
N_frames = len(frame_start_times)
print(N_frames)
N_chunks = 3
slice_width = N_frames//N_chunks


for chunk in range(N_chunks):
    data.clear_filters()
    start_slice = chunk*slice_width
    end_slice = (chunk+1)*slice_width - 1
    data.only_keep_data_time_between(f"filter", frame_start_times[start_slice], frame_start_times[end_slice] +0.01)
    output_name = f'HIFI00195790_chunk{chunk+1}.nxs'
    output_file = os.path.join('..', 'Output_files', output_name)
    print(f'Chunk number {chunk}:\n', data.report_filters(), f'number of frames in slice {end_slice - start_slice}', '\n')
    data.save_histograms(output_file)
WARNING: The metadata **RUN** is missing. Using fallback values
WARNING: The metadata **TITLE** is missing. Using fallback values
WARNING: The metadata **EXPERIMENT IDENTIFIER** is missing. Using fallback values
88
Resolution: 0.016 μs
Chunk number 0:
 Filters(time_filters=TimeFilters(keep_filters=[Filter(name='filter', start=np.float64(0.99375522), end=np.float64(1.70375772))], remove_filters=[]), sample_log_filters=[], peak_property=PeakProperty(Amplitudes=0.0), histogram_settings=HistogramSettings(min_time=0.0, max_time=32.768, num_bins=2048)) number of frames in slice 28

WARNING: The target 0.0 is before the first frame start time 0.99375522 seconds. Difference is 0.99375522 seconds
WARNING: The target 4.1737624 is after the last frame start time 3.1737624 seconds. Difference is 1.0 seconds
Resolution: 0.016 μs
Chunk number 1:
 Filters(time_filters=TimeFilters(keep_filters=[Filter(name='filter', start=np.float64(1.7137576600000002), end=np.float64(2.4237601399999997))], remove_filters=[]), sample_log_filters=[], peak_property=PeakProperty(Amplitudes=0.0), histogram_settings=HistogramSettings(min_time=0.0, max_time=32.768, num_bins=2048)) number of frames in slice 28

WARNING: The target 0.0 is before the first frame start time 0.99375522 seconds. Difference is 0.99375522 seconds
WARNING: The target 4.1737624 is after the last frame start time 3.1737624 seconds. Difference is 1.0 seconds
Resolution: 0.016 μs
Chunk number 2:
 Filters(time_filters=TimeFilters(keep_filters=[Filter(name='filter', start=np.float64(2.45376014), end=np.float64(3.1637625))], remove_filters=[]), sample_log_filters=[], peak_property=PeakProperty(Amplitudes=0.0), histogram_settings=HistogramSettings(min_time=0.0, max_time=32.768, num_bins=2048)) number of frames in slice 28

WARNING: The target 0.0 is before the first frame start time 0.99375522 seconds. Difference is 0.99375522 seconds
WARNING: The target 4.1737624 is after the last frame start time 3.1737624 seconds. Difference is 1.0 seconds
[2]:
# clean up
for chunk in range(N_chunks):
    output_name = f'HIFI00195790_chunk{chunk+1}.nxs'
    output_file = os.path.join('..', 'Output_files', output_name)
    os.remove(output_file)