Skip to content

Commit

Permalink
Merge pull request #236 from kujaku11/fix_issue_235
Browse files Browse the repository at this point in the history
Fix issue 235, and other PR's above.
  • Loading branch information
kujaku11 authored Jan 3, 2025
2 parents 03505ee + b595379 commit 6972659
Show file tree
Hide file tree
Showing 20 changed files with 632 additions and 372 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ recursive-include mt_metadata/transfer_functions/io/edi/metadata/standards *.jso
recursive-include mt_metadata/transfer_functions/io/zfiles/metadata/standards *.json
recursive-include mt_metadata/transfer_functions/io/jfiles/metadata/standards *.json
recursive-include mt_metadata/transfer_functions/io/zonge/metadata/standards *.json
recursive-include mt_metadata/transfer_functions/processing/standards *.json
recursive-include mt_metadata/transfer_functions/processing/aurora/standards *.json
recursive-include mt_metadata/transfer_functions/processing/fourier_coefficients/standards *.json
recursive-include mt_metadata/data/mt_xml *.xml
Expand Down
2 changes: 1 addition & 1 deletion mt_metadata/transfer_functions/processing/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@

from .time_series_decimation import TimeSeriesDecimation
5 changes: 2 additions & 3 deletions mt_metadata/transfer_functions/processing/aurora/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
from .band import Band
from .channel import Channel
from .channel_nomenclature import ChannelNomenclature
from .window import Window
from .decimation import Decimation
from .decimation_level import DecimationLevel
from .estimator import Estimator
from .frequency_bands import FrequencyBands
from .processing import Processing
from .regression import Regression
from .run import Run
from .station import Station
from .stations import Stations

from .window import Window

