-
Notifications
You must be signed in to change notification settings - Fork 0
/
probe_potential_surface.py
executable file
·188 lines (158 loc) · 8.11 KB
/
probe_potential_surface.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from src.model import configSimulation, simulationLoopUnsafe
from jax.tree_util import Partial
from numpyro.infer.reparam import TransformReparam
import os
import sys
import time
from functools import partial
from jax import jit, lax
import numpyro.distributions as dist
import jax.numpy as jnp
import jax
import numpyro
from numpyro.infer import MCMC
import numpy as np
import itertools
from jax import jit, grad, jacfwd, value_and_grad
from jax.test_util import check_grads
os.chdir(os.path.dirname(__file__))
jax.config.update("jax_enable_x64", True)
numpyro.set_host_device_count(1)
config_filename = ""
if len(sys.argv) == 1:
# base cases
#modelname = "single-artery"
#modelname = "tapering"
#modelname = "conjunction"
#modelname = "bifurcation"
#modelname = "aspirator"
# openBF-hub
modelname = "test/adan56/adan56.yml"
# vascularmodels.com
#modelname = "0007_H_AO_H"
#modelname = "0029_H_ABAO_H"
#modelname = "0053_H_CERE_H"
input_filename = "test/" + modelname + "/" + modelname + ".yml"
else:
config_filename = "test/" + sys.argv[1] + "/" + sys.argv[1] + ".yml"
verbose = True
(N, B, J,
sim_dat, sim_dat_aux,
sim_dat_const, sim_dat_const_aux,
timepoints, conv_tol, Ccfl, edges, input_data, rho,
masks, strides, edges,
vessel_names, cardiac_T) = configSimulation(config_filename, verbose)
num_iterations = 1000
sim_loop_old_jit = partial(jit, static_argnums=(0, 1, 12))(simulationLoopUnsafe)
sim_dat_obs, t_obs, P_obs = sim_loop_old_jit(N, B,
sim_dat, sim_dat_aux,
sim_dat_const, sim_dat_const_aux,
Ccfl, input_data, rho,
masks, strides, edges,
num_iterations)
R_index = 1
var_index = 7
R1 = sim_dat_const[var_index,strides[R_index,1]]
#R_scales = np.linspace(1.1*R1, 2*R1, 16)
total_num_points = 1e3
R_scales = np.linspace(0.1, 10, int(total_num_points))
def simLoopWrapper(Ccfl, N, B,
sim_dat, sim_dat_aux,
sim_dat_const, sim_dat_const_aux,
input_data, rho,
masks, strides, edges):
#R=R*R1
#ones = jnp.ones(M)
##sim_dat_const_new = sim_dat_const_new.at[var_index, start:end].set(R*ones)
#sim_dat_const_new = lax.dynamic_update_slice(sim_dat_const,
# ((R*ones)[:,jnp.newaxis]*jnp.ones(1)[jnp.newaxis,:]).transpose(),
# (var_index, start))
_, _, P = sim_loop_old_jit(N, B,
sim_dat, sim_dat_aux,
sim_dat_const, sim_dat_const_aux,
Ccfl, input_data, rho,
masks, strides, edges,
1000)
return jnp.mean(jnp.square((P-P_obs)))/jnp.mean(jnp.square((P_obs)))
def simLoopWrapper1(R, R_index, R1, var_index, P_obs, N, B, M, start, end,
sim_dat, sim_dat_aux,
sim_dat_const, sim_dat_const_aux,
Ccfl, input_data, rho,
masks, strides, edges):
sim_dat, _, P = sim_loop_old_jit(N, B,
sim_dat, sim_dat_aux,
sim_dat_const, sim_dat_const_aux,
Ccfl, input_data, rho,
masks, strides, edges,
120000)
return sim_dat
sim_loop_wrapper_jit = partial(jit, static_argnums=(1, 2))(simLoopWrapper)
#sim_loop_wrapper_grad_jit = partial(jit, static_argnums=(2, 6, 7, 8, 9, 10))(jacfwd(simLoopWrapper,14))
#sim_loop_wrapper_grad_jit1 = partial(jit, static_argnums=(1, 5, 6, 7, 8, 9))(value_and_grad(simLoopWrapper1,14))
#sim_loop_wrapper_grad_jit = jit(jacfwd(simLoopWrapper, 0))
results_folder = "results/potential_surface"
if not os.path.isdir(results_folder):
os.makedirs(results_folder, mode = 0o777)
slices = int(total_num_points/int(sys.argv[3]))
gradients = np.zeros(slices)
gradients_averaged = np.zeros(slices)
gradient = 1
#for i in range(1):
# results_file = results_folder + "/potential_surface_new.txt"
# R = R_scales[100]
# M = strides[R_index,1]-strides[R_index,0]+4
# start = strides[R_index,0]-2
# end = strides[R_index,1]+2
# sim_dat = simLoopWrapper1(R, R_index, R1, var_index, P_obs, N, B, M, start, end,
# sim_dat, sim_dat_aux,
# sim_dat_const, sim_dat_const_aux,
# Ccfl, input_data, rho,
# masks, strides, edges)
# gradient *= sim_loop_wrapper_grad_jit1(R, R_index, R1, var_index, P_obs, N, B, M, start, end,
# sim_dat, sim_dat_aux,
# sim_dat_const, sim_dat_const_aux,
# Ccfl, input_data, rho,
# masks, strides, edges)
# #gradient *= sim_loop_wrapper_grad_jit(R, R_index, R1, var_index, P_obs, N, B, M, start, end,
# # sim_dat, sim_dat_aux,
# # sim_dat_const, sim_dat_const_aux,
# # Ccfl, input_data, rho,
# # masks, strides, edges)
# print(gradient)
for i in range(int(sys.argv[2])*slices,(int(sys.argv[2])+1)*slices):
results_file = results_folder + "/potential_surface_new.txt"
R = R_scales[i]
M = strides[R_index,1]-strides[R_index,0]+4
start = strides[R_index,0]-2
end = strides[R_index,1]+2
#loss = sim_loop_wrapper_jit(Ccfl, R, R_index, R1, var_index, P_obs, N, B, M, start, end,
# sim_dat, sim_dat_aux,
# sim_dat_const, sim_dat_const_aux,
# input_data, rho,
# masks, strides, edges)
sim_loop_wrapper_R = Partial(sim_loop_wrapper_jit, N=N, B=B,
sim_dat=sim_dat, sim_dat_aux=sim_dat_aux,
sim_dat_const=sim_dat_const, sim_dat_const_aux=sim_dat_const_aux,
input_data=input_data, rho=rho,
masks=masks, strides=strides, edges=edges)
print(i)
check_grads(sim_loop_wrapper_R, (Ccfl*0.8,), order=1)
#M = strides[R_index,1]-strides[R_index,0]+4
#start = strides[R_index,0]-2
#end = strides[R_index,1]+2
#gradients[i-int(sys.argv[2])*slices]= sim_loop_wrapper_grad_jit(R, R_index, R1, var_index, P_obs, N, B, M, start, end,
# sim_dat, sim_dat_aux,
# sim_dat_const, sim_dat_const_aux,
# Ccfl, input_data, rho,
# masks, strides, edges)
#if abs(gradients[i-int(sys.argv[2])*slices]) > 1e3:
# gradients[i-int(sys.argv[2])*slices] = np.sign(gradients[i-int(sys.argv[2])*slices])*1e3
#if i >= int(sys.argv[2])*slices + 1000:
# gradients_averaged[i-int(sys.argv[2])*slices] = gradients[i-int(sys.argv[2])*slices-999:i-int(sys.argv[2])*slices+1].mean()
# gradients_averaged[i-int(sys.argv[2])*slices] = gradients[i-int(sys.argv[2])*slices-999:i-int(sys.argv[2])*slices+1].mean()
#else:
# gradients_averaged[i-int(sys.argv[2])*slices] = gradients[:i-int(sys.argv[2])*slices+1].mean()
#
#file = open(results_file, "a")
#file.write(str(R) + " " + str(loss) + " " + str(gradients[i-int(sys.argv[2])*slices]) + " " + str(gradients_averaged[i-int(sys.argv[2])*slices]) + "\n")
#file.close()