-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_3Dimage.py
171 lines (134 loc) · 5.55 KB
/
input_3Dimage.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# A script to load images and make batch.
# Dependency: 'nibabel' to load MRI (NIFTI) images
# Reference: http://blog.naver.com/kjpark79/220783765651
import csv
#import cv2
import os
import tensorflow as tf
from pip._vendor.progress.bar import Bar
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
import numpy as np
import random
import nibabel as nib
import matplotlib.pyplot as plt
from dicom import logger
from medpy.filter import anisotropic_diffusion, gauss_xminus1d
from medpy.io import load, get_pixel_spacing
from medpy.filter import otsu
from nilearn import image as img
FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_integer("IMAGE_WIDTH",80,"image width")
tf.app.flags.DEFINE_integer("IMAGE_HEIGHT",80,"image heigth")
tf.app.flags.DEFINE_integer("IMAGE_DEPTH",50,"image depth")
batch_index = 0
filenames = []
# user selection
tf.app.flags.DEFINE_string("DATA_DIRECTORY","C:\\Users\SHYAM\Documents\Minor Project\Data","data directory")
tf.app.flags.DEFINE_integer("NUMBER_OF_CLASSES",2,"num of classes")
def get_filenames():
with open(FLAGS.DATA_DIRECTORY +'\participants.tsv') as csvfile:
reader=csv.DictReader(csvfile,delimiter='\t')
for row in reader:
filenames.append(row['participant_id'])
print(filenames)
def get_data_jpeg(sess, data_set, batch_size):
global batch_index, filenames
get_filenames()
max = len(filenames)
print("max")
print(max)
batch_index = 0
begin = batch_index
end = batch_index + batch_size
x_data = np.array([], np.float32)
y_data = np.array([],np.float32) # zero-filled list for 'one hot encoding'
print("Y_DATA SHAPEE")
print(y_data.shape)
#print(begin,end)
for i in range(begin, end):
print(filenames[i])
imagePath = FLAGS.DATA_DIRECTORY + '\\' + 'ds171_R1.0.0' + '\\' + filenames[i] + '\\' + 'func' +'\\' + filenames[i] + '_task-nonmusic_run-4_bold.nii'
print(imagePath)
FA_org = nib.load(imagePath)
FA_data, image_header = load(imagePath)
FA_data = FA_org.get_data() # 256x256x176; numpy.ndarray
print(FA_data.shape)
print(type(FA_data))
#data_output = FA_data > threshold
# apply the watershed
logger.info('Applying anisotropic diffusion with settings: niter={} / kappa={} / gamma={}...'.format(100,0.2,0.4))
#data_output = anisotropic_diffusion(FA_data,100,0,2,0.4,get_pixel_spacing(image_header))
#data_output=gauss_xminus1d(FA_data,1,3)
'''fig = plt.figure()
for num, each_slice in enumerate(FA_data[:10]):
y = fig.add_subplot(2,5,num+1)
new_img = cv2.resize(np.array(each_slice),150)
y.imshow(new_img)
plt.show()'''
# save file
#np.save(data_output,'/output', image_header, 2)
# TensorShape([Dimension(256), Dimension(256), Dimension(176)])
resized_image = tf.image.resize_images(images=FA_data, size=(FLAGS.IMAGE_WIDTH,FLAGS.IMAGE_HEIGHT), method=1)
print("x_data")
print(x_data.shape)
print("resized_image")
print(resized_image.shape)
image = sess.run(resized_image) # (256,256,40)
print("image shape")
print(image.shape)
x_data = np.append(x_data, np.asarray(image, dtype='float32')) # (image.data, dtype='float32')
y_data = np.append(y_data,0)
batch_index += batch_size # update index for the next batch
print("x data shaope")
print(x_data.shape)
x_data_ = x_data.reshape(batch_size, FLAGS.IMAGE_WIDTH * FLAGS.IMAGE_HEIGHT * FLAGS.IMAGE_DEPTH)
#y_data_ = y_data.reshape(batch_size, FLAGS.height * FLAGS.width * FLAGS.depth, -1)
print("x data shaope")
print(len(x_data))
print("y data shape")
print(y_data)
return x_data_, y_data
def PCA_calculate(data):
nsamples, nx, ny = data.shape
X_std = StandardScaler().fit_transform(data.reshape((nsamples,nx*ny)))
mean_vec = np.mean(X_std, axis=0)
cov_mat = (X_std - mean_vec).T.dot((X_std - mean_vec)) / (X_std.shape[0] - 1)
print('Covariance matrix \n%s' % cov_mat)
#print('NumPy covariance matrix: \n%s' % np.cov(X_std.T))
eig_vals, eig_vecs = np.linalg.eig(cov_mat)
print('Eigenvectors \n%s' % eig_vecs)
print('\nEigenvalues \n%s' % eig_vals)
cor_mat1 = np.corrcoef(X_std.T)
eig_vals, eig_vecs = np.linalg.eig(cor_mat1)
print('Eigenvectors \n%s' % eig_vecs)
print('\nEigenvalues \n%s' % eig_vals)
cor_mat2 = np.corrcoef(data.T)
eig_vals, eig_vecs = np.linalg.eig(cor_mat2)
print('Eigenvectors \n%s' % eig_vecs)
print('\nEigenvalues \n%s' % eig_vals)
u, s, v = np.linalg.svd(X_std.T)
for ev in eig_vecs:
np.testing.assert_array_almost_equal(1.0, np.linalg.norm(ev))
print('Everything ok!')
# Make a list of (eigenvalue, eigenvector) tuples
eig_pairs = [(np.abs(eig_vals[i]), eig_vecs[:, i]) for i in range(len(eig_vals))]
# Sort the (eigenvalue, eigenvector) tuples from high to low
eig_pairs.sort()
eig_pairs.reverse()
# Visually confirm that the list is correctly sorted by decreasing eigenvalues
print('Eigenvalues in descending order:')
for i in eig_pairs:
print(i[0])
tot = sum(eig_vals)
var_exp = [(i / tot) * 100 for i in sorted(eig_vals, reverse=True)]
cum_var_exp = np.cumsum(var_exp)
trace1 = Bar(
x=['PC %s' % i for i in range(1, 5)],
y=var_exp,
showlegend=False)
trace2 = Scatter(
x=['PC %s' % i for i in range(1, 5)],
y=cum_var_exp,
name='cumulative explained variance')
data = Data([trace1, trace2])