diff --git a/skais/ais/ais_points.py b/skais/ais/ais_points.py index 3367cfd7e489cd913b5e0de2101eb35063ef7676..2a9df82a1241843d9df6647dba68eb9c43087d9e 100644 --- a/skais/ais/ais_points.py +++ b/skais/ais/ais_points.py @@ -36,9 +36,12 @@ from scipy.stats import stats class AISPoints: + + # Todo: Should be more elegant def __init__(self, df=None): if 'ts' in df: df['ts'] = pd.to_datetime(df.ts) + if 'mmsi' in df: df = df.sort_values(['mmsi', 'ts']) self.df = df diff --git a/skais/tests/ais/test_ais_points.py b/skais/tests/ais/test_ais_points.py index eea6ea7fa877f46d30d6fafd9426313285a4ade8..40fe6b0f1e04ba0493a80dbfead063f377bb98d3 100644 --- a/skais/tests/ais/test_ais_points.py +++ b/skais/tests/ais/test_ais_points.py @@ -9,12 +9,29 @@ from skais.ais.ais_trajectory import AISTrajectory class TestAISPositions(unittest.TestCase): + # Lazy test + def test_init(self): + ais_points = AISPoints( + pd.DataFrame( + { + "sog": [2, 3, 7, 15, 14, 12, 18, 25, 21, 12, 11, 16, 19, 2, 5, 15, 12, 7, 8, 9, 1], + "diff": [35, 45, 59, 12, 1, 2, 54, 5, 47, 86, 119, 68, 75, 54, 55, 12, 32, 62, 159, 157, 132], + "label": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], + "ts": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], + "mmsi": [0 for i in range(21)] + } + ) + ) + + self.assertIsNotNone(ais_points) + def test_describe(self): ais_points = AISPoints(pd.DataFrame( { "sog": [2, 3, 7, 15, 14, 12, 18, 25, 21, 12, 11, 16, 19, 2, 5, 15, 12, 7, 8, 9, 1], "diff": [35, 45, 59, 12, 1, 2, 54, 5, 47, 86, 119, 68, 75, 54, 55, 12, 32, 62, 159, 157, 132], "label": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], + "ts": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], "mmsi": [0 for i in range(21)] } )) @@ -29,7 +46,6 @@ class TestAISPositions(unittest.TestCase): 'average diff': 1271 / 21 }) - def test_remove_outliers_simple(self): ais_points = AISPoints(pd.DataFrame( { @@ -46,7 +62,6 @@ class TestAISPositions(unittest.TestCase): ais_points.remove_outliers(["cog", "heading"]) pd.testing.assert_frame_equal(expected.reset_index(drop=True), ais_points.df.reset_index(drop=True)) - def test_remove_outliers_rank(self): ais_points = AISPoints(pd.DataFrame( { @@ -91,7 +106,6 @@ class TestAISPositions(unittest.TestCase): with self.assertRaises(ValueError): ais_points.remove_outliers(["cog"], rank=0) - def test_clean_angles(self): ais_points = AISPoints(pd.DataFrame( { @@ -161,6 +175,22 @@ class TestAISPositions(unittest.TestCase): pd.testing.assert_frame_equal(expected.reset_index(drop=True), result.reset_index(drop=True), check_exact=False, rtol=0.05) + def test_normalize_raise(self): + ais_points = AISPoints(pd.DataFrame( + { + "cog": [i for i in range(0, 359, 10)], + "heading": [180 for i in range(0, 359, 10)] + } + ) + ) + + self.assertRaises( + ValueError, + ais_points.normalize, + ['cog', 'heading'], + normalization_type="non-existing-normalization" + ) + def test_compute_drift(self): ais_points = AISPoints(pd.DataFrame( @@ -226,11 +256,20 @@ class TestAISPositions(unittest.TestCase): # # np.testing.assert_array_equal(ground_truth, self.ais_points.histogram_y_knowing_x(x_nb_bins=3)) - # def test_load_from_csv(self): - # ais_points = AISPoints.load_from_csv("test_load_from_csv.csv") - # - # pd.testing.assert_frame_equal(ais_points.df, self.ais_points.df) + def test_load_from_csv(self): + result = AISPoints.load_from_csv("skais/tests/ais/test_load_from_csv.csv") + + ais_points = AISPoints(pd.DataFrame( + { + "sog": [2, 3, 7, 15, 14, 12, 18, 25, 21, 12, 11, 16, 19, 2, 5, 15, 12, 7, 8, 9, 1], + "diff": [35, 45, 59, 12, 1, 2, 54, 5, 47, 86, 119, 68, 75, 54, 55, 12, 32, 62, 159, 157, 132], + "label": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], + "ts": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], + "mmsi": [0 for i in range(21)] + } + )) + pd.testing.assert_frame_equal(result.df.reset_index(drop=True), ais_points.df.reset_index(drop=True)) # def test_histogram_x(self): # ground_truth = np.array([[5, 1, 3], @@ -241,13 +280,13 @@ class TestAISPositions(unittest.TestCase): # self.ais_points.histogram(features=["sog", "diff"], bins=3, # ranges=[[0, 30], [0, 180]])) - def test_fuse_single(self): ais_points = AISPoints(pd.DataFrame( { "sog": [2, 3, 7, 15, 14, 12, 18, 25, 21, 12, 11, 16, 19, 2, 5, 15, 12, 7, 8, 9, 1], "diff": [35, 45, 59, 12, 1, 2, 54, 5, 47, 86, 119, 68, 75, 54, 55, 12, 32, 62, 159, 157, 132], "label": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], + "ts": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], "mmsi": [0 for i in range(21)] } )) @@ -260,6 +299,7 @@ class TestAISPositions(unittest.TestCase): "sog": [2, 3, 7, 15, 14, 12, 18, 25, 21, 12, 11, 16, 19, 2, 5, 15, 12, 7, 8, 9, 1], "diff": [35, 45, 59, 12, 1, 2, 54, 5, 47, 86, 119, 68, 75, 54, 55, 12, 32, 62, 159, 157, 132], "label": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], + "ts": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], "mmsi": [0 for i in range(21)] } )) @@ -272,6 +312,7 @@ class TestAISPositions(unittest.TestCase): "sog": [2, 3, 7, 15, 14, 12, 18, 25, 21, 12, 11, 16, 19, 2, 5, 15, 12, 7, 8, 9, 1], "diff": [35, 45, 59, 12, 1, 2, 54, 5, 47, 86, 119, 68, 75, 54, 55, 12, 32, 62, 159, 157, 132], "label": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], + "ts": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "mmsi": [0 for i in range(21)] } )) @@ -284,9 +325,13 @@ class TestAISPositions(unittest.TestCase): 59, 12, 1, 2, 54, 5, 47, 86, 119, 68, 75, 54, 55, 12, 32, 62, 159, 157, 132], "label": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], + "ts": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "mmsi": [0 for i in range(42)] } ) + + value['ts'] = pd.to_datetime(value.ts) pd.testing.assert_frame_equal(AISPoints.fuse(ais_points, ais_points).df.reset_index(drop=True), value.reset_index(drop=True)) # @@ -354,7 +399,6 @@ class TestAISPositions(unittest.TestCase): # for r, e in zip(result, expected): # pd.testing.assert_frame_equal(e.reset_index(drop=True), r.df.reset_index(drop=True)) - # def test_disjointed_histogram_label_none(self): # ais_points = AISPoints(pd.DataFrame( # { @@ -425,5 +469,3 @@ class TestAISPositions(unittest.TestCase): # # for r, e in zip(result, expected): # np.testing.assert_array_equal(e, r[0]) - - diff --git a/skais/tests/ais/test_load_from_csv.csv b/skais/tests/ais/test_load_from_csv.csv index c0de7772d0f91aeabdf5f6b43ffe92ffa24092f4..0b13d5a623b95755e49b1a787b54f67a568d0703 100644 --- a/skais/tests/ais/test_load_from_csv.csv +++ b/skais/tests/ais/test_load_from_csv.csv @@ -1,22 +1,22 @@ -sog,diff,label,mmsi -2,35,0,0 -3,45,0,0 -7,59,0,0 -15,12,0,0 -14,1,0,0 -12,2,0,0 -18,54,0,0 -25,5,0,0 -21,47,0,0 -12,86,0,0 -11,119,0,0 -16,68,0,0 -19,75,0,0 -2,54,1,0 -5,55,1,0 -15,12,1,0 -12,32,1,0 -7,62,1,0 -8,159,1,0 -9,157,1,0 -1,132,1,0 \ No newline at end of file +sog,diff,label,ts,mmsi +2,35,0,0,0 +3,45,0,0,0 +7,59,0,0,0 +15,12,0,0,0 +14,1,0,0,0 +12,2,0,0,0 +18,54,0,0,0 +25,5,0,0,0 +21,47,0,0,0 +12,86,0,0,0 +11,119,0,0,0 +16,68,0,0,0 +19,75,0,0,0 +2,54,1,1,0 +5,55,1,1,0 +15,12,1,1,0 +12,32,1,1,0 +7,62,1,1,0 +8,159,1,1,0 +9,157,1,1,0 +1,132,1,1,0 \ No newline at end of file