.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/error_example.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_error_example.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_error_example.py:


Error and units example
=======================

Here is a simple example of errors and unit propagation

Notice that the base nddata class supplies error and propagation similar
to uncertainty-type libraries.

For the units, `pint` is doing the heavy lifting here.

.. GENERATED FROM PYTHON SOURCE LINES 12-17

.. code-block:: Python


    import pyspecdata as psd
    from numpy import r_
    import matplotlib.pyplot as plt








.. GENERATED FROM PYTHON SOURCE LINES 18-21

As a simple example, say that we perform several measurements of a
volume (not sure physically why we would have such variability, but
let's roll with it to keep the example simple!)

.. GENERATED FROM PYTHON SOURCE LINES 21-24

.. code-block:: Python


    vol = psd.nddata(r_[1.10, 1.11, 1.02, 1.03, 1.00, 1.05]).set_units("L")








.. GENERATED FROM PYTHON SOURCE LINES 25-26

Similarly, let's take some measurements of the weight of a solute!

.. GENERATED FROM PYTHON SOURCE LINES 26-29

.. code-block:: Python


    weight = psd.nddata(r_[2.10, 2.61, 2.002, 2.73, 2.33, 2.69]).set_units("μg")








.. GENERATED FROM PYTHON SOURCE LINES 30-38

To test our error propagation below, we're going to divide the two
arrays here -- because the variability of this number should be somewhat
similar to the propagated error below (though of course, there is a
statistical difference, and doing the two things does mean something
different).
Notice how, during string conversion, we always give the standard error
2 significant figures, and then base the significant figures of the
number on the error.

.. GENERATED FROM PYTHON SOURCE LINES 38-43

.. code-block:: Python


    conc_indiv = weight / vol
    conc_indiv.mean(stderr=True)
    print(conc_indiv)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    2.3 ± 0.11 µg/L




.. GENERATED FROM PYTHON SOURCE LINES 44-46

We take the mean, allowing it to accumulate the standard error.
(See comment above about significant figures.)

.. GENERATED FROM PYTHON SOURCE LINES 46-74

.. code-block:: Python


    vol.mean(stderr=True)
    print(vol)

    weight.mean(stderr=True)
    print(weight)

    print(weight / vol)

    # Because we want this to show up in sphinx gallery, we have
    # to make some type of figure

    fig = plt.figure()
    text = plt.Text(
        x=0.5,
        y=0.5,
        text=(
            "Compare one-by-one division,\nfollowed by standard"
            f" error:\n{conc_indiv}\nto division with error"
            f" propagation:\n{weight/vol}"
        ),
        fontsize=40,
        ha="center",
        va="center",
    )
    fig.add_artist(text)
    fig.tight_layout()
    plt.show()



.. image-sg:: /auto_examples/images/sphx_glr_error_example_001.png
   :alt: error example
   :srcset: /auto_examples/images/sphx_glr_error_example_001.png, /auto_examples/images/sphx_glr_error_example_001_2_00x.png 2.00x
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    1.05 ± 0.017 L
    2.4 ± 0.12 µg
    2.3 ± 0.12 µg/L





.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.537 seconds)


.. _sphx_glr_download_auto_examples_error_example.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: error_example.ipynb <error_example.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: error_example.py <error_example.py>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_