-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess_csv.py
More file actions
48 lines (40 loc) · 1.65 KB
/
Copy pathpreprocess_csv.py
File metadata and controls
48 lines (40 loc) · 1.65 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
import os
import argparse
import pickle
import pandas as pd
def main():
# Initialize the parser
parser = argparse.ArgumentParser(description="A script that processes two arguments.")
# Add the arguments
parser.add_argument("-i", type=str, help="Input CSV (DLMUSE ROI Volumes + Clinical data (AD_Status (CN, MCI, AD), ADAS-COG-13, MMSE) + Demographics (Age, Sex, PTID, Scan_Time))")
parser.add_argument("-o", type=str, help="Output path")
# Parse the arguments
args = parser.parse_args()
print(args.o)
df_all = pd.read_csv(args.i)
# 1. check all columns exist
with open('references/preprocess_columns_all.pkl','rb') as f:
all_cols = pickle.load(f)
if not set(all_cols).issubset(df_all.columns):
print("Failed to detect all necessary columns from the input CSV.")
return
else:
print("All columns checked. Splitting the CSV.")
# split & recorder csv into each files
files = [
'data_dl_muse_nichart_test_unnorm.csv',
'data_dl_muse_nichart_spare_test_unnorm.csv',
'data_dl_muse_nichart_spare_test_unnorm.csv',
'data_dl_muse_nichart_mmse_test_unnorm.csv',
'data_dl_muse_nichart_adas_test_unnorm.csv'
]
for i in range(len(files)):
ref_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), f"references/columns_{files[i].split('.')[0]}.pkl")
with open(ref_path, 'rb') as cp:
columns = pickle.load(cp)
out_fpath = os.path.join(args.o, files[i])
print(out_fpath)
df_all[columns].to_csv(out_fpath, index=False)
print("Saved:",files[i])
if __name__ == "__main__":
main()