-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccuracy_run.py
137 lines (104 loc) · 3.75 KB
/
accuracy_run.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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_quality = pd.read_csv('/cis/home/jdey4/data_MRI/QC_catreport.csv')
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().reshape(-1)
file_count +=1
except:
break
for file in files:
if 'mwp2' in file:
try:
img = np.concatenate((img, nb.load(file).get_fdata().reshape(-1)))
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)
y = np.array(y)
print('data shape', X.shape)
print('mean label', np.mean(y))
####################################################
total_models = 1000
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
test_ids = idx[train_samples:]
X_test = X[test_ids].copy()
y_test = y[test_ids].copy()
del X
del y
predicted_proba_ = np.zeros((len(y_test),2), dtype=float)
for ii in tqdm(range(total_models)):
model_path = '/cis/home/jdey4/sex_classification/morf_models/model'+str(ii)+'_gray.pickle'
if os.path.exists(model_path):
with open(model_path,'rb') as f:
morf = pickle.load(f)
else:
model_path = '/cis/project/jdey4_projects/morf_models/model'+str(ii)+'_gray.pickle'
with open(model_path,'rb') as f:
morf = pickle.load(f)
predicted_proba_ += morf.predict_proba(X_test[:,:113*137*113])
del morf
####################################################
model_path = '/cis/home/jdey4/sex_classification/morf_models/model'+str(ii)+'_white.pickle'
if os.path.exists(model_path):
with open(model_path,'rb') as f:
morf = pickle.load(f)
else:
model_path = '/cis/project/jdey4_projects/morf_models/model'+str(ii)+'_white.pickle'
with open(model_path,'rb') as f:
morf = pickle.load(f)
predicted_proba_ += morf.predict_proba(X_test[:,:113*137*113])
del morf
#predicted_proba = np.mean(predicted_proba_,axis=0)
predicted_label = np.argmax(predicted_proba_/(2*total_models),axis=1)
print('MORF accuracy ', np.mean(predicted_label==y_test))