Fixed another bug with saving and loading of pulse sequences. Added constraints to numerical parameters.

This commit is contained in:
jupfi 2024-04-26 13:29:02 +02:00
parent f593c82ec5
commit 35e05cb25c

View file

@ -36,6 +36,12 @@ class Option:
value: The value of the option. value: The value of the option.
""" """
subclasses = []
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
cls.subclasses.append(cls)
def __init__(self, name: str, value) -> None: def __init__(self, name: str, value) -> None:
"""Initializes the option.""" """Initializes the option."""
self.name = name self.name = name
@ -54,7 +60,7 @@ class Option:
Returns: Returns:
dict: The json representation of the option. dict: The json representation of the option.
""" """
return {"name": self.name, "value": self.value, "type": self.TYPE} return {"name": self.name, "value": self.value, "class": self.__class__.__name__}
@classmethod @classmethod
def from_json(cls, data) -> Option: def from_json(cls, data) -> Option:
@ -66,8 +72,9 @@ class Option:
Returns: Returns:
Option: The option. Option: The option.
""" """
for subclass in cls.__subclasses__(): for subclass in cls.subclasses:
if subclass.TYPE == data["type"]: logger.debug(f"Keys data: {data.keys()}")
if subclass.__name__ == data["class"]:
cls = subclass cls = subclass
break break
@ -95,9 +102,24 @@ class NumericOption(Option):
TYPE = "Numeric" TYPE = "Numeric"
def __init__(
self, name: str, value, is_float=True, min_value=None, max_value=None
) -> None:
super().__init__(name, value)
self.is_float = is_float
self.min_value = min_value
self.max_value = max_value
def set_value(self, value): def set_value(self, value):
"""Sets the value of the option.""" """Sets the value of the option."""
self.value = float(value) if value < self.min_value:
self.value = self.min_value
elif value >= self.max_value:
self.value = self.max_value
else:
raise ValueError(
f"Value {value} is not in the range of {self.min_value} to {self.max_value}. This should have been cought earlier."
)
class FunctionOption(Option): class FunctionOption(Option):
@ -149,7 +171,7 @@ class FunctionOption(Option):
Returns: Returns:
dict: The json representation of the option. dict: The json representation of the option.
""" """
return {"name": self.name, "value": self.value.to_json(), "type": self.TYPE} return {"name": self.name, "value": self.value.to_json(), "class": self.__class__.__name__, "functions" : [function.to_json() for function in self.functions]}
@classmethod @classmethod
def from_json(cls, data): def from_json(cls, data):
@ -161,7 +183,9 @@ class FunctionOption(Option):
Returns: Returns:
FunctionOption: The FunctionOption. FunctionOption: The FunctionOption.
""" """
functions = [function() for function in Function.__subclasses__()] logger.debug(f"Data: {data}")
# These are all available functions
functions = [Function.from_json(function) for function in data["functions"]]
obj = cls(data["name"], functions) obj = cls(data["name"], functions)
obj.value = Function.from_json(data["value"]) obj.value = Function.from_json(data["value"])
return obj return obj
@ -186,11 +210,13 @@ class TXRectFunction(RectFunction):
"""Returns the pixmaps of the function.""" """Returns the pixmaps of the function."""
return PulseParamters.TXRect() return PulseParamters.TXRect()
class TXSincFunction(SincFunction): class TXSincFunction(SincFunction):
"""TX Sinc function. """TX Sinc function.
Adds the pixmap of the function to the class. Adds the pixmap of the function to the class.
""" """
def __init__(self) -> None: def __init__(self) -> None:
"""Initializes the TX Sinc function.""" """Initializes the TX Sinc function."""
super().__init__() super().__init__()
@ -206,6 +232,7 @@ class TXGaussianFunction(GaussianFunction):
Adds the pixmap of the function to the class. Adds the pixmap of the function to the class.
""" """
def __init__(self) -> None: def __init__(self) -> None:
"""Initializes the TX Gaussian function.""" """Initializes the TX Gaussian function."""
super().__init__() super().__init__()
@ -221,6 +248,7 @@ class TXCustomFunction(CustomFunction):
Adds the pixmap of the function to the class. Adds the pixmap of the function to the class.
""" """
def __init__(self) -> None: def __init__(self) -> None:
"""Initializes the TX Custom function.""" """Initializes the TX Custom function."""
super().__init__() super().__init__()
@ -236,24 +264,23 @@ class TXPulse(BaseSpectrometerModel.PulseParameter):
Args: Args:
name (str): The name of the pulse parameter. name (str): The name of the pulse parameter.
Attributes:
RELATIVE_AMPLITUDE (str): The relative amplitude of the pulse.
TX_PHASE (str): The phase of the pulse.
TX_PULSE_SHAPE (str): The pulse shape.
""" """
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"
def __init__(self, name) -> None: def __init__(self, name: str) -> None:
"""Initializes the TX Pulse Parameter. """Initializes the TX Pulse Parameter.
It adds the options for the relative amplitude, the phase and the pulse shape. It adds the options for the relative amplitude, the phase and the pulse shape.
""" """
super().__init__(name) super().__init__(name)
self.add_option(NumericOption(self.RELATIVE_AMPLITUDE, 0)) self.add_option(
NumericOption(
self.RELATIVE_AMPLITUDE, 0, is_float=False, min_value=0, max_value=100
)
)
self.add_option(NumericOption(self.TX_PHASE, 0)) self.add_option(NumericOption(self.TX_PHASE, 0))
self.add_option( self.add_option(
FunctionOption( FunctionOption(