-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from fjebaker/fergus/convolutions
Convolutions
- Loading branch information
Showing
8 changed files
with
239 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,3 +65,5 @@ function _or_else(value::Union{Nothing,T}, v::T)::T where {T} | |
value | ||
end | ||
end | ||
|
||
export AsConvolution |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
struct AsConvolution{M,T,V,P} <: AbstractModelWrapper{M,T,Convolutional} | ||
model::M | ||
# the domain on which we evaluate this model | ||
domain::V | ||
# an additional output cache | ||
cache::NTuple{2,Vector{P}} | ||
function AsConvolution( | ||
model::AbstractSpectralModel{T}, | ||
domain::V, | ||
cache::NTuple{2,Vector{P}}, | ||
) where {T,V,P} | ||
new{typeof(model),T,V,P}(model, domain, cache) | ||
end | ||
end | ||
|
||
function AsConvolution( | ||
model::AbstractSpectralModel{T}; | ||
domain = collect(range(0, 2, 100)), | ||
) where {T} | ||
output = invokemodel(domain, model) | ||
AsConvolution(model, domain, (output, deepcopy(output))) | ||
end | ||
|
||
function invoke!(output, domain, model::AsConvolution{M,T}) where {M,T} | ||
D = promote_type(eltype(domain), T) | ||
model_output, _ = | ||
_reinterpret_dual(typeof(model), D, model.cache[1], length(model.domain) - 1) | ||
convolution_cache, _ = _reinterpret_dual( | ||
typeof(model), | ||
D, | ||
model.cache[2], | ||
length(output) + length(model_output) - 1, | ||
) | ||
|
||
# invoke the child model | ||
invoke!(model_output, model.domain, model.model) | ||
|
||
# do the convolution | ||
convolve!(convolution_cache, output, model_output) | ||
|
||
# overwrite the output | ||
shift = div(length(model_output), 2) | ||
@views output .= convolution_cache[1+shift:length(output)+shift] | ||
end | ||
|
||
function Reflection.get_parameter_symbols( | ||
::Type{<:AsConvolution{M}}, | ||
) where {M<:AbstractSpectralModel{T,K}} where {T,K} | ||
syms = Reflection.get_parameter_symbols(M) | ||
if K === Additive | ||
# we need to lose the normalisation parameter | ||
(syms[2:end]...,) | ||
else | ||
syms | ||
end | ||
end | ||
|
||
function Reflection.make_constructor( | ||
M::Type{<:AsConvolution{Model}}, | ||
closures::Vector, | ||
params::Vector, | ||
T::Type, | ||
) where {Model<:AbstractSpectralModel{Q,K}} where {Q,K} | ||
num_closures = fieldcount(M) - 1 # ignore the `model` field | ||
my_closures = closures[1:num_closures] | ||
|
||
model_params = if K === Additive | ||
# insert a dummy normalisation to the constructor | ||
vcat(:(one($T)), params) | ||
else | ||
params | ||
end | ||
|
||
model_constructor = | ||
Reflection.make_constructor(Model, closures[num_closures+1:end], model_params, T) | ||
:($(Base.typename(M).name)($(model_constructor), $(my_closures...))) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
function _convolve_implementation!( | ||
output::AbstractVector{T}, | ||
vec_A::AbstractVector{T}, | ||
kernel::AbstractVector{T}, | ||
) where {T<:Number} | ||
# Based on https://discourse.julialang.org/t/97658/15 | ||
J = length(vec_A) | ||
K = length(kernel) | ||
@assert length(output) == J + K - 1 "Ouput is $(length(output)); should be $(J + K - 1)" | ||
|
||
# do the kernel's side first | ||
for i = 1:K-1 | ||
total = zero(T) | ||
for k = 1:K | ||
ib = (i >= k) | ||
oa = ib ? vec_A[i-k+1] : zero(T) | ||
total += kernel[k] * oa | ||
end | ||
output[i] = total | ||
end | ||
# now the middle | ||
for i = K:J-1 | ||
total = zero(T) | ||
for k = 1:K | ||
oa = vec_A[i-k+1] | ||
total += kernel[k] * oa | ||
end | ||
output[i] = total | ||
end | ||
# and finally the end | ||
for i = J:(J+K-1) | ||
total = zero(T) | ||
for k = 1:K | ||
ib = (i < J + k) | ||
oa = ib ? vec_A[i-k+1] : zero(T) | ||
total += kernel[k] * oa | ||
end | ||
output[i] = total | ||
end | ||
output | ||
end | ||
|
||
convolve!(output, A, kernel) = _convolve_implementation!(output, A, kernel) | ||
function convolve(A, kernel) | ||
output = zeros(eltype(A), length(A) + length(kernel) - 1) | ||
convolve!(output, A, kernel) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using SpectralFitting | ||
using Test | ||
|
||
include("../dummies.jl") | ||
|
||
# put a couple of delta emission lines together | ||
lines = DeltaLine(; E = FitParam(3.0), K = FitParam(2.0)) + DeltaLine(; E = FitParam(7.0)) | ||
|
||
# construct the convolutional wrapper | ||
base_model = GaussianLine(; μ = FitParam(1.0), σ = FitParam(0.3)) | ||
conv = AsConvolution(base_model) | ||
|
||
model = conv(lines) | ||
|
||
domain = collect(range(0.0, 10.0, 150)) | ||
|
||
plot(domain[1:end-1], invokemodel(domain, lines)) | ||
plot(domain[1:end-1], invokemodel(domain, model)) | ||
|
||
output = invokemodel(domain, model) | ||
|
||
@test sum(output) ≈ 3.2570820013702395 atol = 1e-4 | ||
@test output[10] ≈ 0.0036345342427057687 atol = 1e-4 | ||
@test output[40] ≈ 0.055218163108951814 atol = 1e-4 | ||
|
||
# simulate a model spectrum | ||
dummy_data = make_dummy_dataset((E) -> (E^(-3.0)); units = u"counts / (s * keV)") | ||
sim = simulate(model, dummy_data; seed = 42) | ||
|
||
model.μ_1.frozen = true | ||
model.K_1.frozen = true | ||
model.K_2.frozen = true | ||
model.E_1.frozen = true | ||
model.E_2.frozen = true | ||
|
||
# change the width | ||
model.σ_1.value = 0.1 | ||
model | ||
|
||
begin | ||
prob = FittingProblem(model => sim) | ||
result = fit(prob, LevenbergMarquadt()) | ||
end | ||
@test result.χ2 ≈ 76.15221077389369 atol = 1e-3 | ||
|
||
# put a couple of delta emission lines together | ||
lines = | ||
DeltaLine(; E = FitParam(3.0), K = FitParam(2.0), width = 0.1) + | ||
DeltaLine(; E = FitParam(7.0)) | ||
model = conv(lines) | ||
|
||
sim = simulate(model, dummy_data; seed = 42) | ||
|
||
# now see if we can fit the delta line | ||
model.μ_1.frozen = true | ||
model.K_1.frozen = true | ||
model.K_2.frozen = true | ||
model.E_1.frozen = true | ||
model.E_2.frozen = true | ||
model.σ_1.frozen = true | ||
|
||
model.E_2.frozen = false | ||
model.E_2.value = 2.0 | ||
model.K_2.frozen = true | ||
# model.K_2.value = 2.0 | ||
|
||
model | ||
begin | ||
prob = FittingProblem(model => sim) | ||
result = fit(prob, LevenbergMarquadt(); verbose = true) | ||
end | ||
@test result.χ2 ≈ 75.736 atol = 1e-3 |