-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_acc.py
114 lines (93 loc) · 3.13 KB
/
eval_acc.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import pandas as pd
import nibabel as nb
import numpy as np
import matplotlib.pyplot as plt
import os
import glob
from sklearn.ensemble import RandomForestClassifier
from sktree import ObliqueRandomForestClassifier, PatchObliqueRandomForestClassifier
from tqdm import tqdm
from sklearn.model_selection import train_test_split
import random
import pickle
import scipy.stats as ss
####################################################
df = pd.read_excel('/cis/home/jdey4/data_MRI/subjects_age_sex_data_MRI.xlsx')
#df.head()
#####################################################
df_quality = pd.read_csv('/cis/home/jdey4/data_MRI/QC_catreport.csv')
#df_quality.head()
####################################################
path = '/cis/home/jdey4/data_MRI/'
subjects = os.listdir(path)
X = []
y = []
file_no = 0
count = 0
count_ = 0
IDs = set(df['ID'])
quality_ID = set(df_quality['sub'])
for subject in tqdm(subjects):
if subject in IDs and subject in quality_ID:
#print(df[df['ID']==subject]['Sex'])
IQR = list(df_quality[df_quality['sub']==subject]['Weighted average (IQR)'])[0]
count += 1
#print(IQR)
if IQR is np.nan:
continue
if IQR[-1] == '%':
continue
if float(IQR) < 60:
continue
count_ += 1
#print(count, count_)
gender = list(df[df['ID']==subject]['Sex'])
sex = int(gender[0]=='FEMALE')
current_file = os.path.join(path, subject)
file_count = 0
files = glob.glob(current_file+'/mri/*')
for file in files:
if 'mwp1' in file:
try:
img = nb.load(file).get_fdata()
file_count +=1
except:
break
for file in files:
if 'mwp2' in file:
try:
img = np.concatenate((img, nb.load(file).get_fdata()),axis=0)
file_count +=1
except:
break
'''if len(tmp)<2:
print(subject, ' has less files')'''
if file_count==2:
X.append(img.reshape(1,-1))
y.append(sex)
X = np.concatenate(X,axis=0)
################################################################
idx = list(range(len(y)))
np.random.seed(0)
np.random.shuffle(idx)
train_samples = int(len(y)*0.8)
test_samples = len(y) - train_samples
train_ids = idx[:train_samples]
predicted_proba_ = []
total_models = 500
for ii in tqdm(range(total_models)):
with open('morf_models/model'+str(ii)+'_gray.pickle','rb') as f:
morf = pickle.load(f)
predicted_proba_.append(
morf.predict_proba(x_test[:,:113*137*113])
)
del morf
with open('morf_models/model'+str(ii)+'_white.pickle','rb') as f:
morf = pickle.load(f)
predicted_proba_.append(
morf.predict_proba(x_test[:,113*137*113:])
)
del morf
predicted_proba = np.mean(predicted_proba_,axis=0)
predicted_label = np.argmax(predicted_proba,axis=1)
print('MORF accuracy', np.mean(predicted_label==y_test))