1# Lint as: python3
2# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#         http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15# ==============================================================================
16
17"""Test for data_split.py."""
18
19from __future__ import absolute_import
20from __future__ import division
21from __future__ import print_function
22
23import json
24import unittest
25from data_split import read_data
26from data_split import split_data
27
28
29class TestSplit(unittest.TestCase):
30
31    def setUp(self):    # pylint: disable=g-missing-super-call
32        self.data = read_data("./data/complete_data")
33        self.num_dic = {"wing": 0, "ring": 0, "slope": 0, "negative": 0}
34        with open("./data/complete_data", "r") as f:
35            lines = f.readlines()
36            self.num = len(lines)
37
38    def test_read_data(self):
39        self.assertEqual(len(self.data), self.num)
40        self.assertIsInstance(self.data, list)
41        self.assertIsInstance(self.data[0], dict)
42        self.assertEqual(
43                set(list(self.data[-1])), set(["gesture", "accel_ms2_xyz", "name"]))
44
45    def test_split_data(self):
46        with open("./data/complete_data", "r") as f:
47            lines = f.readlines()
48            for idx, line in enumerate(lines):    # pylint: disable=unused-variable
49                dic = json.loads(line)
50                for ges in self.num_dic:
51                    if dic["gesture"] == ges:
52                        self.num_dic[ges] += 1
53        train_data_0, valid_data_0, test_data_100 = split_data(self.data, 0, 0)
54        train_data_50, valid_data_50, test_data_0 = split_data(self.data, 0.5, 0.5)
55        train_data_60, valid_data_20, test_data_20 = split_data(self.data, 0.6, 0.2)
56        len_60 = int(self.num_dic["wing"] * 0.6) + int(
57                self.num_dic["ring"] * 0.6) + int(self.num_dic["slope"] * 0.6) + int(
58                        self.num_dic["negative"] * 0.6)
59        len_50 = int(self.num_dic["wing"] * 0.5) + int(
60                self.num_dic["ring"] * 0.5) + int(self.num_dic["slope"] * 0.5) + int(
61                        self.num_dic["negative"] * 0.5)
62        len_20 = int(self.num_dic["wing"] * 0.2) + int(
63                self.num_dic["ring"] * 0.2) + int(self.num_dic["slope"] * 0.2) + int(
64                        self.num_dic["negative"] * 0.2)
65        self.assertEqual(len(train_data_0), 0)
66        self.assertEqual(len(train_data_50), len_50)
67        self.assertEqual(len(train_data_60), len_60)
68        self.assertEqual(len(valid_data_0), 0)
69        self.assertEqual(len(valid_data_50), len_50)
70        self.assertEqual(len(valid_data_20), len_20)
71        self.assertEqual(len(test_data_100), self.num)
72        self.assertEqual(len(test_data_0), (self.num - 2 * len_50))
73        self.assertEqual(len(test_data_20), (self.num - len_60 - len_20))
74
75
76if __name__ == "__main__":
77    unittest.main()
78