{ "cells": [ { "cell_type": "markdown", "id": "1a7118fc-0e6c-4376-9888-f526f1838b42", "metadata": {}, "source": [ "# MuonDataLib Tutorial 5: Peak Property Filtering and Discrimination\n" ] }, { "cell_type": "markdown", "id": "e4706a89-6c64-4125-948b-00dd82de4e14", "metadata": {}, "source": [ "In the previous tutorials we used the sample logs and times to filter the event data. These filters removed events corresponding to a whole frame. It is also possible to discriminate/filter on individual events. These filters operate on the properties of the peaks that define the events, for example the peak amplitude. \n", "\n", "First lets create a histogram for some unfiltered data. \n" ] }, { "cell_type": "code", "execution_count": null, "id": "17a8a944-357c-4b1d-99ae-712c4c99cb34", "metadata": {}, "outputs": [], "source": [ "from MuonDataLib.data.loader.load_events import load_events\n", "from MuonDataLib.plot.basic import Figure\n", "import os\n", "import numpy as np\n", "\n", "file_name = 'HIFI00195790.nxs'\n", "input_file = os.path.join('..', '..', '..', '..', 'test', 'data_files', file_name)\n", "data = load_events(input_file, 64)\n", "\n", "no_filter_hist, bins = data.histogram()\n", "fig = Figure(y_label='Counts')\n", "fig.plot_from_histogram(bins, no_filter_hist, [0])\n", "fig.show()" ] }, { "cell_type": "markdown", "id": "34e7a894-447e-4839-81e8-65113dd5deed", "metadata": {}, "source": [ "## Inspecting a Peak Property\n", "\n", "At present the only available peak property is \"Amplitudes\", which relates to the height of the peak in the trace from the detector. Being able to descriminate/filter the events so that they all have amplitudes above some value is important for instrument calibration. To help the user decide on the value a method has been added to create a histogram of the peak property. " ] }, { "cell_type": "code", "execution_count": null, "id": "c1ba64a6-bade-4df0-b329-9611059569ed", "metadata": {}, "outputs": [], "source": [ "hist, bins = data.get_peak_property_histogram('Amplitudes')\n", "fig = Figure(y_label='Counts', x_label='Amplitude')\n", "fig.plot_peak_property_histogram(hist, bins, 'unfiltered, ')\n", "fig.show()" ] }, { "cell_type": "markdown", "id": "b61f30dd-140f-4f7f-9471-f86dcaca8af6", "metadata": {}, "source": [ "Notice that the plot was generated with the `plot_peak_property_histogram` method. The above plot shows a rapid decrease in the number of events as the size of the amplitude increases. Lets remove the steepest part of the curve by using the `keep_data_peak_property_above` method to filter out the data below 2600." ] }, { "cell_type": "code", "execution_count": null, "id": "ad24827c-78ca-4eca-9eae-eca7a04933cf", "metadata": {}, "outputs": [], "source": [ "data.keep_data_peak_property_above(\"Amplitudes\", 2600)" ] }, { "cell_type": "markdown", "id": "0ed1f0a6-b127-46f6-9879-3efa56754d36", "metadata": {}, "source": [ "Next lets look at the impact it has on the histograms " ] }, { "cell_type": "code", "execution_count": null, "id": "3e1cc928-47c1-4b36-848b-f6249b7f08de", "metadata": {}, "outputs": [], "source": [ "hist_amp_filter, bins = data.histogram()\n", "fig = Figure(y_label='Counts')\n", "fig.plot_from_histogram(bins, no_filter_hist, [0], 'unfiltered, ')\n", "fig.plot_from_histogram(bins, hist_amp_filter, [0], 'filtered (amplitude), ')\n", "fig.show()" ] }, { "cell_type": "markdown", "id": "bb52e27e-c50f-4342-86fd-2709a69a150b", "metadata": {}, "source": [ "We can see that the filters have removed some data as expected." ] }, { "cell_type": "markdown", "id": "fc61b639-09c5-435e-aab3-5c9beff539d4", "metadata": {}, "source": [ "## Removing the Filter\n", "\n", "The filter value can be changed by using the 'keep_data_peak_property` method with the new threshold value. Whereas, the `delete_data_peak_property_above` method will remove the filter, in reality this sets the threshold to 0." ] }, { "cell_type": "code", "execution_count": null, "id": "5c39ecc1", "metadata": {}, "outputs": [], "source": [ "data.delete_data_peak_property_above(\"Amplitudes\")" ] }, { "cell_type": "markdown", "id": "a60951df", "metadata": {}, "source": [ "Fianlly lets check that the histograms have been updated. " ] }, { "cell_type": "code", "execution_count": null, "id": "5bf38376-20c2-43e9-b315-b6b9893c20fc", "metadata": {}, "outputs": [], "source": [ "hist_del_filter, bins = data.histogram()\n", "fig = Figure(y_label='Counts')\n", "fig.plot_from_histogram(bins, no_filter_hist, [0], 'unfiltered, ')\n", "fig.plot_from_histogram(bins, hist_del_filter, [0], 'filter removed, ')\n", "fig.show()" ] }, { "cell_type": "markdown", "id": "2a4f9d6c-b737-41aa-b23b-bd9aaece1e40", "metadata": {}, "source": [ "As expected this data is identical to the unfiltered data. " ] } ], "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" } }, "nbformat": 4, "nbformat_minor": 5 }