Skip to content

Commit

Permalink
Merge branch 'main' into ml/levmar
Browse files Browse the repository at this point in the history
  • Loading branch information
mileslucas authored Mar 29, 2024
2 parents 9e5b25f + fa89416 commit 23eaff1
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PSFModels"
uuid = "9ba017d1-7760-46cd-84a3-1e79e9ae9ddc"
authors = ["Miles Lucas <[email protected]> and contributors"]
version = "0.7.2"
version = "0.8.0"

[deps]
CoordinateTransformations = "150eb455-5306-5404-9cee-2592286d6298"
Expand Down
6 changes: 3 additions & 3 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"

[compat]
Documenter = "0.25"
HCIDatasets = "0.3"
Optim = "1"
Documenter = "0.25, 0.26, 0.27"
Plots = "1"
Optim = "1"
HCIDatasets = "0.3"
9 changes: 2 additions & 7 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ setup = quote
end

DocMeta.setdocmeta!(PSFModels, :DocTestSetup, setup; recursive = true)

include("pages.jl")
makedocs(;
modules=[PSFModels],
authors="Miles Lucas <[email protected]> and contributors",
Expand All @@ -18,12 +18,7 @@ makedocs(;
canonical="https://juliaastro.github.io/PSFModels.jl",
assets=String[],
),
pages=[
"Home" => "index.md",
"Examples" => "examples.md",
"Benchmarks" => "bench.md",
"API/Reference" => "api.md",
],
pages=pages
)