__all__ = [
"Band",
Expand Down
155 changes: 18 additions & 137 deletions mt_metadata/transfer_functions/processing/aurora/band.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from mt_metadata.base.helpers import write_lines
from mt_metadata.base import get_schema, Base
from .standards import SCHEMA_FN_PATHS
from typing import Optional

# =============================================================================
attr_dict = get_schema("band", SCHEMA_FN_PATHS)
Expand All @@ -22,28 +23,33 @@ class Band(Base):
__doc__ = write_lines(attr_dict)

def __init__(self, **kwargs):
"""
Constructor.
:param kwargs: TODO description here
"""

super().__init__(attr_dict=attr_dict, **kwargs)
self._name = None

@property
def lower_bound(self):
def lower_bound(self) -> float:
return self.frequency_min

@property
def upper_bound(self):
def upper_bound(self) -> float:
return self.frequency_max

@property
def lower_closed(self):
def lower_closed(self) -> bool:
return self.to_interval().closed_left

@property
def upper_closed(self):
def upper_closed(self) -> bool:
return self.to_interval().closed_right

@property
def name(self):
def name(self) -> str:
"""
:return: The name of the frequency band (currently defaults to fstring with 6 decimal places.
:rtype: str
Expand All @@ -57,7 +63,7 @@ def name(self):
def name(self, value):
self._name = value

def _indices_from_frequencies(self, frequencies):
def _indices_from_frequencies(self, frequencies: np.ndarray) -> np.ndarray:
"""
Parameters
Expand All @@ -84,7 +90,7 @@ def _indices_from_frequencies(self, frequencies):
indices = np.where(cond1 & cond2)[0]
return indices

def set_indices_from_frequencies(self, frequencies):
def set_indices_from_frequencies(self, frequencies: np.ndarray) -> None:
"""assumes min/max freqs are defined"""
indices = self._indices_from_frequencies(frequencies)
self.index_min = indices[0]
Expand All @@ -98,15 +104,15 @@ def to_interval(self):
@property
def harmonic_indices(self):
"""
Assumes all harmincs between min and max are present in the band
Assumes all harmoincs between min and max are present in the band
Returns
-------
numpy array of integers corresponding to harminic indices
"""
return np.arange(self.index_min, self.index_max + 1)

def in_band_harmonics(self, frequencies):
def in_band_harmonics(self, frequencies: np.ndarray):
"""
Parameters
----------
Expand All @@ -122,7 +128,7 @@ def in_band_harmonics(self, frequencies):
return harmonics

@property
def center_frequency(self):
def center_frequency(self) -> float:
"""
Returns
-------
Expand All @@ -135,131 +141,6 @@ def center_frequency(self):
return (self.lower_bound + self.upper_bound) / 2

@property
def center_period(self):
def center_period(self) -> float:
""" Returns the inverse of center frequency."""
return 1.0 / self.center_frequency


class FrequencyBands(object):
"""
This is just collection of objects of class Band.
It is intended to be used at a single decimation level
The core underlying variable is "band_edges", a 2D array, with one row per
frequency band and two columns, one for the left-hand (lower bound) of the
frequency band and one for the right-hand (upper bound).
Note there are some "clever" ways to define the bands using a 1-D array but this
assumes the bands to be adjacent, and there is no good reason to bake this
constriant in -- band edges is thus 2-D.
"""

def __init__(self, **kwargs):
"""
Parameters
----------
kwargs
band_edges: 2d numpy array
"""
self.band_edges = kwargs.get("band_edges", None)

@property
def number_of_bands(self):
return self.band_edges.shape[0]

def validate(self):
"""
Placeholder for sanity checks.
Main reason for this is in anticipation of an append() method that accepts Band objects.
In that case we may wish to re-order the band edges.
"""
band_centers = self.band_centers()

# check band centers are monotonically increasing
monotone_condition = np.all(band_centers[1:] > band_centers[:-1])
if monotone_condition:
pass
else:
print(
"WARNING Band Centers are Not Monotonic. This probably means that "
"the bands are being defined in an adhoc way"
)
print("This condition untested 20210720")
print("Attempting to reorganize bands")
# use np.argsort to rorganize the bands
self.band_edges = self.band_edges[np.argsort(band_centers), :]

return

def bands(self, direction="increasing_frequency"):
"""
make this a generator for iteration over bands
Returns
-------
"""
band_indices = range(self.number_of_bands)
if direction == "increasing_period":
band_indices = np.flip(band_indices)
return (self.band(i_band) for i_band in band_indices)

def band(self, i_band):
"""
Parameters
----------
i_band: integer (zero-indexed)
Specifies the band to return
Returns
-------
frequency_band: Band()
Class that represents a frequency band
"""

frequency_band = Band(
frequency_min=self.band_edges[i_band, 0],
frequency_max=self.band_edges[i_band, 1],
)
return frequency_band

def band_centers(self, frequency_or_period="frequency"):
"""
Parameters
----------
frequency_or_period : str
One of ["frequency" , "period"]. Determines if the vector of band
centers is returned in "Hz" or "s"
Returns
-------
band_centers : numpy array
center frequencies of the bands in Hz or in s
"""
band_centers = np.full(self.number_of_bands, np.nan)
for i_band in range(self.number_of_bands):
frequency_band = self.band(i_band)
band_centers[i_band] = frequency_band.center_frequency
if frequency_or_period == "period":
band_centers = 1.0 / band_centers
return band_centers

def from_decimation_object(self, decimation_object):
"""
Define band_edges array from config object,
Parameters
----------
decimation_object: mt_metadata.transfer_functions.processing.aurora.Decimation
"""
# replace below with decimation_object.delta_frequency ?
df = (
decimation_object.decimation.sample_rate
/ decimation_object.window.num_samples
)
half_df = df / 2.0
# half_df /=100
lower_edges = (decimation_object.lower_bounds * df) - half_df
upper_edges = (decimation_object.upper_bounds * df) + half_df
band_edges = np.vstack((lower_edges, upper_edges)).T
self.band_edges = band_edges
24 changes: 0 additions & 24 deletions mt_metadata/transfer_functions/processing/aurora/decimation.py

This file was deleted.

Loading

0 comments on commit 6972659

Please sign in to comment.