From b1c49f987e56d32b9f7f3301c0731b89c272daab Mon Sep 17 00:00:00 2001 From: arne Date: Mon, 2 Dec 2019 19:23:10 +0100 Subject: [PATCH] compatibility with Python 2.7 --- map.py | 2 +- reference_path.py | 10 +++++----- spatial_bicycle_models.py | 35 ++++++++++++++++++++++++++--------- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/map.py b/map.py index 383220a..bbb5c24 100644 --- a/map.py +++ b/map.py @@ -1,6 +1,6 @@ import numpy as np import matplotlib.pyplot as plt -from skimage.morphology import remove_small_holes +# from skimage.morphology import remove_small_holes from PIL import Image diff --git a/reference_path.py b/reference_path.py index 83c8fe8..35014db 100644 --- a/reference_path.py +++ b/reference_path.py @@ -1,7 +1,7 @@ import numpy as np import math from map import Map -from bresenham import bresenham +from skimage.draw import line import matplotlib.pyplot as plt import matplotlib.patches as plt_patches from scipy.signal import savgol_filter @@ -297,8 +297,8 @@ class ReferencePath: # Get Bresenham paths to all possible cells paths = [] for t_x, t_y in zip(tn_x, tn_y): - path = list(bresenham(wp_x, wp_y, t_x, t_y)) - paths.append(path) + x_list, y_list = line(wp_x, wp_y, t_x, t_y) + paths.append(zip(x_list, y_list)) # Compute minimum distance to border cell min_width = max_width @@ -340,7 +340,7 @@ class ReferencePath: lb_p = self.map.w2m(wp.border_cells[1][0], wp.border_cells[1][1]) # Compute path from left border cell to right border cell - path = list(bresenham(ub_p[0], ub_p[1], lb_p[0], lb_p[1])) + x_list, y_list = line(ub_p[0], ub_p[1], lb_p[0], lb_p[1]) # Initialize upper and lower bound of drivable area to # upper bound of path @@ -351,7 +351,7 @@ class ReferencePath: ub_ls, lb_ls = ub_p, ub_p # Iterate over path from left border to right border - for x, y in path: + for x, y in zip(x_list, y_list): # If cell is free, update lower bound if self.map.data[y, x] == 1: lb_o = (x, y) diff --git a/spatial_bicycle_models.py b/spatial_bicycle_models.py index e307456..74181f8 100644 --- a/spatial_bicycle_models.py +++ b/spatial_bicycle_models.py @@ -1,5 +1,13 @@ import numpy as np -from abc import ABC, abstractmethod +from abc import abstractmethod +try: + from abc import ABC +except: + # for Python 2.7 + from abc import ABCMeta + class ABC(object): + __metaclass__ = ABCMeta + pass import matplotlib.pyplot as plt import matplotlib.patches as plt_patches import math @@ -26,14 +34,16 @@ class TemporalState: self.y = y self.psi = psi + self.members = ['x', 'y', 'psi'] + def __iadd__(self, other): """ Overload Sum-Add operator. :param other: numpy array to be added to state vector """ - for state_id, state in enumerate(vars(self).values()): - vars(self)[list(vars(self).keys())[state_id]] += other[state_id] + for state_id in range(len(self.members)): + vars(self)[self.members[state_id]] += other[state_id] return self @@ -48,16 +58,21 @@ class SpatialState(ABC): @abstractmethod def __init__(self): + self.members = None pass def __getitem__(self, item): - return list(vars(self).values())[item] + if isinstance(item, int): + members = [self.members[item]] + else: + members = self.members[item] + return [vars(self)[key] for key in members] def __setitem__(self, key, value): - vars(self)[list(vars(self).keys())[key]] = value + vars(self)[self.members[key]] = value def __len__(self): - return len(vars(self)) + return len(self.members) def __iadd__(self, other): """ @@ -65,15 +80,15 @@ class SpatialState(ABC): :param other: numpy array to be added to state vector """ - for state_id, state in enumerate(vars(self).values()): - vars(self)[list(vars(self).keys())[state_id]] += other[state_id] + for state_id in range(len(self.members)): + vars(self)[self.members[state_id]] += other[state_id] return self def list_states(self): """ Return list of names of all states. """ - return list(vars(self).keys()) + return self.members class SimpleSpatialState(SpatialState): @@ -91,6 +106,8 @@ class SimpleSpatialState(SpatialState): self.e_psi = e_psi self.t = t + self.members = ['e_y', 'e_psi', 't'] + #################################### # Spatial Bicycle Model Base Class #