From d4be62879a5d7acec56e6fe08ef948d26c7596c9 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 13:46:49 +0000 Subject: [PATCH] Notebook 03: keep the essentials, the whole scan, the weighted fit and the series comparison go to the open part Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01RKuN833EwRkwYgUKwN8sKq --- notebooks/02_saving_loading.ipynb | 2 +- notebooks/03_analysis.ipynb | 157 +++++------------------------- 2 files changed, 25 insertions(+), 134 deletions(-) diff --git a/notebooks/02_saving_loading.ipynb b/notebooks/02_saving_loading.ipynb index c93861b..b7c20fe 100644 --- a/notebooks/02_saving_loading.ipynb +++ b/notebooks/02_saving_loading.ipynb @@ -19,7 +19,7 @@ "from pymodaq_data.data import Axis, DataRaw, DataToExport\n", "from pymodaq_data.h5modules.data_saving import DataToExportSaver, DataSaverLoader, DataLoader\n", "\n", - "DATA = Path('../data')\n", + "DATA = Path('../data') # a pathlib.Path: the operator / joins path parts (DATA / 'file.h5')\n", "\n", "# the objects of the sequence 1\n", "wavelength = Axis('wavelength', 'nm', data=np.linspace(500, 560, 121), index=0)\n", diff --git a/notebooks/03_analysis.ipynb b/notebooks/03_analysis.ipynb index ac2f7b3..e376068 100644 --- a/notebooks/03_analysis.ipynb +++ b/notebooks/03_analysis.ipynb @@ -21,7 +21,7 @@ "from pymodaq_utils.math_utils import gauss1D\n", "from pymodaq_data.h5modules.data_saving import DataLoader\n", "\n", - "DATA = Path('../data')\n", + "DATA = Path('../data') # a pathlib.Path: the operator / joins path parts (DATA / 'file.h5')\n", "SCAN = '/RawData/Scan000/Detector000/Data0D/CH00/Data00' # one scan\n", "SCAN8 = '/RawData/Scan001/Detector000/Data0D/CH00/Data00' # the same scan repeated 8 times (scan_average = 8)\n", "\n", @@ -138,86 +138,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## 5. Fit the whole scan, without cropping\n", - "\n", - "Compare the background (offset) with the one of the cropped fit: what do the satellite lines do? On the whole scan\n", - "you will also meet a point that the cropped window did not contain." - ] - }, - { - "cell_type": "code", - "metadata": { - "tags": [ - "exercise" - ] - }, - "source": [ - "full = ... # a copy of mean8... (hint: deepcopy)\n", - "... # ...whose navigation axis is declared as a signal axis (hint: nav_indexes)\n", - "x_full = full.get_axis_from_index(0)[0].get_data()\n", - "y_full = full.data[0]\n", - "finite = ... # mask of the finite values, curve_fit refuses NaN (hint: np.isfinite; look at np.isnan(scan8.data[0]): what happened?)\n", - "popt_full, pcov_full = curve_fit(model, x_full[finite], y_full[finite], p0=p0)\n", - "print('offset, cropped:', popt[3], ' whole scan:', popt_full[3])" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 6. Uncertainties from the repetitions, weighted fit" - ] - }, - { - "cell_type": "code", - "metadata": { - "tags": [ - "exercise" - ] - }, - "source": [ - "mean8 = ... # the mean of the 8 repetitions, with their standard error of the mean in `errors` (hint: scan8.mean_with_errors)\n", - "window = ... # crop again between 505 and 555 nm: the errors follow the slicing (hint: vnav)\n", - "window.nav_indexes = ()\n", - "popt_w, pcov_w = ... # weighted fit (hint: curve_fit with sigma=window.errors[0] and absolute_sigma=True)\n", - "chi2_red = np.sum(((window.data[0] - model(x, *popt_w)) / window.errors[0]) ** 2) / (len(x) - len(popt_w))\n", - "print(popt_w, np.sqrt(np.diag(pcov_w)))\n", - "print('reduced chi2:', chi2_red) # larger than 1: why?" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Plot the averaged data with its error bars and the weighted fit: are the residuals inside the error bars?" - ] - }, - { - "cell_type": "code", - "metadata": { - "tags": [ - "exercise" - ] - }, - "source": [ - "fig, ax = plt.subplots()\n", - "ax.errorbar(x, window.data[0], yerr=..., fmt='.', label='mean of 8 scans') # the errors of `window` (hint: window.errors)\n", - "ax.plot(x, model(x, *popt_w), '-', label='weighted fit')\n", - "ax.set_xlabel('wavelength (nm)')\n", - "ax.legend()" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 7. The axis in eV" + "## 5. The axis in eV" ] }, { @@ -244,7 +165,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## 8. One spectrum with Naverage = 200: the errors come with the data (PyMoDAQ 5.3.9 or later)" + "## 6. One spectrum with Naverage = 200: the errors come with the data (PyMoDAQ 5.3.9 or later)" ] }, { @@ -288,65 +209,35 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## 9. Compare with the standard error of the continuous recording" - ] - }, - { - "cell_type": "code", - "metadata": { - "tags": [ - "exercise" - ] - }, - "source": [ - "with DataLoader(DATA / 'continuous_spectra.h5') as loader:\n", - " series = ... # the enlargeable array '/RawData/Detector000/Data1D/CH00/EnlData00', with its navigation axis (hint: load_data)\n", - "n_spectra = series.shape[0]\n", - "sem_series = ... # the standard error of the mean of the series over the acquisitions (hint: series.mean_with_errors, then its errors)\n", - "i_peak = int(np.argmax(spectrum.data[0]))\n", - "print('standard error at the peak, Naverage=200:', spectrum.errors[0][i_peak], ' series:', sem_series[i_peak])\n", - "print('standard error far from the peak, Naverage=200:', spectrum.errors[0][:10].mean(), ' series:', sem_series[:10].mean())\n", - "fig, ax = plt.subplots()\n", - "ax.plot(series.data[0][:, i_peak], '.-') # find the slow drift and estimate its period\n", - "ax.set_xlabel('acquisition number')\n", - "ax.set_ylabel('intensity at the peak')" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Control question 3\n", - "\n", - "At the top of the line, the standard error given by the DAQ_Viewer (200 acquisitions) and the standard deviation of\n", - "the series divided by √200 are close. Far from the line they are close too, but much smaller: why? What does the\n", - "comparison of the standard deviation at the top with the one far from the line say about the origin of the noise?" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 10. Going further (open, for those who are done)\n", + "## 7. Going further (open, for those who are done)\n", "\n", "No hole to fill here and no single answer: pick one or several items, write your own cells, and keep what you find\n", "in your handout. Everything you need is in the files of the `data` folder and in the objects above.\n", "\n", - "1. **Model the drift.** In the continuous recording, fit the intensity at the peak versus the acquisition number with\n", + "1. **The whole scan.** Fit `mean8` without cropping (three gaussians and one background, or one gaussian and the\n", + " whole scan). Compare the background with the one of the cropped fit of the part 3: what do the satellite lines do?\n", + " You will meet a point that the cropped window did not contain: look at `np.isnan(scan8.data[0])` and at what\n", + " `curve_fit` says.\n", + "2. **Uncertainties from the repetitions.** `scan8.mean_with_errors(0)` puts the standard error of the mean of the 8\n", + " repetitions in `errors`. Crop it again (the errors follow the slicing), plot it with `errorbar`, fit it with\n", + " `curve_fit(..., sigma=window.errors[0], absolute_sigma=True)` and compute the reduced chi2. If it is larger than 1,\n", + " are the error bars too small or is the model incomplete?\n", + "3. **Two ways to get a standard error.** Load `continuous_spectra.h5`\n", + " (`'/RawData/Detector000/Data1D/CH00/EnlData00'`) and compare `series.mean_with_errors(0).errors[0]` with the\n", + " `errors` of the spectrum of the part 6, at the top of the line and far from it. They are close at both places but\n", + " much smaller far from the line: why? What does the standard deviation at the top, compared with the one far from\n", + " the line, say about the origin of the noise?\n", + "4. **Model the drift.** In the continuous recording, fit the intensity at the peak versus the acquisition number with\n", " a sinusoid. What are its period (in acquisitions) and its relative depth? Divide the series by the fitted drift\n", " and compute again the standard deviation at the peak: what is left?\n", - "2. **All the lines at once.** Fit the whole averaged scan with three gaussians and one background. Compare the\n", - " centre, the width and the background of the main line with the cropped fit of the part 3.\n", - "3. **The fit as a data object.** Build a `DataCalculated` holding the data of `window` and the fitted curve as two\n", - " channels (same axis, labels 'data' and 'fit'), plot it with the PyMoDAQ viewer (`import pymodaq_gui` first, then `%gui qt` and `.plot('qt')`), then save it\n", - " with its errors in an HDF5 file and open it in H5Browser.\n", - "4. **Navigation and signal on the series.** `series` has a navigation axis (time) and a signal axis (wavelength).\n", + "5. **The fit as a data object.** Build a `DataCalculated` holding the data of `window` and the fitted curve as two\n", + " channels (same axis, labels 'data' and 'fit'), plot it with the PyMoDAQ viewer (`import pymodaq_gui` first, then\n", + " `%gui qt` and `.plot('qt')`), then save it with its errors in an HDF5 file and open it in H5Browser.\n", + "6. **Navigation and signal on the series.** `series` has a navigation axis (time) and a signal axis (wavelength).\n", " Keep 525 to 532 nm on the signal axis with `vsig`: why does PyMoDAQ refuse (look at `series.distribution`)? Do it\n", " with `isig` instead (the axis has a `find_index` method), sum over the signal axis to get one number per\n", - " acquisition, and compare with the intensity at the peak of the part 9. Then keep the first 50 acquisitions only.\n", - "5. **Your own measurement.** Record a scan of the photodiode yourself in the DAQ_Scan (module A), with the satellite\n", + " acquisition, and compare with the intensity at the peak. Then keep the first 50 acquisitions only.\n", + "7. **Your own measurement.** Record a scan of the photodiode yourself in the DAQ_Scan (module A), with the satellite\n", " lines and the drift switched on in the settings of the plugin, and run this notebook on your file." ] },