deploydocs(;
Expand Down
8 changes: 8 additions & 0 deletions docs/pages.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

pages=[
"Home" => "index.md",
"Examples" => "examples.md",
"Benchmarks" => "bench.md",
"API/Reference" => "api.md",
]
requiredmods = Symbol[]
16 changes: 6 additions & 10 deletions src/airy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,12 @@ function _airydisk(px, py, x, y, fwhm, ratio, amp, theta, background)
# unnormalized airydisk
r = sqrt(dx^2 + dy^2)
# short-circuit
iszero(r) && return amp / (1 - ratio^2)^2 + background
iszero(r) && return amp + background
q = AIRY_PRE * r / fwhm
I1 = 2 * besselj1(q) / q
if iszero(ratio)
return amp * I1^2 + background
end
iszero(ratio) && return amp * I1^2 + background
I2 = 2 * ratio * besselj1(q * ratio) / q
return amp * ((I1 - I2) / (1 - ratio^2))^2 + background
return amp * (I1 - I2)^2 + background
end

# bivariate
Expand All @@ -66,11 +64,9 @@ function _airydisk(px, py, x, y, fwhm::BivariateLike, ratio, amp, theta, backgro
fwhmx, fwhmy = fwhm
q = AIRY_PRE * sqrt((dx / fwhmx)^2 + (dy / fwhmy)^2)
# short-circuit
iszero(q) && return amp / (1 - ratio^2)^2 + background
iszero(q) && return amp + background
I1 = 2 * besselj1(q) / q
if iszero(ratio)
return amp * I1^2 + background
end
iszero(ratio) && return amp * I1^2 + background
I2 = 2 * ratio * besselj1(q * ratio) / q
return amp * ((I1 - I2) / (1 - ratio^2))^2 + background
return amp * (I1 - I2)^2+ background
end
12 changes: 8 additions & 4 deletions src/moffat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ function _moffat(px, py, x, y, fwhm, alpha, amp, theta, background)
# rotate
!iszero(theta) && @warn "isotropic moffat is not affected by non-zero rotation angle $theta"
# unnormalized moffat
dist = dx^2 + dy^2
dist /= (fwhm / 2)^2
gamma = _moffat_fwhm_to_gamma(fwhm, alpha)
dist = (dx / gamma)^2 + (dy / gamma)^2
return amp / (1 + dist)^alpha + background
end

# http://openafox.com/science/peak-function-derivations.html#moffat
_moffat_gamma_to_fwhm(gamma, alpha) = 2 * gamma * sqrt(2^(1/alpha) - 1)
_moffat_fwhm_to_gamma(fwhm, alpha) = fwhm / (2 * sqrt(2^(1/alpha) - 1))

# bivariate
function _moffat(px, py, x, y, fwhm::BivariateLike, alpha, amp, theta, background)
# find offset from center
Expand All @@ -38,8 +42,8 @@ function _moffat(px, py, x, y, fwhm::BivariateLike, alpha, amp, theta, backgroun
dx, dy = rotate_point(dx, dy, theta)
end
# unnormalized moffat
fwhmx, fwhmy = fwhm
gammax, gammay = _moffat_fwhm_to_gamma.(fwhm, alpha)
# unnormalized moffat
dist = (dx / (fwhmx/2))^2 + (dy / (fwhmy/2))^2
dist = (dx / gammax)^2 + (dy / gammay)^2
return amp / (1 + dist)^alpha + background
end
25 changes: 21 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ function test_model_interface(K)
val_dir = @inferred K(12, 13; x=12, y=13, fwhm=10)
@test m(12, 13) val_dir 1

# test position off pixel grid
m = K(x=12.5, y=13.5, fwhm=10)
val_dir = @inferred K(12.5, 13.5; x=12.5, y=13.5, fwhm=10)
@test m(12.5, 13.5) val_dir 1


# test diagonal fwhm
m = K(x=0, y=0, fwhm=(10, 9))
@test m(3, 5) K(3, 5; x=0, y=0, fwhm=(10, 9)) K(3, 5; x=0, y=0, fwhm=SA[10, 9])
Expand Down Expand Up @@ -103,16 +109,16 @@ end
@test m(0, -r2) 0 atol=1e-5

# test with ratio
mratio = airydisk(x=0, y=0, fwhm=10, ratio=sqrt(0.5))
mratio = airydisk(x=0, y=0, fwhm=10, amp=1, ratio=sqrt(0.5))
# test attenuation
@test mratio(0, 0) 4
@test mratio(0, 0) 1
@test mratio(radius, 0) > m(radius, 0)
@test mratio(-radius, 0) > m(-radius, 0)
@test mratio(0, radius) > m(0, radius)
@test mratio(0, -radius) > m(0, -radius)


mratio = airydisk(x=0, y=0, fwhm=(10, 6), ratio=sqrt(0.5))
mratio = airydisk(x=0, y=0, fwhm=(10, 6), ratio=sqrt(0.5), amp=4)
r1 = fwhm[1] * 1.18677
r2 = fwhm[2] * 1.18677
# test attenuation
Expand All @@ -121,6 +127,10 @@ end
@test mratio(-r1, 0) > m(-r1, 0)
@test mratio(0, r2) > m(0, r2)
@test mratio(0, -r2) > m(0, -r2)

# https://github.com/JuliaAstro/PSFModels.jl/issues/14
mratio = airydisk(x = 40.5, y=40.5, fwhm=10, ratio=0.2,amp=1)
@test mratio(40.5, 40.5) 1
end

@testset "moffat" begin
Expand All @@ -135,8 +145,15 @@ end

# different alpha
m = moffat(x=0, y=0, fwhm=10, alpha=2)
expected = inv(1 + sum(abs2, SA[1, 2]) / 25)^2
expected = inv(1 + sum(abs2, SA[1, 2] ./ PSFModels._moffat_fwhm_to_gamma(10, 2)))^2
@test m(1, 2) expected

fwhms = randn(rng, 100) .+ 10
for alpha in [0.5, 1.0, 1.5]
gammas = PSFModels._moffat_fwhm_to_gamma.(fwhms, alpha)
fwhm_calc = PSFModels._moffat_gamma_to_fwhm.(gammas, alpha)
@test fwhms fwhm_calc
end
end

include("plotting.jl")
Expand Down

0 comments on commit 23eaff1

Please sign in to comment.