Commit ced6f0fc authored by Alex Fout's avatar Alex Fout

reconfigured so it trains on con and nocon and can select a certain number of data ponts to keep

parent f1801294
......@@ -4,10 +4,37 @@ from itertools import cycle
import sys
from config import pid_noConcussion, pid_3stepProtocol, pid_testRetest, pid_concussion, feature_functions, epoch_size, \
embedding_args, pid_testlist
embedding_args, pid_testlist, channels
from patient import Patient
from embedding import Embedding
colors = cycle(['r', 'b', 'g', 'y', 'c', 'm', 'k'])
def embed_and_plot(emb, examples):
pre_post_distances = []
alpha = 0.5 / np.log(len(examples)) if len(examples) > 1 else 1
for tup in examples:
if sys.version_info < (3, 0):
# for python2 use
color = colors.next()
else:
# for python3 use
color = next(colors)
pid = tup[0]
pre_emb = emb.embed(tup[1])
post_emb = emb.embed(tup[2])
plt.plot(pre_emb[:, 0], pre_emb[:, 1], linestyle='None', marker="x", color=color, label=str(pid) + "_pre", alpha=alpha)
plt.plot(post_emb[:, 0], post_emb[:, 1], linestyle='None', marker="o", color=color, label=str(pid) + "_post", alpha=alpha)
# calculate centriods and plot a line
pre_cent = centroid(pre_emb)
post_cent = centroid(post_emb)
plt.plot([pre_cent[0], post_cent[0]], [pre_cent[1], post_cent[1]], '-', linewidth=3, color=color)
# record distance
pre_post_distances.append(np.linalg.norm(post_cent - pre_cent))
return pre_post_distances
def centroid(data):
length = len(data)
x_sum = np.sum(data[:, 0])
......@@ -15,57 +42,72 @@ def centroid(data):
return np.array([float(x_sum)/length, float(y_sum)/length])
# get training data from un-concussed individuals
noCon_pats= []
step_pats = []
retest_pats = []
con_pats =[]
noCon_ex= []
step_ex = []
retest_ex = []
con_ex =[]
n_keep = -1
# for lst, pat_list in zip([pid_noConcussion, pid_3stepProtocol, pid_testRetest, pid_concussion], [noCon_pats, step_pats, retest_pats, con_pats]):
#for lst, pat_list in zip([pid_noConcussion], [noCon_pats]):
for lst, pat_list in zip([pid_testlist], [noCon_pats]):
for pid in lst:
print("Processing pid: {}".format(pid))
p = Patient(pid)
# get examples from pre_test
if p.pre_test is not None:
p.pre_test.remove_artifacts()
p.pre_test.get_examples(feature_functions, epoch_size=epoch_size)
# get examples from post_test
if p.post_test is not None:
p.post_test.remove_artifacts()
post = p.post_test.get_examples(feature_functions, epoch_size=epoch_size)
if p.pre_test is not None and p.post_test is not None:
pat_list.append(p)
for pid in pid_noConcussion:
print("Processing pid: {}".format(pid))
p = Patient(pid, load_session_raw=False, load_session_examples=True)
# get examples from pre_test
pre = post = None
if p.pre_test is not None:
pre = p.pre_test.load_examples()
if pre is not None:
np.random.shuffle(pre)
# get examples from post_test
if p.post_test is not None:
post = p.post_test.load_examples()
if post is not None:
np.random.shuffle(post)
if post is not None and pre is not None:
noCon_ex.append((pid, pre, post))
for pid in pid_concussion:
print("Processing pid: {}".format(pid))
p = Patient(pid, load_session_raw=False, load_session_examples=True)
# get examples from pre_test
pre = post = None
if p.pre_test is not None:
pre = p.pre_test.load_examples()
if pre is not None:
np.random.shuffle(pre)
pre = pre[:n_keep]
# get examples from post_test
if p.post_test is not None:
post = p.post_test.load_examples()
if post is not None:
np.random.shuffle(post)
post = post[:n_keep]
if post is not None and pre is not None:
con_ex.append((pid, pre, post))
# create training data
train_data = np.vstack([p.pre_test.examples for p in noCon_pats if p.pre_test is not None] +
[p.post_test.examples for p in noCon_pats if p.post_test is not None])
train_data = np.vstack([tup[1][:n_keep] for tup in noCon_ex] + [tup[2][n_keep] for tup in noCon_ex] +
[tup[1][:n_keep] for tup in con_ex] + [tup[2][n_keep] for tup in con_ex])
# create and train embedding
emb = Embedding(**embedding_args)
emb.train(train_data)
# visualize embedding
colors = cycle(['r', 'b', 'g', 'y'])
pre_post_distances = []
for p in noCon_pats:
if (sys.version_info < (3,0)):
# for python2 use
color = colors.next()
else:
# for python3 use
color = next(colors)
pre_emb = emb.embed(p.pre_test.examples)
post_emb = emb.embed(p.post_test.examples)
plt.plot(pre_emb[:, 0], pre_emb[:, 1], linestyle='None', marker="x", color=color, label=p.pid + "_pre")
plt.plot(post_emb[:, 0], post_emb[:, 1], linestyle='None', marker="o", color=color, label=p.pid + "_post")
# calculate centriods and plot a line
pre_cent = centroid(pre_emb)
post_cent = centroid(post_emb)
plt.plot([pre_cent[0], post_cent[0]], [pre_cent[1], post_cent[1]], '-', linewidth=4, color=color)
# record distance
pre_post_distances.append(np.linalg.norm(post_cent - pre_cent))
nocon_distances = embed_and_plot(emb, noCon_ex)
plt.title("No concussion, pre/post test centroid distance")
plt.legend()
plt.show()
plt.savefig()
con_distances = embed_and_plot(emb, con_ex)
plt.title("Concussion, pre/post test centroid distance")
plt.legend()
plt.show()
plt.hist(pre_post_distances)
plt.hist(nocon_distances)
plt.title("No concussion, pre/post test centroid distance")
plt.show()
plt.hist(con_distances)
plt.title("Concussion, pre/post test centroid distance")
plt.show()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment