Skip to content

Commit

Permalink
modified to use EvalMultiPoly.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
hzarei4 committed Jul 23, 2024
1 parent bc04932 commit 4e117fb
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 254 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["RainerHeintzmann <[email protected]>"]
version = "0.1.0"

[deps]
EvalMultiPoly = "c78649ec-f9ed-405e-be6d-0472f43586aa"
FourierTools = "b18b359b-aebc-45ac-a139-9c0ccbb2871e"
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Expand Down
2 changes: 2 additions & 0 deletions examples/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
CoordinateTransformations = "150eb455-5306-5404-9cee-2592286d6298"
DataToFunctions = "64cfdffa-4d02-49ee-ae8b-a805370874f5"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
EvalMultiPoly = "c78649ec-f9ed-405e-be6d-0472f43586aa"
FindShift = "643ec891-bf64-479f-8088-26ff5ce1b396"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
FourierTools = "b18b359b-aebc-45ac-a139-9c0ccbb2871e"
Expand Down
96 changes: 53 additions & 43 deletions examples/deform_testimage.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### A Pluto.jl notebook ###
# v0.19.43
# v0.19.42

using Markdown
using InteractiveUtils
Expand All @@ -20,11 +20,14 @@ using Pkg
# ╔═╡ 0ae2da4f-3f75-47bb-a899-9e89c5c3f17c
Pkg.activate(".")

# ╔═╡ 4af0c13d-fc42-4fe7-97e6-2248e36b63e2
Pkg.add("PlutoUI");

# ╔═╡ a2d75cfb-feab-4130-8439-30c543618d04
using DataToFunctions, ImageShow, TestImages, PlutoUI, Images

# ╔═╡ 4af0c13d-fc42-4fe7-97e6-2248e36b63e2
# Pkg.add("PlutoUI")
# ╔═╡ 1c744bec-f085-4812-ab1a-40a32c2ac176
import PlutoUI: combine

# ╔═╡ 5ac1123d-5df3-4c9d-aff1-ffe91d931497
data = Float32.(testimage("resolution_test_512"))
Expand All @@ -33,7 +36,7 @@ data = Float32.(testimage("resolution_test_512"))
simshow(data)

# ╔═╡ 2fce0208-732f-4259-a47d-7f78921bfd87
f = get_function(data, super_sampling=1)
f = get_interpolated_function(data, AffineMode, super_sampling=1)

# ╔═╡ dd535378-3f2a-4429-914c-8b7608b99706
@bind shift_x Slider(-100:0.02:100, default=0)
Expand All @@ -48,57 +51,68 @@ f = get_function(data, super_sampling=1)
@bind zoom_y Slider(0.2:0.02:4, default=1)

# ╔═╡ 64b64d3b-092f-4553-916d-f7db1fdfa428
f((shift_x, shift_y), (1/zoom_x, 1/zoom_y))
simshow(f((shift_x, shift_y, 1/zoom_x, 1/zoom_y, 0.0, 0.0, 0.0)))

# ╔═╡ c3fe6b25-f4c0-4a80-9d15-9ee30136d43b
typeof(f((shift_x, shift_y), (1/zoom_x, 1/zoom_y)))
typeof(f((shift_x, shift_y, 1/zoom_x, 1/zoom_y, 0.0, 0.0, 0.0)))

# ╔═╡ ff143f0d-b070-4220-8fa6-0b5a93a56303
typeof(data)

# ╔═╡ 1bed34fb-b29a-4042-a493-4835fdb69a9d
g =DataToFunctions.get_function_affine(data, super_sampling=1)
# ╔═╡ 6676ba35-3efd-49f5-9819-411ad8f8a95c
md"""
# Polynomial transformations
"""

# ╔═╡ c287fa80-426b-11ef-125e-5fda207e605c
# ╠═╡ disabled = true
#=╠═╡
g()
╠═╡ =#

# ╔═╡ 87be45c0-8b2e-4d49-abd1-a274b3c1815e
h = get_function_poly(data, 1)
# ╔═╡ 13461a95-95ea-4bad-8673-e94e06776254
md"""
First order polynomial transformation which is as follows:
# ╔═╡ 473d635e-d06d-4cdb-991f-22c82a06b491
@bind c1 Slider(-20f0:0.05f0:20f0, default=0)
``x^{\prime} = c_{1} + c_{2}{x}^{1} + c_{3}{y}^{1}``
# ╔═╡ 7fb3ef17-d0a0-4919-b4d5-8e66b4a0fe60
@bind c2 Slider(0.2f0:0.05f0:2f0, default=1)
``y^{\prime} = c_{4} + c_{5}{x}^{1} + c_{6}{y}^{1}``
"""

