1# Lint as: python3
2# coding=utf-8
3# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#         http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16# ==============================================================================
17
18"""Prepare data for further process.
19
20Read data from "/slope", "/ring", "/wing", "/negative" and save them
21in "/data/complete_data" in python dict format.
22
23It will generate a new file with the following structure:
24├── data
25│   └── complete_data
26"""
27
28from __future__ import absolute_import
29from __future__ import division
30from __future__ import print_function
31
32import csv
33import json
34import os
35import random
36
37LABEL_NAME = "gesture"
38DATA_NAME = "accel_ms2_xyz"
39folders = ["wing", "ring", "slope"]
40names = [
41        "hyw", "shiyun", "tangsy", "dengyl", "zhangxy", "pengxl", "liucx",
42        "jiangyh", "xunkai"
43]
44
45
46def prepare_original_data(folder, name, data, file_to_read):
47    """Read collected data from files."""
48    if folder != "negative":
49        with open(file_to_read, "r") as f:
50            lines = csv.reader(f)
51            data_new = {}
52            data_new[LABEL_NAME] = folder
53            data_new[DATA_NAME] = []
54            data_new["name"] = name
55            for idx, line in enumerate(lines):    # pylint: disable=unused-variable
56                if len(line) == 3:
57                    if line[2] == "-" and data_new[DATA_NAME]:
58                        data.append(data_new)
59                        data_new = {}
60                        data_new[LABEL_NAME] = folder
61                        data_new[DATA_NAME] = []
62                        data_new["name"] = name
63                    elif line[2] != "-":
64                        data_new[DATA_NAME].append([float(i) for i in line[0:3]])
65            data.append(data_new)
66    else:
67        with open(file_to_read, "r") as f:
68            lines = csv.reader(f)
69            data_new = {}
70            data_new[LABEL_NAME] = folder
71            data_new[DATA_NAME] = []
72            data_new["name"] = name
73            for idx, line in enumerate(lines):
74                if len(line) == 3 and line[2] != "-":
75                    if len(data_new[DATA_NAME]) == 120:
76                        data.append(data_new)
77                        data_new = {}
78                        data_new[LABEL_NAME] = folder
79                        data_new[DATA_NAME] = []
80                        data_new["name"] = name
81                    else:
82                        data_new[DATA_NAME].append([float(i) for i in line[0:3]])
83            data.append(data_new)
84
85
86def generate_negative_data(data):
87    """Generate negative data labeled as 'negative6~8'."""
88    # Big movement -> around straight line
89    for i in range(100):
90        if i > 80:
91            dic = {DATA_NAME: [], LABEL_NAME: "negative", "name": "negative8"}
92        elif i > 60:
93            dic = {DATA_NAME: [], LABEL_NAME: "negative", "name": "negative7"}
94        else:
95            dic = {DATA_NAME: [], LABEL_NAME: "negative", "name": "negative6"}
96        start_x = (random.random() - 0.5) * 2000
97        start_y = (random.random() - 0.5) * 2000
98        start_z = (random.random() - 0.5) * 2000
99        x_increase = (random.random() - 0.5) * 10
100        y_increase = (random.random() - 0.5) * 10
101        z_increase = (random.random() - 0.5) * 10
102        for j in range(128):
103            dic[DATA_NAME].append([
104                    start_x + j * x_increase + (random.random() - 0.5) * 6,
105                    start_y + j * y_increase + (random.random() - 0.5) * 6,
106                    start_z + j * z_increase + (random.random() - 0.5) * 6
107            ])
108        data.append(dic)
109    # Random
110    for i in range(100):
111        if i > 80:
112            dic = {DATA_NAME: [], LABEL_NAME: "negative", "name": "negative8"}
113        elif i > 60:
114            dic = {DATA_NAME: [], LABEL_NAME: "negative", "name": "negative7"}
115        else:
116            dic = {DATA_NAME: [], LABEL_NAME: "negative", "name": "negative6"}
117        for j in range(128):
118            dic[DATA_NAME].append([(random.random() - 0.5) * 1000,
119                                                         (random.random() - 0.5) * 1000,
120                                                         (random.random() - 0.5) * 1000])
121        data.append(dic)
122    # Stay still
123    for i in range(100):
124        if i > 80:
125            dic = {DATA_NAME: [], LABEL_NAME: "negative", "name": "negative8"}
126        elif i > 60:
127            dic = {DATA_NAME: [], LABEL_NAME: "negative", "name": "negative7"}
128        else:
129            dic = {DATA_NAME: [], LABEL_NAME: "negative", "name": "negative6"}
130        start_x = (random.random() - 0.5) * 2000
131        start_y = (random.random() - 0.5) * 2000
132        start_z = (random.random() - 0.5) * 2000
133        for j in range(128):
134            dic[DATA_NAME].append([
135                    start_x + (random.random() - 0.5) * 40,
136                    start_y + (random.random() - 0.5) * 40,
137                    start_z + (random.random() - 0.5) * 40
138            ])
139        data.append(dic)
140
141
142# Write data to file
143def write_data(data_to_write, path):
144    with open(path, "w") as f:
145        for idx, item in enumerate(data_to_write):    # pylint: disable=unused-variable
146            dic = json.dumps(item, ensure_ascii=False)
147            f.write(dic)
148            f.write("\n")
149
150
151if __name__ == "__main__":
152    data = []
153    for idx1, folder in enumerate(folders):
154        for idx2, name in enumerate(names):
155            prepare_original_data(folder, name, data,
156                                                        "./%s/output_%s_%s.txt" % (folder, folder, name))
157    for idx in range(5):
158        prepare_original_data("negative", "negative%d" % (idx + 1), data,
159                                                    "./negative/output_negative_%d.txt" % (idx + 1))
160    generate_negative_data(data)
161    print("data_length: " + str(len(data)))
162    if not os.path.exists("./data"):
163        os.makedirs("./data")
164    write_data(data, "./data/complete_data")
165