.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/tutorial_start.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_tutorial_start.py: Starting tutorial ================= .. GENERATED FROM PYTHON SOURCE LINES 7-9 First we import numpy, matplotlib and skultrafast. For reproducebilty we should always print out the version of skultrafast .. GENERATED FROM PYTHON SOURCE LINES 9-18 .. code-block:: Python import matplotlib.pyplot as plt from skultrafast.dataset import TimeResSpec from skultrafast import plot_helpers import skultrafast from skultrafast import data_io skultrafast.__version__ .. rst-class:: sphx-glr-script-out .. code-block:: none '6.0' .. GENERATED FROM PYTHON SOURCE LINES 19-20 Some matplotlib setting for nicer pictures. .. GENERATED FROM PYTHON SOURCE LINES 20-25 .. code-block:: Python plt.rcParams['figure.dpi'] = 150 plt.rcParams['figure.figsize'] = (4, 3) plt.rcParams['figure.autolayout'] = True plt.rcParams['font.size'] = 9 .. GENERATED FROM PYTHON SOURCE LINES 26-32 Creating a TimeResSpec ---------------------- In this tuturial we use the example data which is provided by *skultrafast*. The `load_example` function gives us three arrays. One containing the wavelengths, another one containing the delay times and one two dimensional array containing the data in mOD. .. GENERATED FROM PYTHON SOURCE LINES 32-35 .. code-block:: Python wavelengths, t_ps, data_mOD = data_io.load_example() .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/skultrafast/envs/latest/lib/python3.11/site-packages/skultrafast/data_io.py:75: UserWarning: Reading `.npy` or `.npz` file required additional header parsing as it was created on Python 2. Save the file again to speed up loading and avoid this warning. wl, data, t = a["wl"], a["data"], a["t"] .. GENERATED FROM PYTHON SOURCE LINES 36-38 Lets look at the constructor of the `TimeResSpec` (Time resolved Spectra) class, which is the main object when working with single data: .. GENERATED FROM PYTHON SOURCE LINES 38-41 .. code-block:: Python print(TimeResSpec.__init__.__doc__) .. rst-class:: sphx-glr-script-out .. code-block:: none Class for working with time-resolved spectra. If offers methods for analyzing and pre-processing the data. To visualize the data, each `TimeResSpec` object has an instance of an `DataSetPlotter` object accessible under `plot`. Parameters ---------- wl : array of shape(n) Array of the spectral dimension t : array of shape(m) Array with the delay times. data : array of shape(n, m) Array with the data for each point. err : array of shape(n, m) or None (optional) Contains the std err of the data, can be `None`. name : str (optional) Identifier for data set. freq_unit : 'nm' or 'cm' (optional) Unit of the wavelength array, default is 'nm'. disp_freq_unit : 'nm','cm' or None (optional) Unit which is used by default for plotting, masking and cutting the dataset. If `None`, it defaults to `freq_unit`. Attributes ---------- wavelengths, wavenumbers, t, data : ndarray Arrays with the data itself. plot : TimeResSpecPlotter Helper class which can plot the dataset using `matplotlib`. t_idx : function Helper function to find the nearest index in t for a given time. wl_idx : function Helper function to search for the nearest wavelength index for a given wavelength. wn_idx : function Helper function to search for the nearest wavelength index for a given wavelength. auto_plot : bool When True, some function will display their result automatically. .. GENERATED FROM PYTHON SOURCE LINES 42-44 As we see, we can supply all the required parameters. Since the `freq_unit` defaults to 'nm' we don't need to supply this argument. .. GENERATED FROM PYTHON SOURCE LINES 44-48 .. code-block:: Python ds = TimeResSpec(wavelengths, t_ps, data_mOD) .. GENERATED FROM PYTHON SOURCE LINES 49-52 The TimeResSpec object simply consists of data itself and methods using that data. The attributes containing the data can be accessed under `ds.data`, `ds.wavenumbers`, `ds.wavelengths` and `ds.t`. .. GENERATED FROM PYTHON SOURCE LINES 52-55 .. code-block:: Python print(ds.data.shape, ds.t.shape, ds.wavelengths.shape) .. rst-class:: sphx-glr-script-out .. code-block:: none (409, 800) (409,) (800,) .. GENERATED FROM PYTHON SOURCE LINES 56-60 The TimeResSpec object also has some helper methods to work with the data. These functions find the index of the nearest value for a given number, e.g. to find the position in the time array where the time is zero we can call the `t_idx` method .. GENERATED FROM PYTHON SOURCE LINES 60-63 .. code-block:: Python print(ds.t_idx(0), ds.t[ds.t_idx(0)]) .. rst-class:: sphx-glr-script-out .. code-block:: none 75 0.0 .. GENERATED FROM PYTHON SOURCE LINES 64-65 Hence the spectrum at t = 0 is given by .. GENERATED FROM PYTHON SOURCE LINES 65-68 .. code-block:: Python y_0 = ds.data[ds.t_idx(0), :]; .. GENERATED FROM PYTHON SOURCE LINES 69-70 In addition, there is also a shorthand to return the data at these indices directly. .. GENERATED FROM PYTHON SOURCE LINES 70-73 .. code-block:: Python assert(sum(ds.t_d(0) - y_0) == 0) .. GENERATED FROM PYTHON SOURCE LINES 74-81 Overview map ------------ To get an general idea of the transient spectra we need to see it. All plotting functions are in the `TimeResSpec.plot` object, which is an instance of `TimeResSpecPlotter`. The plotting functions are using the `disp_freq_unit` of the dataset as frequency scale by default. This can be changed by changing the `disp_freq_unit` of the `TimeResSpecPlotter` object. .. GENERATED FROM PYTHON SOURCE LINES 83-86 .. code-block:: Python ds.plot.disp_freq_unit = 'nm' # does nothing, since 'nm' is the default # ds.plot.disp_freq_unit = 'cm' would use wavenumbers .. GENERATED FROM PYTHON SOURCE LINES 87-89 First, we want to check if the dataset is corrected for dispersion. For that we plot a colormap around the time-zero. .. GENERATED FROM PYTHON SOURCE LINES 89-93 .. code-block:: Python ds.plot.map(symlog=0, con_step=10., con_filter=(3, 10)) plt.ylim(-2, 2) .. image-sg:: /auto_examples/images/sphx_glr_tutorial_start_001.png :alt: tutorial start :srcset: /auto_examples/images/sphx_glr_tutorial_start_001.png, /auto_examples/images/sphx_glr_tutorial_start_001_1_50x.png 1.50x, /auto_examples/images/sphx_glr_tutorial_start_001_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (-2.0, 2.0) .. GENERATED FROM PYTHON SOURCE LINES 94-108 Evidently, the dataset is not corrected for dispersion. Since it is easier to work with a dispersion corrected dataset, we try to estimate the dispersion using the data directly. Dispersion estimation and correction ------------------------------------ *skultrafast* does this by first using a simple heuristic for determining the time- zero for each transient. The resulting dispersion curve is then fitted with a poly- nominal, using a robust fitting method. More details are given in the documentation. To estimate the dispersion just call the function. It will plot two colormaps, one with the original dataset, the time-zeros found by the heuristic and the robust polynomial fit of these values. The bottom color map shows the dispersion corrected data. .. GENERATED FROM PYTHON SOURCE LINES 108-111 .. code-block:: Python res = ds.estimate_dispersion(heuristic_args=(1.5,), deg=3) .. image-sg:: /auto_examples/images/sphx_glr_tutorial_start_002.png :alt: tutorial start :srcset: /auto_examples/images/sphx_glr_tutorial_start_002.png, /auto_examples/images/sphx_glr_tutorial_start_002_1_50x.png 1.50x, /auto_examples/images/sphx_glr_tutorial_start_002_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 112-125 By default, *skultrafast* uses a very simple heuristic to find the time-zero. It looks for the earliest value above a given limit in each transient, and therefore underestimates the time-zero systematically. Therefore we slightly shift the time-zero. This generally works surprisingly well. But if the exact time-zero is necessary, I recommend to try other methods or measure the dispersion directly. **WARNING**: The cell below changes the dataset inplace. Therefore repeated calls to the cell will shift the time-zero again and again. The shifting can also be applied setting the `shift_result` parameter in the call to `ds.estimate_dispersio`. .. GENERATED FROM PYTHON SOURCE LINES 125-129 .. code-block:: Python new_ds = res.correct_ds # Warning, this does not copy the dataset! new_ds.t -= 0.2 .. GENERATED FROM PYTHON SOURCE LINES 130-135 Plotting spectra and transients ------------------------------- A major part of *skultrafast* are convenience functions for generating figures. Starting with the colormap from above, we see now that our dataset looks correct: .. GENERATED FROM PYTHON SOURCE LINES 135-138 .. code-block:: Python new_ds.plot.map(con_step=10., con_filter=(3, 5)) .. image-sg:: /auto_examples/images/sphx_glr_tutorial_start_003.png :alt: tutorial start :srcset: /auto_examples/images/sphx_glr_tutorial_start_003.png, /auto_examples/images/sphx_glr_tutorial_start_003_1_50x.png 1.50x, /auto_examples/images/sphx_glr_tutorial_start_003_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (, ) .. GENERATED FROM PYTHON SOURCE LINES 139-140 To plot spectra at given delay times: .. GENERATED FROM PYTHON SOURCE LINES 140-143 .. code-block:: Python lines = res.correct_ds.plot.spec(-.2, 0.05, 0.3, 1, 2, 150) .. image-sg:: /auto_examples/images/sphx_glr_tutorial_start_004.png :alt: tutorial start :srcset: /auto_examples/images/sphx_glr_tutorial_start_004.png, /auto_examples/images/sphx_glr_tutorial_start_004_1_50x.png 1.50x, /auto_examples/images/sphx_glr_tutorial_start_004_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 144-145 Or plot transients for given wavelengths: .. GENERATED FROM PYTHON SOURCE LINES 145-148 .. code-block:: Python lines = res.correct_ds.plot.trans(500, 550, 620, 680) .. image-sg:: /auto_examples/images/sphx_glr_tutorial_start_005.png :alt: tutorial start :srcset: /auto_examples/images/sphx_glr_tutorial_start_005.png, /auto_examples/images/sphx_glr_tutorial_start_005_1_50x.png 1.50x, /auto_examples/images/sphx_glr_tutorial_start_005_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 149-158 All these function offer a number of options. More information can be found in their docstrings. Exponential fitting ------------------- Fitting a decay-associated spectra (DAS) is a one-liner in skultrafast. If the dataset is dispersion corrected, only a starting guess is necessary. Please look at the docstring to see how the starting guess is structured. _Note_, the the fitting interface may change in the future. .. GENERATED FROM PYTHON SOURCE LINES 158-163 .. code-block:: Python fit_res = new_ds.fit_exp([-0.0, 0.05, 0.2, 2, 20, 10000], model_coh=True, fix_sigma=False, fix_t0=False) fit_res.lmfit_res.params .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 164-165 Lets plot the DAS .. GENERATED FROM PYTHON SOURCE LINES 165-167 .. code-block:: Python new_ds.plot.das() .. image-sg:: /auto_examples/images/sphx_glr_tutorial_start_006.png :alt: tutorial start :srcset: /auto_examples/images/sphx_glr_tutorial_start_006.png, /auto_examples/images/sphx_glr_tutorial_start_006_1_50x.png 1.50x, /auto_examples/images/sphx_glr_tutorial_start_006_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [, , , ] .. GENERATED FROM PYTHON SOURCE LINES 168-170 We can always work with the results directly to make plots manually. Here, the `t_idx`, `wl_idx` and `wn_idx` methods of the dataset are very useful: .. GENERATED FROM PYTHON SOURCE LINES 170-179 .. code-block:: Python for wl in [500, 580, 620]: t0 = fit_res.lmfit_res.params['p0'].value idx = new_ds.wl_idx(wl) plt.plot(new_ds.t - t0, fit_res.fitter.data[:, idx], 'o', color='k', ms=4, alpha=0.4) plt.plot(new_ds.t - t0, fit_res.fitter.model[:, idx], lw=2, label='%d nm' % wl) plt.xlim(-1, 10) plot_helpers.lbl_trans(use_symlog=False) plt.legend(loc='best', ncol=1) .. image-sg:: /auto_examples/images/sphx_glr_tutorial_start_007.png :alt: tutorial start :srcset: /auto_examples/images/sphx_glr_tutorial_start_007.png, /auto_examples/images/sphx_glr_tutorial_start_007_1_50x.png 1.50x, /auto_examples/images/sphx_glr_tutorial_start_007_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 4.707 seconds) .. _sphx_glr_download_auto_examples_tutorial_start.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: tutorial_start.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: tutorial_start.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: tutorial_start.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_