Skip to content
Snippets Groups Projects
Commit 55e29634 authored by Raphael's avatar Raphael
Browse files

improved performances

parent d310ce75
Branches
No related tags found
2 merge requests!20Resolve "Improve performances of image generation",!13Draft: Develop
This commit is part of merge request !13. Comments created here will be created in the context of that merge request.
...@@ -60,14 +60,16 @@ def apply_time_sequence(dat, time, func): ...@@ -60,14 +60,16 @@ def apply_time_sequence(dat, time, func):
return result return result
@jit(nopython=True)
def __get_image_value__(features, bounds): def __get_image_value__(features, bounds):
if len(bounds) < 1: value = np.zeros((len(features)))
return [1] i = 0
value = []
for f, b in zip(features, bounds): for f, b in zip(features, bounds):
value.append(1 - (b[1] - f - b[0]) / (b[1] - b[0])) value[i] = (1 - (b[1] - f - b[0]) / (b[1] - b[0]))
i = i + 1
return value return value
def __get_bounding_box__(bounding_box, positions, ref_index): def __get_bounding_box__(bounding_box, positions, ref_index):
if bounding_box == 'fit': if bounding_box == 'fit':
lower_lon, upper_lon = (min(positions[:, 0]), max(positions[:, 0])) lower_lon, upper_lon = (min(positions[:, 0]), max(positions[:, 0]))
...@@ -110,6 +112,73 @@ def __get_bounding_box__(bounding_box, positions, ref_index): ...@@ -110,6 +112,73 @@ def __get_bounding_box__(bounding_box, positions, ref_index):
return lower_lon, upper_lon, lower_lat, upper_lat return lower_lon, upper_lon, lower_lat, upper_lat
@jit(nopython=True)
def generate_points(data, positions, height, width, node_size, lower_lat, upper_lat, lower_lon, upper_lon):
for longitude, latitude in positions:
x_coord, y_coord = get_coord(latitude, longitude, height, width, lower_lat, upper_lat, lower_lon,
upper_lon)
x_lower_bound = max(0, x_coord - node_size)
x_upper_bound = min(height - 1, x_coord + node_size)
y_lower_bound = max(0, y_coord - node_size)
y_upper_bound = min(width - 1, y_coord + node_size)
for x in range(x_lower_bound, x_upper_bound + 1):
for y in range(y_lower_bound, y_upper_bound + 1):
data[x, y] = [1]
@jit(nopython=True)
def generate_links(data, positions, height, width, lower_lat, upper_lat, lower_lon, upper_lon):
lon, lat = positions[0, 0], positions[0, 1]
for longitude, latitude in positions[1:]:
x_prv, y_prev = get_coord(lat, lon, height, width, lower_lat, upper_lat, lower_lon, upper_lon)
x_nxt, y_nxt = get_coord(latitude, longitude, height, width, lower_lat, upper_lat, lower_lon,
upper_lon)
lon, lat = longitude, latitude
for x, y in bresenham(x_prv, y_prev, x_nxt, y_nxt):
data[x, y] = [1]
@jit(nopython=True)
def generate_points_with_features(data, positions, features_vectors, bounds, node_size, height, width,
lower_lat, upper_lat, lower_lon, upper_lon, link):
for pos, f in zip(positions, features_vectors):
latitude = pos[1]
longitude = pos[0]
x_coord, y_coord = get_coord(latitude, longitude, height, width, lower_lat, upper_lat, lower_lon,
upper_lon)
value = __get_image_value__(f, bounds)
x_lower_bound = max(0, x_coord - node_size)
x_upper_bound = min(height - 1, x_coord + node_size)
y_lower_bound = max(0, y_coord - node_size)
y_upper_bound = min(width - 1, y_coord + node_size)
for x in range(x_lower_bound, x_upper_bound + 1):
for y in range(y_lower_bound, y_upper_bound + 1):
for i, v in enumerate(value):
data[x, y, i] = v
if link:
lon, lat = positions[0, 0], positions[0, 1]
value = __get_image_value__(features_vectors[0], bounds)
for pos, f in zip(positions[1:], features_vectors[1:]):
latitude = pos[1]
longitude = pos[0]
x_prv, y_prev = get_coord(lat, lon, height, width, lower_lat, upper_lat, lower_lon, upper_lon)
x_nxt, y_nxt = get_coord(latitude, longitude, height, width, lower_lat, upper_lat, lower_lon,
upper_lon)
lon, lat = longitude, latitude
for x, y in bresenham(x_prv, y_prev, x_nxt, y_nxt):
for i, v in enumerate(value):
data[x, y, i] = v
value = __get_image_value__(f, bounds)
class AISTrajectory(AISPoints): class AISTrajectory(AISPoints):
def __init__(self, df, mmsi=0, interpolation_time=None): def __init__(self, df, mmsi=0, interpolation_time=None):
df = df.drop_duplicates(subset=['ts_sec']) df = df.drop_duplicates(subset=['ts_sec'])
...@@ -276,8 +345,6 @@ class AISTrajectory(AISPoints): ...@@ -276,8 +345,6 @@ class AISTrajectory(AISPoints):
result.append((row['ts_sec'], current_label)) result.append((row['ts_sec'], current_label))
return result return result
def generate_array_from_positions(self, height=256, width=256, link=True, bounding_box='fit', ref_index=-1, def generate_array_from_positions(self, height=256, width=256, link=True, bounding_box='fit', ref_index=-1,
features=None, node_size=0): features=None, node_size=0):
...@@ -310,59 +377,12 @@ class AISTrajectory(AISPoints): ...@@ -310,59 +377,12 @@ class AISTrajectory(AISPoints):
if features_vectors is None: if features_vectors is None:
for longitude, latitude in positions: generate_points(data, positions, height, width, node_size, lower_lat, upper_lat, lower_lon, upper_lon)
x_coord, y_coord = get_coord(latitude, longitude, height, width, lower_lat, upper_lat, lower_lon,
upper_lon)
x_lower_bound = max(0, x_coord - node_size)
x_upper_bound = min(height - 1, x_coord + node_size)
y_lower_bound = max(0, y_coord - node_size)
y_upper_bound = min(width - 1, y_coord + node_size)
for x in range(x_lower_bound, x_upper_bound + 1):
for y in range(y_lower_bound, y_upper_bound + 1):
data[x, y] = [1]
if link: if link:
lon, lat = positions[0, 0], positions[0, 1] generate_links(data, positions, height, width, lower_lat, upper_lat, lower_lon, upper_lon)
for longitude, latitude in positions[1:]:
x_prv, y_prev = get_coord(lat, lon, height, width, lower_lat, upper_lat, lower_lon, upper_lon)
x_nxt, y_nxt = get_coord(latitude, longitude, height, width, lower_lat, upper_lat, lower_lon,
upper_lon)
lon, lat = longitude, latitude
for x, y in bresenham(x_prv, y_prev, x_nxt, y_nxt):
data[x, y] = [1]
else: else:
for pos, f in zip(positions, features_vectors): generate_points_with_features(data, positions, features_vectors, bounds, node_size, height, width,
latitude = pos[1] lower_lat, upper_lat, lower_lon, upper_lon, link)
longitude = pos[0]
x_coord, y_coord = get_coord(latitude, longitude, height, width, lower_lat, upper_lat, lower_lon,
upper_lon)
value = __get_image_value__(f, bounds)
x_lower_bound = max(0, x_coord - node_size)
x_upper_bound = min(height - 1, x_coord + node_size)
y_lower_bound = max(0, y_coord - node_size)
y_upper_bound = min(width - 1, y_coord + node_size)
for x in range(x_lower_bound, x_upper_bound + 1):
for y in range(y_lower_bound, y_upper_bound + 1):
for i, v in enumerate(value):
data[x, y, i] = v
if link:
lon, lat = positions[0, 0], positions[0, 1]
value = __get_image_value__(features_vectors[0], bounds)
for pos, f in zip(positions[1:], features_vectors[1:]):
latitude = pos[1]
longitude = pos[0]
x_prv, y_prev = get_coord(lat, lon, height, width, lower_lat, upper_lat, lower_lon, upper_lon)
x_nxt, y_nxt = get_coord(latitude, longitude, height, width, lower_lat, upper_lat, lower_lon,
upper_lon)
lon, lat = longitude, latitude
for x, y in bresenham(x_prv, y_prev, x_nxt, y_nxt):
for i, v in enumerate(value):
data[x, y, i] = v
value = __get_image_value__(f, bounds)
return data return data
...@@ -23,6 +23,7 @@ def great_circle(lat1, lat2, long1, long2): ...@@ -23,6 +23,7 @@ def great_circle(lat1, lat2, long1, long2):
return d return d
@jit(nopython=True)
def get_coord(lat, lon, height, width, min_lat, max_lat, min_lon, max_lon): def get_coord(lat, lon, height, width, min_lat, max_lat, min_lon, max_lon):
x_coord = max(min(height - int(height * (lat - min_lat) / (max_lat - min_lat)) - 1, height - 1), 0) x_coord = max(min(height - int(height * (lat - min_lat) / (max_lat - min_lat)) - 1, height - 1), 0)
y_coord = max(min(int((width - 1) * (lon - min_lon) / (max_lon - min_lon)), width - 1), 0) y_coord = max(min(int((width - 1) * (lon - min_lon) / (max_lon - min_lon)), width - 1), 0)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment