From 46b5cb9a6e413e034c1395e772ad0b515ce448ba Mon Sep 17 00:00:00 2001
From: pcain-usgs <pcain@usgs.gov>
Date: Thu, 10 Sep 2020 12:21:21 -0600
Subject: [PATCH] Refactor _validate_step

---
 geomagio/algorithm/FilterAlgorithm.py       | 10 +++++-----
 test/algorithm_test/FilterAlgorithm_test.py | 18 ++++++++----------
 2 files changed, 13 insertions(+), 15 deletions(-)

diff --git a/geomagio/algorithm/FilterAlgorithm.py b/geomagio/algorithm/FilterAlgorithm.py
index 1f1d30ae3..afcefbf4a 100644
--- a/geomagio/algorithm/FilterAlgorithm.py
+++ b/geomagio/algorithm/FilterAlgorithm.py
@@ -114,7 +114,8 @@ class FilterAlgorithm(Algorithm):
         self.load_state()
         #  ensure correctly aligned coefficients in each step
         self.steps = self.steps or []
-        self._validate_steps()
+        for step in self.steps:
+            self._validate_step(step)
 
     def load_state(self):
         """Load filter coefficients from json file if custom filter is used.
@@ -173,11 +174,10 @@ class FilterAlgorithm(Algorithm):
                 steps.append(step)
         return steps
 
-    def _validate_steps(self):
+    def _validate_step(self, step):
         """Verifies whether or not firfirlter steps have an odd number of coefficients"""
-        for step in self.steps:
-            if step["type"] == "firfilter" and len(step["window"]) % 2 != 1:
-                raise ValueError("Firfilter requires an odd number of coefficients")
+        if step["type"] == "firfilter" and len(step["window"]) % 2 != 1:
+            raise ValueError("Firfilter requires an odd number of coefficients")
 
     def can_produce_data(self, starttime, endtime, stream):
         """Can Produce data
diff --git a/test/algorithm_test/FilterAlgorithm_test.py b/test/algorithm_test/FilterAlgorithm_test.py
index 2dcb627e3..21558ef57 100644
--- a/test/algorithm_test/FilterAlgorithm_test.py
+++ b/test/algorithm_test/FilterAlgorithm_test.py
@@ -292,7 +292,7 @@ def test_align_trace():
     assert_equal(filtered[0].stats.endtime, UTCDateTime("2020-08-31T03:29:30"))
 
 
-def test_validate_steps():
+def test_validate_step():
     """algorithm_test.FilterAlgorithm_test.test_validate_steps()
     Validates algorithm steps 10 Hz to second with custom coefficients.
     """
@@ -303,14 +303,12 @@ def test_validate_steps():
     half = numtaps // 2
     # check initial assumption
     assert_equal(numtaps % 2, 1)
-    f._validate_steps()
+    f._validate_step(step)
     # expect step to raise a value error when window has an even length
-    f.steps = [
-        {
-            "window": np.delete(step["window"], numtaps // 2, 0),
-            "type": "firfilter",
-        }
-    ]
-    assert_equal(len(f.steps[0]["window"]) % 2, 0)
+    step = {
+        "window": np.delete(step["window"], numtaps // 2, 0),
+        "type": "firfilter",
+    }
+    assert_equal(len(step["window"]) % 2, 0)
     with pytest.raises(ValueError):
-        f._validate_steps()
+        f._validate_step(step)
-- 
GitLab