-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
287 lines (197 loc) · 7.92 KB
/
utils.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import pickle
import os
import numpy as np
import matplotlib.pyplot as plt
from model import classifier
from constant import *
def time_taken(start, end):
"""Human readable time between `start` and `end`
:param start: time.time()
:param end: time.time()
:returns: day:hour:minute:second.millisecond
"""
my_time = end-start
day = my_time // (24 * 3600)
my_time = my_time % (24 * 3600)
hour = my_time // 3600
my_time %= 3600
minutes = my_time // 60
my_time %= 60
seconds = my_time
milliseconds = ((end - start)-int(end - start))
day_hour_min_sec = str('%02d' % int(day))+":"+str('%02d' % int(hour))+":"+str('%02d' % int(minutes))+":"+str('%02d' % int(seconds)+"."+str('%.3f' % milliseconds)[2:])
return day_hour_min_sec
def find_modality_bin_behavior(a_path, db_file_name):
"""
Finds modality, bins, behavior by using `path` and `dataset` file name
:param a_path: Dataset path
:param db_file_name: Dataset file name
:return: modality, bins, behavior
"""
modality = a_path.split(os.sep)[1].split("_")[0].capitalize()
bins = a_path.split(os.sep)[1].split("_")[1]
if modality == "Proprioception":
modality = "Haptic"
if (db_file_name.split(".")[0].split("_")[0]) == 'low':
behavior = "Drop"
else:
behavior = db_file_name.split(".")[0].split("_")[0].capitalize()
if behavior == "Crush":
behavior = 'Press'
return modality, bins, behavior
def reshape_full_data(data):
"""
Reshape data into (Categories, Objects, Trials)
:param data: Dataset list
:return: reshaped Dataset list
"""
return data.reshape(NUM_OF_CATEGORY, OBJECTS_PER_CATEGORY, TRIALS_PER_OBJECT, -1)
def read_dataset(a_path, db_file_name):
"""
Read dataset
:param a_path: Dataset path
:param db_file_name: Dataset file name
:return: interaction_data, category_labels, object_labels
"""
bin_file = open(a_path + os.sep + db_file_name, "rb")
interaction_data = pickle.load(bin_file)
category_labels = pickle.load(bin_file)
object_labels = pickle.load(bin_file)
bin_file.close()
return reshape_full_data(interaction_data), reshape_full_data(category_labels), reshape_full_data(object_labels)
def repeat_trials(interaction_data_1_train, interaction_data_2_train):
"""
Repeat trials for both robots
:param interaction_data_1_train: Source robot dataset
:param interaction_data_2_train: Target robot dataset
:return: Repeated source robot dataset, Repeated target robot dataset
"""
# Source
# One example of the source robot can be mapped to all the example of the target robot
# So, repeating each example of the source robot for each example of target robot
interaction_data_1_train_repeat = np.repeat(interaction_data_1_train, TRIALS_PER_OBJECT, axis=2)
# Target
# Concatenating same examples of target robot to make it same size as source robot
interaction_data_2_train_repeat = interaction_data_2_train
for _ in range(TRIALS_PER_OBJECT - 1):
interaction_data_2_train_repeat = np.concatenate((interaction_data_2_train_repeat, interaction_data_2_train),
axis=2)
return interaction_data_1_train_repeat, interaction_data_2_train_repeat
def object_recognition_classifier(clf, data_train, data_test, label_train, label_test, num_of_features):
"""
Train a classifier and test it based on provided data
:param clf:
:param data_train:
:param data_test:
:param label_train:
:param label_test:
:param num_of_features:
:return: accuracy, prediction
"""
train_cats_data = data_train.reshape(-1, num_of_features)
train_cats_label = label_train.reshape(-1, 1).flatten()
test_cats_data = data_test.reshape(-1, num_of_features)
test_cats_label = label_test.reshape(-1, 1).flatten()
y_acc, y_pred = classifier(clf, train_cats_data, test_cats_data, train_cats_label, test_cats_label)
return y_acc, y_pred
def print_discretized_data(data, x_values, y_values, modality, behavior, file_path=None):
"""
prints the data point and save it
:param data: one data point
:param x_values: temporal bins
:param y_values:
:param modality:
:param behavior:
:param file_path:
:return:
"""
data = data.reshape(x_values, y_values)
plt.imshow(data.T)
title_name = " ".join([behavior, modality, "Features"])
plt.title(title_name, fontsize=16)
plt.xlabel("Temporal Bins", fontsize=16)
if modality == 'Haptic':
y_label = "Joints"
elif modality == 'Audio':
y_label = "Frequency Bins"
else:
y_label = ""
plt.ylabel(y_label, fontsize=16)
ax = plt.gca()
ax.set_xticks(np.arange(0, x_values, 1))
ax.set_yticks(np.arange(0, y_values, 1))
ax.set_xticklabels(np.arange(1, x_values + 1, 1))
ax.set_yticklabels(np.arange(1, y_values + 1, 1))
plt.colorbar()
if file_path != None:
plt.savefig(file_path, bbox_inches='tight', dpi=100)
#plt.show()
plt.close()
""" Setting 1 """
# Target Robot never interacts with a few categories
def reshape_data_setting1(num_of_category, data):
"""
Reshape data into (Categories, Objects, Trials)
:param num_of_category:
:param data: Dataset list
:return: reshaped Dataset list
"""
return data.reshape(num_of_category, OBJECTS_PER_CATEGORY, TRIALS_PER_OBJECT, -1)
def get_data_label_for_given_labels(given_labels, interaction_data, category_labels):
"""
Get all the examples of the given labels
:param given_labels: labels to find
:param interaction_data: examples
:param category_labels: labels
:return: Dataset, labels
"""
data = []
label = []
for a_label in given_labels:
data.append(interaction_data[a_label])
label.append(category_labels[a_label])
return np.array(data), np.array(label)
def train_test_splits(num_of_objects):
"""
Split the data into object based 5 fold cross validation
:param num_of_objects:
:return: dictionary containing train test index of 5 folds
"""
n_folds = 5
tt_splits = {}
for a_fold in range(n_folds):
train_index = []
test_index = np.arange(a_fold, (a_fold + 1))
if a_fold > 0:
train_index.extend(np.arange(0, a_fold))
if (a_fold + 1) - 1 < num_of_objects - 1:
train_index.extend(np.arange((a_fold + 1), num_of_objects))
tt_splits.setdefault("fold_" + str(a_fold), {}).setdefault("train", []).extend(train_index)
tt_splits.setdefault("fold_" + str(a_fold), {}).setdefault("test", []).extend(test_index)
return tt_splits
def object_based_5_fold_cross_validation(clf, data_train, data_test, labels, num_of_features):
"""
Perform object based 5 fold cross validation and return mean accuracy
:param clf: classifier
:param data_train: Training dataset
:param data_test: Testing dataset
:param labels: True labels
:param num_of_features: Number of features of the robot
:return: mean accuracy of 5 fold validation
"""
tts = train_test_splits(OBJECTS_PER_CATEGORY)
my_acc = []
for a_fold in sorted(tts):
train_cats_index = tts[a_fold]["train"]
test_cats_index = tts[a_fold]["test"]
train_cats_data = data_train[:, train_cats_index]
train_cats_label = labels[:, train_cats_index]
train_cats_data = train_cats_data.reshape(-1, num_of_features)
train_cats_label = train_cats_label.reshape(-1, 1).flatten()
test_cats_data = data_test[:, test_cats_index]
test_cats_label = labels[:, test_cats_index]
test_cats_data = test_cats_data.reshape(-1, num_of_features)
test_cats_label = test_cats_label.reshape(-1, 1).flatten()
y_acc, y_pred = classifier(clf, train_cats_data, test_cats_data, train_cats_label, test_cats_label)
my_acc.append(y_acc)
return np.mean(my_acc)