Repository navigation
Expand file tree
/
Copy pathplot.py
More file actions
executable file
·231 lines (185 loc) · 8.69 KB
/
Copy pathplot.py
File metadata and controls
executable file
·231 lines (185 loc) · 8.69 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
from numpy import genfromtxt
import math
import textwrap
import argparse
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.manifold import TSNE
from io import StringIO
import numpy as np
import pandas as pd
import sys
def plot_dataset_tsne(dataset_file_name, dataset_file_name2, dataset_file_name3, episdes_file_name, episdes_file_name2, episdes_file_name3):
train_matrix = genfromtxt(dataset_file_name, delimiter=',')
train_2_matrix = genfromtxt(dataset_file_name2, delimiter=',')
train_3_matrix = genfromtxt(dataset_file_name3, delimiter=',')
Big_matrix = np.concatenate((train_matrix, train_2_matrix, train_3_matrix))
print("Original shape: 1", train_matrix.shape)
print("Original shape: 2", train_2_matrix.shape)
print("Original shape: 3", train_3_matrix.shape)
print("Shape of big matrix", Big_matrix.shape)
data_1_color_vec = np.loadtxt(episdes_file_name)
data_2_color_vec = np.loadtxt(episdes_file_name2)
data_3_color_vec = np.loadtxt(episdes_file_name3)
tsne = TSNE(n_components=2, random_state=0)
mat = tsne.fit_transform(Big_matrix)
print(mat.shape)
data_1 = mat[:np.size(train_matrix,0), :]
print("Original shape: ", train_matrix.shape, "New Shape: ", data_1.shape)
print("Mat shape", mat.shape)
data_2 = mat[np.size(train_matrix, 0):np.size(train_matrix, 0) + np.size(train_2_matrix, 0) , :]
print("Original shape: ", train_2_matrix.shape, "New Shape: ", data_2.shape)
data_3 = mat[np.size(train_2_matrix,0): np.size(train_2_matrix, 0) + np.size(train_3_matrix, 0), :]
print("Original shape: ", train_3_matrix.shape, "New Shape: ", data_3.shape)
plt.figure()
plt.scatter(data_1[:,0], data_1[:, 1], c=data_1_color_vec, cmap='plasma')
plt.colorbar()
figure = plt.gcf() # get current figure
figure.set_size_inches(23, 18)
plt.savefig(dataset_file_name + ".svg", dpi=600, format="svg")
plt.figure()
plt.scatter(data_2[:,0], data_2[:, 1], c=data_2_color_vec, cmap='plasma')
plt.colorbar()
figure = plt.gcf() # get current figure
figure.set_size_inches(23, 18)
plt.savefig(dataset_file_name2 + ".svg", dpi=600, format="svg")
plt.figure()
plt.scatter(data_3[:,0], data_3[:, 1], c=data_3_color_vec, cmap='plasma')
plt.colorbar()
figure = plt.gcf() # get current figure
figure.set_size_inches(23, 18)
plt.savefig(dataset_file_name3 + ".svg", dpi=600, format="svg")
def drop_columns(dataset_file_name, column_nubmer):
df = pd.read_csv(dataset_file_name)
df.drop([df.columns[0],
df.columns[9]],
axis = 1, inplace = True)
df.to_csv(dataset_file_name_dropped, index = False)
def plot_2labels_generic(generic_file_name):
num_lines = sum(1 for line in open(generic_file_name))
dataset = np.loadtxt(generic_file_name, delimiter=",")
y = dataset[:, 0]
z = dataset[:, 1]
x = np.arange(y.size)
plt.ylim(-1, 12)
plt.plot(x, y)
plt.plot(x, z)
figure = plt.gcf() # get current figure
figure.set_size_inches(10, 6)
plt.savefig(generic_file_name + ".svg", dpi=100)
def plot_generic(generic_file_name):
num_lines = sum(1 for line in open(generic_file_name))
print (num_lines)
y = np.loadtxt(generic_file_name)
x = np.arange(y.size)
return x,y
def plot_2d_generic(generic_file_name):
num_lines = sum(1 for line in open(generic_file_name))
print (num_lines)
dataset = np.loadtxt(generic_file_name, delimiter=",")
x = dataset[:, 0]
y = dataset[:, 1]
return x,y
def plot_3d_generic(generic_file_name):
num_lines = sum(1 for line in open(generic_file_name))
print (num_lines)
dataset = np.loadtxt(generic_file_name, delimiter=",")
x = dataset[:, 0]
y = dataset[:, 1]
z = dataset[:, 2]
return x,y,z
def plot_3D_position(*file_names):
figure = plt.figure()
ax = figure.gca(projection='3d') # get current figure
ax.legend()
ax.set_zlim3d(0,100)
for file_name in file_names:
a, b, c = plot_3d_generic(file_name)
ax.plot(a, b, c)
figure.set_size_inches(12, 8)
plt.savefig(file_name + ".svg", dpi=100)
def plot_2D_position(*file_names):
figure = plt.figure()
for file_name in file_names:
a, b = plot_2d_generic(file_name)
plt.plot(a, b)
figure.set_size_inches(12, 8)
plt.savefig(file_name + ".svg", dpi=100)
def plot_histogram(count_file_name, histogram_file_name):
x = np.loadtxt(count_file_name)
plt.hist(x, bins=3)
plt.xlabel('Number of time steps per episode')
plt.ylabel('Frequency')
figure = plt.gcf()
figure.set_size_inches(25, 6)
plt.savefig(histogram_file_name + ".png", dpi=100)
def plot_histogram_2d(histogram_file_name):
data = pd.read_csv(histogram_file_name, sep=' ',header=None, index_col =0)
data.plot(kind='bar')
plt.xlabel('Number of time steps per episode')
plt.ylabel('Frequency')
figure = plt.gcf() # get current figure
figure.set_size_inches(10, 6)
plt.savefig(histogram_file_name + ".png", dpi=100)
def cumulative_histogram(filename):
data = genfromtxt(filename, delimiter=' ')
x = data[:, 0:1]
y = data[:, 1:2]
frequency_sum = np.sum(y)
z = []
for i in range(1, len(y)+1):
z.append(np.sum(y[0:i]) / frequency_sum)
return x, z
def plot_cumulative_histogram(*histogram_file_names):
for histogram_file_name in histogram_file_names:
x, y = cumulative_histogram(histogram_file_name)
plt.plot(x, y)
plt.title("Cumulative distribution function")
plt.xlabel('Number of time steps executed by the follower per episode')
plt.ylabel('Percentage of episodes')
plt.legend()
plt.grid()
figure = plt.gcf() # get current figure
figure.set_size_inches(12, 8)
plt.savefig(histogram_file_name + "cumulative_.svg", dpi=100)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
prog='Plotting script',
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent('''\
PLotting script!
--------------------------------
This script is used to plot the result generated by the
quadcopters in the simulator
It can be used to plot one of the files,
or all of them at the same time.
You have to provide the file format as described in the above comments.
'''))
parser.add_argument('--dataset_file_name', metavar="dataset file name", type=str, nargs="+", help="Enter dataset file name to plot")
parser.add_argument('--position_3D_files_name', metavar="3D position file name", type=str, nargs="+", help="Enter 3d position files to plot")
parser.add_argument('--position_2D_files_name', metavar="2D position file name", type=str, nargs="+", help="Enter 3d position files to plot")
parser.add_argument('--tsne_dataset_file_name', metavar="dataset file name", type=str, nargs="+", help="Enter dataset file name to plot using tsne")
parser.add_argument('--generic_file_name', metavar="generic file name", type=str, help="Enter a generic file to plot with one column")
parser.add_argument('--generic_label_file_name', metavar="generic label file name", type=str, help="Enter generic file name to plot y,z")
parser.add_argument('--histogram_file_name', metavar="histogram file name", type=str, help="Enter a histogram file name, two column file name, nuumber of steps and frequency")
parser.add_argument('--cumulative_histogram_files_name', metavar=" histogram file name", type=str, nargs="+" ,help="Enter one or more histogram file name, each file is a two column file name, number of steps and frequency, the script will generate automatically the cumulative histogram")
parser.add_argument('--drop_columns', metavar=" drop column", type=int ,help="Enter column number to delete from dataset")
args = parser.parse_args()
plt.rcParams.update({'font.size': 9})
if len(sys.argv)==1:
parser.print_help(sys.stderr)
sys.exit(1)
if args.generic_label_file_name:
plot_2labels_generic(args.generic_label_file_name)
elif args.dataset_file_name:
modify_labels(args.dataset_file_name)
elif args.position_3D_files_name:
plot_3D_position(*args.position_3D_files_name)
elif args.position_2D_files_name:
plot_2D_position(*args.position_2D_files_name)
elif args.tsne_dataset_file_name:
plot_dataset_tsne(args.tsne_dataset_file_name[0], args.tsne_dataset_file_name[1], args.tsne_dataset_file_name[2], args.tsne_dataset_file_name[3], args.tsne_dataset_file_name[4], args.tsne_dataset_file_name[5])
elif args.histogram_file_name:
plot_histogram_2d(args.histogram_file_name)
elif args.cumulative_histogram_files_name:
plot_cumulative_histogram(*args.cumulative_histogram_files_name)