Skip to content

Commit 8bdc916

Browse files
authored
Merge pull request #55 from CUQI-DTU/sprint27_all_paper_2_notebooks
Sprint27 all paper 2 notebooks
2 parents 89a40ec + 829365a commit 8bdc916

16 files changed

+2185
-616
lines changed

EIT/EIT.ipynb

Lines changed: 648 additions & 0 deletions
Large diffs are not rendered by default.

EIT/EIT.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
import dolfin as dl
33
import matplotlib.pyplot as plt
44
import os
5+
import sys
56
from cuqi.geometry import Continuous1D
67
from cuqi.model import PDEModel
78
from cuqi.distribution import Gaussian, JointDistribution
89
from cuqi.sampler import MH
910
from cuqipy_fenics.geometry import FEniCSContinuous, MaternKLExpansion,\
1011
FEniCSMappedGeometry
1112
from cuqipy_fenics.pde import SteadyStateLinearFEniCSPDE
13+
import cuqi
14+
import cuqipy_fenics
1215

1316
# Function for extracting the indices of the boundary nodes
1417
def extract_boundary_dofs_indices(solution_space):
@@ -55,8 +58,33 @@ def Heaviside(func):
5558
return G_Heavi
5659

5760
if __name__ == "__main__":
61+
# Parse command line arguments: noise level, number of samples,
62+
# number of burn-in samples, thinning factor, and random seed.
63+
# Parse noise level which is passed as a command line argument. Only
64+
# 5, 10, and 20 percent noise levels are supported.
65+
66+
if len(sys.argv) != 6:
67+
print("Usage: python EIT.py <noise_percent> <num_samples> <num_burnin> <thinning_factor> <random_seed>")
68+
sys.exit(1)
69+
70+
noise_percent = int(sys.argv[1])
71+
Ns = int(sys.argv[2])
72+
Nb = int(sys.argv[3])
73+
Nt = int(sys.argv[4])
74+
seed = int(sys.argv[5])
75+
76+
# Check if noise level is supported
77+
if noise_percent not in [5, 10, 20]:
78+
print("Only 5, 10, and 20 percent noise levels are supported")
79+
sys.exit(1)
80+
print("Running EIT with noise level: ", noise_percent, "%")
81+
82+
# Print cuqi and cuqipy_fenics version
83+
print("cuqi version: ", cuqi.__version__)
84+
print("cuqipy_fenics version: ", cuqipy_fenics.__version__)
85+
5886
# Fix the random seed for reproducibility
59-
np.random.seed(2)
87+
np.random.seed(seed)
6088

6189
#%% 1 setting up FEniCS
6290
# loading computational mesh
@@ -165,7 +193,6 @@ def observation4(sigma, v4):
165193

166194
#%% 4 Creating the posterior distribution
167195
# loading signal from file
168-
noise_percent = 5
169196
obs_data = np.load('./data/obs_circular_inclusion_2_'+str(noise_percent)+'per_noise.npz')
170197
b_exact = obs_data['b_exact']
171198
s_noise_list = np.sqrt(obs_data['sigma2']) # read the noise variance and
@@ -228,7 +255,7 @@ def observation4(sigma, v4):
228255
sampler = MH(posterior)
229256

230257
# Sampling using the Metropolis-Hastings sampler
231-
posterior_samples = sampler.sample_adapt(1000000)
258+
posterior_samples = sampler.sample_adapt(Ns)
232259

233260
#%% 6 visualization
234261
# plotting prior samples
@@ -246,7 +273,7 @@ def observation4(sigma, v4):
246273
plt.savefig("plot_prior_samples"+str(noise_percent)+".png")
247274

248275
# plotting posterior samples
249-
idx = np.random.permutation(1000000) # create randomized index
276+
idx = np.random.permutation(Ns) # create randomized index
250277
f, axes = plt.subplots(1,3)
251278
plt.sca(axes[0])
252279
posterior_samples.plot(idx[0],subplots=False)
@@ -258,7 +285,7 @@ def observation4(sigma, v4):
258285
plt.savefig("plot_posterior_samples"+str(noise_percent)+".png")
259286

