Skip to content

Commit

Permalink
Merge pull request #247 from kujaku11/fix_issue_238_housekeeping
Browse files Browse the repository at this point in the history
remove anti_alias_filter accessors from FCDecimation and AuroraDecima…
  • Loading branch information
kkappler authored Jan 4, 2025
2 parents d64f892 + f9c136f commit e5099f6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,6 @@ def __init__(self, **kwargs):

super().__init__(attr_dict=attr_dict, **kwargs)

# if self.decimation.level == 0:
# self.anti_alias_filter = None

@property
def window(self) -> Window:
"""
Convenience access to STFT window metadata.
This was placed here to allow access to legacy Decimation's window attribute.
Note: This maybe deprecated in future to use only direct access via self.stft.window.
"""
return self.stft.window

@property
def anti_alias_filter(self) -> str:
"""
get anti_alais_filter from TimeSeriesDecimation.
"""
return self.decimation.anti_alias_filter

@property
def bands(self) -> list:
"""
Expand Down Expand Up @@ -224,7 +201,7 @@ def frequency_sample_interval(self) -> float:
Returns the delta_f in frequency domain df = 1 / (N * dt)
Here dt is the sample interval after decimation
"""
return self.sample_rate_decimation / self.window.num_samples
return self.decimation.sample_rate / self.stft.window.num_samples

@property
def band_edges(self) -> np.ndarray:
Expand All @@ -239,7 +216,7 @@ def band_edges(self) -> np.ndarray:
).T
return band_edges

def frequency_bands_obj(self) -> FrequencyBands: # TODO: FIXME circular import when correctly dtyped -> FrequencyBands:
def frequency_bands_obj(self) -> FrequencyBands:
"""
Gets a FrequencyBands object that is used as input to processing.
Expand All @@ -256,33 +233,6 @@ def frequency_bands_obj(self) -> FrequencyBands: # TODO: FIXME circular import
frequency_bands = FrequencyBands(band_edges=self.band_edges)
return frequency_bands

# # TODO: FIXME WIP
# def to_frequency_bands_obj(self):
# """
# Define band_edges array from decimation_level object,
#
# Development Notes.
# This function was originally in FrequencyBands class, it was called:
# from_decimation_object. Circular imports were encountered when it was correctly dtyped.
# There is no reason to have FrequencyBands.from_decimation_object(decimation_level)
# _and_ decimation_level.to_frequency_bands_obj()
# The function above already does the task of generating a frequency bands.
# Keeping this commented until documentation improves.
# Below looks like an alternative, and more readable way to get band_edges,
# without passing through the dataframe. At a minimum a test should be created
# that makes band edges both ways and asserts equal.
# (a few command line tests showed that they are, Dec 2024).
#
# """
# df = self.frequency_sample_interval
# half_df = df / 2.0
#
# lower_edges = (self.lower_bounds * df) - half_df
# upper_edges = (self.upper_bounds * df) + half_df
# band_edges = np.vstack((lower_edges, upper_edges)).T
# return FrequencyBands(band_edges=band_edges)


@property
def fft_frequencies(self) -> np.ndarray:
"""
Expand All @@ -291,18 +241,9 @@ def fft_frequencies(self) -> np.ndarray:
:return freqs: The frequencies at which the stft will be available.
:rtype freqs: np.ndarray
"""
freqs = self.window.fft_harmonics(self.decimation.sample_rate)
freqs = self.stft.window.fft_harmonics(self.decimation.sample_rate)
return freqs

@property
def sample_rate_decimation(self) -> float:
"""
Returns the sample rate of the data after decimation.
TODO: Delete this method and replace calls to self.sample_rate_decimation with self.decimation.sample_rate
"""
return self.decimation.sample_rate

