Skip to content

Commit 5c9c9a0

Browse files
committed
adding mth5_path property to kds
1 parent 55f52d4 commit 5c9c9a0

File tree

3 files changed

+178
-31
lines changed

3 files changed

+178
-31
lines changed

mtpy/processing/kernel_dataset.py

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
# =============================================================================
6161
# Imports
6262
# =============================================================================
63+
from pathlib import Path
6364
import copy
6465
from typing import Optional, Union
6566

@@ -197,6 +198,38 @@ def df(self, value):
197198
self._set_datetime_columns(self._add_columns(value)), inplace=False
198199
)
199200

201+
def _has_df(self):
202+
"""
203+
check to see if dataframe is set
204+
"""
205+
if self._df is not None:
206+
if not self._df.empty:
207+
return True
208+
return False
209+
return False
210+
211+
def _df_has_local_station_id(self):
212+
"""
213+
Check to make sure the dataframe has the local station id
214+
215+
:return: DESCRIPTION
216+
:rtype: bool
217+
218+
"""
219+
if self._has_df():
220+
return (self._df.station == self.local_station_id).any()
221+
222+
def _df_has_remote_station_id(self):
223+
"""
224+
Check to make sure the dataframe has the local station id
225+
226+
:return: DESCRIPTION
227+
:rtype: bool
228+
229+
"""
230+
if self._has_df():
231+
return (self._df.station == self.remote_station_id).any()
232+
200233
def _set_datetime_columns(self, df):
201234
"""
202235
be sure to set start and end to be date time objects
@@ -241,6 +274,119 @@ def _add_columns(self, df):
241274
)
242275
return df
243276

277+
@property
278+
def local_station_id(self):
279+
return self._local_station_id
280+
281+
@local_station_id.setter
282+
def local_station_id(self, value):
283+
if value is None:
284+
self._local_station_id = None
285+
else:
286+
try:
287+
self._local_station_id = str(value)
288+
except ValueError:
289+
raise ValueError(
290+
f"Bad type {type(value)}. "
291+
"Cannot convert local_station_id value to string."
292+
)
293+
if self._has_df():
294+
if not self._df_has_local_station_id():
295+
raise NameError(
296+
f"Could not find {self._local_station_id} in dataframe"
297+
)
298+
299+
@property
300+
def local_mth5_path(self):
301+
"""
302+
303+
:return: Local station MTH5 path, a property extracted from the dataframe
304+
:rtype: Path
305+
306+
"""
307+
if self._has_df():
308+
return Path(
309+
self._df.loc[
310+
self._df.station == self.local_station_id, "mth5_path"
311+
].unique()[0]
312+
)
313+
else:
314+
return None
315+
316+
# @local_mth5_path.setter
317+
# def local_mth5_path(self, value):
318+
# self._local_mth5_path = self.set_path(value)
319+
320+
def has_local_mth5(self):
321+
"""test if local mth5 exists"""
322+
if self.local_mth5_path is None:
323+
return False
324+
else:
325+
return self.local_mth5_path.exists()
326+
327+
@property
328+
def remote_station_id(self):
329+
return self._remote_station_id
330+
331+
@remote_station_id.setter
332+
def remote_station_id(self, value):
333+
if value is None:
334+
self._remote_station_id = None
335+
else:
336+
try:
337+
self._remote_station_id = str(value)
338+
except ValueError:
339+
raise ValueError(
340+
f"Bad type {type(value)}. "
341+
"Cannot convert remote_station_id value to string."
342+
)
343+
if self._has_df():
344+
if not self._df_has_remote_station_id():
345+
raise NameError(
346+
f"Could not find {self._remote_station_id} in dataframe"
347+
)
348+
349+
@property
350+
def remote_mth5_path(self):
351+
"""
352+
353+
:return: remote station MTH5 path, a property extracted from the dataframe
354+
:rtype: Path
355+
356+
"""
357+
if self._has_df() and self.remote_station_id is not None:
358+
return Path(
359+
self._df.loc[
360+
self._df.station == self.remote_station_id, "mth5_path"
361+
].unique()[0]
362+
)
363+
else:
364+
return None
365+
366+
# @remote_mth5_path.setter
367+
# def remote_mth5_path(self, value):
368+
# self._remote_mth5_path = self.set_path(value)
369+
370+
def has_remote_mth5(self):
371+
"""test if remote mth5 exists"""
372+
if self.remote_mth5_path is None:
373+
return False
374+
else:
375+
return self.remote_mth5_path.exists()
376+
377+
@classmethod
378+
def set_path(self, value):
379+
return_path = None
380+
if value is not None:
381+
if isinstance(value, (str, Path)):
382+
return_path = Path(value)
383+
if not return_path.exists():
384+
raise IOError(f"Cannot find file: {return_path}")
385+
else:
386+
raise ValueError(f"Cannot convert type{type(value)} to Path")
387+
388+
return return_path
389+
244390
def from_run_summary(
245391
self,
246392
run_summary: RunSummary,

mtpy/processing/run_summary.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -189,20 +189,6 @@ def set_sample_rate(self, sample_rate: float, inplace: bool = False):
189189
new_rs.df = new_rs.df[new_rs.df.sample_rate == sample_rate]
190190
return new_rs
191191

192-
# BELOW FUNCTION CAN BE COPIED FROM METHOD IN KernelDataset()
193-
# def drop_runs_shorter_than(self, duration, units="s"):
194-
# if units != "s":
195-
# raise NotImplementedError
196-
# if "duration" not in self.df.columns:
197-
# self.add_duration()
198-
# drop_cond = self.df.duration < duration
199-
# # df = self.df[drop_cond]
200-
# self.df.drop(self.df[drop_cond].index, inplace=True)
201-
# df = df.reset_index()
202-
#
203-
# self.df = df
204-
# return df
205-
206192

207193
def extract_run_summary_from_mth5(mth5_obj, summary_type="run"):
208194
"""

