-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.py
More file actions
67 lines (52 loc) · 2.16 KB
/
Copy pathbootstrap.py
File metadata and controls
67 lines (52 loc) · 2.16 KB
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
import os
import glob
import pandas as pd
import numpy as np
# Set your source and destination folders
source_folder = '/home/input/'
dest_folder = '/home/output/'
# Keep track of the synthetic MRNs to ensure uniqueness
synthetic_mrns = set()
# Initialize MRN folder counter
mrn_folder_counter = 0
# Initialize total MRNs required
total_mrn_required = 10000
# Loop through each patient MRN folder
for patient_mrn in os.listdir(source_folder):
# Stop if the required MRN folders are generated
if mrn_folder_counter >= total_mrn_required:
break
# Get the list of CSV files for this patient
csv_files = glob.glob(os.path.join(source_folder, patient_mrn, 'RD.*.csv'))
# Skip if no CSV files are found
if not csv_files:
print(f'No CSV files found for {patient_mrn}')
continue
# Loop to generate multiple synthetic MRNs from each original MRN
while mrn_folder_counter < total_mrn_required:
# Generate a unique synthetic MRN
while True:
new_mrn = np.random.randint(10000000, 99999999)
if new_mrn not in synthetic_mrns:
synthetic_mrns.add(new_mrn)
break
# Create a new folder for the synthetic patient data
new_folder = os.path.join(dest_folder, str(new_mrn))
os.makedirs(new_folder, exist_ok=True)
# Increment the MRN folder counter
mrn_folder_counter += 1
# Loop through each CSV file for bootstrapping
for csv_file in csv_files:
try:
# Read the CSV file
csv_data = pd.read_csv(csv_file, on_bad_lines='skip')
# Perform bootstrapping
csv_syn = csv_data.sample(len(csv_data), replace=True)
# Save the synthetic CSV file
csv_name = os.path.basename(csv_file).replace('.csv', '_synthetic.csv')
csv_syn.to_csv(os.path.join(new_folder, csv_name), index=False)
except Exception as e:
# Log any errors
print(f"Error while processing {csv_file}: {e}")
# Bootstrap process is complete
print(f'Bootstrap complete! Total MRN folders generated: {mrn_folder_counter}')