train running

This commit is contained in:
Steffen Illium
2020-03-09 21:41:50 +01:00
parent daed810958
commit 6cc978e464
7 changed files with 68 additions and 57 deletions

View File

@ -1,4 +1,5 @@
import shelve
from collections import UserDict
from pathlib import Path
import copy
@ -69,7 +70,7 @@ class Map(object):
# Check pixels for their color (determine if walkable)
for idx, value in np.ndenumerate(self.map_array):
if value == self.white:
if value != self.black:
# IF walkable, add node
graph.add_node(idx, count=0)
# Fully connect to all surrounding neighbors
@ -90,6 +91,7 @@ class Map(object):
if image.mode != 'L':
image = image.convert('L')
map_array = np.expand_dims(np.array(image), axis=0)
map_array = np.where(np.asarray(map_array) == cls.white, 1, 0)
if embedding_size:
assert isinstance(embedding_size, tuple), f'embedding_size was of type: {type(embedding_size)}'
embedding = np.zeros(embedding_size)
@ -147,9 +149,9 @@ class Map(object):
img = Image.new('L', (self.height, self.width), 0)
draw = ImageDraw.Draw(img)
draw.polygon(polyline, outline=self.white, fill=self.white)
draw.polygon(polyline, outline=1, fill=1)
a = (np.where(np.asarray(img) == self.white, 1, 0) * np.where(self.as_2d_array == self.black, 1, 0)).sum()
a = (np.asarray(img) * np.where(self.as_2d_array == self.black, 1, 0)).sum()
if a:
return V.ALTERNATIVE # Non-Homotoph
@ -165,32 +167,25 @@ class Map(object):
return dict(img=img, fig=fig, ax=ax)
class MapStorage(object):
class MapStorage(UserDict):
@property
def keys(self):
return list(self.data.keys())
def keys_list(self):
return list(super(MapStorage, self).keys())
def __init__(self, map_root, load_all=False):
self.data = dict()
def __init__(self, map_root, *args, **kwargs):
super(MapStorage, self).__init__(*args, **kwargs)
self.map_root = Path(map_root)
if load_all:
for map_file in self.map_root.glob('*.bmp'):
_ = self[map_file.name]
def __getitem__(self, item):
if item in self.data.keys():
return self.data.get(item)
else:
current_map = Map().from_image(self.map_root / item)
self.data.__setitem__(item, np.asarray(current_map))
return self[item]
map_files = list(self.map_root.glob('*.bmp'))
self.max_map_size = (1, ) + tuple(
reversed(
tuple(
map(
max, *[Image.open(map_file).size for map_file in map_files])
)
)
)
for map_file in map_files:
current_map = Map().from_image(map_file, embedding_size=self.max_map_size)
self.__setitem__(map_file.name, current_map)

View File

@ -41,6 +41,12 @@ class Trajectory(object):
def as_paired_list(self):
return list(zip(self._vertices[:-1], self._vertices[1:]))
def draw_in_array(self, shape):
trajectory_space = np.zeros(shape)
for index in self.vertices:
trajectory_space[index] = 1
return trajectory_space
@property
def np_vertices(self):
return [np.array(vertice) for vertice in self._vertices]