# ╔═╡ 200bab4d-b444-47c6-b4d8-d5d3c450e5f9
@bind c3 Slider(-2f0:0.05f0:2f0, default=0)

# ╔═╡ f17402a6-ba63-44e9-8e22-da027b07ffc3
@bind c4 Slider(-20f0:0.05f0:20f0, default=0)

# ╔═╡ d913278e-1658-4bee-9e12-ad778e530c1b
@bind c5 Slider(-2f0:0.05f0:2f0, default=0)

# ╔═╡ cee301da-2b3e-432f-9551-0fe83bf6c8ec
@bind c6 Slider(0.2f0:0.05f0:2f0, default=1)
# ╔═╡ 87be45c0-8b2e-4d49-abd1-a274b3c1815e
h = get_interpolated_function(data, PolynomialMode, 1);

# ╔═╡ 68f771aa-2cde-41cd-990c-9ec7dc2146a4
function coeffs_input(coeffs::Vector)

return combine() do Child

inputs = [
md""" $(name): $(
Child(name, Slider(-2f0:0.05f0:2f0, default=0, show_value=true))
)"""

for name in coeffs
]

md"""
#### Transfrorm coefficients
$(inputs)
"""
end
end;

# ╔═╡ 69331d73-75a3-4727-acda-e79779a2bd03
@bind c coeffs_input(["c1", "c2", "c3", "c4", "c5", "c6"])

# ╔═╡ 74228a9d-6cc2-4aaf-97da-67f32670341e
simshow(h((c1, c2, c3, c4, c5, c6)), cmap=:turbo)#,0f0,0f0,0f0,0f0,0f0,0f0,0f0,0f0,0f0)))
simshow(h((c.c1, c.c2, c.c3, c.c4, c.c5, c.c6)), cmap=:turbo)#,0f0,0f0,0f0,0f0,0f0,0f0,0f0,0f0,0f0)))

# ╔═╡ 687a198b-9020-4959-8e27-fd0896d4b1fc
maximum(h((c1,c2,c3,c4,c5,c6)))
maximum(h((c.c1,c.c2,c.c3,c.c4,c.c5,c.c6)))

# ╔═╡ 6584aacc-440e-457b-bf52-83f8db40c999
h((c1,c2,c3,c4,c5,c6))
h((c.c1,c.c2,c.c3,c.c4,c.c5,c.c6))

# ╔═╡ Cell order:
# ╠═28975586-853e-4e19-b9eb-65c41fa61a43
# ╠═0ae2da4f-3f75-47bb-a899-9e89c5c3f17c
# ╠═4af0c13d-fc42-4fe7-97e6-2248e36b63e2
# ╠═1c744bec-f085-4812-ab1a-40a32c2ac176
# ╠═a2d75cfb-feab-4130-8439-30c543618d04
# ╠═5ac1123d-5df3-4c9d-aff1-ffe91d931497
# ╠═b0c8d15e-1bd3-4e66-b2b9-57885636eb48
Expand All @@ -110,15 +124,11 @@ h((c1,c2,c3,c4,c5,c6))
# ╠═64b64d3b-092f-4553-916d-f7db1fdfa428
# ╠═c3fe6b25-f4c0-4a80-9d15-9ee30136d43b
# ╠═ff143f0d-b070-4220-8fa6-0b5a93a56303
# ╠═1bed34fb-b29a-4042-a493-4835fdb69a9d
# ╠═c287fa80-426b-11ef-125e-5fda207e605c
# ╟─6676ba35-3efd-49f5-9819-411ad8f8a95c
# ╟─13461a95-95ea-4bad-8673-e94e06776254
# ╠═87be45c0-8b2e-4d49-abd1-a274b3c1815e
# ╠═473d635e-d06d-4cdb-991f-22c82a06b491
# ╠═7fb3ef17-d0a0-4919-b4d5-8e66b4a0fe60
# ╠═200bab4d-b444-47c6-b4d8-d5d3c450e5f9
# ╠═f17402a6-ba63-44e9-8e22-da027b07ffc3
# ╠═d913278e-1658-4bee-9e12-ad778e530c1b
# ╠═cee301da-2b3e-432f-9551-0fe83bf6c8ec
# ╠═74228a9d-6cc2-4aaf-97da-67f32670341e
# ╟─69331d73-75a3-4727-acda-e79779a2bd03
# ╟─68f771aa-2cde-41cd-990c-9ec7dc2146a4
# ╟─74228a9d-6cc2-4aaf-97da-67f32670341e
# ╠═687a198b-9020-4959-8e27-fd0896d4b1fc
# ╠═6584aacc-440e-457b-bf52-83f8db40c999
25 changes: 11 additions & 14 deletions examples/polynomial_apply.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,45 +29,42 @@ function poly_test(;sz=64, dtype=Float64, n_photons=1000)
#sample_data = p_psf ./ maximum(p_psf)

