-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp_main.py
154 lines (133 loc) · 6.22 KB
/
temp_main.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
import logging
import numpy as np
import htfa_torch.dtfa as DTFA
import htfa_torch.niidb as niidb
import htfa_torch.utils as utils
import matplotlib.pyplot as plt
from ordered_set import OrderedSet
import os
from torch.nn.functional import softplus
import torch
import itertools
from htfa_torch import tfa_models
import nilearn.plotting as niplot
import imageio
def getEquidistantPoints(p1, p2, parts):
return zip(np.linspace(p1[0], p2[0], parts + 1), np.linspace(p1[1], p2[1], parts + 1))
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %H:%M:%S',
level=logging.INFO)
synthetic_db = niidb.FMriActivationsDb('/home/zulqarnain/algorithm4/htfatorch/data/simulated_simplified_data_3_tiny.db')
dtfa = DTFA.DeepTFA(synthetic_db.all(), mask='/home/zulqarnain/fmri_data/degeneracy_scenario_1_data/wholebrain.nii.gz',
num_factors=9, embedding_dim=2)
losses = dtfa.train(num_steps=2, learning_rate={'q': 1e-2, 'p': 1e-4}, log_level=logging.INFO, num_particles=1,
batch_size=60, use_cuda=True, checkpoint_steps=500, blocks_batch_size=20, patience=50)
# dtfa.load_state('participant_CHECK_01062020_134125') #for scenario 1
# dtfa.load_state('participant_CHECK_01062020_155320') # for scenario 0
def task_labeler(task):
if task == 'heights_high':
return 'h_h'
elif task == 'heights_low':
return 'h_l'
elif task == 'social_high':
return 'so_h'
elif task == 'social_low':
return 'so_l'
elif task == 'spider_high':
return 'sp_h'
elif task == 'spider_low':
return 'sp_l'
else:
return 'Other'
def subject_labeler(subject):
return str(subject)
hyperparams = dtfa.variational.hyperparams.state_vardict()
z_i_mu = hyperparams['interactions']['mu'].data
z_pf_mu = hyperparams['subject']['mu'].data
z_pw_mu = hyperparams['subject_weight']['mu'].data
z_s_mu = hyperparams['task_weight']['mu'].data
tasks = dtfa.stimuli()
task_category = dtfa.tasks()
subjects = dtfa.subjects()
interactions = OrderedSet(list(itertools.product(subjects, tasks)))
i_count = 0
for p in range(len(subjects)):
for s in range(len(tasks)):
temp_i_mu = z_i_mu[i_count, :]
i_count += 1
temp_pf_mu = z_pf_mu[p, :]
temp_pw_mu = z_pw_mu[p, :]
for t in range(len(task_category)):
if task_category[t] in tasks[s]:
temp_s_mu = z_s_mu[t, :]
joint_embed = torch.cat((temp_pw_mu, temp_s_mu, temp_i_mu), dim=-1)
weight_predictions = dtfa.decoder.weights_embedding(joint_embed).view(
-1, dtfa.num_factors, 2
)
mean_weight = weight_predictions[:, :, 0]
factor_params = dtfa.decoder.factors_embedding(temp_pf_mu).view(
-1, dtfa.num_factors, 4, 2
)
centers_predictions = factor_params[:, :, :3]
log_widths_predictions = factor_params[:, :, 3]
centers_predictions = centers_predictions[:, :, :, 0]
log_widths_predictions = log_widths_predictions[:, :, 0]
mean_factors = tfa_models.radial_basis(dtfa.voxel_locations,
centers_predictions.data,
log_widths_predictions.data)[0, :, :]
mean_brain = mean_weight @ mean_factors
image = utils.cmu2nii(mean_brain.data.numpy(),
dtfa.voxel_locations.numpy(),
dtfa._templates[0])
filename = 'results/subject_' + str(subjects[p]) + '_task_' + str(tasks[s]) + '_generated_interaction_brain.png'
plot = niplot.plot_glass_brain(
image, title='subject_' + str(subjects[p]) + '_task_' + str(tasks[s]), plot_abs=False,
colorbar=True, symmetric_cbar=True, output_file=filename
)
for p in range(len(subjects)):
use_subject = subjects[p]
print(use_subject)
# for stimulus in ['spider','social','heights']:
for (t, stimulus) in enumerate(task_category):
# temp_z_ps = z_i_mu[4,:]
idx = [id for (id, inter) in enumerate(interactions) if inter == (use_subject, stimulus + '_high')][0]
print(idx)
temp_z_ps_high = z_i_mu[idx]
idx = [id for (id, inter) in enumerate(interactions) if inter == (use_subject, stimulus + '_low')][0]
temp_z_ps_low = z_i_mu[idx]
use_points = list(getEquidistantPoints(temp_z_ps_low.data, temp_z_ps_high.data, 10))
temp_pf_mu = z_pf_mu[p, :]
temp_pw_mu = z_pw_mu[p, :]
temp_s_mu = z_s_mu[t, :]
filenames = []
for (i, points) in enumerate(use_points):
joint_embed = torch.cat((temp_pw_mu, temp_s_mu, torch.tensor(points)), dim=-1)
weight_predictions = dtfa.decoder.weights_embedding(joint_embed).view(
-1, dtfa.num_factors, 2
)
mean_weight = weight_predictions[:, :, 0]
factor_params = dtfa.decoder.factors_embedding(temp_pf_mu).view(
-1, dtfa.num_factors, 4, 2
)
centers_predictions = factor_params[:, :, :3, 0]
log_widths_predictions = factor_params[:, :, 3, 0]
mean_factors = tfa_models.radial_basis(dtfa.voxel_locations,
centers_predictions.data,
log_widths_predictions.data)[0, :, :]
mean_brain = mean_weight @ mean_factors
vmax = torch.max(mean_brain).data.numpy()
# if vmax < 1:
# vmax = 1
image = utils.cmu2nii(mean_brain.data.numpy(),
dtfa.voxel_locations.numpy(),
dtfa._templates[0])
filenames.append(
'results/gifs/subject_' + str(use_subject) + '_stimulus_' + str(stimulus) + '_' + str(i) + '.png')
plot = niplot.plot_glass_brain(
image, plot_abs=False, colorbar=True, symmetric_cbar=True,
title="Mean Image of Interaction %d" % i,
vmin=-3, vmax=3, output_file=filenames[-1])
images = []
for filename in filenames:
images.append(imageio.imread(filename))
imageio.mimsave('results/gifs/subject_' + str(use_subject) + '_stimulus_' + stimulus + '.gif', images)
r = 3