Added methods for event creation and deletion.

This commit is contained in:
jupfi 2024-05-28 16:48:20 +02:00
parent bd40004dd3
commit b77ba30085
2 changed files with 56 additions and 4 deletions

View file

@ -8,6 +8,7 @@ from __future__ import annotations
import logging import logging
from numpy.core.multiarray import array as array from numpy.core.multiarray import array as array
from PyQt6.QtCore import pyqtSignal
from nqrduck.assets.icons import PulseParamters from nqrduck.assets.icons import PulseParamters
from nqrduck.helpers.functions import ( from nqrduck.helpers.functions import (
@ -37,6 +38,7 @@ class Option:
""" """
subclasses = [] subclasses = []
value_changed = pyqtSignal()
def __init_subclass__(cls, **kwargs): def __init_subclass__(cls, **kwargs):
"""Adds the subclass to the list of subclasses.""" """Adds the subclass to the list of subclasses."""
@ -98,34 +100,45 @@ class BooleanOption(Option):
def set_value(self, value): def set_value(self, value):
"""Sets the value of the option.""" """Sets the value of the option."""
self.value = value self.value = value
self.value_changed.emit()
class NumericOption(Option): class NumericOption(Option):
"""Defines a numeric option for a pulse parameter option.""" """Defines a numeric option for a pulse parameter option."""
def __init__( def __init__(
self, name: str, value, is_float=True, min_value=None, max_value=None self,
name: str,
value,
is_float=True,
min_value=None,
max_value=None,
slider=False,
) -> None: ) -> None:
"""Initializes the NumericOption. """Initializes the NumericOption.
Args: Args:
name (str): The name of the option. name (str): The name of the option.
value: The value of the option. value: The value of the option.
is_float (bool): If the value is a float. is_float (bool): If the value is a float.
min_value: The minimum value of the option. min_value: The minimum value of the option.
max_value: The maximum value of the option. max_value: The maximum value of the option.
slider (bool): If the option should be displayed as a slider.
""" """
super().__init__(name, value) super().__init__(name, value)
self.is_float = is_float self.is_float = is_float
self.min_value = min_value self.min_value = min_value
self.max_value = max_value self.max_value = max_value
self.slider = slider
def set_value(self, value): def set_value(self, value):
"""Sets the value of the option.""" """Sets the value of the option."""
if value < self.min_value: if value < self.min_value:
self.value = self.min_value self.value = self.min_value
self.value_changed.emit()
elif value >= self.max_value: elif value >= self.max_value:
self.value = self.max_value self.value = self.max_value
self.value_changed.emit()
else: else:
raise ValueError( raise ValueError(
f"Value {value} is not in the range of {self.min_value} to {self.max_value}. This should have been cought earlier." f"Value {value} is not in the range of {self.min_value} to {self.max_value}. This should have been cought earlier."
@ -145,7 +158,7 @@ class NumericOption(Option):
"min_value": self.min_value, "min_value": self.min_value,
"max_value": self.max_value, "max_value": self.max_value,
} }
@classmethod @classmethod
def from_json(cls, data): def from_json(cls, data):
"""Creates a NumericOption from a json representation. """Creates a NumericOption from a json representation.
@ -192,6 +205,7 @@ class FunctionOption(Option):
value: The value of the option. value: The value of the option.
""" """
self.value = value self.value = value
self.value_changed.emit()
def get_function_by_name(self, name): def get_function_by_name(self, name):
"""Returns the function with the given name. """Returns the function with the given name.
@ -316,6 +330,8 @@ class TXPulse(BaseSpectrometerModel.PulseParameter):
RELATIVE_AMPLITUDE = "Relative TX Amplitude (%)" RELATIVE_AMPLITUDE = "Relative TX Amplitude (%)"
TX_PHASE = "TX Phase" TX_PHASE = "TX Phase"
TX_PULSE_SHAPE = "TX Pulse Shape" TX_PULSE_SHAPE = "TX Pulse Shape"
N_PHASE_CYCLES = "Number of Phase Cycles"
PHASE_CYCLE_LEVEL = "Phase Cycle Level"
def __init__(self, name: str) -> None: def __init__(self, name: str) -> None:
"""Initializes the TX Pulse Parameter. """Initializes the TX Pulse Parameter.
@ -325,10 +341,25 @@ class TXPulse(BaseSpectrometerModel.PulseParameter):
super().__init__(name) super().__init__(name)
self.add_option( self.add_option(
NumericOption( NumericOption(
self.RELATIVE_AMPLITUDE, 0, is_float=False, min_value=0, max_value=100 self.RELATIVE_AMPLITUDE,
0,
is_float=False,
min_value=0,
max_value=100,
slider=True,
) )
) )
self.add_option(NumericOption(self.TX_PHASE, 0)) self.add_option(NumericOption(self.TX_PHASE, 0))
self.add_option(
NumericOption(
self.N_PHASE_CYCLES, 1, is_float=False, min_value=1, max_value=360
)
)
self.add_option(
NumericOption(
self.PHASE_CYCLE_LEVEL, 0, is_float=False, min_value=0, max_value=10
)
)
self.add_option( self.add_option(
FunctionOption( FunctionOption(
self.TX_PULSE_SHAPE, self.TX_PULSE_SHAPE,

View file

@ -37,6 +37,27 @@ class PulseSequence:
list: The names of the events list: The names of the events
""" """
return [event.name for event in self.events] return [event.name for event in self.events]
def add_event(self, event_name: str, duration: float) -> None:
"""Add a new event to the pulse sequence.
Args:
event_name (str): The name of the event
duration (float): The duration of the event
"""
self.events.append(self.Event(event_name, f"{float(duration):.16g}u"))
def delete_event(self, event_name: str) -> None:
"""Deletes an event from the pulse sequence.
Args:
event_name (str): The name of the event to delete
"""
for event in self.events:
if event.name == event_name:
self.events.remove(event)
break
class Event: class Event:
"""An event is a part of a pulse sequence. It has a name and a duration and different parameters that have to be set. """An event is a part of a pulse sequence. It has a name and a duration and different parameters that have to be set.