def test_write_data(self): data_path_test = "./data/data0" write_data(self.data, data_path_test) with open(data_path_test, "r") as f: lines = f.readlines() self.assertEqual(len(lines), len(self.data)) self.assertEqual(json.loads(lines[0]), self.data[0]) self.assertEqual(json.loads(lines[-1]), self.data[-1]) os.remove(data_path_test)
print(num_dic) train_num_dic = {} valid_num_dic = {} for i in num_dic: train_num_dic[i] = int(train_ratio * num_dic[i]) valid_num_dic[i] = int(valid_ratio * num_dic[i]) random.seed(30) random.shuffle(data) for idx, item in enumerate(data): for i in num_dic: if item["gesture"] == i: if train_num_dic[i] > 0: train_data.append(item) train_num_dic[i] -= 1 elif valid_num_dic[i] > 0: valid_data.append(item) valid_num_dic[i] -= 1 else: test_data.append(item) print("train_length:" + str(len(train_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_data, valid_data, test_data = split_data(data, 0.6, 0.2) write_data(train_data, "./data/train") write_data(valid_data, "./data/valid") write_data(test_data, "./data/test")