Skip to content

Commit 4b4bfff

Browse files
committed
updating processing classes to inherit eachother
1 parent 486ce15 commit 4b4bfff

File tree

4 files changed

+58
-163
lines changed

4 files changed

+58
-163
lines changed

mtpy/processing/aurora/process_aurora.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
from aurora.config.config_creator import ConfigCreator
1717
from aurora.pipelines.process_mth5 import process_mth5
1818

19-
from aurora.transfer_function.kernel_dataset import KernelDataset
20-
2119
from mth5.helpers import close_open_files
2220
from mth5.mth5 import MTH5
2321

@@ -29,5 +27,21 @@
2927
warnings.filterwarnings("ignore")
3028
# =============================================================================
3129

30+
3231
class AuroraProcessing(BaseProcessing):
33-
32+
"""
33+
convenience class to process with Aurora
34+
35+
"""
36+
37+
def __init__(self, **kwargs):
38+
super().__init__(**kwargs)
39+
40+
def create_config(self):
41+
pass
42+
43+
def build_kernel_dataset(self):
44+
pass
45+
46+
def process(self):
47+
pass

mtpy/processing/base.py

Lines changed: 14 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -8,128 +8,42 @@
88
# =============================================================================
99
# Imports
1010
# =============================================================================
11-
from pathlib import Path
1211

1312
from mtpy.processing.run_summary import RunSummary
1413
from mtpy.processing.kernel_dataset import KernelDataset
1514

1615
# =============================================================================
1716

1817

19-
class BaseProcessing:
18+
class BaseProcessing(KernelDataset):
2019
"""
2120
Base processing class contains path to various files
2221
"""
2322

2423
def __init__(self, **kwargs):
25-
self.local_station_id = None
26-
self.remote_station_id = None
27-
self.local_mth5_path = None
28-
self.remote_mth5_path = None
29-
self.config = None
30-
self.run_summary = RunSummary()
31-
self.kernel_dataset = KernelDataset()
32-
33-
for key, value in kwargs.items():
34-
setattr(self, key, value)
35-
36-
@property
37-
def local_station_id(self):
38-
return self._local_station_id
39-
40-
@local_station_id.setter
41-
def local_station_id(self, value):
42-
if value is None:
43-
self._local_station_id = None
44-
else:
45-
try:
46-
self._local_station_id = str(value)
47-
except ValueError:
48-
raise ValueError(
49-
f"Bad type {type(value)}. "
50-
"Cannot convert local_station_id value to string."
51-
)
52-
53-
@property
54-
def local_mth5_path(self):
55-
return self._local_mth5_path
56-
57-
@local_mth5_path.setter
58-
def local_mth5_path(self, value):
59-
self._local_mth5_path = self.set_path(value)
60-
61-
def has_local_mth5(self):
62-
"""test if local mth5 exists"""
63-
if self.local_mth5_path is None:
64-
return False
65-
else:
66-
return self.local_mth5_path.exists()
67-
68-
@property
69-
def remote_station_id(self):
70-
return self._remote_station_id
71-
72-
@remote_station_id.setter
73-
def remote_station_id(self, value):
74-
if value is None:
75-
self._remote_station_id = None
76-
else:
77-
try:
78-
self._remote_station_id = str(value)
79-
except ValueError:
80-
raise ValueError(
81-
f"Bad type {type(value)}. "
82-
"Cannot convert remote_station_id value to string."
83-
)
84-
85-
@property
86-
def remote_mth5_path(self):
87-
return self._remote_mth5_path
24+
super().__init__(**kwargs)
8825

89-
@remote_mth5_path.setter
90-
def remote_mth5_path(self, value):
91-
self._remote_mth5_path = self.set_path(value)
92-
93-
def has_remote_mth5(self):
94-
"""test if remote mth5 exists"""
95-
if self.remote_mth5_path is None:
96-
return False
97-
else:
98-
return self.remote_mth5_path.exists()
26+
self.config = None
9927

10028
@property
10129
def mth5_list(self):
10230
"""
103-
list of mth5's as [local, remote]
10431
105-
:return: DESCRIPTION
106-
:rtype: TYPE
32+
:return: list of mth5 to get run summary from
33+
:rtype: list
10734
10835
"""
10936
mth5_list = []
11037
if self.has_local_mth5():
11138
mth5_list.append(self.local_mth5_path)
112-
if self.has_remote_mth5():
113-
if self.local_mth5_path != self.remote_mth5_path:
114-
mth5_list.append(self.remote_mth5_path)
115-
else:
116-
raise IOError("Local MTH5 path must be set with a valid file path.")
117-
return mth5_list
118-
119-
@classmethod
120-
def set_path(self, value):
121-
return_path = None
122-
if value is not None:
123-
if isinstance(value, (str, Path)):
124-
return_path = Path(value)
125-
if not return_path.exists():
126-
raise IOError(f"Cannot find file: {return_path}")
127-
else:
128-
raise ValueError(f"Cannot convert type{type(value)} to Path")
39+
if self.has_remote_mth5():
40+
mth5_list.append(self.remote_mth5_path)
12941

130-
return return_path
42+
if len(mth5_list) == 0:
43+
raise ValueError("No MTH5 file paths set. Return list is empty.")
44+
return mth5_list
13145

132-
def get_run_summary(self, inplace: bool = True):
46+
def get_run_summary(self):
13347
"""
13448
Get the RunSummary object
13549
@@ -142,47 +56,9 @@ def get_run_summary(self, inplace: bool = True):
14256
14357
"""
14458

145-
if inplace:
146-
self.run_summary.from_mth5s(self.mth5_list)
147-
else:
148-
run_summary = RunSummary()
149-
run_summary.from_mth5s(self.mth5_list)
150-
return run_summary
151-
152-
def has_run_summary(self):
153-
"""
154-
test if has run summary
155-
156-
:return: DESCRIPTION
157-
:rtype: TYPE
158-
159-
"""
160-
if self.run_summary.df is not None:
161-
return True
162-
return False
163-
164-
def get_kernel_dataset(self, inplace: bool = True):
165-
"""
166-
167-
:param inplace: DESCRIPTION, defaults to True
168-
:type inplace: bool, optional
169-
:return: DESCRIPTION
170-
:rtype: TYPE
171-
172-
"""
173-
if not self.has_run_summary():
174-
self.get_run_summary()
175-
if inplace:
176-
177-
self.kernel_dataset.from_run_summary(
178-
self.run_summary, self.local_station_id, self.remote_station_id
179-
)
180-
else:
181-
kds = KernelDataset()
182-
kds.from_run_summary(
183-
self.run_summary, self.local_station_id, self.remote_station_id
184-
)
185-
return kds
59+
run_summary = RunSummary()
60+
run_summary.from_mth5s(self.mth5_list)
61+
return run_summary
18662

18763
def has_kernel_dataset(self):
18864
"""

mtpy/processing/kernel_dataset.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def __init__(
132132
df: Optional[Union[pd.DataFrame, None]] = None,
133133
local_station_id: Optional[str] = "",
134134
remote_station_id: Optional[Union[str, None]] = None,
135+
**kwargs,
135136
):
136137
"""
137138
Constructor.
@@ -154,6 +155,11 @@ def __init__(
154155
self.initialized = False
155156
self.local_mth5_obj = None
156157
self.remote_mth5_obj = None
158+
self._local_mth5_path = None
159+
self._remote_mth5_path = None
160+
161+
for key, value in kwargs.items():
162+
setattr(self, key, value)
157163

158164
def __str__(self):
159165
return str(self.mini_summary.head())
@@ -313,11 +319,11 @@ def local_mth5_path(self):
313319
].unique()[0]
314320
)
315321
else:
316-
return None
322+
return self._local_mth5_path
317323

318-
# @local_mth5_path.setter
319-
# def local_mth5_path(self, value):
320-
# self._local_mth5_path = self.set_path(value)
324+
@local_mth5_path.setter
325+
def local_mth5_path(self, value):
326+
self._local_mth5_path = self.set_path(value)
321327

322328
def has_local_mth5(self):
323329
"""test if local mth5 exists"""
@@ -365,9 +371,9 @@ def remote_mth5_path(self):
365371
else:
366372
return None
367373

368-
# @remote_mth5_path.setter
369-
# def remote_mth5_path(self, value):
370-
# self._remote_mth5_path = self.set_path(value)
374+
@remote_mth5_path.setter
375+
def remote_mth5_path(self, value):
376+
self._remote_mth5_path = self.set_path(value)
371377

372378
def has_remote_mth5(self):
373379
"""test if remote mth5 exists"""
@@ -392,7 +398,7 @@ def set_path(self, value):
392398
def from_run_summary(
393399
self,
394400
run_summary: RunSummary,
395-
local_station_id: str,
401+
local_station_id: Optional[Union(str, None)] = None,
396402
remote_station_id: Optional[Union[str, None]] = None,
397403
) -> None:
398404
"""
@@ -408,12 +414,14 @@ def from_run_summary(
408414
Label of the remote reference station
409415
410416
"""
411-
self.local_station_id = local_station_id
412-
self.remote_station_id = remote_station_id
417+
if local_station_id is not None:
418+
self.local_station_id = local_station_id
419+
if remote_station_id is not None:
420+
self.remote_station_id = remote_station_id
413421

414-
station_ids = [local_station_id]
422+
station_ids = [self.local_station_id]
415423
if remote_station_id:
416-
station_ids.append(remote_station_id)
424+
station_ids.append(self.remote_station_id)
417425
df = restrict_to_station_list(
418426
run_summary.df, station_ids, inplace=False
419427
)
@@ -428,20 +436,21 @@ def from_run_summary(
428436
df = self._add_columns(df)
429437

430438
# set remote reference
431-
if remote_station_id:
432-
cond = df.station == remote_station_id
439+
if self.remote_station_id:
440+
cond = df.station == self.remote_station_id
433441
df.remote = cond
434442

435443
# be sure to set date time columns and restrict to simultaneous runs
436444
df = self._set_datetime_columns(df)
437-
if remote_station_id:
445+
if self.remote_station_id:
438446
df = self.restrict_run_intervals_to_simultaneous(df)
439447

440448
# Again check df is non-empty
441449
if len(df) == 0:
442450
msg = (
443-
f"Local: {local_station_id} and remote: {remote_station_id} do "
444-
f"not overlap, Remote reference processing not a valid option."
451+
f"Local: {self.local_station_id} and remote: "
452+
f"{self.remote_station_id} do not overlap. Remote reference "
453+
"processing not a valid option."
445454
)
446455
logger.error(msg)
447456
raise ValueError(msg)

tests/processing/aurora/test_aurora_synthetic.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@
88
# =============================================================================
99
# Imports
1010
# =============================================================================
11-
from pathlib import Path
12-
import pandas as pd
11+
1312
import unittest
1413

1514
from mth5.data.make_mth5_from_asc import MTH5_PATH, create_test12rr_h5
1615
from mth5.utils.helpers import close_open_files
1716

1817
from mtpy.processing.run_summary import RunSummary
19-
20-
from mtpy.processing.kernel_dataset import intervals_overlap
21-
from mtpy.processing.kernel_dataset import overlap
2218
from mtpy.processing.kernel_dataset import KernelDataset
2319
from mtpy import MT
2420

0 commit comments

Comments
 (0)