File size: 3,219 Bytes
51543c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import string

import pandas as pd


def read_new_data(metadata_path):
    df = pd.read_csv(metadata_path, sep=',')
    return df


if __name__ == '__main__':
    folder_path = "C:\\Users\\B-brave_090624\\Documents\\audio3110_3\\"
    metadata_path = "metadata_3110_3.csv"
    new_df = read_new_data(metadata_path)
    existing_df = pd.read_csv("metadata.csv", sep=',')
    existing_train_df = existing_df[existing_df['file_name'].str.startswith('data/train/')]
    existing_test_df = existing_df[existing_df['file_name'].str.startswith('data/test/')]
    new_train_df = new_df[new_df['file_name'].str.startswith('data/train/')]
    new_test_df = new_df[new_df['file_name'].str.startswith('data/test/')]
    sum_length = len(existing_train_df) + len(existing_test_df) + len(new_train_df) + len(new_test_df)

    for i, value in enumerate(new_train_df['file_name']):
        if value in existing_train_df['file_name'].values:
            solved = False
            for suffix in string.ascii_lowercase:
                new_value = value.split('.')[0] + '_' + suffix + '.wav'
                if new_value not in existing_train_df['file_name'].values:
                    solved = True
                    new_train_df.iloc[i, new_train_df.columns.get_loc('file_name')] = new_value
                    file_name = value.split('/')[-1]
                    new_file_name = file_name.split('.')[0] + '_' + suffix + '.wav'
                    os.rename(folder_path + file_name, folder_path + new_file_name)
                    print(f"{file_name} renamed to {new_file_name} in TRAINING SET")
                    break
            if not solved:
                print(f"{value} not solved")

    print(f"EXISTING TRAIN LENGTH: {len(existing_train_df)}")
    print(f"NEW TRAIN LENGTH: {len(new_train_df)}")

    existing_train_df = pd.concat([existing_train_df, new_train_df], ignore_index=True, sort=False)
    for i, value in enumerate(new_test_df['file_name']):
        if value in existing_test_df['file_name'].values:
            solved = False
            for suffix in string.ascii_lowercase:
                new_value = value.split('.')[0] + '_' + suffix + '.wav'
                if new_value not in existing_test_df['file_name'].values:
                    solved = True
                    new_test_df.iloc[i, new_test_df.columns.get_loc('file_name')] = new_value
                    file_name = value.split('/')[-1]
                    new_file_name = file_name.split('.')[0] + '_' + suffix + '.wav'
                    os.rename(folder_path + file_name, folder_path + new_file_name)
                    print(f"{file_name} renamed to {new_file_name} in TEST SET")
                    break
            if not solved:
                print(f"{value} not solved")

    print(f"EXISTING TEST LENGTH: {len(existing_test_df)}")
    print(f"NEW TEST LENGTH: {len(new_test_df)}")

    existing_test_df = pd.concat([existing_test_df, new_test_df], ignore_index=True, sort=False)

    result = pd.concat([existing_train_df, existing_test_df], ignore_index=True, sort=False)
    print(f"FINAL DF LENGTH: {len(result)}")
    print(f"SUM LENGTH: {sum_length}")

    result.to_csv("metadata.csv", sep=',', index=False)