-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathpre_processing.py
91 lines (70 loc) · 3.01 KB
/
pre_processing.py
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
# basic libs
import pandas as pd
import numpy as np
import json
from sklearn.model_selection import KFold
import os
import gc
from tqdm import tqdm
from shutil import rmtree
from sklearn.preprocessing import MinMaxScaler
from scipy.signal import resample
# pytorch
from torch import nn
import torch
class PrepareData:
def __init__(self, input_folders, split_folder, split_table_name):
self.input_folders = input_folders
self.split_folder = split_folder
self.split_table_name = split_table_name
def run(self):
# download a json file for exclusions
exclude = json.load(open(self.split_folder + 'exclude.json'))
# get a list of patients
self.patients = []
for input_folder in self.input_folders:
for patient in [i for i in os.listdir(input_folder) if i.find('.npy') != -1]:
if patient[:-4] in exclude.items():
continue
else:
self.patients.append(patient)
# split data into folds
self.split_table = self.create_split_table()
print('Total number of patients: ', len(self.patients))
return 0
def load_labels(self, name):
y = json.load(open(name + '.json'))
return y
def create_split_table(self):
kfold = KFold(n_splits=6, random_state=42, shuffle=True)
split_table = []
for index, (train, val) in enumerate(kfold.split(self.input_folders)):
split = {}
train = [i for index, i in enumerate(self.input_folders) if index in train.tolist()]
val = [i for index, i in enumerate(self.input_folders) if index in val.tolist()]
patients_train = []
patients_val = []
for dataset in train:
patients = [i for i in os.listdir(dataset) if i.find('.npy') != -1]
print(f'Start checking each patient in dataset {dataset[7]}... for training')
for patient in tqdm(patients):
y = self.load_labels(dataset + patient[:-4])
if y['labels_training_merged'] == None:
continue
patients_train.append(dataset + patient[:-4])
for dataset in val:
patients = [i for i in os.listdir(dataset) if i.find('.npy') != -1]
print(f'Start checking each patient in dataset {dataset[7]}... for validation')
for patient in patients:
y = self.load_labels(dataset + patient[:-4])
if y['labels_training_merged'] == None:
continue
patients_val.append(dataset + patient[:-4])
split['train'] = patients_train
split['val'] = patients_val
split_table.append(split)
with open(self.split_folder + str(index) + '_fold.json', 'w') as outfile:
json.dump(split, outfile)
# Generate DataFrame
split_table = pd.DataFrame(split_table)
return split_table