tests/processing/test_kernel_dataset.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# =============================================================================
22
# Imports
33
# =============================================================================
4+
from pathlib import Path
45
import pandas as pd
56
import unittest
67

@@ -166,7 +167,7 @@ def setUp(self):
166167
],
167168
"has_data": [True, True, True, True],
168169
"input_channels": ["hx, hy"] * 4,
169-
"mth5_path": ["path"] * 4,
170+
"mth5_path": ["path"] * 2 + ["remote_path"] * 2,
170171
"n_samples": [86400, 86400, 79200, 7200],
171172
"output_channels": ["hz, ex, ey"] * 4,
172173
"run": ["01", "02", "03", "04"],
@@ -187,23 +188,25 @@ def setUp(self):
187188
self.kd = KernelDataset()
188189
self.kd.from_run_summary(self.run_summary, self.local, self.remote)
189190

190-
def test_from_run_summary(self):
191+
def test_from_run_summary_local_station_id(self):
192+
self.assertEqual(self.kd.local_station_id, self.local)
191193

192-
with self.subTest("local_station_id"):
193-
self.assertEqual(self.kd.local_station_id, self.local)
194-
with self.subTest("remote_station_id"):
195-
self.assertEqual(self.kd.remote_station_id, self.remote)
196-
with self.subTest("has remote column"):
197-
self.assertIn("remote", self.kd.df.columns)
198-
with self.subTest("has fc column"):
199-
self.assertIn("fc", self.kd.df.columns)
200-
with self.subTest("has_duration"):
201-
self.assertFalse((self.kd.df.duration == 0).all())
202-
203-
with self.subTest("has_all_columns"):
204-
self.assertListEqual(
205-
sorted(self.kd.df.columns), sorted(KERNEL_DATASET_COLUMNS)
206-
)
194+
def test_from_run_summary_remote_station_id(self):
195+
self.assertEqual(self.kd.remote_station_id, self.remote)
196+
197+
def test_from_run_summary_has_duration(self):
198+
self.assertFalse((self.kd.df.duration == 0).all())
199+
200+
def test_from_run_summary_has_all_columns(self):
201+
self.assertListEqual(
202+
sorted(self.kd.df.columns), sorted(KERNEL_DATASET_COLUMNS)
203+
)
204+
205+
def test_from_run_summary_local_mth5_path(self):
206+
self.assertEqual(self.kd.local_mth5_path, Path("path"))
207+
208+
def test_from_run_summary_remote_mth5_path(self):
209+
self.assertEqual(self.kd.remote_mth5_path, Path("remote_path"))
207210

208211
def test_num_sample_rates(self):
209212
self.assertEqual(self.kd.num_sample_rates, 1)
@@ -223,6 +226,18 @@ def test_update_duration_column_not_inplace(self):
223226

224227
self.assertTrue((new_df.duration == self.kd.df.duration).all())
225228

229+
def test_set_local_station_id_fail(self):
230+
def set_station(value):
231+
self.kd.local_station_id = value
232+
233+
self.assertRaises(NameError, set_station, "mt03")
234+
235+
def test_set_remote_station_id_fail(self):
236+
def set_station(value):
237+
self.kd.remote_station_id = value
238+
239+
self.assertRaises(NameError, set_station, "mt03")
240+
226241

227242
class TestKernelDatasetMethodsFail(unittest.TestCase):
228243
def setUp(self):

0 commit comments

Comments
 (0)