# normalizing the sample data
sample_data = p_psf ./ maximum(p_psf)
sample_data = p_psf ./ maximum(p_psf) * n_photons

#sample_data = make_grid();
f_1 = get_function_poly(Float64.(sample_data), 1); # get_function_affine(sample_data);
true_vals = dtype[2.1, 1.05, 0.02, 1.5, 0.05, 1.02] # dtype[2.0, 1.0, 1.01, 1.0, 0.0, 0.0, 0.0]
f_1 = get_interpolated_function(Float64.(sample_data), PolynomialMode, 1); # get_function_affine(sample_data);
true_vals = (1.6, 1.05, 0.1, 1.5, 0.01, 1.02) # dtype[2.0, 1.0, 1.01, 1.0, 0.0, 0.0, 0.0]
# true_vals = dtype[rand(-4.0:0.001:4.0), rand(-4.0:0.001:4.0), rand(0.5:0.001:1.5),rand(0.5:0.001:1.5), 0.0, 0.0, rand(0.001:0.001:pi/2.001)]
dat2 = f_1(Tuple(true_vals))

p_img = n_photons .* (dat2 ./ maximum(dat2))
n_img = dtype.(poisson(Float64.(p_img)))
dat2 = f_1(true_vals)

s_data = Float64.(sample_data .* n_photons)
#sample_data = dtype.(TestImages.shepp_logan(sz))
f = get_function_poly(Float64.(s_data), 1);
f = get_interpolated_function(Float64.(sample_data), PolynomialMode, 1);



loss_m(p1::AbstractVector) = sum(abs2.(f(Tuple(p1)) .- n_img))
st_vals = dtype[2.1, 1.01, 0.01, 1.5, 0.01, 0.9] #ones(Float64, 6)./10
loss_p(p1::AbstractArray) = (sum(abs2.(f(Tuple(p1)) .- dat2)))
st_vals = [2.1, 1.00, 0.00, 1.5, 0.00, 1.0] #ones(Float64, 6)./10
#st_vals = Float64[1.0, 0, 0, 0, 0, 0, 1.0, 0, 0, 1.0, 0, 0, 1.0, 0, 0, 0, 0, 0]
# Float64[9.0, 0, 0, 0, 0, 0, 1, 0, 0, 5.0, 0, 0, 1, 0, 0, 0, 0, 0]
# @vv f(Tuple(st_vals))
# loss_m(st_vals)


function g!(G, x) # (G, x)
G .= gradient(loss_m, x)[1]
G .= gradient(loss_p, x)[1]
end
od = OnceDifferentiable(loss_m, g!, st_vals)
od = OnceDifferentiable(loss_p, g!, st_vals)
res = optimize(
od,
loss_p,
st_vals,
#Newton(),
BFGS(; initial_stepnorm = 1e-2),#; linesearch=LineSearches.BackTracking(order=2)),
#LBFGS(),#; linesearch=LineSearches.BackTracking(order=3)),
#lower, upper,
#init_x,
#Fminbox(inner_optimizer),
Optim.Options(store_trace = true, extended_trace = true, iterations=5000, g_tol=1e-3),
#Optim.Options(store_trace = true, extended_trace = true, iterations=5000, g_tol=1e-3),
autodiff = :forward
)

Expand Down
3 changes: 1 addition & 2 deletions src/DataToFunctions.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module DataToFunctions

include("utils.jl")
include("polynomials.jl")
include("transformation_types.jl")
include("transformators.jl")

end # module DataToFunctions
Loading

0 comments on commit 4e117fb

Please sign in to comment.