-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreyson_first.py
164 lines (128 loc) · 5.21 KB
/
greyson_first.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
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 13 14:32:33 2018
@author: Xuan
"""
""" Loading the file """
import tensorflow as tf
import numpy as np
import scipy.io as sio
from sklearn.metrics import r2_score
import nexfile
from my_nex_class import my_nex_class
def get_one_minute_data(bin_size,start_min,X,y):
start_ind = int(np.floor(start_min*60/bin_size))
end_ind = int(start_ind + np.floor(60/bin_size))
X_one_minute = X[start_ind:end_ind,:]
y_one_minute = y[start_ind:end_ind]
return X_one_minute, y_one_minute
def get_n_minute_data(bin_size, start_min, end_min, X, y):
X_n = np.empty((0,np.size(X,1)))
y_n = np.empty((0,np.size(y,1)))
for i in range(start_min, end_min):
temp1, temp2 = get_one_minute_data(0.05,i,X,y)
X_n = np.vstack((X_n, temp1))
y_n = np.vstack((y_n, temp2))
return X_n, y_n
def form_rnn_data(X, y, N):
n_X, dim_X, n_y, dim_y = np.size(X, 0), np.size(X, 1), np.size(y, 0), np.size(y,1)
X_out, y_out = np.empty((n_X - N + 1, N, dim_X)), np.empty((n_y - N + 1, dim_y))
for i in range(N-1, n_X):
temp = np.vstack((X[j,:] for j in range(i-N+1,i+1)))
X_out[i-N+1, :, :] = temp
y_out[i-N+1,:] = y[i, :]
return X_out, y_out
def next_batch(batch_size, X, y):
t0 = np.random.randint(0, len(X))
tn = t0 + batch_size
return X[t0:tn, :, :], y[t0:tn, :]
def reconstruct_vaf(y, y_hat):
return 1 - np.sum((y-y_hat)**2)/np.sum((y - np.mean(y, axis = 0))**2)
def get_data(file_name):
reader = nexfile.Reader(useNumpy=True)
data = reader.ReadNex5File(file_name)
myNex = my_nex_class(file_name)
spike_data = myNex.grab_spike_data()
spike_names = myNex.grab_spike_names()
cont_data = myNex.grab_cont_data()
cont_names = myNex.grab_cont_names()
firing_rate = myNex.bin_spike_data(0.05)
spike = myNex.smooth_firing_rate(firing_rate, 0.05)
kin_p = myNex.cont_downsample(1/0.05)
return spike, kin_p
file_name1 = "Z:/data/Greyson_17L2/NeuroexplorerFile/20180813_Greyson_PG_001.nex5"
file_name2 = "Z:/data/Greyson_17L2/NeuroexplorerFile/20180813_Greyson_PG_002.nex5"
file_name3 = "Z:/data/Greyson_17L2/NeuroexplorerFile/20180813_Greyson_PG_003.nex5"
spike1, kin_p1 = get_data(file_name1)
spike2, kin_p2 = get_data(file_name2)
spike3, kin_p3 = get_data(file_name3)
#%%
from sklearn.preprocessing import MinMaxScaler
tf.reset_default_graph()
n_steps = 10
n_inputs = 96
n_neurons = n_inputs
n_outputs = 1
spike_train, kin_p_train = get_n_minute_data(0.05,0,10,spike3,kin_p3)
spike_train, kin_p_train = form_rnn_data(spike_train, kin_p_train, 10)
kin_p_train = kin_p_train[:,0] - kin_p_train[:,1]
kin_p_train = kin_p_train.reshape((np.size(kin_p_train,0),1))
scaler = MinMaxScaler()
kin_p_train = scaler.fit_transform(kin_p_train)
spike_test, kin_p_test = get_n_minute_data(0.05,10,15,spike3,kin_p3)
spike_test, kin_p_test = form_rnn_data(spike_test, kin_p_test, 10)
kin_p_test = kin_p_test[:,0] - kin_p_test[:,1]
kin_p_test = kin_p_test.reshape((np.size(kin_p_test,0),1))
kin_p_test = scaler.fit_transform(kin_p_test)
X = tf.placeholder(tf.float64, [None, n_steps, n_inputs])
y = tf.placeholder(tf.float64, [None, n_outputs])
basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons, activation=tf.nn.elu)
lstm_cell = tf.contrib.rnn.LSTMCell(num_units=n_neurons)
gru_cell = tf.contrib.rnn.GRUCell(num_units=n_neurons)
rnn_outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float64)
out_temp = rnn_outputs[:, n_steps-1, :]
learning_rate = 0.001
outputs = tf.layers.dense(out_temp, n_outputs, activation = tf.nn.softplus)
loss = tf.reduce_mean(tf.square(outputs - y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
training_op = optimizer.minimize(loss)
init = tf.global_variables_initializer()
n_epoch = 8
batch_size = 50
n_batches = np.size(spike_train,0)//batch_size
with tf.Session() as sess:
init.run()
for epoch in range(n_epoch):
print(epoch)
for iteration in range(n_batches):
X_batch, y_batch = next_batch(batch_size, spike_train, kin_p_train)
sess.run(training_op, feed_dict={X: X_batch, y: y_batch})
if epoch % 2 == 0:
mse = loss.eval(feed_dict={X: X_batch, y: y_batch})
print(epoch, "\tMSE:", mse)
y_pred_basic = sess.run(outputs, feed_dict={X: spike_test})
res_basic = r2_score(kin_p_test, y_pred_basic)
print(res_basic)
#%%
import matplotlib.pyplot as plt
plt.figure(1)
plt.plot(kin_p_test[200:700,0],'b')
plt.plot(y_pred_basic[200:700,0],'r')
#%%
import matplotlib.pyplot as plt
plt.figure(1)
fig,ax = plt.subplots(figsize = (10,4),dpi = 100)
plt.ylim(0,1)
#plt.xlim(0, 34)
plt.subplots_adjust(bottom = 0.15)
t = np.arange(0,400,1)
ax.set_title('Decoding grasping force from cortical signals (Greyson)',fontsize=14)
#plt.xticks(t*0.05)
plt.grid()
ax.set_xlabel(xlabel='time (s)',fontsize=12)
ax.set_ylabel(ylabel='force', fontsize = 12)
p1, = plt.plot(t*0.05,kin_p_test[300:700], 'k-')
p2, = plt.plot(t*0.05,y_pred_basic[300:700], 'b-')
#p3 = plt.plot(t*0.05,y_pred_lstm[200:700], 'r-')
p4, = plt.plot(t*0.05,y_pred_gru[300:700], 'r-')
l1 = plt.legend([p1, p2, p4], ["Actual", "Basic RNN Cell ($R^2$ = 0.63)", "GRU Cell ($R^2$ = 0.69)"], loc='upper right',fontsize = 10)