-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_prepare.py
executable file
·89 lines (78 loc) · 2.82 KB
/
data_prepare.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import csv
import json
import os
import random
import config
def prepare_data(label, data, file_to_read):
"""Read collected data from files."""
with open(file_to_read, "r") as f:
lines = csv.reader(f)
data_new = {}
data_new[config.LABEL_NAME] = label
data_new[config.DATA_NAME] = []
for line in lines:
if len(line) == 3:
if line[2] == "-" and data_new[config.DATA_NAME]:
data.append(data_new)
data_new = {}
data_new[config.LABEL_NAME] = label
data_new[config.DATA_NAME] = []
elif line[2] != "-":
data_new[config.DATA_NAME].append([float(i) for i in line[0:3]])
data.append(data_new)
def generate_negative_data(data):
"""Generate negative data."""
# Big movement -> around straight line
for i in range(100):
dic = {config.DATA_NAME: [], config.LABEL_NAME: "negative"}
start_x = (random.random() - 0.5) * 2000
start_y = (random.random() - 0.5) * 2000
start_z = (random.random() - 0.5) * 2000
x_increase = (random.random() - 0.5) * 10
y_increase = (random.random() - 0.5) * 10
z_increase = (random.random() - 0.5) * 10
for j in range(config.seq_length):
dic[config.DATA_NAME].append([
start_x + j * x_increase + (random.random() - 0.5) * 6,
start_y + j * y_increase + (random.random() - 0.5) * 6,
start_z + j * z_increase + (random.random() - 0.5) * 6
])
data.append(dic)
# Random
for i in range(100):
dic = {config.DATA_NAME: [], config.LABEL_NAME: "negative"}
for _ in range(config.seq_length):
dic[config.DATA_NAME].append([(random.random() - 0.5) * 1000,
(random.random() - 0.5) * 1000,
(random.random() - 0.5) * 1000])
data.append(dic)
# Stay still
for i in range(100):
dic = {config.DATA_NAME: [], config.LABEL_NAME: "negative"}
start_x = (random.random() - 0.5) * 2000
start_y = (random.random() - 0.5) * 2000
start_z = (random.random() - 0.5) * 2000
for _ in range(config.seq_length):
dic[config.DATA_NAME].append([
start_x + (random.random() - 0.5) * 40,
start_y + (random.random() - 0.5) * 40,
start_z + (random.random() - 0.5) * 40
])
data.append(dic)
# Write data to file
def write_data(data_to_write, path):
with open(path, "w") as f:
for item in data_to_write:
dic = json.dumps(item, ensure_ascii=False)
f.write(dic)
f.write("\n")
if __name__ == "__main__":
data = []
for label in config.labels:
prepare_data(label, data, "../../data/raw/%s_%s.txt" % (config.LABEL_NAME, label))
generate_negative_data(data)
print("data_length: " + str(len(data)))
write_data(data, "../../data/dataset")