mirror of
https://github.com/nqrduck/quackseq.git
synced 2024-12-18 10:10:25 +00:00
Improved setting structure.
This commit is contained in:
parent
a265cb7d65
commit
0ec31e541d
2 changed files with 82 additions and 29 deletions
|
@ -2,10 +2,36 @@
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
from typing import Any
|
||||||
from quackseq.spectrometer.spectrometer_settings import Setting
|
from quackseq.spectrometer.spectrometer_settings import Setting
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
class QuackSettings(OrderedDict):
|
||||||
|
def __getattr__(self, key):
|
||||||
|
return self[key].value
|
||||||
|
|
||||||
|
def __setattr__(self, key, value):
|
||||||
|
self[key].value = value
|
||||||
|
|
||||||
|
@property
|
||||||
|
def categories(self):
|
||||||
|
categories = []
|
||||||
|
|
||||||
|
for setting in self.values():
|
||||||
|
if not setting.category in categories:
|
||||||
|
categories.append(setting.category)
|
||||||
|
|
||||||
|
return categories
|
||||||
|
|
||||||
|
def get_settings_by_category(self, category):
|
||||||
|
settings = []
|
||||||
|
|
||||||
|
for setting in self.values():
|
||||||
|
if setting.category == category:
|
||||||
|
settings.append(setting)
|
||||||
|
|
||||||
|
return settings
|
||||||
|
|
||||||
class SpectrometerModel():
|
class SpectrometerModel():
|
||||||
"""The base class for all spectrometer models.
|
"""The base class for all spectrometer models.
|
||||||
|
@ -13,31 +39,30 @@ class SpectrometerModel():
|
||||||
It contains the settings and pulse parameters of the spectrometer.
|
It contains the settings and pulse parameters of the spectrometer.
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
settings (OrderedDict) : The settings of the spectrometer
|
settings (QuackSettings) : The settings of the spectrometer
|
||||||
"""
|
"""
|
||||||
|
|
||||||
settings: OrderedDict
|
settings: QuackSettings
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""Initializes the spectrometer model."""
|
"""Initializes the spectrometer model."""
|
||||||
self.settings = OrderedDict()
|
self.settings = QuackSettings()
|
||||||
|
|
||||||
def add_setting(self, setting: Setting, category: str) -> None:
|
|
||||||
|
def add_setting(self,name: str, setting: Setting) -> None:
|
||||||
"""Adds a setting to the spectrometer.
|
"""Adds a setting to the spectrometer.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
name (str) : The name of the setting as it should be used in the code
|
||||||
setting (Setting) : The setting to add
|
setting (Setting) : The setting to add
|
||||||
category (str) : The category of the setting
|
|
||||||
"""
|
"""
|
||||||
if category not in self.settings.keys():
|
self.settings[name] = setting
|
||||||
self.settings[category] = []
|
|
||||||
self.settings[category].append(setting)
|
|
||||||
|
|
||||||
def get_setting_by_name(self, name: str) -> Setting:
|
def get_setting_by_name(self, name: str) -> Setting:
|
||||||
"""Gets a setting by its name.
|
"""Gets a setting by its name.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
name (str) : The name of the setting
|
name (str) : The name of the setting as it is used in the code
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Setting : The setting with the specified name
|
Setting : The setting with the specified name
|
||||||
|
@ -45,11 +70,28 @@ class SpectrometerModel():
|
||||||
Raises:
|
Raises:
|
||||||
ValueError : If no setting with the specified name is found
|
ValueError : If no setting with the specified name is found
|
||||||
"""
|
"""
|
||||||
for category in self.settings.keys():
|
if name in self.settings:
|
||||||
for setting in self.settings[category]:
|
return self.settings[name]
|
||||||
if setting.name == name:
|
|
||||||
return setting
|
raise ValueError(f"No setting with name {name} found")
|
||||||
raise ValueError(f"Setting with name {name} not found")
|
|
||||||
|
def get_setting_by_display_name(self, display_name : str) -> Setting:
|
||||||
|
"""Gets a setting by its display name.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
display_name (str) : The display name of the setting
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Setting : The setting with the specified display name
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError : If no setting with the specified display name is found
|
||||||
|
"""
|
||||||
|
for setting in self.settings.values():
|
||||||
|
if setting.name == display_name:
|
||||||
|
return setting
|
||||||
|
|
||||||
|
raise ValueError(f"No setting with display name {display_name} found")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def target_frequency(self):
|
def target_frequency(self):
|
||||||
|
|
|
@ -11,7 +11,8 @@ class Setting():
|
||||||
E.g. the Transmit gain or the number of points in a spectrum.
|
E.g. the Transmit gain or the number of points in a spectrum.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
name (str) : The name of the setting
|
name (str) : The name of the setting as it is used in the code
|
||||||
|
category (str) : The category of the setting
|
||||||
description (str) : A description of the setting
|
description (str) : A description of the setting
|
||||||
default : The default value of the setting
|
default : The default value of the setting
|
||||||
|
|
||||||
|
@ -19,9 +20,10 @@ class Setting():
|
||||||
name (str) : The name of the setting
|
name (str) : The name of the setting
|
||||||
description (str) : A description of the setting
|
description (str) : A description of the setting
|
||||||
value : The value of the setting
|
value : The value of the setting
|
||||||
|
category (str) : The category of the setting
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, name: str, description: str = None, default=None) -> None:
|
def __init__(self, name: str, category : str, description: str = None, default=None) -> None:
|
||||||
"""Create a new setting.
|
"""Create a new setting.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
@ -29,9 +31,9 @@ class Setting():
|
||||||
description (str): A description of the setting.
|
description (str): A description of the setting.
|
||||||
default: The default value of the setting.
|
default: The default value of the setting.
|
||||||
"""
|
"""
|
||||||
self.widget = None
|
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.name = name
|
self.name = name
|
||||||
|
self.category = category
|
||||||
self.description = description
|
self.description = description
|
||||||
if default is not None:
|
if default is not None:
|
||||||
self.value = default
|
self.value = default
|
||||||
|
@ -46,11 +48,12 @@ class NumericalSetting(Setting):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, name: str, description: str, default, min_value=None, max_value=None
|
self, name: str, category: str, description: str, default, min_value=None, max_value=None
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Create a new numerical setting."""
|
"""Create a new numerical setting."""
|
||||||
super().__init__(
|
super().__init__(
|
||||||
name,
|
name,
|
||||||
|
category,
|
||||||
self.description_limit_info(description, min_value, max_value),
|
self.description_limit_info(description, min_value, max_value),
|
||||||
default,
|
default,
|
||||||
)
|
)
|
||||||
|
@ -81,6 +84,7 @@ class FloatSetting(NumericalSetting):
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
name (str) : The name of the setting
|
name (str) : The name of the setting
|
||||||
|
category (str) : The category of the setting
|
||||||
default : The default value of the setting
|
default : The default value of the setting
|
||||||
description (str) : A description of the setting
|
description (str) : A description of the setting
|
||||||
min_value : The minimum value of the setting
|
min_value : The minimum value of the setting
|
||||||
|
@ -92,14 +96,14 @@ class FloatSetting(NumericalSetting):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
name: str,
|
name: str,
|
||||||
|
category: str,
|
||||||
default: float,
|
default: float,
|
||||||
description: str,
|
description: str,
|
||||||
min_value: float = None,
|
min_value: float = None,
|
||||||
max_value: float = None,
|
max_value: float = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Create a new float setting."""
|
"""Create a new float setting."""
|
||||||
super().__init__(name, description, default, min_value, max_value)
|
super().__init__(name, category, description, default, min_value, max_value)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def value(self):
|
def value(self):
|
||||||
"""The value of the setting. In this case, a float."""
|
"""The value of the setting. In this case, a float."""
|
||||||
|
@ -116,6 +120,7 @@ class IntSetting(NumericalSetting):
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
name (str) : The name of the setting
|
name (str) : The name of the setting
|
||||||
|
category (str) : The category of the setting
|
||||||
default : The default value of the setting
|
default : The default value of the setting
|
||||||
description (str) : A description of the setting
|
description (str) : A description of the setting
|
||||||
min_value : The minimum value of the setting
|
min_value : The minimum value of the setting
|
||||||
|
@ -125,13 +130,15 @@ class IntSetting(NumericalSetting):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
name: str,
|
name: str,
|
||||||
|
category: str,
|
||||||
default: int,
|
default: int,
|
||||||
description: str,
|
description: str,
|
||||||
min_value=None,
|
min_value=None,
|
||||||
max_value=None,
|
max_value=None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Create a new int setting."""
|
"""Create a new int setting."""
|
||||||
super().__init__(name, description, default, min_value, max_value)
|
super().__init__(name, category, description, default, min_value, max_value)
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def value(self):
|
def value(self):
|
||||||
|
@ -142,6 +149,7 @@ class IntSetting(NumericalSetting):
|
||||||
def value(self, value):
|
def value(self, value):
|
||||||
logger.debug(f"Setting {self.name} to {value}")
|
logger.debug(f"Setting {self.name} to {value}")
|
||||||
value = int(float(value))
|
value = int(float(value))
|
||||||
|
|
||||||
self._value = value
|
self._value = value
|
||||||
|
|
||||||
|
|
||||||
|
@ -150,13 +158,14 @@ class BooleanSetting(Setting):
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
name (str) : The name of the setting
|
name (str) : The name of the setting
|
||||||
|
category (str) : The category of the setting
|
||||||
default : The default value of the setting
|
default : The default value of the setting
|
||||||
description (str) : A description of the setting
|
description (str) : A description of the setting
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, name: str, default: bool, description: str) -> None:
|
def __init__(self, name: str, category : str, default: bool, description: str) -> None:
|
||||||
"""Create a new boolean setting."""
|
"""Create a new boolean setting."""
|
||||||
super().__init__(name, description, default)
|
super().__init__(name, category, description, default)
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -178,16 +187,17 @@ class SelectionSetting(Setting):
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
name (str) : The name of the setting
|
name (str) : The name of the setting
|
||||||
|
category (str) : The category of the setting
|
||||||
options (list) : A list of options to choose from
|
options (list) : A list of options to choose from
|
||||||
default : The default value of the setting
|
default : The default value of the setting
|
||||||
description (str) : A description of the setting
|
description (str) : A description of the setting
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, name: str, options: list, default: str, description: str
|
self, name: str, category : str, options: list, default: str, description: str
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Create a new selection setting."""
|
"""Create a new selection setting."""
|
||||||
super().__init__(name, description, default)
|
super().__init__(name, category, description, default)
|
||||||
# Check if default is in options
|
# Check if default is in options
|
||||||
if default not in options:
|
if default not in options:
|
||||||
raise ValueError("Default value must be one of the options")
|
raise ValueError("Default value must be one of the options")
|
||||||
|
@ -206,7 +216,7 @@ class SelectionSetting(Setting):
|
||||||
if value in self.options:
|
if value in self.options:
|
||||||
self._value = value
|
self._value = value
|
||||||
else:
|
else:
|
||||||
raise ValueError("Value must be one of the options")
|
raise ValueError(f"Value must be one of the options {self.options}")
|
||||||
# This fixes a bug when creating the widget when the options are not yet set
|
# This fixes a bug when creating the widget when the options are not yet set
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
self._value = value
|
self._value = value
|
||||||
|
@ -219,13 +229,14 @@ class StringSetting(Setting):
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
name (str) : The name of the setting
|
name (str) : The name of the setting
|
||||||
|
category (str) : The category of the setting
|
||||||
default : The default value of the setting
|
default : The default value of the setting
|
||||||
description (str) : A description of the setting
|
description (str) : A description of the setting
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, name: str, default: str, description: str) -> None:
|
def __init__(self, name: str, category :str, default: str, description: str) -> None:
|
||||||
"""Create a new string setting."""
|
"""Create a new string setting."""
|
||||||
super().__init__(name, description, default)
|
super().__init__(name, category, description, default)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def value(self):
|
def value(self):
|
||||||
|
|
Loading…
Reference in a new issue