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

Add Moffat Kernel #147

Merged
merged 6 commits into from
Jan 27, 2020
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
1 change: 1 addition & 0 deletions docs/src/function_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Kernel.gaussian
Kernel.DoG
Kernel.LoG
Kernel.Laplacian
Kernel.moffat
```

# KernelFactors
Expand Down
2 changes: 1 addition & 1 deletion src/ImageFiltering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ ArrayLike{T} = Union{ArrayType{T}, AnyIIR{T}}

include("kernel.jl")
using .Kernel
using .Kernel: Laplacian, reflect, ando3, ando4, ando5, scharr, bickley, prewitt, sobel, gabor
using .Kernel: Laplacian, reflect, ando3, ando4, ando5, scharr, bickley, prewitt, sobel, gabor, moffat

NDimKernel{N,K} = Union{AbstractArray{K,N},ReshapedOneD{K,N},Laplacian{N}}

Expand Down
25 changes: 25 additions & 0 deletions src/kernel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,31 @@ function validate_gabor(σ::Real,λ::Real,γ::Real)
end
end

"""
moffat(α, β, ls) -> k

Constructs a 2D, symmetric Moffat kernel `k` with core width, `α`, and power, `β`.
Size of kernel defaults to 4 * full-width-half-max or as specified in `ls`.
See [this notebook](https://nbviewer.jupyter.org/github/ysbach/AO_2017/blob/master/04_Ground_Based_Concept.ipynb#1.2.-Moffat) for details.

# Citation
Moffat, A. F. J. "A theoretical investigation of focal stellar images in the photographic emulsion and application to photographic photometry." Astronomy and Astrophysics 3 (1969): 455.
"""
function moffat(α::Real, β::Real, ls::Tuple{Integer, Integer})
ws = map(n->(ceil(Int,n)>>1), ls)
gerrygralton marked this conversation as resolved.
Show resolved Hide resolved
R = CartesianIndices(map(w->IdentityUnitRange(-w:w), ws))
α2 = α^2
amp = (β - 1)/(π * α2)
@. amp*((1+df(R)/α2)^-β)
end
moffat(α::Real, β::Real, ls::Integer) = moffat(α, β, (ls,ls))
moffat(α::Real, β::Real) = moffat(α, β, ceil(Int, (α*2*sqrt(2^(1/β) - 1))*4))

@inline function df(I::CartesianIndex)
x = SVector(Tuple(I))
sum(x.^2)
end

"""
reflect(kernel) --> reflectedkernel

Expand Down
9 changes: 9 additions & 0 deletions test/specialty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ using ImageFiltering: IdentityUnitRange
ImageFiltering.fillbuf_nan[] = false
@test Kernel.LoG(2.5) == Kernel.LoG((2.5,2.5))
end

@testset "moffat" begin
α = rand()
β = rand()
@test Kernel.moffat(α,β,2) == Kernel.moffat(α,β,(2,2))

fwhm = ceil(Int, (α*2*sqrt(2^(1/β) - 1)) * 4)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's nothing preventing β from being tiny here; this causes occasional test failures like this one:

moffat: Error During Test at /Users/travis/build/JuliaImages/ImageFiltering.jl/test/specialty.jl:220

  Got exception outside of a @test

  InexactError: trunc(Int64, 1.4650326325253482e37)

  Stacktrace:

   [1] trunc at ./float.jl:696 [inlined]

   [2] ceil(::Type{Int64}, ::Float64) at ./float.jl:357

   [3] macro expansion at /Users/travis/build/JuliaImages/ImageFiltering.jl/test/specialty.jl:225 [inlined]

   [4] macro expansion at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.0/Test/src/Test.jl:1083 [inlined]

   [5] macro expansion at /Users/travis/build/JuliaImages/ImageFiltering.jl/test/specialty.jl:221 [inlined]

   [6] macro expansion at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.0/Test/src/Test.jl:1083 [inlined]

   [7] top-level scope at /Users/travis/build/JuliaImages/ImageFiltering.jl/test/specialty.jl:6

   [8] include at ./boot.jl:317 [inlined]

   [9] include_relative(::Module, ::String) at ./loading.jl:1044

   [10] include(::Module, ::String) at ./sysimg.jl:29

   [11] include(::String) at ./client.jl:392

   [12] top-level scope at none:0

   [13] include at ./boot.jl:317 [inlined]

   [14] include_relative(::Module, ::String) at ./loading.jl:1044

   [15] include(::Module, ::String) at ./sysimg.jl:29

   [16] include(::String) at ./client.jl:392

   [17] top-level scope at none:0

   [18] eval(::Module, ::Any) at ./boot.jl:319

   [19] exec_options(::Base.JLOptions) at ./client.jl:243

   [20] _start() at ./client.jl:425

Test Summary: | Pass  Error  Total

specialty     |   98      1     99

  Laplacian   |   63            63

  gaussian    |   25            25

  DoG         |    6             6

  LoG         |    3             3

  moffat      |    1      1      2

While the fix seems obvious, @gerrygralton I'd rather leave this to you since you know the moffat stuff much better than I do.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And a different variant of the same theme:

moffat: Error During Test at /home/travis/build/JuliaImages/ImageFiltering.jl/test/specialty.jl:226

  Test threw exception

  Expression: Kernel.moffat(α, β) == Kernel.moffat(α, β, (fwhm, fwhm))

  OutOfMemoryError()

  Stacktrace:

   [1] Array at ./boot.jl:407 [inlined]

   [2] Array at ./boot.jl:415 [inlined]

   [3] Array at ./boot.jl:422 [inlined]

   [4] similar at /home/travis/.julia/packages/OffsetArrays/fZSaL/src/OffsetArrays.jl:124 [inlined]

   [5] similar at ./broadcast.jl:196 [inlined]

   [6] copy at ./broadcast.jl:840 [inlined]

   [7] materialize at ./broadcast.jl:820 [inlined]

   [8] moffat(::Float64, ::Float64, ::Tuple{Int64,Int64}) at /home/travis/build/JuliaImages/ImageFiltering.jl/src/kernel.jl:408

   [9] moffat at /home/travis/build/JuliaImages/ImageFiltering.jl/src/kernel.jl:410 [inlined]

   [10] moffat(::Float64, ::Float64) at /home/travis/build/JuliaImages/ImageFiltering.jl/src/kernel.jl:411

   [11] top-level scope at /home/travis/build/JuliaImages/ImageFiltering.jl/test/specialty.jl:226

   [12] top-level scope at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Test/src/Test.jl:1113

   [13] top-level scope at /home/travis/build/JuliaImages/ImageFiltering.jl/test/specialty.jl:221

   [14] top-level scope at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Test/src/Test.jl:1113

   [15] top-level scope at /home/travis/build/JuliaImages/ImageFiltering.jl/test/specialty.jl:6

@test Kernel.moffat(α, β) == Kernel.moffat(α, β, (fwhm, fwhm))
end
end

nothing