X-ray Emission Spectroscopy Outlier Removal and Concentration Correction
========================================================================

This example demonstrates processing X-ray emission spectroscopy (XES) data, including
concentration correction and outlier detection/removal with visualization.

.. literalinclude:: ../../examples/xes_outliers_removal.py
    :language: python

Detailed Explanations
---------------------

We start by importing modules and setting up logging for detailed output during
processing.

.. code-block:: python

    import logging
    import sys

    import matplotlib.pyplot as plt

    from daxs.measurements import Xes
    from daxs.sources import Hdf5Source
    from daxs.utils import resources

    logging.basicConfig(level=logging.INFO, stream=sys.stdout)
    logging.getLogger("daxs").setLevel(logging.INFO)

Next, define data mappings for the HDF5 file, specifying x-axis (emission energy),
multiple signal channels, and monitor.

.. code-block:: python

    data_mappings = {
        "x": ".1/measurement/xes_en_texs",
        "signal": [
            ".1/measurement/g02",
            ".1/measurement/g03",
            ".1/measurement/g04",
            ".1/measurement/g05",
            ".1/measurement/g06",
            ".1/measurement/g07",
            ".1/measurement/g09",
            ".1/measurement/g10",
            ".1/measurement/g11",
            ".1/measurement/g12",
            ".1/measurement/g13",
            ".1/measurement/g14",
        ],
        "monitor": ".1/measurement/I0t",
    }

Load the data from scan `134` and create an `Xes` measurement object.

.. code-block:: python

    hdf5_filename = resources.getfile("P_XES.h5")
    source = Hdf5Source(hdf5_filename, 134, data_mappings=data_mappings)
    measurement = Xes(source)

Apply concentration correction using information from scan `135`.

.. code-block:: python

    measurement.concentration_correction(135)

Now that the data is loaded and concentration corrected, detect outliers with a
threshold of `9`. Plot all scans in a single figure to visualize the detected outliers.

.. code-block:: python

    measurement.find_outliers(threshold=9)
    for scan in measurement.scans:
        fig, ax = plt.subplots()
        scan.plot(ax)
    plt.tight_layout()
    plt.show()

.. image:: ../_static/images/xes_scans_with_outliers.png
    :alt: XES scans with outliers
    :align: center
    :width: 80%

Finally, the outliers are removed and the signal before and after removal is plotted for
comparison.

.. code-block:: python

    fig, ax = plt.subplots()
    ax.plot(measurement.x, measurement.signal, label="Before")
    measurement.remove_outliers()
    ax.plot(measurement.x, measurement.signal, label="After")
    ax.set_xlabel("Energy (keV)")
    ax.set_ylabel("Intensity (a.u.)")
    ax.legend(fancybox=False)
    ax.set_title("Outlier Removal")
    plt.tight_layout()
    plt.show()

.. image:: ../_static/images/xes_outliers_removal.png
    :alt: Outliers removal before and after
    :align: center
    :width: 80%