@property
def harmonic_indices(self) -> List[int]:
"""
Expand Down Expand Up @@ -344,15 +285,15 @@ def is_consistent_with_archived_fc_parameters(
Iterates over FCDecimation attributes:
"channels_estimated": to ensure all expected channels are in the group
"anti_alias_filter": check that the expected AAF was applied
"sample_rate,
"method",
"prewhitening_type",
"recoloring",
"pre_fft_detrend_type",
"min_num_stft_windows",
"window",
"harmonic_indices",
"decimation.anti_alias_filter": check that the expected AAF was applied
"decimation.sample_rate,
"decimation.method",
"stft.prewhitening_type",
"stft.recoloring",
"stft.pre_fft_detrend_type",
"stft.min_num_stft_windows",
"stft.window",
"stft.harmonic_indices",
Returns
-------
Expand All @@ -373,18 +314,18 @@ def is_consistent_with_archived_fc_parameters(
self.logger.info(msg)
return False

# anti_alias_filter: Check that the data were
# anti_alias_filter: Check that the data were filtered the same way
try:
assert fc_decimation.decimation_anti_alias_filter == self.decimation.anti_alias_filter
assert fc_decimation.time_series_decimation.anti_alias_filter == self.decimation.anti_alias_filter
except AssertionError:
cond1 = self.anti_alias_filter == "default"
cond2 = fc_decimation.decimation_anti_alias_filter is None
cond1 = self.time_series_decimation.anti_alias_filter == "default"
cond2 = fc_decimation.time_series_decimation.anti_alias_filter is None
if cond1 & cond2:
pass
else:
msg = (
"Antialias Filters Not Compatible -- need to add handling for "
f"{msg} FCdec {fc_decimation.decimation_anti_alias_filter} and "
f"{msg} FCdec {fc_decimation.time_series_decimation.anti_alias_filter} and "
f"{msg} processing config:{self.decimation.anti_alias_filter}"
)
raise NotImplementedError(msg)
Expand Down Expand Up @@ -478,6 +419,8 @@ def is_consistent_with_archived_fc_parameters(
# if harmonic_indices is -1, it means the archive kept all so we can skip this check.
pass
else:
msg = "WIP: harmonic indices in AuroraDecimationlevel are derived from processing bands -- Not robustly tested to compare with FCDecimation"
self.logger.debug(msg)
harmonic_indices_requested = self.harmonic_indices
fcdec_group_set = set(fc_decimation.harmonic_indices)
processing_set = set(harmonic_indices_requested)
Expand Down Expand Up @@ -542,7 +485,7 @@ def to_fc_decimation(
fc_dec_obj.stft.pre_fft_detrend_type = self.stft.pre_fft_detrend_type
fc_dec_obj.stft.prewhitening_type = self.stft.prewhitening_type
fc_dec_obj.stft.recoloring = self.stft.recoloring
fc_dec_obj.time_series_decimation.sample_rate = self.sample_rate_decimation
fc_dec_obj.time_series_decimation.sample_rate = self.decimation.sample_rate
fc_dec_obj.stft.window = self.stft.window

return fc_dec_obj
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def assign_bands(
) # self.decimations_dict[key]
decimation_obj.decimation.factor = d
decimation_obj.decimation.sample_rate = sr
decimation_obj.window.num_samples = num_samples_window[i_level]
decimation_obj.stft.window.num_samples = num_samples_window[i_level]
frequencies = decimation_obj.fft_frequencies

for low, high in band_edges:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ def __init__(self, **kwargs):
logger.debug(msg)
self.short_time_fourier_transform.per_window_detrend_type = ""

# if self.time_series_decimation.level == 0:
# self.time_series_decimation.anti_alias_filter = None

@property
def window(self):
return self.stft.window

def __len__(self) -> int:
return len(self.channels)

Expand All @@ -96,6 +89,14 @@ def __add__(self, other):
raise TypeError(msg)

#----- Begin (Possibly Temporary) methods for integrating TimeSeriesDecimation Class -----#

@property
def decimation(self) -> TimeSeriesDecimation:
"""
Passthrough method to access self.time_series_decimation
"""
return self.time_series_decimation

@property
def factor(self):
"""
Expand Down Expand Up @@ -158,22 +159,6 @@ def decimation_method(self, value: str) -> None:
"""
self.time_series_decimation.method = value

@property
def decimation_anti_alias_filter(self) -> str:
"""
Access the decimation anti_alias_filter description from the TSDecimation
:return: Description of how anti_alias_filtering is performed
:rtype: str
"""
return self.time_series_decimation.anti_alias_filter

@decimation_method.setter
def decimation_anti_alias_filter(self, value: str) -> None:
"""
Set the decimation_anti_alias_filter in the TSDecimation
"""
self.time_series_decimation.anti_alias_filter = value

@property
def decimation_sample_rate(self) -> float:
"""
Expand All @@ -183,29 +168,6 @@ def decimation_sample_rate(self) -> float:
"""
return self.time_series_decimation.sample_rate

@property
def sample_rate_decimation(self) -> float:
"""
TODO: Delete this function in 2025.
Access the decimation sample rate from the TSDecimation
:return:Time series sample rate after decimation (from the TSDecimation)
:rtype: str
"""
msg = "sample_rate_decimation is deprecated -- use self.time_series_decimation.sample_rate"
logger.warning(msg)
return self.time_series_decimation.sample_rate

@sample_rate_decimation.setter
def sample_rate_decimation(self, value: float) -> None:
"""
TODO: Delete this function in 2025.
Set the decimation sample_rate_decimation in the TSDecimation
"""
msg = "sample_rate_decimation is deprecated -- use self.time_series_decimation.sample_rate"
logger.warning(msg)
self.time_series_decimation.sample_rate = value

#----- End (Possibly Temporary) methods for integrating TimeSeriesDecimation Class -----#

Expand Down Expand Up @@ -446,14 +408,14 @@ def is_valid_for_time_series_length(self, n_samples_ts: int) -> bool:
"""
required_num_samples = (
self.window.num_samples
+ (self.stft.min_num_stft_windows - 1) * self.window.num_samples_advance
self.stft.window.num_samples
+ (self.stft.min_num_stft_windows - 1) * self.stft.window.num_samples_advance
)
if n_samples_ts < required_num_samples:
msg = (
f"{n_samples_ts} not enough samples for minimum of "
f"{self.stft.min_num_stft_windows} stft windows of length "
f"{self.window.num_samples} and overlap {self.window.overlap}"
f"{self.stft.window.num_samples} and overlap {self.stft.window.overlap}"
)
self.logger.warning(msg)
return False
Expand All @@ -463,7 +425,7 @@ def is_valid_for_time_series_length(self, n_samples_ts: int) -> bool:
@property
def fft_frequencies(self) -> np.ndarray:
""" Returns the one-sided fft frequencies (without Nyquist)"""
return self.window.fft_harmonics(self.sample_rate)
return self.stft.window.fft_harmonics(self.sample_rate)


