-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpreprocess.py
More file actions
152 lines (135 loc) · 4.66 KB
/
Copy pathpreprocess.py
File metadata and controls
152 lines (135 loc) · 4.66 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import argparse
import os
import cv2
import numpy as np
import pandas as pd
from tqdm import tqdm
LABEL_PATH = "padchest/PADCHEST_chest_x_ray_images_labels_160K_01.02.19.csv"
def parse_args():
parser = argparse.ArgumentParser(description="Preprocess PADCHEST dataset")
parser.add_argument(
"-src",
"--src_dir",
help="Source directory of images",
type=str,
required=True,
)
parser.add_argument(
"-dst",
"--dst_dir",
help="Destination directory of pre-processed images and text files",
type=str,
required=True,
)
parser.add_argument(
"-l",
"--labels",
help="Path to labels csv file",
type=str,
default=LABEL_PATH,
)
parser.add_argument(
"-dir",
"--img_dir",
help="Image directory name",
type=int,
default=None,
)
parser.add_argument(
"-p",
"--prompt",
help="generate prompt text files",
action="store_true",
)
parser.add_argument(
"-c",
"--convert",
help="convert grayscale images to RGB",
action="store_true",
)
return parser.parse_args()
def process_prompts(src_dir, dst_dir, labels, ImageDir=None):
labels = pd.read_csv(labels, index_col=0)
# filter the PD for the ImageDir
if ImageDir:
labels = labels[labels["ImageDir"] == ImageDir]
# Calculate age, If PatientBirth is NA, age is NA otherwise age is year - birth
labels["birth"] = labels["PatientBirth"]
labels["year"] = labels["StudyDate_DICOM"].astype(str).str[:4].astype(int)
labels["age"] = (labels["year"] - labels["birth"]).astype("Int64")
# Generate prompts
labels["age_prompt"] = labels["age"].apply(
lambda x: "" if pd.isna(x) else "age " + str(x)
)
labels["sex_prompt"] = labels["PatientSex_DICOM"].apply(
lambda x: " male, " if x == "M" else " female, " if x == "F" else ", "
)
labels["view_prompt"] = labels["ViewPosition_DICOM"].apply(
lambda x: "" if pd.isna(x) else "view " + str(x) + ", "
)
labels["projection_prompt"] = labels["Projection"].apply(
lambda x: "" if pd.isna(x) else "projection " + str(x) + ", "
)
labels["modality_prompt"] = labels["Modality_DICOM"].apply(
lambda x: "" if pd.isna(x) else "modality " + str(x) + ", "
)
labels["diagnosis_prompt"] = labels["LabelsLocalizationsBySentence"].apply(
lambda x: "" if pd.isna(x) else "diagnosis " + str(x)
)
labels["Prompt"] = (
"Chest X-ray, "
+ labels["age_prompt"]
+ labels["sex_prompt"]
+ labels["view_prompt"]
+ labels["projection_prompt"]
+ labels["modality_prompt"]
+ labels["diagnosis_prompt"]
)
# Remove square brackets and single quotes
labels["Prompt"] = (
labels["Prompt"].str.replace("[", "").str.replace("]", "").str.replace("'", "")
)
# Save a csv file with 3 columns, ImageID, ImageDir, and Prompt
labels[["ImageID", "ImageDir", "Prompt"]].to_csv(
os.path.join(dst_dir, "PADCHEST_Prompt.csv"), index=False
)
print("Generating prompt text files:")
with tqdm(total=len(os.listdir(src_dir))) as pbar:
for image in os.listdir(src_dir):
if image in labels["ImageID"].values:
prompt = labels[labels["ImageID"] == image]["Prompt"].values[0]
with open(
os.path.join(dst_dir, "data", image.split(".")[0] + ".txt"), "w"
) as f:
f.write(prompt)
pbar.update(1)
print("Prompt generation complete!")
def convert_to_RGB(src_dir, dst_dir):
print("Converting grayscale images to RGB:")
with tqdm(total=len(os.listdir(src_dir))) as pbar:
for img_path in os.listdir(src_dir):
if img_path.endswith(".png"):
img = cv2.imread(
os.path.join(src_dir, img_path), cv2.IMREAD_GRAYSCALE
)
# expand grayscale to RGB
img = np.expand_dims(img, axis=2)
img = np.repeat(img, 3, axis=2)
cv2.imwrite(
os.path.join(dst_dir, "data", img_path), img
)
pbar.update(1)
print("Image conversion complete!")
if __name__ == "__main__":
args = parse_args()
if not os.path.exists(args.dst_dir):
os.mkdir(args.dst_dir)
if args.prompt:
process_prompts(
src_dir=args.src_dir,
dst_dir=args.dst_dir,
labels=args.labels,
ImageDir=args.img_dir,
)
if args.convert:
convert_to_RGB(src_dir=args.src_dir, dst_dir=args.dst_dir)