add FitResult fit overlays and close leaked HDF5 files after experiment stop
Build Debian Packages / build (bookworm, debian12) (push) Successful in 8m50s
Build Debian Packages / build (trixie, debian13) (push) Successful in 8m55s

FitResult is a new Drawable in damaris.data with x-values independent of
the measured points, so fits can be sampled on a dense grid. It is stored
as its own data pool entry (e.g. "measurement/fit"), written to and read
from the HDF5 data pool (damaris_type "FitResult", params as attributes),
and drawn by the GUI as overlay on its measurement, including a fit
parameter text box. Fits get no entry in the display tree, so measurement
rows stay directly selectable. Result scripts create them via
FitResult(x, y, title, source, params).

Additionally, HDF5 files that experiment or result scripts left open
(e.g. after a script failure) are closed when the experiment stops.
pytables keeps such handles registered process-wide, which blocked
reopening the dump file and the scripts' own files on the next run.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
markusro committed 2026-10-18 17:11:05 +02:00
1 parent 947c4c7f8d
commit ab943c30ae
7 files changed
+296 -5

No files matched your search

+1
View File
@@ -32,6 +32,7 @@ The data module contains result types, data handling, and signal processing clas
damaris.data.Drawable
damaris.data.Errorable
damaris.data.Error_Result
damaris.data.FitResult
damaris.data.MeasurementResult
damaris.data.Persistance
damaris.data.Resultable
+29
View File
@@ -285,6 +285,35 @@ A typical method to already extract data like amplitudes etc. is possible by usi
Essentially, a dictionary "measurement" is created with *tau* as the key. For each new key the current magnetization is added. This dictionary is then added to the data pool. The advantage over the previous listing is that the magnetization vs. τ curve can be directly displayed in order to gain an overview of the running experiment, because the "measurement" dictionary is added to the data pool. In this example, the value also stores statistics from the accumulation. The data dictionary is stored automatically as described before, including the result and experiment scripts, information about the used back end and a timeline of the experiment.
**Displaying fits**
A fit to a measurement can be shown as an overlay on the measurement curve using the FitResult class. In contrast to the measurement itself, a fit has its own x-values, independent of the measured data points. Therefore the fit can be sampled on a much denser x-grid, which makes the plotted curve smooth. The fit is stored in the data pool under its own key and is drawn by the GUI on top of the measurement it belongs to. Fit parameters are shown in the plot and are stored together with the fit.
The fit is associated with its measurement in two ways: by the ``source`` argument (the data pool key of the measurement) and, optionally, by storing it under a nested key such as ``"Magnetization(tau)/fit"``, so that it is stored next to the measurement in the HDF5 data pool file. The GUI shows the fit only as an overlay on its measurement; fits do not get their own entry in the display tree.
.. code-block:: python
:linenos:
import numpy
from damaris import FitResult
def result(results):
# ... fill "measurement" as in the listing above ...
# fit an exponential decay to the measured points
from scipy.optimize import curve_fit
[k, v, e] = measurement.get_errorplotdata()
popt, pcov = curve_fit(lambda t, m0, t1: m0 * numpy.exp(-t / t1),
k, v, p0=(v[0], 1.0), sigma=e)
# sample the fit on a dense x-grid for a smooth curve
xfit = numpy.linspace(k.min(), k.max(), 200)
fit = FitResult(xfit, popt[0] * numpy.exp(-xfit / popt[1]),
title="Magnetization(tau) fit",
source="Magnetization(tau)",
params={"M0": popt[0], "T1": popt[1]})
data["Magnetization(tau)/fit"] = fit
The fit is written to the HDF5 data pool file as its own table (columns *x* and *y*, plus *y_err* if given), together with the fit parameters as attributes, and is restored when the data pool is read back.
Note that we can also create a HDF5 file from scratch! In this case experiment and result script are not saved in the resulting file. The advantage is that one can create arbitrary complex structured files at the cost of having a more complicated result script.
.. code-block:: python