Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gauss-Newton solving visualization #67

Merged
merged 3 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions examples/eit_static_jac.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 47,32 @@
eit = jac.JAC(mesh_obj, protocol_obj)
eit.setup(p=0.25, lamb=1.0, method="lm")
# lamb = lamb * lamb_decay
ds = eit.gn(v1, lamb_decay=0.1, lamb_min=1e-5, maxiter=20, verbose=True)

# plot
plt.ion()
fig, ax = plt.subplots(figsize=(9, 6))
im = ax.tripcolor(xx, yy, tri, np.real(ds), alpha=1.0, cmap="viridis")
ax.axis("equal")
ax.set_title("Conductivities Reconstructed")
fig.colorbar(im)

# Calculate then show the results
# ds = eit.gn(v1, lamb_decay=0.1, lamb_min=1e-5, maxiter=20, verbose=True)
# im = ax.tripcolor(xx, yy, tri, np.real(ds), alpha=1.0, cmap="viridis")
# fig.colorbar(im)

# Real time update version
colorbar = None
for ds in eit.gn(
v1, lamb_decay=0.1, lamb_min=1e-5, maxiter=20, verbose=True, generator=True
):
im = ax.tripcolor(xx, yy, tri, np.real(ds), alpha=1.0, cmap="viridis")
if (
colorbar is not None
): # Update the colorbar as the min and max values are changing
colorbar.remove()
colorbar = fig.colorbar(im)
fig.canvas.draw() # Update the canvas
fig.canvas.flush_events() # Flush the drawing queue

# fig.savefig('../doc/images/demo_static.png', dpi=96)
plt.show()
plt.show(
block=True
) # Very important when using the generator version, otherwise the program exits automatically
66 changes: 39 additions & 27 deletions pyeit/eit/jac.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 186,7 @@ def gn(
lamb_min: float = 0.0,
method: str = "kotre",
verbose: bool = False,
generator: bool = False,
**kwargs,
) -> np.ndarray:
"""
Expand Down Expand Up @@ -248,33 249,44 @@ def gn(
# convergence test
x0_norm = np.linalg.norm(x0)

for i in range(maxiter):

# forward solver,
jac, v0 = self.fwd.compute_jac(x0)
# Residual
r0 = v - v0

# Damped Gaussian-Newton
h_mat = self._compute_h(jac, p, lamb, method)

# update
d_k = np.dot(h_mat, r0)
x0 = x0 - d_k

# convergence test
c = np.linalg.norm(d_k) / x0_norm
if c < gtol:
break

if verbose:
print("iter = %d, lamb = %f, gtol = %f" % (i, lamb, c))

# update regularization parameter
# lambda can be given in user defined decreasing lists
lamb *= lamb_decay
lamb = max(lamb, lamb_min)
return x0
def generator_gn():
nonlocal x0, lamb
for i in range(maxiter):

# forward solver,
jac, v0 = self.fwd.compute_jac(x0)
# Residual
r0 = v - v0

# Damped Gaussian-Newton
h_mat = self._compute_h(jac, p, lamb, method)

# update
d_k = np.dot(h_mat, r0)
x0 = x0 - d_k

# convergence test
c = np.linalg.norm(d_k) / x0_norm
if c < gtol:
break

if verbose:
print("iter = %d, lamb = %f, gtol = %f" % (i, lamb, c))

# update regularization parameter
# lambda can be given in user defined decreasing lists
lamb *= lamb_decay
lamb = max(lamb, lamb_min)
yield x0

real_gen = generator_gn
if not generator:
item = None
for item in real_gen():
pass
return item
else:
return real_gen()

def project(self, ds: np.ndarray) -> np.ndarray:
"""
Expand Down