Skip to content

Commit

Permalink
Deprecate old emtf methods for frequency bands
Browse files Browse the repository at this point in the history
Streamlined some methods in frequency_bands marked as deprecated
Tracked down last usage to a test for the matlab_z_file_reader
Tuned up the test for the matlab_z_file_reader so that it has an assert
statement

[Issue(s): #150]
  • Loading branch information
kkappler committed Sep 10, 2022
1 parent a77afbf commit 1dbc0e3
Show file tree
Hide file tree
Showing 4 changed files with 417 additions and 199 deletions.
115 changes: 66 additions & 49 deletions aurora/sandbox/io_helpers/garys_matlab_zfiles/matlab_z_file_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import numpy as np
import scipy.io as sio


from aurora.config.metadata.decimation_level import DecimationLevel
from aurora.config.metadata.processing import Processing
from aurora.config.emtf_band_setup import BANDS_256_29_FILE
from aurora.sandbox.io_helpers.emtf_band_setup import EMTFBandSetupFile
from aurora.sandbox.io_helpers.zfile_murphy import read_z_file
from aurora.time_series.frequency_band import FrequencyBands
from aurora.transfer_function.emtf_z_file_helpers import clip_bands_from_z_file
Expand All @@ -23,7 +25,13 @@
from aurora.transfer_function.TTFZ import TTFZ


def test_matlab_zfile_reader():
def test_matlab_zfile_reader(make_plot=False):
"""
Returns
-------
"""
print("??")
CASE = "IAK34ss" # synthetic"
bs_file = BANDS_256_29_FILE
Expand All @@ -41,14 +49,14 @@ def test_matlab_zfile_reader():
tf_dict = {}

p = Processing()
emtf_band_setup = EMTFBandSetupFile(filepath=bs_file, sample_rate=sample_rate)
num_samples_window = 256
band_edges = emtf_band_setup.compute_band_edges(
decimation_factors=[1, 4, 4, 4], num_samples_window=4 * [num_samples_window]
)
for i_dec in range(4):
frequency_bands = FrequencyBands()
frequency_bands.from_emtf_band_setup(
filepath=bs_file,
sample_rate=sample_rate,
decimation_level=i_dec + 1,
num_samples_window=256,
)
edges = np.flipud(band_edges[i_dec])
frequency_bands = FrequencyBands(band_edges=edges)
transfer_function_header = TransferFunctionHeader(
processing_scheme="RME",
local_station_id="test1",
Expand All @@ -65,7 +73,7 @@ def test_matlab_zfile_reader():

dec_level_cfg = DecimationLevel()
dec_level_cfg.decimation.sample_rate = sample_rate
dec_level_cfg.window.num_samples = 256
dec_level_cfg.window.num_samples = num_samples_window
p.add_decimation_level(dec_level_cfg)
tf_obj.processing_config = p

Expand Down Expand Up @@ -134,49 +142,58 @@ def test_matlab_zfile_reader():
if n_periods_clip:
clip_bands_from_z_file(z_file_path, n_periods_clip, n_sensors=5)

archived_z_file_path = "archived_from_matlab.zss"

zfile = read_z_file(z_file_path)
archived_zfile = read_z_file(archived_z_file_path)

zfile.apparent_resistivity(angle=0)

scl = 1.0
fig, axs = plt.subplots(nrows=2, figsize=(11, 8.5), dpi=300, sharex=True)
markersize = 1
plot_rho(
axs[0],
zfile.periods,
zfile.rxy * scl,
label="rxy",
markersize=markersize,
color="red",
)
plot_rho(
axs[0],
zfile.periods,
zfile.ryx * scl,
label="ryx",
markersize=markersize,
color="blue",
)
axs[0].legend()
plot_phi(
axs[1],
zfile.periods,
zfile.pxy,
label="pxy",
markersize=markersize,
color="red",
)
plot_phi(
axs[1],
zfile.periods,
zfile.pyx,
label="pyx",
markersize=markersize,
color="blue",
)
axs[0].set_ylim(1, 1000)
axs[0].set_xlim(1, 10000)
plt.show()
archived_zfile.apparent_resistivity(angle=0)
assert (zfile.rxy == archived_zfile.rxy).all()
assert (zfile.ryx == archived_zfile.ryx).all()
assert (zfile.pxy == archived_zfile.pxy).all()
assert (zfile.pyx == archived_zfile.pyx).all()

if make_plot:
scl = 1.0
fig, axs = plt.subplots(nrows=2, figsize=(11, 8.5), dpi=300, sharex=True)
markersize = 1
plot_rho(
axs[0],
zfile.periods,
zfile.rxy * scl,
label="rxy",
markersize=markersize,
color="red",
)
plot_rho(
axs[0],
zfile.periods,
zfile.ryx * scl,
label="ryx",
markersize=markersize,
color="blue",
)
axs[0].legend()
plot_phi(
axs[1],
zfile.periods,
zfile.pxy,
label="pxy",
markersize=markersize,
color="red",
)
plot_phi(
axs[1],
zfile.periods,
zfile.pyx,
label="pyx",
markersize=markersize,
color="blue",
)
axs[0].set_ylim(1, 1000)
axs[0].set_xlim(1, 10000)
plt.show()
print("success!")


Expand Down
78 changes: 0 additions & 78 deletions aurora/time_series/frequency_band.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from deprecated import deprecated
import numpy as np
import pandas as pd

Expand Down Expand Up @@ -290,83 +289,6 @@ def band_centers(self, frequency_or_period="frequency"):
band_centers = 1.0 / band_centers
return band_centers

@deprecated
def from_emtf_band_setup(
self, filepath, decimation_level, sample_rate, num_samples_window
):
"""
This converts between EMTF band_setup files to a frequency_bands object.
The band_setup file is represented as a dataframe with
columns for decimation_level, first_fc_index,
last_fc_index.
Notes:
1. In EMTF, the the DC terms were not carried in the FC Files so the
integer-index 1 mapped to the first harmonic. In aurora, the DC-term is
kept (for now) and thus, because the fortran arrays index from 1, and
python from 0, we don't need any modification to the Fourier coefficient
indices here.If we wind up dropping the DC term from the STFT arrays we
would need to add -1 to the upper and lower bound indices.
2. EMTF band-setup files do not contain information about frequency.
Frequecny was implicit in the processing scheme but not stated. This
leaves some ambuguity when reading in files with names like
"bs_256.txt". Does 256 refer to the number of taps in the STFT
window, or to the number of positive frequencies (and the window was
512-length). Personal communication with Egbert 2021-06-22 indicates
that normally the integer number in a band-setup file name associates with
the time-domain window length.
Parameters
----------
filepath : str or pathlib.Path()
The full path to the band_setup file
decimation_level : integer
Corresponds to the decimation level from the band setup file to
create FrequecyBands from.
sample_rate : float
The sampling rate of the data at decimation_level
Returns
-------
"""
emtf_band_setup = EMTFBandSetupFile(filepath=filepath)
emtf_band_df = emtf_band_setup.get_decimation_level(decimation_level)
self.from_emtf_band_df(
emtf_band_df, decimation_level, sample_rate, num_samples_window
)

return

@deprecated
def from_emtf_band_df(
self, emtf_band_df, decimation_level, sample_rate, num_samples_window
):
"""
There is some duplication of calculation here ... this definition of edges
from emtf_band_df is also done in
Parameters
----------
emtf_band_df
decimation_level
sample_rate
num_samples_window
Returns
-------
"""
df = sample_rate / (num_samples_window)
half_df = df / 2.0
# half_df /=100
lower_edges = (emtf_band_df.lower_bound_index * df) - half_df
upper_edges = (emtf_band_df.upper_bound_index * df) + half_df
band_edges = np.vstack((lower_edges.values, upper_edges.values)).T
self.band_edges = band_edges

return

def from_decimation_object(self, decimation_object):
"""
Convert band edges from a :class:`aurora.config.Decimation` object,
Expand Down
72 changes: 0 additions & 72 deletions aurora/time_series/frequency_band_helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from deprecated import deprecated
import numpy as np
from aurora.time_series.frequency_band import FrequencyBands

Expand Down Expand Up @@ -85,77 +84,6 @@ def frequency_band_edges(
return fence_posts


@deprecated(version="0.0.3", reason="metadata processing config will handle this")
def configure_frequency_bands(config):
"""
2022-05-30: This method was never put into production and will need to be
modified to use the new Processing class.
May want to make config a "frequency band config object", but maybe not.
For now just using a flat config structure (per decimation level)
These methods could also be placed under FrequencyBands() class as
init_from_emtf()
init_from_bounds_array()
init_from_default()
Parameters
----------
config : aurora.config.metadata.decimation_level.DecimationLevel
The configuration parameters for setting up the frequency bands.
If config["band_setup_style"] is "EMTF" this will look for one of
Gary's "band_setup" files and parse it. It will look for
config.emtf_band_setup_file.
Other options would be :
1. "band_edges", accepts an array of lower_bound, upper_bound pairs
2. "logarithmic range": could accept a lower_bound, and an
upper_bound, and a number of bands inbetween., or it could estimate
a)lower bound from a rule about the minimum number of cycles needed
for an estimate (say 5 or 10)
b) upper bound from a Nyquist rule, say 80% f_Nyquist
Returns
-------
frequency_bands : aurora.time_series.frequency_band.FrequencyBands
a fully populated FrequencyBands object with all info needed to do
band averaging.
"""
frequency_bands = FrequencyBands()
if config["band_setup_style"] == "EMTF":
frequency_bands.from_emtf_band_setup(
filepath=config.emtf_band_setup_file,
sample_rate=config.sample_rate,
decimation_level=config.decimation_level_id + 1,
num_samples_window=config.num_samples_window,
)
elif config["band_setup_style"] == "band edges":
frequency_bands.band_edges = config["band_edges"]
# "Not Yet Supported"
raise NotImplementedError
elif config["band_setup_style"] == "logarithmic range":
lower_bound = config["frequency_bands_lower_bound"]
upper_bound = config["frequency_bands_upper_bound"]
num_bands = config["num_frequency_bands"]
if lower_bound is None:
pass
# suggest lower_bound from a rule
if upper_bound is None:
pass
# suggest upper_bound from a rule
if num_bands is None:
pass
# suggest based on num_bands per octave or decade
# now call logspace(lower, upper, num_bands)
raise NotImplementedError

return frequency_bands


def df_from_bands(band_list):
"""
This is just a utility function that transforms a list of bands into a dataframe
Expand Down
Loading

0 comments on commit 1dbc0e3

Please sign in to comment.