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

fix: bind parameters in models with frozen parameters #118

Merged
merged 2 commits into from
Jun 26, 2024
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
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])
Loading