From 6b9803bff39134f6b9a42c56aacf33ee7c6c0622 Mon Sep 17 00:00:00 2001
From: pcain-usgs <pcain@usgs.gov>
Date: Tue, 16 Jun 2020 15:08:42 -0600
Subject: [PATCH] adapt test, print to stderr

---
 etc/filter/BOU20200101vsec.sec              | 90 ---------------------
 geomagio/algorithm/FilterAlgorithm.py       | 14 ++--
 test/algorithm_test/FilterAlgorithm_test.py | 46 ++++++++---
 3 files changed, 44 insertions(+), 106 deletions(-)

diff --git a/etc/filter/BOU20200101vsec.sec b/etc/filter/BOU20200101vsec.sec
index 017a6a34a..402b43c87 100644
--- a/etc/filter/BOU20200101vsec.sec
+++ b/etc/filter/BOU20200101vsec.sec
@@ -16,51 +16,6 @@
  # through INTERMAGNET and acknowledgement templates can be found at |
  # www.intermagnet.org                                               |
 DATE       TIME         DOY     BOUH      BOUE      BOUZ      BOUF   |
-2019-12-31 23:59:15.000 365     20826.85    -86.84  46874.64  51815.07
-2019-12-31 23:59:16.000 365     20826.84    -86.84  46874.65  51815.07
-2019-12-31 23:59:17.000 365     20826.83    -86.83  46874.64  51815.09
-2019-12-31 23:59:18.000 365     20826.83    -86.84  46874.64  51815.06
-2019-12-31 23:59:19.000 365     20826.83    -86.83  46874.63  51815.08
-2019-12-31 23:59:20.000 365     20826.82    -86.83  46874.64  51815.08
-2019-12-31 23:59:21.000 365     20826.83    -86.83  46874.64  51815.09
-2019-12-31 23:59:22.000 365     20826.81    -86.83  46874.64  51815.06
-2019-12-31 23:59:23.000 365     20826.83    -86.82  46874.63  51815.06
-2019-12-31 23:59:24.000 365     20826.83    -86.80  46874.63  51815.06
-2019-12-31 23:59:25.000 365     20826.82    -86.81  46874.63  51815.07
-2019-12-31 23:59:26.000 365     20826.83    -86.80  46874.62  51815.08
-2019-12-31 23:59:27.000 365     20826.83    -86.79  46874.63  51815.05
-2019-12-31 23:59:28.000 365     20826.82    -86.79  46874.61  51815.05
-2019-12-31 23:59:29.000 365     20826.82    -86.78  46874.62  51815.06
-2019-12-31 23:59:30.000 365     20826.82    -86.79  46874.62  51815.05
-2019-12-31 23:59:31.000 365     20826.83    -86.80  46874.63  51815.04
-2019-12-31 23:59:32.000 365     20826.82    -86.79  46874.63  51815.06
-2019-12-31 23:59:33.000 365     20826.82    -86.78  46874.64  51815.05
-2019-12-31 23:59:34.000 365     20826.80    -86.80  46874.62  51815.06
-2019-12-31 23:59:35.000 365     20826.81    -86.80  46874.61  51815.06
-2019-12-31 23:59:36.000 365     20826.81    -86.78  46874.62  51815.05
-2019-12-31 23:59:37.000 365     20826.82    -86.77  46874.64  51815.05
-2019-12-31 23:59:38.000 365     20826.80    -86.79  46874.63  51815.04
-2019-12-31 23:59:39.000 365     20826.81    -86.79  46874.63  51815.04
-2019-12-31 23:59:40.000 365     20826.81    -86.79  46874.62  51815.04
-2019-12-31 23:59:41.000 365     20826.81    -86.78  46874.63  51815.04
-2019-12-31 23:59:42.000 365     20826.80    -86.80  46874.59  51815.04
-2019-12-31 23:59:43.000 365     20826.80    -86.79  46874.61  51815.06
-2019-12-31 23:59:44.000 365     20826.78    -86.79  46874.61  51815.04
-2019-12-31 23:59:45.000 365     20826.79    -86.79  46874.62  51815.06
-2019-12-31 23:59:46.000 365     20826.80    -86.77  46874.62  51815.05
-2019-12-31 23:59:47.000 365     20826.80    -86.77  46874.62  51815.07
-2019-12-31 23:59:48.000 365     20826.80    -86.78  46874.60  51815.07
-2019-12-31 23:59:49.000 365     20826.82    -86.76  46874.62  51815.06
-2019-12-31 23:59:50.000 365     20826.83    -86.77  46874.63  51815.06
-2019-12-31 23:59:51.000 365     20826.83    -86.77  46874.62  51815.06
-2019-12-31 23:59:52.000 365     20826.81    -86.77  46874.61  51815.04
-2019-12-31 23:59:53.000 365     20826.82    -86.75  46874.62  51815.05
-2019-12-31 23:59:54.000 365     20826.82    -86.74  46874.64  51815.05
-2019-12-31 23:59:55.000 365     20826.82    -86.75  46874.62  51815.04
-2019-12-31 23:59:56.000 365     20826.83    -86.75  46874.63  51815.04
-2019-12-31 23:59:57.000 365     20826.82    -86.75  46874.61  51815.05
-2019-12-31 23:59:58.000 365     20826.84    -86.76  46874.61  51815.06
-2019-12-31 23:59:59.000 365     20826.84    -86.75  46874.62  51815.03
 2020-01-01 00:00:00.000 001     20826.85    -86.75  46874.62  51815.05
 2020-01-01 00:00:01.000 001     20826.85    -86.74  46874.64  51815.03
 2020-01-01 00:00:02.000 001     20826.83    -86.75  46874.61  51815.05
