Skip to content

Commit

Permalink
Merge pull request #228 from kujaku11/tipper_flip
Browse files Browse the repository at this point in the history
- Fixes bug when reading an EDI and asserting that the transfer function is ordered by descending frequency.
  • Loading branch information
kujaku11 authored Sep 30, 2024
2 parents 6936e35 + e430b7f commit 72f4b55
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 33 deletions.
15 changes: 7 additions & 8 deletions mt_metadata/transfer_functions/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def _validate_run_metadata(self, run_metadata):
)
self.logger.error(msg)
raise TypeError(msg)
return run_metadata.copy()
return run_metadata

def _validate_station_metadata(self, station_metadata):
"""
Expand All @@ -298,7 +298,7 @@ def _validate_station_metadata(self, station_metadata):
)
self.logger.error(msg)
raise TypeError(msg)
return station_metadata.copy()
return station_metadata

def _validate_survey_metadata(self, survey_metadata):
"""
Expand Down Expand Up @@ -328,7 +328,7 @@ def _validate_survey_metadata(self, survey_metadata):
)
self.logger.error(msg)
raise TypeError(msg)
return survey_metadata.copy()
return survey_metadata

### Properties ------------------------------------------------------------
@property
Expand Down Expand Up @@ -1475,9 +1475,8 @@ def station(self, station_name):
"""
self.station_metadata.id = validate_name(station_name)
if self.station_metadata.runs[0].id is None:
r = self.station_metadata.runs[0].copy()
r = self.station_metadata.runs.pop(None)
r.id = f"{self.station_metadata.id}a"
self.station_metadata.runs.remove(None)
self.station_metadata.runs.append(r)

@property
Expand Down Expand Up @@ -2164,9 +2163,9 @@ def to_zmm(self):
"""
zmm_kwargs = {}
zmm_kwargs["channel_nomenclature"] = self.channel_nomenclature
zmm_kwargs[
"inverse_channel_nomenclature"
] = self.inverse_channel_nomenclature
zmm_kwargs["inverse_channel_nomenclature"] = (
self.inverse_channel_nomenclature
)
if hasattr(self, "decimation_dict"):
zmm_kwargs["decimation_dict"] = self.decimation_dict
zmm_obj = ZMM(**zmm_kwargs)
Expand Down
25 changes: 18 additions & 7 deletions mt_metadata/transfer_functions/io/edi/edi.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,22 @@ def period(self):
return 1.0 / self.frequency
return None

def _assert_descending_frequency(self):
"""
Assert that the transfer function is ordered from high frequency to low
frequency.
"""
if self.frequency[0] < self.frequency[1]:
self.logger.debug(
"Ordered arrays to be arranged from high to low frequency"
)
self.frequency = self.frequency[::-1]
self.z = self.z[::-1]
self.z_err = self.z_err[::-1]
self.t = self.t[::-1]
self.t_err = self.t_err[::-1]

def read(self, fn=None, get_elevation=False):
"""
Read in an edi file and fill attributes of each section's classes.
Expand Down Expand Up @@ -456,13 +472,8 @@ def _read_mt(self, data_lines):
except KeyError as error:
self.logger.debug(error)
# check for order of frequency, we want high togit low
if self.frequency[0] < self.frequency[1]:
self.logger.debug(
"Ordered arrays to be arranged from high to low frequency"
)
self.frequency = self.frequency[::-1]
self.z = self.z[::-1]
self.z_err = self.z_err[::-1]
self._assert_descending_frequency()

try:
self.rotation_angle = np.array(data_dict["zrot"])
except KeyError:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
# =============================================================================


def df_from_bands(band_list):
def df_from_bands(band_list: list) -> pd.DataFrame:
"""
Utility function that transforms a list of bands into a dataframe
Expand Down Expand Up @@ -70,13 +70,13 @@ def df_from_bands(band_list):
out_df.reset_index(inplace=True, drop=True)
return out_df

