diff --git a/skais/ais/ais_trajectory.py b/skais/ais/ais_trajectory.py index 37d58bb4802d4209d1b50f22c1199c99080ba1d4..8ac8a11726c6ca62773e46fffcbf28399792ee8c 100644 --- a/skais/ais/ais_trajectory.py +++ b/skais/ais/ais_trajectory.py @@ -153,7 +153,8 @@ def generate_points_with_features(data, positions, features_vectors, bounds, nod @jit(nopython=True) -def generate_links(data, positions, height, width, lower_lat, upper_lat, lower_lon, upper_lon, values, interpolate=False): +def generate_links(data, positions, height, width, lower_lat, upper_lat, lower_lon, upper_lon, values, + link_type=None): lon, lat = positions[0, 0], positions[0, 1] current_value = values[0] @@ -164,19 +165,24 @@ def generate_links(data, positions, height, width, lower_lat, upper_lat, lower_l x_nxt, y_nxt = get_coord(latitude, longitude, height, width, lower_lat, upper_lat, lower_lon, upper_lon) lon, lat = longitude, latitude - if interpolate and (nxt_value != current_value).all() and (x_prv != x_nxt) and (y_prev!= y_nxt): - dist = dist_on_grid(x_prv, y_prev, x_nxt, y_nxt) - for x, y in bresenham(x_prv, y_prev, x_nxt, y_nxt): - dist_prev = dist_on_grid(x_prv, y_prev, x, y) - dist_next = dist_on_grid(x, y, x_nxt, y_nxt) - - pixel_color = current_value * (1-dist_prev/dist) + nxt_value * (1-dist_next/dist) - for i in range(len(pixel_color)): + + dist = dist_on_grid(x_prv, y_prev, x_nxt, y_nxt) + for x, y in bresenham(x_prv, y_prev, x_nxt, y_nxt): + dist_prev = dist_on_grid(x_prv, y_prev, x, y) + dist_next = dist_on_grid(x, y, x_nxt, y_nxt) + + pixel_color = current_value * (1 - dist_prev / dist) + nxt_value * (1 - dist_next / dist) + for i in range(len(pixel_color)): + if nxt_value[i] == current_value[i]: + data[x, y, i] = current_value[i] + elif link_type == 'interpolate': data[x, y, i] = pixel_color[i] - else: - for x, y in bresenham(x_prv, y_prev, x_nxt, y_nxt): - for i in range(len(current_value)): + elif link_type == 'previous': data[x, y, i] = current_value[i] + elif link_type == 'next': + data[x, y, i] = nxt_value[i] + elif link_type == 'solid': + data[x, y, i] = 1 current_value = nxt_value @@ -358,8 +364,8 @@ class AISTrajectory(AISPoints): result.append((row['ts_sec'], current_label)) return result - def generate_array_from_positions(self, height=256, width=256, link=True, bounding_box='fit', ref_index=-1, - features=None, node_size=0, interpolation=False): + def generate_array_from_positions(self, height=256, width=256, link=None, bounding_box='fit', ref_index=-1, + features=None, node_size=0): positions = self.df[['longitude', 'latitude']].to_numpy() @@ -393,14 +399,15 @@ class AISTrajectory(AISPoints): generate_points(data, positions, height, width, node_size, lower_lat, upper_lat, lower_lon, upper_lon) - if link: + if link is not None: generate_links(data, positions, height, width, lower_lat, upper_lat, lower_lon, upper_lon, - np.ones((len(positions), 1))) + np.ones((len(positions), 1)), link) else: generate_points_with_features(data, positions, features_vectors, np.array(bounds), node_size, height, width, lower_lat, upper_lat, lower_lon, upper_lon) - if link: + if link is not None: generate_links(data, positions, height, width, lower_lat, upper_lat, lower_lon, upper_lon, - generate_values(features_vectors, bounds), interpolate=interpolation) + generate_values(features_vectors, bounds), link) + return data diff --git a/skais/tests/.numba_config.yaml b/skais/tests/.numba_config.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c4f93e3317c44da50bbab1b1662221be650cb761 --- /dev/null +++ b/skais/tests/.numba_config.yaml @@ -0,0 +1 @@ +DISABLE_JIT: 1 \ No newline at end of file diff --git a/skais/tests/ais/test_ais_trajectory.py b/skais/tests/ais/test_ais_trajectory.py index c491f48c8f0e83a0715b58c6cba655b582677665..7b377d24d74473eac0789c1402aed05c9a64e4f5 100644 --- a/skais/tests/ais/test_ais_trajectory.py +++ b/skais/tests/ais/test_ais_trajectory.py @@ -1,3 +1,4 @@ +import numba import unittest from skais.ais.ais_trajectory import * @@ -311,12 +312,12 @@ class TestAISTrajectory(unittest.TestCase): def test_compute_trajectory(self): times = np.array([i for i in range(0, 3001, 600)] + [i for i in range(4001, 7001, 600)]) - self.assertEqual(6, compute_trajectory.py_func(times, 800)) + self.assertEqual(6, compute_trajectory(times, 800)) def test_compute_trajectory_empty(self): times = np.array([]) - self.assertEqual(0, compute_trajectory.py_func(times, 800)) + self.assertEqual(0, compute_trajectory(times, 800)) def test_apply_func_on_window(self): self.assertRaises(ValueError, apply_func_on_window, np.arange(10), 0, 0, 'not valid string') @@ -394,7 +395,7 @@ class TestAISTrajectoryImageGeneration(unittest.TestCase): ) def test_generate_array_from_positions(self): - result = self.trajectory.generate_array_from_positions(height=9, width=9, link=False, bounding_box='fit', + result = self.trajectory.generate_array_from_positions(height=9, width=9, link=None, bounding_box='fit', features=None, node_size=0).reshape((9, 9)) expected = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0], @@ -409,7 +410,7 @@ class TestAISTrajectoryImageGeneration(unittest.TestCase): np.testing.assert_array_equal(result, expected) def test_generate_array_from_positions_node_size(self): - result = self.trajectory.generate_array_from_positions(height=9, width=9, link=False, bounding_box='fit', + result = self.trajectory.generate_array_from_positions(height=9, width=9, link=None, bounding_box='fit', features=None, node_size=1).reshape((9, 9)) expected = np.array([[0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 1, 1], @@ -434,7 +435,7 @@ class TestAISTrajectoryImageGeneration(unittest.TestCase): ) ) - result = trajectory.generate_array_from_positions(height=9, width=18, link=True, bounding_box='fit', + result = trajectory.generate_array_from_positions(height=9, width=18, link='solid', bounding_box='fit', features=None, node_size=0).reshape((9, 18)) expected = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], @@ -510,7 +511,7 @@ class TestAISTrajectoryImageGeneration(unittest.TestCase): ) ) - result = trajectory.generate_array_from_positions(height=9, width=18, link=True, bounding_box='fit', + result = trajectory.generate_array_from_positions(height=9, width=18, link='previous', bounding_box='fit', features="sog", node_size=0).reshape((9, 18)) expected = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5], @@ -537,7 +538,7 @@ class TestAISTrajectoryImageGeneration(unittest.TestCase): ) ) - result = trajectory.generate_array_from_positions(height=9, width=18, link=True, bounding_box='fit', + result = trajectory.generate_array_from_positions(height=9, width=18, link='previous', bounding_box='fit', features=['sog', 'cog'], node_size=0) expected = np.array([[[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.5, 0.25]], @@ -561,7 +562,7 @@ class TestAISTrajectoryImageGeneration(unittest.TestCase): np.testing.assert_array_equal(result, expected) def test_generate_array_centered(self): - result = self.trajectory.generate_array_from_positions(height=9, width=9, link=False, bounding_box='centered', + result = self.trajectory.generate_array_from_positions(height=9, width=9, link=None, bounding_box='centered', features=None, node_size=0).reshape((9, 9)) expected = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0], @@ -576,7 +577,7 @@ class TestAISTrajectoryImageGeneration(unittest.TestCase): np.testing.assert_array_equal(result, expected) def test_generate_array_bounding_box(self): - result = self.trajectory.generate_array_from_positions(height=9, width=9, link=False, + result = self.trajectory.generate_array_from_positions(height=9, width=9, link=None, bounding_box=[(0, 0), (10, 10)], features=None, node_size=0).reshape((9, 9)) expected = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 1], @@ -603,7 +604,7 @@ class TestAISTrajectoryImageGeneration(unittest.TestCase): ) ) - result = trajectory.generate_array_from_positions(height=9, width=18, link=True, bounding_box='fit', + result = trajectory.generate_array_from_positions(height=9, width=18, link='previous', bounding_box='fit', features={"sog": (0, 80)}, node_size=0).reshape((9, 18)) expected = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5], @@ -614,7 +615,8 @@ class TestAISTrajectoryImageGeneration(unittest.TestCase): [0, 0, 0, 0.25, 0.25, 0, 0, 0, 0, 0, 0, 0, 0.25, 0.25, 0, 0, 0, 0.5], [0, 0.25, 0.25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.25, 0.25, 0, 0.5], [0.25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.25, 0.5]]) / 2 - + with np.printoptions(threshold=320): + print(result) np.testing.assert_array_equal(result, expected) def test_generate_array_from_positions_with_line_multi_channel_dict(self): @@ -630,7 +632,7 @@ class TestAISTrajectoryImageGeneration(unittest.TestCase): ) ) - result = trajectory.generate_array_from_positions(height=9, width=18, link=True, bounding_box='fit', + result = trajectory.generate_array_from_positions(height=9, width=18, link='previous', bounding_box='fit', features={'sog': (0, 40), 'cog': (0, 40)}, node_size=0) expected = np.array([[[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.5, 0.25]], @@ -665,10 +667,9 @@ class TestAISTrajectoryImageGeneration(unittest.TestCase): ) ) - result = trajectory.generate_array_from_positions(height=9, width=18, link=True, bounding_box='fit', - features={"sog": (0, 80)}, node_size=0, - interpolation='linear').reshape((9, 18)) - print(result) + result = trajectory.generate_array_from_positions(height=9, width=18, link='interpolate', bounding_box='fit', + features={"sog": (0, 80)}, node_size=0).reshape((9, 18)) + expected = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.46875], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.4375], diff --git a/skais/tests/process/test_geography.py b/skais/tests/process/test_geography.py index be1f844dfd0cfdddf9bb284ab75b6f5d1c9ab30e..14b7baab50a3b58827941552b5e9e737291438e7 100644 --- a/skais/tests/process/test_geography.py +++ b/skais/tests/process/test_geography.py @@ -4,97 +4,97 @@ from skais.process.geography import * class MyTestCase(unittest.TestCase): def test_to_rad_0(self): - result = to_rad.py_func(0) + result = to_rad(0) expected = 0 self.assertAlmostEqual(result, expected) def test_to_rad_90(self): - result = to_rad.py_func(90) + result = to_rad(90) expected = np.pi / 2 self.assertAlmostEqual(result, expected) def test_to_rad_180(self): - result = to_rad.py_func(180) + result = to_rad(180) expected = np.pi self.assertAlmostEqual(result, expected) def test_to_rad_360(self): - result = to_rad.py_func(360) + result = to_rad(360) expected = 2 * np.pi self.assertAlmostEqual(result, expected) def test_to_rad_m180(self): - result = to_rad.py_func(-180) + result = to_rad(-180) expected = -np.pi self.assertAlmostEqual(result, expected) def test_to_rad_m90(self): - result = to_rad.py_func(-90) + result = to_rad(-90) expected = -np.pi / 2 self.assertAlmostEqual(result, expected) def test_to_rad_270(self): - result = to_rad.py_func(270) + result = to_rad(270) expected = 3 * np.pi / 2 self.assertAlmostEqual(result, expected) def test_to_rad_810(self): - result = to_rad.py_func(810) + result = to_rad(810) expected = 9 * np.pi / 2 self.assertAlmostEqual(result, expected) def test_to_deg_0(self): - result = to_deg.py_func(0) + result = to_deg(0) expected = 0 self.assertAlmostEqual(result, expected) def test_to_deg_90(self): - result = to_deg.py_func(np.pi / 2) + result = to_deg(np.pi / 2) expected = 90 self.assertAlmostEqual(result, expected) def test_to_deg_180(self): - result = to_deg.py_func(np.pi) + result = to_deg(np.pi) expected = 180 self.assertAlmostEqual(result, expected) def test_to_deg_360(self): - result = to_deg.py_func(2 * np.pi) + result = to_deg(2 * np.pi) expected = 360 self.assertAlmostEqual(result, expected) def test_to_deg_m180(self): - result = to_deg.py_func(-np.pi) + result = to_deg(-np.pi) expected = -180 self.assertAlmostEqual(result, expected) def test_to_deg_m90(self): - result = to_deg.py_func(-np.pi / 2) + result = to_deg(-np.pi / 2) expected = -90 self.assertAlmostEqual(result, expected) def test_to_deg_270(self): - result = to_deg.py_func(3 * np.pi / 2) + result = to_deg(3 * np.pi / 2) expected = 270 self.assertAlmostEqual(result, expected) def test_to_deg_810(self): - result = to_deg.py_func(9 * np.pi / 2) + result = to_deg(9 * np.pi / 2) expected = 810 self.assertAlmostEqual(result, expected) @@ -103,7 +103,7 @@ class MyTestCase(unittest.TestCase): paris = (2.3522, 48.8566) marseille = (5.3698, 43.2965) - result = bearing.py_func(paris, marseille) + result = bearing(paris, marseille) expected = 158.2694 self.assertAlmostEqual(result, expected, places=4) @@ -112,7 +112,7 @@ class MyTestCase(unittest.TestCase): p1 = (0, 0) p2 = (90, 0) - result = bearing.py_func(p1, p2) + result = bearing(p1, p2) expected = 90 self.assertAlmostEqual(result, expected) @@ -121,7 +121,7 @@ class MyTestCase(unittest.TestCase): p1 = (0, 0) p2 = (0, 90) - result = bearing.py_func(p1, p2) + result = bearing(p1, p2) expected = 0 self.assertAlmostEqual(result, expected) @@ -131,28 +131,28 @@ class MyTestCase(unittest.TestCase): p2 = (0, 0) p3 = (10, 0) - self.assertAlmostEqual(90, angle_between_three_points.py_func(p1, p2, p3), places=3) + self.assertAlmostEqual(90, angle_between_three_points(p1, p2, p3), places=3) def test_angle_between_three_points_2(self): p1 = (0, -10) p2 = (0, 0) p3 = (-10, 0) - self.assertAlmostEqual(-90, angle_between_three_points.py_func(p1, p2, p3), places=3) + self.assertAlmostEqual(-90, angle_between_three_points(p1, p2, p3), places=3) def test_angle_between_three_points_3(self): p1 = (0, -10) p2 = (0, 0) p3 = (10, 10) - self.assertAlmostEqual(180 - 44.56139, angle_between_three_points.py_func(p1, p2, p3), places=3) + self.assertAlmostEqual(180 - 44.56139, angle_between_three_points(p1, p2, p3), places=3) def test_angle_between_three_points_4(self): p1 = (0, 0) p2 = (0, 10) p3 = (0, 0) - self.assertAlmostEqual(0, abs(angle_between_three_points.py_func(p1, p2, p3)), places=3) + self.assertAlmostEqual(0, abs(angle_between_three_points(p1, p2, p3)), places=3) def test_compute_point_angles(self): dat = np.array([(0, 0), (0, 10), (10, 10), (0, 10)])