From 4c46b543d32b0add5f1e5a488313356e579aaa8c Mon Sep 17 00:00:00 2001
From: Raphael <raphael.sturgis@gmail.com>
Date: Fri, 12 Nov 2021 14:09:11 +0100
Subject: [PATCH] basic features operation

---
 skais/ais/ais_trajectory.py                | 29 ++++++++++++++++++----
 skais/process/basic_features_operations.py |  4 +++
 2 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/skais/ais/ais_trajectory.py b/skais/ais/ais_trajectory.py
index ea9dad1..c3ea2a6 100644
--- a/skais/ais/ais_trajectory.py
+++ b/skais/ais/ais_trajectory.py
@@ -147,6 +147,7 @@ def angle_between_three_points(p1, p2, p3):
     else:
         return result
 
+
 def compute_point_angles(dat):
     angles = np.zeros(dat.shape[0])
 
@@ -193,7 +194,7 @@ def angle_dispersion(dat, radius):
 
     return disp
 
-@jit(nopython=True)
+
 def apply_func_on_window(dat, func, radius, on_edge='copy'):
     result = np.zeros(dat.shape)
     if on_edge == 'copy':
@@ -202,8 +203,15 @@ 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
-    else:
-        raise ValueError(f"{on_edge} not recognised, options are: ['copy']")
+
+
+def apply_time_sequence(dat, time, func):
+    result = np.empty(dat.shape[0])
+    result[0] = func(dat[0], dat[1], time[0], time[1])
+    for i in range(1, dat.shape[0]):
+        result[i] = func(dat[i-1], dat[i], time[i-1], time[i])
+    return result
+
 
 class AISTrajectory:
     def __init__(self, df, interpolation_time=None):
@@ -401,10 +409,21 @@ class AISTrajectory:
         return result
 
     def apply_func_on_time_window(self, func, radius, column, new_column=None):
-        dat = self.df[column]
+        dat = self.df[column].to_numpy()
         result = apply_func_on_window(dat, func, radius, on_edge='copy')
 
         if new_column is None:
             self.df[column] = result
         else:
-            self.df[new_column] = result
\ No newline at end of file
+            self.df[new_column] = result
+
+    def apply_time_sequence_func(self, func, column, new_column=None):
+        dat = self.df[column].to_numpy()
+        time = self.df['ts_sec'].to_numpy()
+
+        result = apply_time_sequence(dat, time, func)
+
+        if new_column is None:
+            self.df[column] = result
+        else:
+            self.df[new_column] = result
diff --git a/skais/process/basic_features_operations.py b/skais/process/basic_features_operations.py
index 1a805fa..c4c7d3b 100644
--- a/skais/process/basic_features_operations.py
+++ b/skais/process/basic_features_operations.py
@@ -40,3 +40,7 @@ def angular_time_derivative(angle1, angle2, ts1, ts2):
 
     z = z2 / z1
     return cmath.polar(z)[1] / (ts2 - ts1)
+
+
+def time_derivative(f1, f2, ts1, ts2):
+    return (f2 - f1) / (ts2 - ts1)
-- 
GitLab