luigimontaleone's picture
dati aggiornati al 31.10
51543c3
raw
history blame
3.22 kB
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)