@@ -962,48 +917,3 @@ DATE       TIME         DOY     BOUH      BOUE      BOUZ      BOUF   |
 2020-01-01 00:14:58.000 001     20826.46    -86.09  46874.35  51814.68
 2020-01-01 00:14:59.000 001     20826.47    -86.08  46874.36  51814.68
 2020-01-01 00:15:00.000 001     20826.46    -86.10  46874.36  51814.68
-2020-01-01 00:15:01.000 001     20826.45    -86.07  46874.38  51814.68
-2020-01-01 00:15:02.000 001     20826.43    -86.08  46874.36  51814.67
-2020-01-01 00:15:03.000 001     20826.45    -86.09  46874.37  51814.69
-2020-01-01 00:15:04.000 001     20826.46    -86.08  46874.38  51814.68
-2020-01-01 00:15:05.000 001     20826.46    -86.09  46874.35  51814.68
-2020-01-01 00:15:06.000 001     20826.46    -86.09  46874.35  51814.68
-2020-01-01 00:15:07.000 001     20826.44    -86.09  46874.35  51814.66
-2020-01-01 00:15:08.000 001     20826.44    -86.11  46874.36  51814.70
-2020-01-01 00:15:09.000 001     20826.44    -86.09  46874.37  51814.68
-2020-01-01 00:15:10.000 001     20826.44    -86.11  46874.35  51814.68
-2020-01-01 00:15:11.000 001     20826.43    -86.10  46874.38  51814.68
-2020-01-01 00:15:12.000 001     20826.41    -86.11  46874.36  51814.69
-2020-01-01 00:15:13.000 001     20826.41    -86.13  46874.38  51814.68
-2020-01-01 00:15:14.000 001     20826.40    -86.13  46874.37  51814.66
-2020-01-01 00:15:15.000 001     20826.43    -86.12  46874.39  51814.69
-2020-01-01 00:15:16.000 001     20826.42    -86.13  46874.40  51814.66
-2020-01-01 00:15:17.000 001     20826.39    -86.15  46874.39  51814.68
-2020-01-01 00:15:18.000 001     20826.42    -86.14  46874.38  51814.68
-2020-01-01 00:15:19.000 001     20826.38    -86.14  46874.38  51814.67
-2020-01-01 00:15:20.000 001     20826.38    -86.14  46874.39  51814.66
-2020-01-01 00:15:21.000 001     20826.38    -86.15  46874.37  51814.67
-2020-01-01 00:15:22.000 001     20826.38    -86.17  46874.39  51814.68
-2020-01-01 00:15:23.000 001     20826.38    -86.15  46874.40  51814.66
-2020-01-01 00:15:24.000 001     20826.36    -86.16  46874.40  51814.68
-2020-01-01 00:15:25.000 001     20826.39    -86.15  46874.41  51814.67
-2020-01-01 00:15:26.000 001     20826.39    -86.16  46874.42  51814.66
-2020-01-01 00:15:27.000 001     20826.37    -86.16  46874.39  51814.67
-2020-01-01 00:15:28.000 001     20826.38    -86.15  46874.39  51814.66
-2020-01-01 00:15:29.000 001     20826.35    -86.16  46874.37  51814.67
-2020-01-01 00:15:30.000 001     20826.35    -86.14  46874.38  51814.68
-2020-01-01 00:15:31.000 001     20826.35    -86.16  46874.37  51814.68
-2020-01-01 00:15:32.000 001     20826.37    -86.15  46874.39  51814.67
-2020-01-01 00:15:33.000 001     20826.36    -86.17  46874.36  51814.66
-2020-01-01 00:15:34.000 001     20826.34    -86.17  46874.37  51814.64
-2020-01-01 00:15:35.000 001     20826.35    -86.17  46874.36  51814.64
-2020-01-01 00:15:36.000 001     20826.36    -86.16  46874.37  51814.66
-2020-01-01 00:15:37.000 001     20826.34    -86.17  46874.37  51814.64
-2020-01-01 00:15:38.000 001     20826.35    -86.19  46874.36  51814.65
-2020-01-01 00:15:39.000 001     20826.35    -86.17  46874.37  51814.66
-2020-01-01 00:15:40.000 001     20826.36    -86.18  46874.38  51814.64
-2020-01-01 00:15:41.000 001     20826.36    -86.19  46874.38  51814.64
-2020-01-01 00:15:42.000 001     20826.35    -86.19  46874.38  51814.66
-2020-01-01 00:15:43.000 001     20826.35    -86.19  46874.38  51814.64
-2020-01-01 00:15:44.000 001     20826.37    -86.19  46874.39  51814.64
-2020-01-01 00:15:45.000 001     20826.38    -86.19  46874.40  51814.66
diff --git a/geomagio/algorithm/FilterAlgorithm.py b/geomagio/algorithm/FilterAlgorithm.py
index f45ae20eb..28a031e58 100644
--- a/geomagio/algorithm/FilterAlgorithm.py
+++ b/geomagio/algorithm/FilterAlgorithm.py
@@ -76,11 +76,11 @@ class FilterAlgorithm(Algorithm):
                 "name": "name" in data and data["name"] or "custom",
                 "input_sample_period": self.input_sample_period,
                 "output_sample_period": self.output_sample_period,
