-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutilities.py
More file actions
119 lines (90 loc) · 3.44 KB
/
utilities.py
File metadata and controls
119 lines (90 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
import math
import matplotlib.colors as Colors
def plotSDRBinaryMap(axes, name, data, colors=["black", "lime", "gray"]):
axes.set_title(name)
axes.set_xlabel("Rows")
axes.set_ylabel("Columns")
plotW = math.ceil(math.sqrt(len(data)))
rf = np.zeros([plotW, plotW], dtype=np.uint8)
for i in range(plotW):
arr = data[i * plotW : i * plotW + plotW] * 5
if len(arr) < plotW:
arr = np.concatenate(
[arr, np.ones(plotW - len(arr)) * 10]
) # fill end where is nothing
rf[:, i] = arr
cm = Colors.LinearSegmentedColormap.from_list("myCMap", colors, N=3)
axes.imshow(rf, interpolation="nearest", norm=Colors.Normalize(0, 10), cmap=cm)
def plotBinaryMap(
axes, name, data, colors=["black", "lime"]
):
# Translate list (with custom datatypes) to the numpy numeric array
width = len(data)
height = len(data[0])
arr = np.zeros((width, height), dtype=np.uint8)
for x in range(width):
for y in range(height):
if data[x][y] != 0:
arr[x][y] = 1
axes.set_title(name)
axes.set_xlabel("x")
axes.set_ylabel("y")
# features will be value 5
arr = arr
cm = Colors.LinearSegmentedColormap.from_list("myCMap", colors, N=2)
axes.set_xticks(np.arange(-0.5, 20, 1))
axes.set_yticks(np.arange(-0.5, 20, 1))
axes.set_xticklabels(np.arange(0, 20, 1))
axes.set_yticklabels(np.arange(0, 20, 1))
axes.grid(color="w", linestyle="-", linewidth=2)
axes.imshow(arr.T, interpolation="nearest", norm=Colors.Normalize(0, 1), cmap=cm)
def plotEnvironment(
axes, name, env, agentPos, colors=["white", "blue", "red", "lightGray", "cyan"]
):
environmentData = env._features
# Translate list (with custom datatypes) to the numpy numeric array
width = env._width
height = env._height
arr = np.zeros((width, height), dtype=np.uint8)
for x in range(width):
for y in range(height):
if environmentData[x][y] != None:
arr[x][y] = 1
axes.set_title(name)
axes.set_xlabel("x")
axes.set_ylabel("y")
# features will be value 5
arr = arr
agX = agentPos[0]
agY = agentPos[1]
arr[agX, agY] = 2 # agent pos
# highlight sensors around him
if agX > 0:
arr[agX - 1, agY] = 4 if arr[agX - 1, agY] == 1 else 3
if agY > 0:
arr[agX, agY - 1] = 4 if arr[agX, agY - 1] == 1 else 3
if agX < env._width:
arr[agX + 1, agY] = 4 if arr[agX + 1, agY] == 1 else 3
if agY < env._height:
arr[agX, agY + 1] = 4 if arr[agX, agY + 1] == 1 else 3
cm = Colors.LinearSegmentedColormap.from_list("myCMap", colors, N=5)
axes.set_xticks(np.arange(-0.5, 20, 1))
axes.set_yticks(np.arange(-0.5, 20, 1))
axes.set_xticklabels(np.arange(0, 20, 1))
axes.set_yticklabels(np.arange(0, 20, 1))
axes.grid(color="w", linestyle="-", linewidth=2)
axes.imshow(arr.T, interpolation="nearest", norm=Colors.Normalize(0, 5), cmap=cm)
def isNotebook():
try:
shell = get_ipython().__class__.__name__
if shell == "ZMQInteractiveShell":
return True # Jupyter notebook or qtconsole
elif shell == "TerminalInteractiveShell":
return False # Terminal running IPython
else:
return False # Other type (?)
except NameError:
return False # Probably standard Python interpreter