260287
# burn-thin the samples
261-
posterior_samples = posterior_samples.burnthin(200000, 4000)
288+
posterior_samples = posterior_samples.burnthin(Nb, Nt)
262289

263290
# plotting the mean
264291
f, axes = plt.subplots(1,2)

EIT/README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
# Instructions for running the EIT Bayesian Inverse Problem
22

3-
To generate the EIT results shown in the paper, run the script `EIT.py` for each of the different noise levels: $5\\%$, $10\\%$, $20\\%$. Then run the script `plot_paper_figures.py` to generate the figures 6, 7, 8, and 9 in the paper. To set the noise level, change the variable `noise_percent` in the script `EIT.py` to be either `5`, `10`, or `20`. The script `EIT.py` will then generate the sampling results for the corresponding noise level and save it in the folder `/stat/`. The script `plot_paper_figures.py` will then use these results to generate the paper figures.
3+
Full instructions for running the EIT Bayesian Inverse Problem are provided in
4+
the notebook `EIT.ipynb`.
45

5-
Observed data for each noise-levels are available in `/data/`:
6-
- `/data/obs_circular_inclusion_2_5per_noise.npz` with 5 percent noise-level.
7-
- `/data/obs_circular_inclusion_2_10per_noise.npz` with 10 percent noise-level.
8-
- `/data/obs_circular_inclusion_2_20per_noise.npz` with 20 percent noise-level.
6+
Observed data are available in `./data/`:
7+
- `./data/obs_circular_inclusion_2_5per_noise.npz` with 5 percent noise-level.
8+
- `./data/obs_circular_inclusion_2_10per_noise.npz` with 10 percent noise-level.
9+
- `./data/obs_circular_inclusion_2_20per_noise.npz` with 20 percent noise-level.
10+
11+
Pre-computed results are available in `./stat_paper/`:
12+
- `./results/stat_circular_inclusion_2_5per_noise_thinned.npz` for the 5 percent noise-level case.
13+
- `./results/stat_circular_inclusion_2_10per_noise_thinned.npz` for the 10 percent noise-level case.
14+
- `./results/stat_circular_inclusion_2_20per_noise_thinned.npz` for the 20 percent noise-level case.

EIT/figures_util.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,16 @@ def eval(self,values,x):
116116
subfigs[1].subplots_adjust(wspace=0, bottom=0.2, top=0.8)
117117

118118

119-
def plot_figure7(prior_samples, cuqi_samples1, cuqi_samples2, cuqi_samples3):
119+
def plot_figure7(prior_samples, cuqi_samples1, cuqi_samples2, cuqi_samples3,
120+
prior_samples_idx_to_plot=[0, 2],
121+
posterior_samples_idx_to_plot=[0, 1]):
120122
cm_to_in = 1/2.54
121123
fig = plt.figure( figsize=(17.8*cm_to_in, 10.5*cm_to_in),layout='constrained')
122124
subfigs = fig.subfigures(2, 1)
123125

124126
axes = subfigs[0].subplots(1,4,sharey=True)
125127
plt.sca(axes[0])
126-
prior_samples.plot([ 0 ], subplots=False)
128+
prior_samples.plot([prior_samples_idx_to_plot[0]], subplots=False)
127129
axes[0].set_xlim([-1.1,1.1])
128130
axes[0].set_ylim([-1.1,1.1])
129131
axes[0].set_xticks([-1,0,1])
@@ -134,7 +136,7 @@ def plot_figure7(prior_samples, cuqi_samples1, cuqi_samples2, cuqi_samples3):
134136
axes[0].set_ylabel(r'$\xi_2$')
135137

