-
Notifications
You must be signed in to change notification settings - Fork 6
/
util.py
executable file
·84 lines (67 loc) · 2.61 KB
/
util.py
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
import sys
import numpy as np
from sklearn import metrics
import csv
import argparse
import util2
def read_txy_csv(fn):
data = readCsvFile(fn)
Xtest = data[:, :3]
Ytest = data[:, 3][:, np.newaxis]
return Xtest, Ytest
def read_carmen(fn_gfs):
parser = argparse.ArgumentParser()
parser.add_argument(
"--logfile",
default=fn_gfs, #wombot_test_2016-02-05-11-51-14.gfs.log SimonFewLasers.gfs.log
help="Logfile in CARMEN format to process"
)
args = parser.parse_args()
# Load data and split it into training and testing data
train_data, test_data = util2.create_test_train_split(args.logfile, 0)
train_data = np.asarray( train_data["scans"])
return train_data
def get_mesh_grid(resolution=1, limits=[-120, 120, -20, 120]):
x_spaced = np.arange(limits[0], limits[1], resolution )
y_spaced = np.arange(limits[2], limits[3], resolution)
xx, yy = np.meshgrid(x_spaced, y_spaced)
X_plot = np.vstack((xx.flatten(),yy.flatten())).T
return X_plot
def generate_test_data_everyother(fn, X_all, Y_all, step=2):
#temp - rand method
#ith_scan_indx_plus1 = X_all[:, 0] > ith_scan
#Xtest = X_all[ith_scan_indx_plus1, 1:]
#Ytest = Y_all[ith_scan_indx_plus1, :]
for jth_scan in range(1, len(np.unique(X_all[:, 0])), step): #start from 1
jth_scan_indx = X_all[:, 0] == jth_scan
X_jth_scan = X_all[jth_scan_indx, :]
Y_jth_scan = Y_all[jth_scan_indx, :]
X_jth_0 = X_jth_scan[Y_jth_scan[:,0]==0, :]
X_jth_1 = X_jth_scan[Y_jth_scan[:,0]==1, :]
if X_jth_1.shape[0] < X_jth_0.shape[0]: #number of ones are less than number of zeros
n_X_jth_1 = X_jth_1.shape[0]
indx = np.random.choice(X_jth_0.shape[0], n_X_jth_1, replace=False)
X_jth_0 = X_jth_0[indx, :]
n_X_jth_0 = n_X_jth_1
else:
n_X_jth_0 = X_jth_0.shape[0]
indx = np.random.choice(X_jth_1.shape[0], n_X_jth_0, replace=False)
X_jth_1 = X_jth_1[indx, :]
n_X_jth_1 = n_X_jth_0
print('ouch!', jth_scan)
X_jth = np.vstack((X_jth_0, X_jth_1))
Y_jth = np.array([[0]*n_X_jth_0 + [1]*n_X_jth_1]).T
if jth_scan == 1: #first scan to store
Xtest = X_jth
Ytest = Y_jth
else:
Xtest = np.vstack((Xtest, X_jth))
Ytest = np.vstack((Ytest, Y_jth))
points_labels = np.hstack((Xtest, Ytest))
np.savetxt(fn, points_labels, delimiter=",")
def readCsvFile( fileName ):
reader = csv.reader(open(fileName,'r') )
dataList = []
for row in reader:
dataList.append( [float(elem) for elem in row ] )
return np.array( dataList )