diff --git a/skais/ais/ais_trajectory.py b/skais/ais/ais_trajectory.py index a08674ec4e24e3110fb4a35af84731bbb632f83c..7e1307e18352bf3caf025b5d89c6d73f1f02faba 100644 --- a/skais/ais/ais_trajectory.py +++ b/skais/ais/ais_trajectory.py @@ -62,7 +62,7 @@ def apply_time_sequence(dat, time, func): def __get_image_value__(features, bounds): value = [] for f, b in zip(features, bounds): - value.append(1 - (b[1] - f) / b[1]) + value.append(1 - (b[1] - f - b[0]) / (b[1] - b[0])) return value @@ -236,12 +236,11 @@ class AISTrajectory(AISPoints): node_size=0): nb_channels = 1 + positions = self.df[['longitude', 'latitude']].to_numpy() if bounding_box == 'fit': - positions = self.df[['longitude', 'latitude']].to_numpy() lower_lon, upper_lon = (min(positions[:, 0]), max(positions[:, 0])) lower_lat, upper_lat = (min(positions[:, 1]), max(positions[:, 1])) elif bounding_box == 'centered': - positions = self.df[['longitude', 'latitude']].to_numpy() center_lon, center_lat = positions[-1] min_lon, max_lon = (min(positions[:, 0]), max(positions[:, 0])) min_lat, max_lat = (min(positions[:, 1]), max(positions[:, 1])) @@ -253,7 +252,11 @@ class AISTrajectory(AISPoints): lower_lat = center_lat - distance_to_center upper_lon = center_lon + distance_to_center lower_lon = center_lon - distance_to_center - + elif type(bounding_box) is list: + upper_lon = bounding_box[1][0] + lower_lon = bounding_box[0][0] + upper_lat = bounding_box[1][1] + lower_lat = bounding_box[0][1] else: raise ValueError(f"Option not supported: {bounding_box}") @@ -291,17 +294,23 @@ class AISTrajectory(AISPoints): data[x, y] = [1] else: + bounds = [] if type(features) is list: nb_channels = len(features) + features_vectors = self.df[features].to_numpy() + for c in features_vectors.T: + bounds.append((0, max(c))) elif type(features) is str: - features = [features] + features_vectors = self.df[[features]].to_numpy() + for c in features_vectors.T: + bounds.append((0, max(c))) + elif type(features) is dict: + bounds = list(features.values()) + features_vectors = self.df[features.keys()].to_numpy() else: raise TypeError("Type not supported") data = np.zeros((height, width, nb_channels), dtype=np.float) - features_vectors = self.df[features].to_numpy() - bounds = [] - for c in features_vectors.T: - bounds.append((min(c), max(c))) + for pos, f in zip(positions, features_vectors): latitude = pos[1] diff --git a/skais/tests/ais/test_ais_trajectory.py b/skais/tests/ais/test_ais_trajectory.py index 68085c864532384f6046e8dfa98dcce3dfa12b07..56aa71cd0dfbc24deed6671584a38f7862a732e8 100644 --- a/skais/tests/ais/test_ais_trajectory.py +++ b/skais/tests/ais/test_ais_trajectory.py @@ -380,6 +380,7 @@ class TestAISTrajectory(unittest.TestCase): self.assertListEqual(result, expected) + class TestAISTrajectoryImageGeneration(unittest.TestCase): def setUp(self) -> None: self.trajectory = AISTrajectory( @@ -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', - features=None, node_size=0).reshape((9, 9)) + 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], [0, 0, 0, 0, 0, 0, 0, 0, 0], @@ -408,9 +409,8 @@ 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', - features=None, node_size=1).reshape((9, 9)) + 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], [0, 0, 0, 0, 0, 0, 0, 0, 0], @@ -505,7 +505,7 @@ class TestAISTrajectoryImageGeneration(unittest.TestCase): "latitude": [0, 10, 0, 20], "longitude": [0, 10, 20, 20], "ts_sec": [i for i in range(4)], - "sog": [10,10,20,40] + "sog": [10, 10, 20, 40] } ) ) @@ -531,7 +531,7 @@ class TestAISTrajectoryImageGeneration(unittest.TestCase): "latitude": [0, 10, 0, 20], "longitude": [0, 10, 20, 20], "ts_sec": [i for i in range(4)], - "sog": [10,10,20,40], + "sog": [10, 10, 20, 40], "cog": [40, 20, 10, 10] } ) @@ -539,15 +539,24 @@ class TestAISTrajectoryImageGeneration(unittest.TestCase): result = trajectory.generate_array_from_positions(height=9, width=18, link=True, 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]], - [[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]], - [[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]], - [[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]], - [[0,0], [0,0], [0,0], [0,0], [0,0], [0,0], [0,0], [0.25,1], [0.25,0.5], [0.25,0.5], [0,0], [0,0], [0,0], [0,0], [0,0], [0,0], [0,0], [0.5,0.25]], - [[0,0], [0,0], [0,0], [0,0], [0,0], [0.25,1], [0.25,1], [0,0], [0,0], [0,0], [0.25,0.5], [0.25,0.5], [0,0], [0,0], [0,0], [0,0], [0,0], [0.5,0.25]], - [[0,0], [0,0], [0,0], [0.25,1], [0.25,1], [0,0], [0,0], [0,0], [0,0], [0,0], [0,0], [0,0], [0.25,0.5], [0.25,0.5], [0,0], [0,0], [0,0], [0.5,0.25]], - [[0,0], [0.25,1], [0.25,1], [0,0], [0,0], [0,0], [0,0], [0,0], [0,0], [0,0], [0,0], [0,0], [0,0], [0,0], [0.25,0.5], [0.25,0.5], [0,0], [0.5,0.25]], - [[0.25,1], [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.25,0.5], [0.5,0.25]]]) + 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]], + [[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]], + [[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]], + [[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]], + [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0.25, 1], [0.25, 0.5], + [0.25, 0.5], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0.5, 0.25]], + [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0.25, 1], [0.25, 1], [0, 0], [0, 0], [0, 0], + [0.25, 0.5], [0.25, 0.5], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0.5, 0.25]], + [[0, 0], [0, 0], [0, 0], [0.25, 1], [0.25, 1], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], + [0, 0], [0, 0], [0.25, 0.5], [0.25, 0.5], [0, 0], [0, 0], [0, 0], [0.5, 0.25]], + [[0, 0], [0.25, 1], [0.25, 1], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], + [0, 0], [0, 0], [0, 0], [0, 0], [0.25, 0.5], [0.25, 0.5], [0, 0], [0.5, 0.25]], + [[0.25, 1], [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.25, 0.5], [0.5, 0.25]]]) np.testing.assert_array_equal(result, expected) @@ -567,7 +576,8 @@ 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, bounding_box=[(0, 0), (10,10)], + result = self.trajectory.generate_array_from_positions(height=9, width=9, link=False, + 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], [0, 0, 0, 0, 0, 0, 0, 0, 0], @@ -577,24 +587,24 @@ class TestAISTrajectoryImageGeneration(unittest.TestCase): [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], - [1, 0, 0, 0, 0, 0, 0, 0, 0]]) + [1, 0, 0, 0, 0, 0, 0, 0, 1]]) np.testing.assert_array_equal(result, expected) - def test_generate_array_from_positions_with_line_grey_scale(self): + def test_generate_array_feature_bounds(self): trajectory = AISTrajectory( pd.DataFrame( { "latitude": [0, 10, 0, 20], "longitude": [0, 10, 20, 20], "ts_sec": [i for i in range(4)], - "sog": [10,10,20,40] + "sog": [10, 10, 20, 40] } ) ) result = trajectory.generate_array_from_positions(height=9, width=18, link=True, bounding_box='fit', - features=("sog", (0,80)), node_size=0).reshape((9, 18)) + 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], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5], @@ -603,6 +613,6 @@ class TestAISTrajectoryImageGeneration(unittest.TestCase): [0, 0, 0, 0, 0, 0.25, 0.25, 0, 0, 0, 0.25, 0.25, 0, 0, 0, 0, 0, 0.5], [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 + [0.25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.25, 0.5]]) / 2 - np.testing.assert_array_equal(result, expected) \ No newline at end of file + np.testing.assert_array_equal(result, expected)