.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/04_Tools/plot_02_using_numpy_functions.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_04_Tools_plot_02_using_numpy_functions.py: .. _plot_02_using_numpy_functions: =============================================== Using common NumPy functions on dnpdata objects =============================================== This example demonstrates how to use common numpy functions to manipulate dnpdata objects. Note, this is still an experimental feature. .. GENERATED FROM PYTHON SOURCE LINES 15-29 =================================================== How numpy array functions are operating on DNPData =================================================== Many numpy functions can directly be used on DNPData. What the function returns depends on the result: * when the result is a scalar a salar is returned * when the result is a ndarray a DNPData object is returned when the axis keyword is support by the numpy function one can provide the dimension (e.g. np.sum(mydata, axis='f2')) The corresponding axis is consumed and no longer in the returned DNPData object. The following example shows how this can be conveniently used. To get started, first, setup the python environment: .. GENERATED FROM PYTHON SOURCE LINES 29-34 .. code-block:: Python import dnplab as dnp import numpy as np import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 35-37 Let's load some example data the data consists of 4 fid that are phase cycled (0-90-180-270) .. GENERATED FROM PYTHON SOURCE LINES 37-40 .. code-block:: Python data = dnp.load("../../data/prospa/water_phase_cycled/data.2d") .. GENERATED FROM PYTHON SOURCE LINES 41-42 we are interested in the spectra .. GENERATED FROM PYTHON SOURCE LINES 42-45 .. code-block:: Python data = dnp.fourier_transform(data) .. GENERATED FROM PYTHON SOURCE LINES 46-47 since we don't know what the spectra is made of, we want to have a quick look at the magnitude: .. GENERATED FROM PYTHON SOURCE LINES 47-50 .. code-block:: Python data_magn = np.abs(data) .. GENERATED FROM PYTHON SOURCE LINES 51-52 and lets plot the magnitude spectrum for all 4 cycles .. GENERATED FROM PYTHON SOURCE LINES 52-55 .. code-block:: Python dnp.fancy_plot(data_magn) .. image-sg:: /auto_examples/04_Tools/images/sphx_glr_plot_02_using_numpy_functions_001.png :alt: plot 02 using numpy functions :srcset: /auto_examples/04_Tools/images/sphx_glr_plot_02_using_numpy_functions_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [, , , ] .. GENERATED FROM PYTHON SOURCE LINES 56-58 since the spectra are phase cycled the mean of the real part of the spectrum should be 0. Let's check and plot that: .. GENERATED FROM PYTHON SOURCE LINES 58-66 .. code-block:: Python mean_real_spectrum = np.real(np.mean(data, axis="Average")) total_mean = np.mean(mean_real_spectrum) average_in_dims = "Average" in mean_real_spectrum.dims print("The sum of the mean spectrum is {0} ".format(total_mean)) print("Average in mean_real_spectrum.dims: {0}".format(average_in_dims)) dnp.fancy_plot(mean_real_spectrum) .. image-sg:: /auto_examples/04_Tools/images/sphx_glr_plot_02_using_numpy_functions_002.png :alt: plot 02 using numpy functions :srcset: /auto_examples/04_Tools/images/sphx_glr_plot_02_using_numpy_functions_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none The sum of the mean spectrum is -1.037494719028472 Average in mean_real_spectrum.dims: False [] .. GENERATED FROM PYTHON SOURCE LINES 67-68 the total mean could also be calculated directly using np.mean() without the axis keyword (up to numerical precision) .. GENERATED FROM PYTHON SOURCE LINES 68-70 .. code-block:: Python print(np.mean(np.real(data)), "~=", total_mean) dnp.plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none -1.037494719028473 ~= -1.037494719028472 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.251 seconds) .. _sphx_glr_download_auto_examples_04_Tools_plot_02_using_numpy_functions.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_using_numpy_functions.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_using_numpy_functions.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_