def get_fft_harmonics(samples_per_window, sample_rate):
def get_fft_harmonics(samples_per_window: int, sample_rate: float) -> np.ndarray:
"""
Works for odd and even number of points.
Could be midified with kwargs to support one_sided, two_sided, ignore_dc
ignore_nyquist, and etc. Could actally take FrequencyBands as an argument
if we wanted as well.
Development notes:
Could be modified with kwargs to support one_sided, two_sided, ignore_dc
ignore_nyquist, and etc. Consider taking FrequencyBands as an argument.
Parameters
----------
Expand Down
31 changes: 19 additions & 12 deletions mt_metadata/utils/list_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,14 @@ def __len__(self):

def _get_key_from_index(self, index):
try:
return next(
key for ii, key in enumerate(self._home) if ii == index
)
return next(key for ii, key in enumerate(self._home) if ii == index)

except StopIteration:
raise KeyError(f"Could not find {index}")

def _get_index_from_key(self, key):
try:
return next(
index for index, k in enumerate(self._home) if k == key
)
return next(index for index, k in enumerate(self._home) if k == key)

except StopIteration:
raise KeyError(f"Could not find {key}")
Expand All @@ -75,9 +71,7 @@ def _get_key_from_object(self, obj):
elif hasattr(obj, "component"):
return obj.component
else:
raise TypeError(
"could not identify an appropriate key from object"
)
raise TypeError("could not identify an appropriate key from object")

def __deepcopy__(self, memodict={}):
"""
Expand Down Expand Up @@ -230,9 +224,7 @@ def remove(self, key):
raise (KeyError("Could not find None in keys."))

else:
raise TypeError(
"could not identify an appropriate key from object"
)
raise TypeError("could not identify an appropriate key from object")

def extend(self, other, skip_keys=[]):
"""
Expand Down Expand Up @@ -282,3 +274,18 @@ def update(self, other):
)

self._home.update(other)

def pop(self, key):
"""
pop item off of dictionary. The key must be verbatim
:param key: key of item to be popped off of dictionary
:type key: string
:return: item popped
"""

if key in self.keys():
return dict([self._home.popitem(key)])
else:
raise KeyError(f"{key} is not in ListDict keys.")
11 changes: 11 additions & 0 deletions tests/test_list_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ def test_copy(self):
lc = self.ld.copy()
self.assertEqual(self.ld, lc)

def test_pop(self):
self.ld["b"] = 1
b = self.ld.pop("b")
with self.subTest("value of b"):
self.assertEqual(b["b"], 1)
with self.subTest("b not in keys"):
self.assertNotIn("b", self.ld.keys())

def test_pop_fail(self):
self.assertRaises(KeyError, self.ld.pop, "h")


class TestListDictSetIndex(unittest.TestCase):
@classmethod
Expand Down
32 changes: 31 additions & 1 deletion tests/tf/io/edi/test_edi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
# =============================================================================
import unittest

from collections import OrderedDict
import numpy as np

from mt_metadata.transfer_functions.io.edi.metadata import (
Header,
EMeasurement,
HMeasurement,
DefineMeasurement,
)
from mt_metadata.transfer_functions.io.edi import EDI


# =============================================================================
Expand Down Expand Up @@ -235,6 +237,34 @@ def test_read_lines(self):
self.assertListEqual(self.meas_list, self.dm.measurement_list)


class TestAssertDescendingFrequency(unittest.TestCase):
@classmethod
def setUpClass(self):
self.edi = EDI()
self.nf = 7
self.edi.frequency = np.logspace(-3, 3, self.nf)
self.z = np.arange(self.nf * 4).reshape((self.nf, 2, 2))
self.t = np.arange(self.nf * 2).reshape((self.nf, 1, 2))
self.edi.z = self.z.copy()
self.edi.z_err = self.z.copy()
self.edi.t = self.t.copy()
self.edi.t_err = self.t.copy()

self.edi._assert_descending_frequency()

def test_z(self):
self.assertTrue(np.allclose(self.z[::-1], self.edi.z))

def test_z_err(self):
self.assertTrue(np.allclose(self.z[::-1], self.edi.z_err))

def test_t(self):
self.assertTrue(np.allclose(self.t[::-1], self.edi.t))

def test_t_err(self):
self.assertTrue(np.allclose(self.t[::-1], self.edi.t_err))


# =============================================================================
# run
# =============================================================================
Expand Down

0 comments on commit 72f4b55

Please sign in to comment.