deepsynthbody/deepfake_ecg_full_train_validation_test
222
1import pandas as pd2 3import os4import shutil5 6def split_csv(input_file, train_file, validate_file, test_file, train_size, validate_size, test_size):7 # Read the input CSV file8 data = pd.read_csv(input_file)9 10 # Split the data into train, validate, and test subsets11 train_data = data.iloc[:train_size]12 validate_data = data.iloc[train_size:train_size + validate_size]13 test_data = data.iloc[train_size + validate_size:train_size + validate_size + test_size]14 15 # Save the subsets to separate CSV files16 train_data.to_csv(train_file, index=False)17 validate_data.to_csv(validate_file, index=False)18 test_data.to_csv(test_file, index=False)19 20def copy_ecg_files(csv_file, destination_folder, src_folder):21 # Read the CSV file22 data = pd.read_csv(csv_file)23 24 # Create the destination folder if it doesn't exist25 if not os.path.exists(destination_folder):26 os.makedirs(destination_folder)27 28 # Copy the corresponding ECG files to the destination folder29 for index, row in data.iterrows():30 ecg_file = str(row['patid']) + ".asc" # Replace 'ecg_filename' with the appropriate column name containing the ECG file names31 src_path = os.path.join(src_folder, ecg_file)32 dst_path = os.path.join(destination_folder, ecg_file)33 shutil.copy(src_path, dst_path)34 35if __name__ == '__main__':36 37 # Set the input CSV file and the source folder38 dst_dir = '/work/vajira/data/deepfake_ecg_full_train_validation_test'39 input_file = '/work/vajira/data/Deepfake-ecg/filtered_all_normals_121977_ground_truth.csv' # Change this to the name of your CSV file40 train_file = f'{dst_dir}/train.csv'41 validate_file = f'{dst_dir}/validate.csv'42 test_file = f'{dst_dir}/test.csv'43 src_folder = '/work/vajira/data/Deepfake-ecg/filtered_all_normals_121977/from_006_chck_2500_150k_filtered_all_normals_121977' # Change this to the name of the folder containing the ECG files44 45 # Set the sizes of the train, validate, and test subsets from full size of the dataset 12197746 train_size = 97581 # 80% of 121977=9758147 validate_size = 12198 # 10% of 121977=1219848 test_size = 12198 # 10% of 121977=1219849 50 split_csv(input_file, train_file, validate_file, test_file, train_size, validate_size, test_size)51 52 # Call the copy_ecg_files function to copy the ECG files into the corresponding folders53 copy_ecg_files(train_file, f'{dst_dir}/train', src_folder)54 copy_ecg_files(validate_file, f'{dst_dir}/validation', src_folder)55 copy_ecg_files(test_file, f'{dst_dir}/test', src_folder)56 