From 5a2c2790ccb0c726712b7f8cbe7c55dd28e73e8b Mon Sep 17 00:00:00 2001 From: jupfi Date: Wed, 22 May 2024 17:51:41 +0200 Subject: [PATCH] Implemented basic fitting. --- README.md | 12 +++- src/nqrduck_measurement/controller.py | 34 ++++++++++- .../signalprocessing_options.py | 58 ++++++++++++++++++- src/nqrduck_measurement/view.py | 45 +++++++++++++- 4 files changed, 142 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c7b5a32..2433bf1 100644 --- a/README.md +++ b/README.md @@ -5,12 +5,15 @@ A module for the [nqrduck](https://github.com/nqrduck/nqrduck) project. This mod ## Installation ### Requirements + Dependencies are handled via the pyproject.toml file. ### Setup + To install the module you need the NQRduck core. You can find the installation instructions for the NQRduck core [here](https://github.com/nqrduck/nqrduck). Ideally you should install the module in a virtual environment. You can create a virtual environment by running the following command in the terminal: + ```bash python -m venv nqrduck # Activate the virtual environment @@ -18,22 +21,25 @@ python -m venv nqrduck ``` You can install this module and the dependencies by running the following command in the terminal while the virtual environment is activated and you are in the root directory of this module: + ```bash pip install . ``` Alternatively, you can install the module and the dependencies by running the following command in the terminal while the virtual environment is activated: + ```bash pip install nqrduck-measurement ``` ## Usage + The module is used with the [Spectrometer](https://github.com/nqrduck/nqrduck-spectrometer) module. However you need to use an actual submodule of the spectrometer module like: - [nqrduck-spectrometer-limenqr](https://github.com/nqrduck/nqrduck-spectrometer-limenqr) A module used for magnetic resonance experiments with the LimeSDR (USB or Mini 2.0). - [nqrduck-spectrometer-simulator](https://github.com/nqrduck/nqrduck-spectrometer-simulator) A module used for simulating magnetic resonance experiments. -The pulse sequence and spectrometer settings can be adjusted using the 'Spectrometer' tab. +The pulse sequence and spectrometer settings can be adjusted using the 'Spectrometer' tab. drawing @@ -42,8 +48,12 @@ The pulse sequence and spectrometer settings can be adjusted using the 'Spectrom - c.) The 'Measurement Plot'. Here the measured data is displayed. One can switch time and frequency domain plots. - d.) The import and export buttons for the measurement data. +You can then remove the folder of the virtual environment. + ## License + This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details ## Contributing + If you're interested in contributing to the project, start by checking out our [nqrduck-module template](https://github.com/nqrduck/nqrduck-module). To contribute to existing modules, please first open an issue in the respective module repository to discuss your ideas or report bugs. diff --git a/src/nqrduck_measurement/controller.py b/src/nqrduck_measurement/controller.py index e4f3c12..7002b87 100644 --- a/src/nqrduck_measurement/controller.py +++ b/src/nqrduck_measurement/controller.py @@ -4,7 +4,7 @@ import logging import json from PyQt6.QtCore import pyqtSlot, pyqtSignal from PyQt6.QtWidgets import QApplication -from .signalprocessing_options import Apodization +from .signalprocessing_options import Apodization, Fitting from nqrduck.module.module_controller import ModuleController from nqrduck_spectrometer.measurement import Measurement @@ -231,6 +231,38 @@ class MeasurementController(ModuleController): self.module.model.displayed_measurement = apodized_measurement self.module.model.add_measurement(apodized_measurement) + + def show_fitting_dialog(self) -> None: + """Show fitting dialog.""" + logger.debug("Showing fitting dialog.") + # First we check if there is a measurement. + if not self.module.model.displayed_measurement: + logger.debug("No measurement to fit.") + self.module.nqrduck_signal.emit( + "notification", ["Error", "No measurement to fit."] + ) + return + + measurement = self.module.model.displayed_measurement + + dialog = Fitting(measurement, parent=self.module.view) + result = dialog.exec() + + logger.debug("Dialog result: %s", result) + if not result: + return + + fit = dialog.get_fit()[1] + + logger.debug("Fitting function: %s", fit) + + params = fit.fit() + + measurement.add_fit(fit) + + self.module.view.update_displayed_measurement() + + dialog.deleteLater() @pyqtSlot() def change_displayed_measurement(self, measurement=None) -> None: diff --git a/src/nqrduck_measurement/signalprocessing_options.py b/src/nqrduck_measurement/signalprocessing_options.py index eac33fd..91d8f20 100644 --- a/src/nqrduck_measurement/signalprocessing_options.py +++ b/src/nqrduck_measurement/signalprocessing_options.py @@ -2,9 +2,14 @@ import logging import sympy -from nqrduck_spectrometer.measurement import Measurement +from nqrduck_spectrometer.measurement import Measurement, Fit, T2StarFit from nqrduck.helpers.functions import Function, GaussianFunction, CustomFunction -from nqrduck.helpers.formbuilder import DuckFormBuilder, DuckFormFunctionSelectionField +from nqrduck.helpers.formbuilder import ( + DuckFormBuilder, + DuckFormFunctionSelectionField, + DuckFormDropdownField, + DuckLabelField, +) logger = logging.getLogger(__name__) @@ -24,6 +29,21 @@ class FIDFunction(Function): self.add_parameter(Function.Parameter("T2star (microseconds)", "T2star", 10)) +class LorentzianFunction(Function): + """The Lorentzian function.""" + + name = "Lorentzian" + + def __init__(self) -> None: + """Lorentzian function.""" + expr = sympy.sympify("1 / (1 + (x / T2star)^2)") + super().__init__(expr) + self.start_x = 0 + self.end_x = 30 + + self.add_parameter(Function.Parameter("T2star (microseconds)", "T2star", 10)) + + class Apodization(DuckFormBuilder): """Apodization parameter. @@ -62,3 +82,37 @@ class Apodization(DuckFormBuilder): Function: The selected function. """ return self.get_values()[0] + + +class Fitting(DuckFormBuilder): + """Fitting parameter. + + This parameter is used to apply fitting functions to the signal. + The fitting functions are used to reduce the noise in the signal. + """ + + def __init__(self, measurement: Measurement, parent=None) -> None: + """Fitting parameter.""" + super().__init__("Fitting", parent=parent) + + self.measurement = measurement + + fits = {} + fits["T2*"] = T2StarFit(self.measurement) + + selection_field = DuckFormDropdownField( + text=None, + tooltip=None, + options=fits, + default_option=0, + ) + + self.add_field(selection_field) + + def get_fit(self) -> Fit: + """Get the selected fit. + + Returns: + Fit: The selected fit. + """ + return self.get_values()[0] diff --git a/src/nqrduck_measurement/view.py b/src/nqrduck_measurement/view.py index af344d9..b29e06b 100644 --- a/src/nqrduck_measurement/view.py +++ b/src/nqrduck_measurement/view.py @@ -89,6 +89,10 @@ class MeasurementView(ModuleView): self.module.controller.show_apodization_dialog ) + self._ui_form.fittingButton.clicked.connect( + self.module.controller.show_fitting_dialog + ) + # Add logos self._ui_form.buttonStart.setIcon(Logos.Play_16x16()) self._ui_form.buttonStart.setIconSize(self._ui_form.buttonStart.size()) @@ -120,7 +124,7 @@ class MeasurementView(ModuleView): self._ui_form.averagesEdit.set_min_value(1) self._ui_form.averagesEdit.set_max_value(1e6) - # Connect selectionBox signal fors switching the displayed measurement + # Connect selectionBox signal for switching the displayed measurement self._ui_form.selectionBox.valueChanged.connect( self.module.controller.change_displayed_measurement ) @@ -207,6 +211,9 @@ class MeasurementView(ModuleView): x, np.abs(y), label="Magnitude", color="blue" ) + # Plot fits + self.plot_fits() + # Add legend self._ui_form.plotter.canvas.ax.legend() @@ -230,10 +237,42 @@ class MeasurementView(ModuleView): ) break - except AttributeError: - logger.debug("No measurement data to display.") + except AttributeError as e: + logger.debug(f"No measurement data to display: {e}") + self._ui_form.plotter.canvas.draw() + def plot_fits(self): + """Plots the according fits to the displayed measurement if there are any and if the view mode is correct.""" + measurement = self.module.model.displayed_measurement + + if not measurement.fits: + logger.debug("No fits to plot.") + return + + for fit in measurement.fits: + logger.debug(f"Plotting fit {fit.name}.") + if fit.domain == self.module.model.view_mode: + x = fit.x + y = fit.y + self._ui_form.plotter.canvas.ax.plot(x, y, label=f"{fit.name} Fit", color="black", linestyle="--") + + # Add the parameters to the plot + offset = 0 + for name, value in fit.parameters.items(): + if name == "covariance": + continue + + # Only two digits after the comma + value = round(value, 2) + + self._ui_form.plotter.canvas.ax.text( + max(x) / 90, + max(y)/2 + offset, + f"{name}: {value}", + ) + offset += max(y)/10 + @pyqtSlot() def on_measurement_start_button_clicked(self) -> None: """Slot for when the measurement start button is clicked."""