From 58ca07aa93b6a1e670fd3b761d504ddafbd32008 Mon Sep 17 00:00:00 2001
From: Raphael <raphael.sturgis@gmail.com>
Date: Tue, 22 Feb 2022 10:30:48 +0100
Subject: [PATCH] added ignore for apply_func_on_time_window_function

---
 skais/ais/ais_trajectory.py            | 11 +++++++++--
 skais/tests/ais/test_ais_trajectory.py | 14 ++++++++++++++
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/skais/ais/ais_trajectory.py b/skais/ais/ais_trajectory.py
index 5b2af98..709440b 100644
--- a/skais/ais/ais_trajectory.py
+++ b/skais/ais/ais_trajectory.py
@@ -39,6 +39,13 @@ def apply_func_on_window(dat, func, radius, on_edge='copy'):
             data = dat[i - radius:i + radius + 1]
             result[i - radius] = func(data)
         return result
+    elif on_edge == 'ignore':
+        for i in range(0, dat.shape[0]):
+            lower_bound = max(0, i-radius)
+            upper_bound = min(dat.shape[0], i + radius + 1)
+            data = dat[lower_bound:upper_bound]
+            result[i] = func(data)
+        return result
     else:
         raise ValueError
 
@@ -96,9 +103,9 @@ class AISTrajectory(AISPoints):
 
         return result
 
-    def apply_func_on_time_window(self, func, radius, column, new_column=None):
+    def apply_func_on_time_window(self, func, radius, column, new_column=None, on_edge='copy'):
         dat = self.df[column].to_numpy()
-        result = apply_func_on_window(dat, func, radius, on_edge='copy')
+        result = apply_func_on_window(dat, func, radius, on_edge)
 
         if new_column is None:
             self.df[column] = result
diff --git a/skais/tests/ais/test_ais_trajectory.py b/skais/tests/ais/test_ais_trajectory.py
index 1ee2d6d..d4bb0d6 100644
--- a/skais/tests/ais/test_ais_trajectory.py
+++ b/skais/tests/ais/test_ais_trajectory.py
@@ -320,3 +320,17 @@ class TestAISTrajectory(unittest.TestCase):
 
     def test_apply_func_on_window(self):
         self.assertRaises(ValueError, apply_func_on_window,np.arange(10), 0, 0, 'not valid string')
+
+    def test_apply_func_on_window_ignore(self):
+        result = apply_func_on_window(np.arange(10), np.mean, 2, 'ignore')
+
+        expected = np.array([1, 1.5, 2, 3, 4, 5, 6, 7, 7.5, 8])
+
+        np.testing.assert_equal(result, expected)
+
+    def test_apply_func_on_window_ignore_short(self):
+        result = apply_func_on_window(np.arange(5), np.mean, 10, 'ignore')
+
+        expected = np.array([2, 2, 2, 2, 2])
+
+        np.testing.assert_equal(result, expected)
\ No newline at end of file
-- 
GitLab