Skip to content

Commit

Permalink
default settings set as class attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
RobBuchananCompPhys committed Feb 14, 2025
1 parent 8e17b43 commit e675289
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ def filter_default_attributes(cls, filter=_filter):
The filter settings dictionary
"""
filter.set_defaults()

return {setting: values["value"] for setting, values in filter.default_settings.items()}

_settings = filter_default_attributes.__func__(object())
Expand Down
276 changes: 121 additions & 155 deletions MDANSE/Src/MDANSE/Mathematics/Signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,6 @@ class FrequencyRangeMethod(Enum):

@abstractmethod
def __init__(self, **kwargs):
if not hasattr(self, "default_settings"):
self.__class__.set_defaults()

# Number of simulation steps
self.n_steps = kwargs.pop("n_steps")
# Simulation sample frequency in pHz
Expand Down Expand Up @@ -642,21 +639,18 @@ def energy_to_freq(cls, energy):
class Butterworth(Filter):
"""Interface for the butterworth filter."""

@classmethod
def set_defaults(cls) -> None:
"""Set up the default filter settings."""
cls.default_settings = {
"order": {"description": "The order of the filter", "value": 1},
"attenuation_type": {
"description": "Filter attenuation type",
"values": {"lowpass", "highpass", "bandpass", "bandstop"},
"value": "lowpass",
},
"cutoff_freq": {
"description": "Cutoff frequency/vibrational energy (may be a 2-length array if bandpass/stop)",
"value": DEFAULT_FILTER_CUTOFF,
},
}
default_settings = {
"order": {"description": "The order of the filter", "value": 1},
"attenuation_type": {
"description": "Filter attenuation type",
"values": {"lowpass", "highpass", "bandpass", "bandstop"},
"value": "lowpass",
},
"cutoff_freq": {
"description": "Cutoff frequency/vibrational energy (may be a 2-length array if bandpass/stop)",
"value": DEFAULT_FILTER_CUTOFF,
},
}

def __init__(self, **kwargs):
super().__init__(**kwargs)
Expand All @@ -675,26 +669,22 @@ def __init__(self, **kwargs):

class ChebyshevTypeI(Filter):
""" """

@classmethod
def set_defaults(cls) -> None:
"""Set up the default filter settings."""
cls.default_settings = {
"order": {"description": "The order of the filter", "value": 1},
"max_ripple": {
"description": "Decibel measure of maximum ripple allowed below unit gain in the passband",
"value": 5.0,
},
"attenuation_type": {
"description": "Filter attenuation type",
"values": {"lowpass", "highpass", "bandpass", "bandstop"},
"value": "lowpass",
},
"cutoff_freq": {
"description": "Cutoff frequency/vibrational energy (may be a 2-length array if bandpass/stop)",
"value": DEFAULT_FILTER_CUTOFF,
},
}
default_settings = {
"order": {"description": "The order of the filter", "value": 1},
"max_ripple": {
"description": "Decibel measure of maximum ripple allowed below unit gain in the passband",
"value": 5.0,
},
"attenuation_type": {
"description": "Filter attenuation type",
"values": {"lowpass", "highpass", "bandpass", "bandstop"},
"value": "lowpass",
},
"cutoff_freq": {
"description": "Cutoff frequency/vibrational energy (may be a 2-length array if bandpass/stop)",
"value": DEFAULT_FILTER_CUTOFF,
},
}

def __init__(self, **kwargs):
super().__init__(**kwargs)
Expand All @@ -714,26 +704,22 @@ def __init__(self, **kwargs):

class ChebyshevTypeII(Filter):
""" """

@classmethod
def set_defaults(cls) -> None:
"""Set up the default filter settings."""
cls.default_settings = {
"order": {"description": "The order of the filter", "value": 1},
"min_attenuation": {
"description": "Decibel measure of minimum attenuation required in the stopband",
"value": 20.0,
},
"attenuation_type": {
"description": "Filter attenuation type",
"values": {"lowpass", "highpass", "bandpass", "bandstop"},
"value": "lowpass",
},
"cutoff_freq": {
"description": "Cutoff frequency/vibrational energy (may be a 2-length array if bandpass/stop)",
"value": DEFAULT_FILTER_CUTOFF,
},
}
default_settings = {
"order": {"description": "The order of the filter", "value": 1},
"min_attenuation": {
"description": "Decibel measure of minimum attenuation required in the stopband",
"value": 20.0,
},
"attenuation_type": {
"description": "Filter attenuation type",
"values": {"lowpass", "highpass", "bandpass", "bandstop"},
"value": "lowpass",
},
"cutoff_freq": {
"description": "Cutoff frequency/vibrational energy (may be a 2-length array if bandpass/stop)",
"value": DEFAULT_FILTER_CUTOFF,
},
}

def __init__(self, **kwargs):
super().__init__(**kwargs)
Expand All @@ -753,30 +739,26 @@ def __init__(self, **kwargs):

class Elliptical(Filter):
""" """

@classmethod
def set_defaults(cls) -> None:
"""Set up the default filter settings."""
cls.default_settings = {
"order": {"description": "The order of the filter", "value": 1},
"max_ripple": {
"description": "Decibel measure of maximum ripple allowed below unit gain in the passband",
"value": 5.0,
},
"min_attenuation": {
"description": "Decibel measure of minimum attenuation required in the stopband",
"value": 20.0,
},
"attenuation_type": {
"description": "Filter attenuation type",
"values": {"lowpass", "highpass", "bandpass", "bandstop"},
"value": "lowpass",
},
"cutoff_freq": {
"description": "Cutoff frequency/vibrational energy (may be a 2-length array if bandpass/stop)",
"value": DEFAULT_FILTER_CUTOFF,
},
}
default_settings = {
"order": {"description": "The order of the filter", "value": 1},
"max_ripple": {
"description": "Decibel measure of maximum ripple allowed below unit gain in the passband",
"value": 5.0,
},
"min_attenuation": {
"description": "Decibel measure of minimum attenuation required in the stopband",
"value": 20.0,
},
"attenuation_type": {
"description": "Filter attenuation type",
"values": {"lowpass", "highpass", "bandpass", "bandstop"},
"value": "lowpass",
},
"cutoff_freq": {
"description": "Cutoff frequency/vibrational energy (may be a 2-length array if bandpass/stop)",
"value": DEFAULT_FILTER_CUTOFF,
},
}

def __init__(self, **kwargs):
super().__init__(**kwargs)
Expand All @@ -797,27 +779,23 @@ def __init__(self, **kwargs):

class Bessel(Filter):
""" """

@classmethod
def set_defaults(cls) -> None:
"""Set up the default filter settings."""
cls.default_settings = {
"order": {"description": "The order of the filter", "value": 1},
"norm": {
"description": "Filter normalization results in the following behaviour at cutoff - phase: phase response obtains midpoint - delay: group delay in passband is the reciprocal of cutoff - mag: gain magnitude is -3 dB",
"values": {"phase", "delay", "mag"},
"value": "phase",
},
"attenuation_type": {
"description": "Filter attenuation type",
"values": {"lowpass", "highpass", "bandpass", "bandstop"},
"value": "lowpass",
},
"cutoff_freq": {
"description": "Cutoff frequency/vibrational energy (may be a 2-length array if bandpass/stop)",
"value": DEFAULT_FILTER_CUTOFF,
},
}
default_settings = {
"order": {"description": "The order of the filter", "value": 1},
"norm": {
"description": "Filter normalization results in the following behaviour at cutoff - phase: phase response obtains midpoint - delay: group delay in passband is the reciprocal of cutoff - mag: gain magnitude is -3 dB",
"values": {"phase", "delay", "mag"},
"value": "phase",
},
"attenuation_type": {
"description": "Filter attenuation type",
"values": {"lowpass", "highpass", "bandpass", "bandstop"},
"value": "lowpass",
},
"cutoff_freq": {
"description": "Cutoff frequency/vibrational energy (may be a 2-length array if bandpass/stop)",
"value": DEFAULT_FILTER_CUTOFF,
},
}

def __init__(self, **kwargs):
super().__init__(**kwargs)
Expand All @@ -837,23 +815,19 @@ def __init__(self, **kwargs):

class Notch(Filter):
""" """
default_settings = {
"fundamental_freq": {
"description": "Spacing between filter peaks (value must evenly divide sample frequency)",
"value": DEFAULT_FILTER_CUTOFF,
},
"quality_factor": {
"description": "Specifies bandwidth, proportional to time taken for filter to decay by a factor of 1/e",
"value": 1.0,
},
}

digital_only = True

@classmethod
def set_defaults(cls) -> None:
"""Set up the default filter settings."""
cls.default_settings = {
"fundamental_freq": {
"description": "Spacing between filter peaks (value must evenly divide sample frequency)",
"value": DEFAULT_FILTER_CUTOFF,
},
"quality_factor": {
"description": "Specifies bandwidth, proportional to time taken for filter to decay by a factor of 1/e",
"value": 1.0,
},
}

def __init__(self, **kwargs):
super().__init__(**kwargs)

Expand All @@ -873,23 +847,19 @@ def compute_frequencies(

class Peak(Filter):
""" """
default_settings = {
"fundamental_freq": {
"description": "Spacing between filter peaks (value must evenly divide sample frequency)",
"value": DEFAULT_FILTER_CUTOFF,
},
"quality_factor": {
"description": "Specifies bandwidth, proportional to time taken for filter to decay by a factor of 1/e",
"value": 1.0,
},
}

digital_only = True

@classmethod
def set_defaults(cls) -> None:
"""Set up the default filter settings."""
cls.default_settings = {
"fundamental_freq": {
"description": "Spacing between filter peaks (value must evenly divide sample frequency)",
"value": DEFAULT_FILTER_CUTOFF,
},
"quality_factor": {
"description": "Specifies bandwidth, proportional to time taken for filter to decay by a factor of 1/e",
"value": 1.0,
},
}

def __init__(self, **kwargs):
super().__init__(**kwargs)

Expand All @@ -909,33 +879,29 @@ def compute_frequencies(

class Comb(Filter):
""" """
default_settings = {
"fundamental_freq": {
"description": "Spacing between filter peaks (value must evenly divide sample frequency)",
"value": DEFAULT_FILTER_CUTOFF,
},
"quality_factor": {
"description": "Specifies bandwidth, proportional to time taken for filter to decay by a factor of 1/e",
"value": 1.0,
},
"comb_type": {
"description": "Determines whether quality factor applies to notches or peaks",
"values": {"peak", "notch"},
"value": "notch",
},
"pass_zero": {
"description": "Determines whether notches or peaks centered on integer multiples of fundamental frequency",
"values": {True, False},
"value": False,
},
}

digital_only = True

@classmethod
def set_defaults(cls) -> None:
"""Set up the default filter settings."""
cls.default_settings = {
"fundamental_freq": {
"description": "Spacing between filter peaks (value must evenly divide sample frequency)",
"value": DEFAULT_FILTER_CUTOFF,
},
"quality_factor": {
"description": "Specifies bandwidth, proportional to time taken for filter to decay by a factor of 1/e",
"value": 1.0,
},
"comb_type": {
"description": "Determines whether quality factor applies to notches or peaks",
"values": {"peak", "notch"},
"value": "notch",
},
"pass_zero": {
"description": "Determines whether notches or peaks centered on integer multiples of fundamental frequency",
"values": {True, False},
"value": False,
},
}

def __init__(self, **kwargs):
super().__init__(**kwargs)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,6 @@ def make_settings_grid(self, filter: Filter, grid: QGridLayout) -> None:
filter : Filter
Selected filter class (one of [Butterworth, ChebyshevTypeI, ChebyshevTypeII, Elliptical, Bessel, Notch, Peak, Comb])
"""
filter.set_defaults()
setting_items = filter.default_settings.items()

# Add filter settings to grid layout
Expand Down

0 comments on commit e675289

Please sign in to comment.