-                "window": window,
+                "window": data["window"],
             }
         ]
         # ensure correctly aligned coefficients in each step
-        self.steps = [_prepare_step(step) for step in steps]
+        self.steps = [self._prepare_step(step) for step in self.steps]
 
     def save_state(self):
         """Save algorithm state to a file.
@@ -111,12 +111,14 @@ class FilterAlgorithm(Algorithm):
                     steps.append(step)
         return steps
 
-    def _prepare_step(step) -> Dict:
-        if len(step["window"]) % 2 == 1:
+    def _prepare_step(self, step) -> Dict:
+        window = step["window"]
+        if len(window) % 2 == 1:
             return step
-        new_window = numpy.array(window)
+        sys.stderr.write("Even number of taps. Appending center coefficient.")
+        new_window = np.array(window)
         i = len(window) // 2
-        numpy.insert(new_window, i + 1, numpy.average(window[i : i + 2]))
+        np.insert(new_window, i + 1, np.average(window[i : i + 2]))
         new_step = dict(step)
         new_step["window"] = new_window
         return new_step
diff --git a/test/algorithm_test/FilterAlgorithm_test.py b/test/algorithm_test/FilterAlgorithm_test.py
index 68bde7e21..2a74eef27 100644
--- a/test/algorithm_test/FilterAlgorithm_test.py
+++ b/test/algorithm_test/FilterAlgorithm_test.py
@@ -2,6 +2,7 @@ from geomagio.algorithm import FilterAlgorithm
 from obspy.core import read
 import geomagio.iaga2002 as i2
 from numpy.testing import assert_almost_equal
+import numpy as np
 
 
 def test_second():
@@ -181,7 +182,6 @@ def test_starttime_shift():
     # generation of BOU20200101vsec.sec
     # starttime = UTCDateTime('2020-01-01T00:00:00Z')
     # endtime = UTCDateTime('2020-01-01T00:15:00Z')
-    # starttime,endtime = f.get_input_interval(starttime,endtime)
     # bou = e.get_timeseries(observatory='BOU',interval='second',type='variation',starttime=starttime,endtime=endtime,channels=["H","E","Z","F"])
     # with open('BOU20200101vsec.sec','wb') as file:
     #     i2w.write(out=file,timeseries=bou,channels=["H","E","Z","F"])
@@ -191,12 +191,38 @@ def test_starttime_shift():
     # gather starttime and endtime from stream
     starttime = bou[0].stats.starttime
     endtime = bou[0].stats.endtime
-    # apply offset of 16 seconds to starttime to generate misaligned stream
-    bou_misaligned = bou.trim(starttime=starttime + 16, endtime=endtime)
-    # process both streams with FilterAlgorithm
-    bou_filtered = f.process(bou)
-    bou_misaligned_filtered = f.process(bou_misaligned)
-    # compare starttimes and dimensions of data to detect correct offset in output
-    len(bou_filtered[0].data) - len(bou_misaligned_filtered[0].data) == (
-        bou_misaligned_filtered[0].stats.starttime - bou_filtered[0].stats.starttime
-    ) / f.output_sample_period
+    bou_expected = f.process(bou)
+    bou_padded = bou.trim(starttime=starttime + 15, endtime=endtime - 45)
+    bou_padded_f = f.process(bou_padded)
+    bou_misaligned = bou_padded.trim(starttime=starttime + 16, endtime=endtime - 16)
+    bou_misaligned_f = f.process(bou_misaligned)
+
+    # check starttime allignment
+    (bou_expected[0].stats.starttime, bou_expected[0].stats.endtime) == (
+        bou_padded_f[0].stats.starttime,
+        bou_padded_f[0].stats.endtime,
+    )
+    # check offset in misaligned trace's output
+    len(bou_expected[0].data) == len(bou_misaligned_f[0]) + 2
+
+
+def test_even_taps():
+    """algorithm_test.FilterAlgorithm_test.test_custom()
+    Tests algorithm for 10Hz to second with custom filter coefficients.
+    """
+    f = FilterAlgorithm(
+        input_sample_period=0.1,
+        output_sample_period=1.0,
+        coeff_filename="etc/filter/coeffs.json",
+    )
+    # gather original's window length
+    window = f.steps[0]["window"]
+    original_length = len(window)
+    # remove center coefficient from original window
+    f.steps[0]["window"] = np.delete(window, original_length // 2, 0)
+    # check for even taps in steps' windows, add center coefficient
+    f.steps = [f._prepare_step(step) for step in f.steps]
+    # gather result's window length
+    result_length = len(f.steps[0]["window"])
+    # verify insertion of center coefficient
+    original_length == result_length
-- 
GitLab