File size: 5,256 Bytes
bfa885e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
from glob import glob
import json
import os
from pathlib import Path
import random
import sys

pwd = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.join(pwd, "../../"))

import pandas as pd
from scipy.io import wavfile
from tqdm import tqdm


def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--file_dir", default="./", type=str)
    parser.add_argument("--filename_patterns", type=str)

    parser.add_argument("--train_dataset", default="train.xlsx", type=str)
    parser.add_argument("--valid_dataset", default="valid.xlsx", type=str)

    parser.add_argument("--label_plan", default="4", type=str)

    args = parser.parse_args()
    return args


def get_dataset(args):
    filename_patterns = args.filename_patterns
    filename_patterns = filename_patterns.split(" ")
    print(filename_patterns)

    file_dir = Path(args.file_dir)
    file_dir.mkdir(exist_ok=True)

    if args.label_plan == "2-voicemail":
        label_map = {
            "bell": "voicemail",
            "white_noise": "non_voicemail",
            "low_white_noise": "non_voicemail",
            "high_white_noise": "non_voicemail",
            # "music": "non_voicemail",
            "mute": "non_voicemail",
            "noise": "non_voicemail",
            "noise_mute": "non_voicemail",
            "voice": "non_voicemail",
            "voicemail": "voicemail",
        }
    elif args.label_plan == "2":
        label_map = {
            "bell": "non_voice",
            "white_noise": "non_voice",
            "low_white_noise": "non_voice",
            "high_white_noise": "non_voice",
            "music": "non_voice",
            "mute": "non_voice",
            "noise": "non_voice",
            "noise_mute": "non_voice",
            "voice": "voice",
            "voicemail": "voice",
        }
    elif args.label_plan == "3":
        label_map = {
            "bell": "voicemail",
            "white_noise": "mute",
            "low_white_noise": "mute",
            "high_white_noise": "mute",
            # "music": "music",
            "mute": "mute",
            "noise": "voice_or_noise",
            "noise_mute": "voice_or_noise",
            "voice": "voice_or_noise",
            "voicemail": "voicemail",
        }
    elif args.label_plan == "4":
        label_map = {
            "bell": "voicemail",
            "white_noise": "mute",
            "low_white_noise": "mute",
            "high_white_noise": "mute",
            # "music": "music",
            "mute": "mute",
            "noise": "noise",
            "noise_mute": "noise",
            "voice": "voice",
            "voicemail": "voicemail",
        }
    elif args.label_plan == "8":
        label_map = {
            "bell": "bell",
            "white_noise": "white_noise",
            "low_white_noise": "white_noise",
            "high_white_noise": "white_noise",
            "music": "music",
            "mute": "mute",
            "noise": "noise",
            "noise_mute": "noise_mute",
            "voice": "voice",
            "voicemail": "voicemail",
        }
    else:
        raise AssertionError

    result = list()
    for filename_pattern in filename_patterns:
        filename_list = glob(filename_pattern)
        for filename in tqdm(filename_list):
            filename = Path(filename)
            sample_rate, signal = wavfile.read(filename.as_posix())
            if len(signal) < sample_rate * 2:
                continue

            folder = filename.parts[-2]
            country = filename.parts[-4]

            if folder not in label_map.keys():
                continue

            labels = label_map[folder]

            random1 = random.random()
            random2 = random.random()

            result.append({
                "filename": filename,
                "folder": folder,
                "category": country,
                "labels": labels,
                "random1": random1,
                "random2": random2,
                "flag": "TRAIN" if random2 < 0.8 else "TEST",
            })

    df = pd.DataFrame(result)
    pivot_table = pd.pivot_table(df, index=["labels"], values=["filename"], aggfunc="count")
    print(pivot_table)

    df = df.sort_values(by=["random1"], ascending=False)
    df.to_excel(
        file_dir / "dataset.xlsx",
        index=False,
        # encoding="utf_8_sig"
    )

    return


def split_dataset(args):
    """分割训练集, 测试集"""
    file_dir = Path(args.file_dir)
    file_dir.mkdir(exist_ok=True)

    df = pd.read_excel(file_dir / "dataset.xlsx")

    train = list()
    test = list()

    for i, row in df.iterrows():
        flag = row["flag"]
        if flag == "TRAIN":
            train.append(row)
        else:
            test.append(row)

    train = pd.DataFrame(train)
    train.to_excel(
        args.train_dataset,
        index=False,
        # encoding="utf_8_sig"
    )
    test = pd.DataFrame(test)
    test.to_excel(
        args.valid_dataset,
        index=False,
        # encoding="utf_8_sig"
    )

    return


def main():
    args = get_args()
    get_dataset(args)
    split_dataset(args)
    return


if __name__ == "__main__":
    main()