|
a |
|
b/src/preprocessing.py |
|
|
1 |
import numpy as np |
|
|
2 |
import pandas as pd |
|
|
3 |
import os |
|
|
4 |
import click |
|
|
5 |
import glob |
|
|
6 |
import cv2 |
|
|
7 |
import pydicom |
|
|
8 |
from tqdm import tqdm |
|
|
9 |
from utils import get_windowing, window_image |
|
|
10 |
from joblib import delayed, Parallel |
|
|
11 |
|
|
|
12 |
|
|
|
13 |
@click.group() |
|
|
14 |
def cli(): |
|
|
15 |
print("CLI") |
|
|
16 |
|
|
|
17 |
|
|
|
18 |
windows_range = { |
|
|
19 |
'brain': [40, 80], |
|
|
20 |
'bone': [600, 2800], |
|
|
21 |
'subdual': [75, 215] |
|
|
22 |
} |
|
|
23 |
|
|
|
24 |
|
|
|
25 |
def convert_dicom_to_jpg(dicomfile, outputdir): |
|
|
26 |
try: |
|
|
27 |
data = pydicom.read_file(dicomfile) |
|
|
28 |
image = data.pixel_array |
|
|
29 |
window_center, window_width, intercept, slope = get_windowing(data) |
|
|
30 |
id = dicomfile.split("/")[-1].split(".")[0] |
|
|
31 |
|
|
|
32 |
images = [] |
|
|
33 |
for k, v in windows_range.items(): |
|
|
34 |
image_windowed = window_image(image, v[0], v[1], intercept, slope) |
|
|
35 |
images.append(image_windowed) |
|
|
36 |
|
|
|
37 |
images = np.asarray(images).transpose((1, 2, 0)) |
|
|
38 |
output_image = os.path.join(outputdir, id + ".jpg") |
|
|
39 |
cv2.imwrite(output_image, images) |
|
|
40 |
except: |
|
|
41 |
print(dicomfile) |
|
|
42 |
|
|
|
43 |
|
|
|
44 |
@cli.command() |
|
|
45 |
@click.option('--inputdir', type=str) |
|
|
46 |
@click.option('--outputdir', type=str) |
|
|
47 |
def extract_images( |
|
|
48 |
inputdir, |
|
|
49 |
outputdir, |
|
|
50 |
): |
|
|
51 |
os.makedirs(outputdir, exist_ok=True) |
|
|
52 |
files = glob.glob(inputdir + "/*.dcm") |
|
|
53 |
Parallel(n_jobs=8)(delayed(convert_dicom_to_jpg)(file, outputdir) for file in tqdm(files, total=len(files))) |
|
|
54 |
|
|
|
55 |
|
|
|
56 |
def split_by_patient( |
|
|
57 |
train_csv, |
|
|
58 |
train_meta_csv, |
|
|
59 |
n_folds, |
|
|
60 |
outdir |
|
|
61 |
): |
|
|
62 |
os.makedirs(outdir, exist_ok=True) |
|
|
63 |
train_df = pd.read_csv(train_csv) |
|
|
64 |
train_meta_df = pd.read_csv(train_meta_csv) |
|
|
65 |
train_meta_df['ID'] = train_meta_df['ID'].apply(lambda x: "_".join(x.split("_")[:2])) |
|
|
66 |
train_meta_df = train_meta_df[['ID', 'PatientID']] |
|
|
67 |
|
|
|
68 |
|
|
|
69 |
|
|
|
70 |
if __name__ == '__main__': |
|
|
71 |
cli() |