Skip to content

Commit

Permalink
Merge pull request #118 from phajy/bind-with-frozen
Browse files Browse the repository at this point in the history
fix: bind parameters in models with frozen parameters
  • Loading branch information
fjebaker authored Jun 26, 2024
2 parents c336468 + 5f7cca6 commit c511239
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/fitting/binding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,18 @@ function _construct_bound_mapping(bindings, parameter_count)
end

function _get_index_of_symbol(model::AbstractSpectralModel, symbol)::Int
symbols = keys(parameter_named_tuple(model))
pnt = parameter_named_tuple(model)
symbols = keys(pnt)
i = findfirst(==(symbol), symbols)
if isnothing(i)
error("Could not match symbol $symbol !")
end
# don't count frozen parameters if they are prior to the parameter of interest
for s = 1:i
if isfrozen(pnt[s])
i -= 1
end
end
i
end

Expand Down
18 changes: 18 additions & 0 deletions test/fitting/test-binding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,21 @@ _, mapping = SpectralFitting._build_parameter_mapping(prob.model, prob.bindings)
# prob = FittingProblem(model1 => dummy_data1, model2 => dummy_data1)
# bind!(prob, :K)
# note that this does not work at present because `_get_index_of_symbol` throws an error if the symbol is not found

# 3 models, 1 frozen parameter (that needs to be skipped), 1 bound parameter
model1 = PowerLaw() + PowerLaw()
prob = FittingProblem(model1 => dummy_data1, model1 => dummy_data1, model1 => dummy_data1)
model1.K_1.frozen = true
bind!(prob, :a_1)
bind!(prob, :a_2)
_, mapping = SpectralFitting._build_parameter_mapping(prob.model, prob.bindings)
@test mapping == ([1, 2, 3], [1, 4, 3], [1, 5, 3])

# 3 models, 1 frozen parameter (that doesn't need to be skipped), 1 bound parameter
prob = FittingProblem(model1 => dummy_data1, model1 => dummy_data1, model1 => dummy_data1)
model1.K_1.frozen = false
model1.a_2.frozen = true
bind!(prob, :K_1)
bind!(prob, :a_1)
_, mapping = SpectralFitting._build_parameter_mapping(prob.model, prob.bindings)
@test mapping == ([1, 2, 3], [1, 2, 4], [1, 2, 5])

0 comments on commit c511239

Please sign in to comment.