def fc_decimations_creator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
["level", "factor", "method", "sample_rate", "anti_alias_filter"],
TODO: Consider adding a parent_sample_rate attribute to this class
Created on Thu Dec 26 12:00:00 2024
@author: kkappler
Expand Down Expand Up @@ -45,28 +45,6 @@ def __init__(self, **kwargs):

super().__init__(attr_dict=attr_dict, **kwargs)

# Temporary workarounds while replacing legacy Decimation class
# @property
# def level(self):
# return self.decimation_level
#
# @property
# def factor(self):
# return self.decimation_factor
#
# @property
# def method(self):
# return self.decimation_method
#
# @property
# def sample_rate(self):
# return self.sample_rate_decimation


#
# def main():
# pass
#
#
# if __name__ == "__main__":
# main()
# TODO: add this logic to __init__ and a test
# if self.level == 0:
# self.anti_alias_filter = None
6 changes: 3 additions & 3 deletions tests/tf/processing/fcs/test_decimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def test_update(self):
def test_fft_frequencies(self):
dl1 = self.dl.copy()
freqs = dl1.fft_frequencies
assert (len(freqs) == dl1.window.num_samples/2)
assert (len(freqs) == dl1.stft.window.num_samples/2)


def test_update_with_match(self):
Expand Down Expand Up @@ -203,7 +203,7 @@ def setUp(self):
self.dl.decimation_factor = 4
self.dl.decimation_level = 1
self.dl.id = 1
self.dl.sample_rate_decimation = 16
self.dl.time_series_decimation.sample_rate = 16
for ch in ["ex", "ey", "hx", "hy", "hz"]:
self.dl.add_channel(Channel(component=ch))

Expand Down Expand Up @@ -253,7 +253,7 @@ def test_pre_fft_detrend_type_false(self):
)

def test_window_false(self):
self.adl.window.type = "dpss"
self.adl.stft.window.type = "dpss"
self.assertEqual(
False, self.adl.is_consistent_with_archived_fc_parameters(self.dl, None)
)
Expand Down

0 comments on commit e5099f6

Please sign in to comment.