# Lint as: python3
# coding=utf-8
# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Split data into train, validation and test dataset according to person.

That is, use some people's data as train, some other people's data as
validation, and the rest ones' data as test. These data would be saved
separately under "/person_split".

It will generate new files with the following structure:
├──person_split
│   ├── test
│   ├── train
│   └──valid
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import random
from data_split import read_data
from data_split import write_data


def person_split(whole_data, train_names, valid_names, test_names):
    """Split data by person."""
    random.seed(30)
    random.shuffle(whole_data)
    train_data = []
    valid_data = []
    test_data = []
    for idx, data in enumerate(whole_data):    # pylint: disable=unused-variable
        if data["name"] in train_names:
            train_data.append(data)
        elif data["name"] in valid_names:
            valid_data.append(data)
        elif data["name"] in test_names:
            test_data.append(data)
    print("train_length:" + str(len(train_data)))
    print("valid_length:" + str(len(valid_data)))
    print("test_length:" + str(len(test_data)))
    return train_data, valid_data, test_data


if __name__ == "__main__":
    data = read_data("./data/complete_data")
    train_names = [
            "hyw", "shiyun", "tangsy", "dengyl", "jiangyh", "xunkai", "negative3",
            "negative4", "negative5", "negative6"
    ]
    valid_names = ["lsj", "pengxl", "negative2", "negative7"]
    test_names = ["liucx", "zhangxy", "negative1", "negative8"]
    train_data, valid_data, test_data = person_split(data, train_names,
                                                                                                     valid_names, test_names)
    if not os.path.exists("./person_split"):
        os.makedirs("./person_split")
    write_data(train_data, "./person_split/train")
    write_data(valid_data, "./person_split/valid")
    write_data(test_data, "./person_split/test")

[((43, 4, 43, 19), 'random.seed', 'random.seed', ({(43, 16, 43, 18): '(30)'}, {}), '(30)', False, 'import random\n'), ((44, 4, 44, 30), 'random.shuffle', 'random.shuffle', ({(44, 19, 44, 29): 'whole_data'}, {}), '(whole_data)', False, 'import random\n'), ((62, 11, 62, 44), 'data_split.read_data', 'read_data', ({(62, 21, 62, 43): '"""./data/complete_data"""'}, {}), "('./data/complete_data')", False, 'from data_split import read_data\n'), ((73, 4, 73, 50), 'data_split.write_data', 'write_data', ({(73, 15, 73, 25): 'train_data', (73, 27, 73, 49): '"""./person_split/train"""'}, {}), "(train_data, './person_split/train')", False, 'from data_split import write_data\n'), ((74, 4, 74, 50), 'data_split.write_data', 'write_data', ({(74, 15, 74, 25): 'valid_data', (74, 27, 74, 49): '"""./person_split/valid"""'}, {}), "(valid_data, './person_split/valid')", False, 'from data_split import write_data\n'), ((75, 4, 75, 48), 'data_split.write_data', 'write_data', ({(75, 15, 75, 24): 'test_data', (75, 26, 75, 47): '"""./person_split/test"""'}, {}), "(test_data, './person_split/test')", False, 'from data_split import write_data\n'), ((71, 11, 71, 43), 'os.path.exists', 'os.path.exists', ({(71, 26, 71, 42): '"""./person_split"""'}, {}), "('./person_split')", False, 'import os\n'), ((72, 8, 72, 37), 'os.makedirs', 'os.makedirs', ({(72, 20, 72, 36): '"""./person_split"""'}, {}), "('./person_split')", False, 'import os\n')]