{ "cells": [ { "cell_type": "markdown", "id": "d772e9be-3780-49b6-a609-f0e0aabeade2", "metadata": {}, "source": [ "# MuonDataLib Example: Slicing data\n", "\n", "In this example we have a simple code for splitting event data into `N_chunks` of non-overlapping data. \n" ] }, { "cell_type": "code", "execution_count": null, "id": "c0ff4b1f-327b-4825-98a8-6436fb65800e", "metadata": { "scrolled": true }, "outputs": [], "source": [ "from MuonDataLib.data.loader.load_events import load_events\n", "import os\n", "\n", "file_name = 'HIFI00195790.nxs'\n", "input_file = os.path.join('..', '..', '..', '..', 'test', 'data_files', file_name)\n", "\n", "data = load_events(input_file, 64)\n", "frame_start_times = data.get_frame_start_times()\n", "\n", "# approx length of 1 frame\n", "N_frames = len(frame_start_times)\n", "print(N_frames)\n", "N_chunks = 3\n", "slice_width = N_frames//N_chunks\n", "\n", "\n", "for chunk in range(N_chunks):\n", " data.clear_filters()\n", " start_slice = chunk*slice_width \n", " end_slice = (chunk+1)*slice_width - 1 \n", " data.only_keep_data_time_between(f\"filter\", frame_start_times[start_slice], frame_start_times[end_slice] +0.01)\n", " output_name = f'HIFI00195790_chunk{chunk+1}.nxs'\n", " output_file = os.path.join('..', 'Output_files', output_name)\n", " print(f'Chunk number {chunk}:\\n', data.report_filters(), f'number of frames in slice {end_slice - start_slice}', '\\n')\n", " data.save_histograms(output_file) " ] }, { "cell_type": "code", "execution_count": null, "id": "081ae62c-2d1b-408a-be7c-58ab4df6deec", "metadata": {}, "outputs": [], "source": [ "# clean up\n", "for chunk in range(N_chunks):\n", " output_name = f'HIFI00195790_chunk{chunk+1}.nxs'\n", " output_file = os.path.join('..', 'Output_files', output_name)\n", " os.remove(output_file)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.5" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }