From d22e983b94ef988ed06e1a9c66af23da16aca4be Mon Sep 17 00:00:00 2001 From: pcain-usgs <pcain@usgs.gov> Date: Wed, 10 Mar 2021 10:34:10 -0700 Subject: [PATCH] Default algorithm's matrix to identity with no statefile --- geomagio/adjusted/AdjustedMatrix.py | 2 +- geomagio/algorithm/AdjustedAlgorithm.py | 7 +++-- test/algorithm_test/AdjustedAlgorithm_test.py | 28 +++++++++++++++---- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/geomagio/adjusted/AdjustedMatrix.py b/geomagio/adjusted/AdjustedMatrix.py index b5513877f..44ca88a77 100644 --- a/geomagio/adjusted/AdjustedMatrix.py +++ b/geomagio/adjusted/AdjustedMatrix.py @@ -23,7 +23,7 @@ class AdjustedMatrix(BaseModel): """ matrix: Optional[Any] = None - pier_correction: Optional[float] = None + pier_correction: float = 0 metrics: Optional[List[Metric]] = None starttime: Optional[UTCDateTime] = None endtime: Optional[UTCDateTime] = None diff --git a/geomagio/algorithm/AdjustedAlgorithm.py b/geomagio/algorithm/AdjustedAlgorithm.py index 9782e9713..d103e2d22 100644 --- a/geomagio/algorithm/AdjustedAlgorithm.py +++ b/geomagio/algorithm/AdjustedAlgorithm.py @@ -43,7 +43,11 @@ class AdjustedAlgorithm(Algorithm): """Load algorithm state from a file. File name is self.statefile. """ + # Adjusted matrix defaults to identity matrix + matrix_size = len([c for c in self.get_input_channels() if c != "F"]) + 1 + matrix = np.eye(matrix_size) if self.statefile is None: + self.matrix = AdjustedMatrix(matrix=matrix) return try: with open(self.statefile, "r") as f: @@ -51,12 +55,9 @@ class AdjustedAlgorithm(Algorithm): data = json.loads(data) except IOError as err: raise FileNotFoundError("statefile not found") - # Adjusted matrix defaults to identity matrix - matrix_size = len([c for c in self.get_input_channels() if c != "F"]) + 1 if "pier_correction" in data: self.matrix = AdjustedMatrix(**data) elif "PC" in data: - matrix = np.eye(matrix_size) # read data from legacy format for row in range(matrix_size): for col in range(matrix_size): diff --git a/test/algorithm_test/AdjustedAlgorithm_test.py b/test/algorithm_test/AdjustedAlgorithm_test.py index 98eba78b4..220d498c6 100644 --- a/test/algorithm_test/AdjustedAlgorithm_test.py +++ b/test/algorithm_test/AdjustedAlgorithm_test.py @@ -1,7 +1,7 @@ from geomagio.adjusted import AdjustedMatrix from geomagio.algorithm import AdjustedAlgorithm import geomagio.iaga2002 as i2 -from numpy.testing import assert_almost_equal, assert_equal +from numpy.testing import assert_almost_equal, assert_array_equal, assert_equal def test_construct(): @@ -24,7 +24,7 @@ def assert_streams_almost_equal(adjusted, expected, channels): def test_process_XYZF_AdjustedMatrix(): - """algorithm_test.AdjustedAlgorithm_test.test_process() + """algorithm_test.AdjustedAlgorithm_test.test_process_XYZF_AdjustedMatrix() Check adjusted data processing versus files generated from original script @@ -72,7 +72,7 @@ def test_process_XYZF_AdjustedMatrix(): def test_process_reverse_polarity_AdjustedMatrix(): - """algorithm_test.AdjustedAlgorithm_test.test_process() + """algorithm_test.AdjustedAlgorithm_test.test_process_reverse_polarity_AdjustedMatrix() Check adjusted data processing versus files generated from original script. Tests reverse polarity martix. @@ -106,7 +106,7 @@ def test_process_reverse_polarity_AdjustedMatrix(): def test_process_XYZF_statefile(): - """algorithm_test.AdjustedAlgorithm_test.test_process() + """algorithm_test.AdjustedAlgorithm_test.test_process_XYZF_statefile() Check adjusted data processing versus files generated from original script @@ -131,7 +131,7 @@ def test_process_XYZF_statefile(): def test_process_reverse_polarity_statefile(): - """algorithm_test.AdjustedAlgorithm_test.test_process() + """algorithm_test.AdjustedAlgorithm_test.test_process_reverse_polarity_statefile() Check adjusted data processing versus files generated from original script. Tests reverse polarity martix. @@ -157,3 +157,21 @@ def test_process_reverse_polarity_statefile(): assert_streams_almost_equal( adjusted=adjusted, expected=expected, channels=["H", "E"] ) + + +def test_process_no_statefile(): + """algorithm_test.AdjustedAlgorithm_test.test_process_no_statefile() + + Check adjusted data processing versus raw data + + Uses default AdjustedMatrix with identity transform + """ + # initialize adjusted algorithm with no statefile + a = AdjustedAlgorithm() + # load boulder Jan 16 files from /etc/ directory + with open("etc/adjusted/BOU201601vmin.min") as f: + raw = i2.IAGA2002Factory().parse_string(f.read()) + # process hezf (raw) channels with identity transform + adjusted = a.process(raw) + for i in range(len(adjusted)): + assert_array_equal(adjusted[i].data, raw[i].data) -- GitLab