136138
plt.sca(axes[1])
137-
cuqi_samples1.plot([ 0 ], subplots=False)
139+
cuqi_samples1.plot([posterior_samples_idx_to_plot[0]], subplots=False)
138140
axes[1].set_ylabel('')
139141
axes[1].set_xlim([-1.1,1.1])
140142
axes[1].set_ylim([-1.1,1.1])
@@ -143,7 +145,7 @@ def plot_figure7(prior_samples, cuqi_samples1, cuqi_samples2, cuqi_samples3):
143145
axes[1].set_xlabel(r'$\xi_1$')
144146

145147
plt.sca(axes[2])
146-
cuqi_samples2.plot([ 0 ], subplots=False)
148+
cuqi_samples2.plot([posterior_samples_idx_to_plot[0]], subplots=False)
147149
axes[2].set_ylabel('')
148150
axes[2].set_xlim([-1.1,1.1])
149151
axes[2].set_ylim([-1.1,1.1])
@@ -152,9 +154,7 @@ def plot_figure7(prior_samples, cuqi_samples1, cuqi_samples2, cuqi_samples3):
152154
axes[2].set_xlabel(r'$\xi_1$')
153155

154156
plt.sca(axes[3])
155-
cuqi_samples3.plot([ 0 ], subplots=False)
156-
#prior_samples.plot([ 3 ], subplots=False)
157-
#axes[0,3].set_yticks([])
157+
cuqi_samples3.plot([posterior_samples_idx_to_plot[0]], subplots=False)
158158
axes[3].set_ylabel('')
159159
axes[3].set_xlim([-1.1,1.1])
160160
axes[3].set_ylim([-1.1,1.1])
@@ -164,7 +164,7 @@ def plot_figure7(prior_samples, cuqi_samples1, cuqi_samples2, cuqi_samples3):
164164

165165
axes = subfigs[1].subplots(1,4,sharey=True)
166166
plt.sca(axes[0])
167-
prior_samples.plot([ 2 ], subplots=False)
167+
prior_samples.plot([prior_samples_idx_to_plot[1]], subplots=False)
168168
axes[0].set_xlim([-1.1,1.1])
169169
axes[0].set_ylim([-1.1,1.1])
170170
axes[0].set_xticks([-1,0,1])
@@ -175,7 +175,7 @@ def plot_figure7(prior_samples, cuqi_samples1, cuqi_samples2, cuqi_samples3):
175175
axes[0].set_ylabel(r'$\xi_2$')
176176

177177
plt.sca(axes[1])
178-
cuqi_samples1.plot([ -10 ], subplots=False)
178+
cuqi_samples1.plot([posterior_samples_idx_to_plot[1]], subplots=False)
179179
axes[1].set_ylabel('')
180180
axes[1].set_xlim([-1.1,1.1])
181181
axes[1].set_ylim([-1.1,1.1])
@@ -184,7 +184,7 @@ def plot_figure7(prior_samples, cuqi_samples1, cuqi_samples2, cuqi_samples3):
184184
axes[1].set_xlabel(r'$\xi_1$')
185185

186186
plt.sca(axes[2])
187-
cuqi_samples2.plot([ -10 ], subplots=False)
187+
cuqi_samples2.plot([posterior_samples_idx_to_plot[1]], subplots=False)
188188
axes[2].set_ylabel('')
189189
axes[2].set_xlim([-1.1,1.1])
190190
axes[2].set_ylim([-1.1,1.1])
@@ -193,7 +193,7 @@ def plot_figure7(prior_samples, cuqi_samples1, cuqi_samples2, cuqi_samples3):
193193
axes[2].set_xlabel(r'$\xi_1$')
194194

195195
plt.sca(axes[3])
196-
cuqi_samples3.plot([ -10 ], subplots=False)
196+
cuqi_samples3.plot([posterior_samples_idx_to_plot[1]], subplots=False)
197197
axes[3].set_ylabel('')
198198
axes[3].set_xlim([-1.1,1.1])
199199
axes[3].set_ylim([-1.1,1.1])

EIT/plot_paper_figures.py

Lines changed: 0 additions & 90 deletions
This file was deleted.
Binary file not shown.
Binary file not shown.
100 KB
Binary file not shown.

0 commit comments

Comments
 (0)