-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
133 lines (116 loc) · 4.62 KB
/
Copy pathparser.py
File metadata and controls
133 lines (116 loc) · 4.62 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
Parser for the longitudinal dataset. Moves files to the classified folders.
"""
import argparse
import gc
import os
import shutil
import pandas as pd
from parser_long_cfg import IN_DIR, OUT_DIR
import ast
parser = argparse.ArgumentParser(description="Process some files.")
parser.add_argument(
"--start-file",
type=str,
help="The file to start processing from",
# default="2305625_file_paths.csv",
)
args = parser.parse_args()
start_file = args.start_file
def extract_ids_from_path(path):
"""
Extracts the patient and scan ids from the path.
"""
ids = path.split("/")[-3:-1]
return ids
sub_directories = ["FLAIR", "T1", "T1c", "T2", "OTHER", "NO PREDICTION"]
ignore_strings = [
"spine",
"ctl",
"lumbar",
"tlsp",
"tsp",
"lsp",
"csp",
"t1_flair",
"stir",
"left",
"right",
"dti",
"dwi",
]
for sub_dir in sub_directories:
output_dir = OUT_DIR
os.makedirs(os.path.join(output_dir, sub_dir), exist_ok=True)
csv_files = [f for f in os.listdir(IN_DIR) if f.endswith(".csv")]
file_started = False if start_file else True
for file_name in csv_files:
if not file_started:
if file_name == start_file:
file_started = True
else:
continue
print(f"\tProcessing file: {file_name}")
for chunk in pd.read_csv(
os.path.join(IN_DIR, file_name), chunksize=10000
): # adjust chunksize based on your system's memory
chunk["Image Spacing (x,y,z)"] = chunk["Image Spacing (x,y,z)"].apply(
lambda x: ast.literal_eval(x) if pd.notnull(x) else (0, 0, 0)
)
chunk["Image Dimensions (x,y,z)"] = chunk["Image Dimensions (x,y,z)"].apply(
lambda x: ast.literal_eval(x) if pd.notnull(x) else (0, 0, 0)
)
chunk["Spacing_X"], chunk["Spacing_Y"], _ = zip(*chunk["Image Spacing (x,y,z)"])
chunk["Dimension_X"], chunk["Dimension_Y"], _ = zip(*chunk["Image Dimensions (x,y,z)"])
mask = (
(chunk["Spacing_X"] < 2.0)
& (chunk["Spacing_Y"] < 2.0)
& (chunk["Dimension_X"] >= 256)
& (chunk["Dimension_Y"] >= 256)
)
for prediction in sub_directories:
if prediction != "NO PREDICTION":
df_filtered = chunk[mask & (chunk["Prediction"] == prediction)]
if not df_filtered.empty:
df_filtered.to_csv(os.path.join(output_dir, prediction, file_name), index=False)
for _, row in df_filtered.iterrows():
assert os.path.exists(row["Path"])
if not any(
ignore_string in row["Path"] for ignore_string in ignore_strings
):
patient_id, scan_id = extract_ids_from_path(row["Path"])
dest_path = os.path.join(
output_dir,
prediction,
f'{patient_id}_{scan_id}_{os.path.basename(row["Path"])}',
)
if not os.path.exists(dest_path):
try:
shutil.copyfile(row["Path"], dest_path)
except IOError as e:
print(f"Unable to copy file. {e}")
except SystemError as error:
print("Unexpected error:", error)
no_prediction_df = chunk[~mask | chunk["Prediction"].str.startswith("NO PREDICTION")]
if not no_prediction_df.empty:
no_prediction_df.to_csv(
os.path.join(output_dir, "NO PREDICTION", file_name), index=False
)
for _, row in no_prediction_df.iterrows():
assert os.path.exists(row["Path"])
if not any(ignore_string in row["Path"] for ignore_string in ignore_strings):
patient_id, scan_id = extract_ids_from_path(row["Path"])
dest_path = os.path.join(
output_dir,
"NO PREDICTION",
f'{patient_id}_{scan_id}_{os.path.basename(row["Path"])}',
)
if not os.path.exists(dest_path):
try:
shutil.copyfile(row["Path"], dest_path)
except IOError as e:
print(f"Unable to copy file. {e}")
except SystemError as error:
print("Unexpected error:", error)
del chunk
gc.collect()