Merge pull request #15 from nqrduck/formbuilder-and-function-optimization

Formbuilder and function optimization
This commit is contained in:
Julia P 2024-04-26 17:55:15 +02:00 committed by GitHub
commit 822264bc79
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 47 additions and 62 deletions

View file

@ -1,5 +1,8 @@
# Changelog
### Version 0.0.3 (26-04-2024)
- Switched to new formbuilder. This should make implementation of signal processing methods more robust and easier.
### Version 0.0.2 (17-04-2024)
- Deployment to PyPi via github actions

View file

@ -7,7 +7,7 @@ allow-direct-references = true
[project]
name = "nqrduck-measurement"
version = "0.0.2"
version = "0.0.3"
authors = [
{ name="jupfi", email="support@nqrduck.cool" },
]

View file

@ -6,7 +6,6 @@ import numpy as np
from decimal import Decimal
from PyQt6.QtCore import pyqtSlot, pyqtSignal
from PyQt6.QtWidgets import QApplication
from nqrduck_pulseprogrammer.view import OptionsDialog
from nqrduck_spectrometer.pulsesequence import PulseSequence
from .signalprocessing_options import Apodization
from nqrduck.module.module_controller import ModuleController
@ -211,48 +210,22 @@ class MeasurementController(ModuleController):
)
return
# We need to create a event which corresponds to the measurement.
event_duration = self.module.model.displayed_measurement.tdx[-1] * 1e-6
measurement = self.module.model.displayed_measurement
event = PulseSequence.Event(name="Apodization", duration=str(event_duration))
parameter = Apodization()
parameter.start_x = 0
parameter.end_x = event_duration
dialog = OptionsDialog(event, parameter, self.module.view)
dialog = Apodization(measurement, parent=self.module.view)
result = dialog.exec()
if result:
for option, function in dialog.return_functions.items():
logger.debug(
"Setting option %s of parameter %s in event %s to %s",
option,
parameter,
event,
function(),
)
option.set_value(function())
logger.debug("Dialog result: %s", result)
if not result:
return
function = dialog.get_function()
# Get the function from the Apodization function
function = parameter.get_option_by_name(Apodization.APODIZATION_FUNCTIONS).value
logger.debug("Apodization function: %s", function)
# Get the y data weights from the function
resolution = (
self.module.model.displayed_measurement.tdx[1]
- self.module.model.displayed_measurement.tdx[0]
) * 1e-6
y_weight = function.get_pulse_amplitude(event.duration, Decimal(resolution))
# Append the last point to the end of the array
y_weight = np.append(y_weight, y_weight[-1])
apodized_measurement = measurement.apodization(function)
tdy_measurement = self.module.model.displayed_measurement.tdy * y_weight
dialog.deleteLater()
measurement = Measurement(
self.module.model.displayed_measurement.tdx,
tdy_measurement,
target_frequency=self.module.model.displayed_measurement.target_frequency,
IF_frequency=self.module.model.displayed_measurement.IF_frequency,
)
self.module.model.displayed_measurement = measurement
self.module.model.add_measurement(measurement)
self.module.model.displayed_measurement = apodized_measurement
self.module.model.add_measurement(apodized_measurement)

View file

@ -1,16 +1,13 @@
"""Signal processing options."""
import logging
from decimal import Decimal
import numpy as np
import sympy
from nqrduck_spectrometer.base_spectrometer_model import BaseSpectrometerModel
from nqrduck_spectrometer.pulseparameters import (
FunctionOption,
GaussianFunction,
CustomFunction,
Function,
)
# We implement the signal processing options as PulseParamterOptions because we can then easily use the automatic UI generation
from nqrduck_spectrometer.measurement import Measurement
from nqrduck.helpers.functions import Function, GaussianFunction, CustomFunction
from nqrduck.helpers.formbuilder import DuckFormBuilder, DuckFormFunctionSelectionField
logger = logging.getLogger(__name__)
class FIDFunction(Function):
"""The exponetial FID function."""
@ -27,25 +24,37 @@ class FIDFunction(Function):
self.add_parameter(Function.Parameter("T2star (microseconds)", "T2star", 10))
class Apodization(BaseSpectrometerModel.PulseParameter):
class Apodization(DuckFormBuilder):
"""Apodization parameter.
This parameter is used to apply apodization functions to the signal.
The apodization functions are used to reduce the noise in the signal.
Attributes:
APODIZATION_FUNCTIONS (str): The name of the apodization functions option.
"""
APODIZATION_FUNCTIONS = "Apodization functions"
def __init__(self):
def __init__(self, measurement: Measurement, parent=None) -> None:
"""Apodization parameter."""
super().__init__("Apodization")
super().__init__("Apodization", parent=parent)
self.add_option(
FunctionOption(
self.APODIZATION_FUNCTIONS,
[FIDFunction(), GaussianFunction(), CustomFunction()],
),
self.measurement = measurement
functions = [
FIDFunction(),
GaussianFunction(),
CustomFunction(),
]
self.duration = (self.measurement.tdx[-1] - self.measurement.tdx[0]) * 1e-6
function_selection_field = DuckFormFunctionSelectionField(
text=None, tooltip=None, functions=functions, duration=self.duration, parent=parent, default_function=0
)
self.add_field(function_selection_field)
def get_function(self) -> Function:
"""Get the selected function.
Returns:
Function: The selected function.
"""
return self.get_values()[0]