From 2354a3278577b80f5750a8953dfc14b3f1608187 Mon Sep 17 00:00:00 2001 From: phajy Date: Wed, 26 Jun 2024 20:27:29 +0100 Subject: [PATCH 1/2] fix: bind parameters in models with frozen parameters --- src/fitting/binding.jl | 9 ++++++++- test/fitting/test-binding.jl | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/fitting/binding.jl b/src/fitting/binding.jl index 81053310..d90b312b 100644 --- a/src/fitting/binding.jl +++ b/src/fitting/binding.jl @@ -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 diff --git a/test/fitting/test-binding.jl b/test/fitting/test-binding.jl index 11a31dd1..be51187f 100644 --- a/test/fitting/test-binding.jl +++ b/test/fitting/test-binding.jl @@ -78,3 +78,20 @@ _, 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 +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]) From 5f7cca609d2ac48f251e175fd4527c83874e6a62 Mon Sep 17 00:00:00 2001 From: phajy Date: Wed, 26 Jun 2024 20:52:49 +0100 Subject: [PATCH 2/2] fix: model specification added to binding frozen model test case --- test/fitting/test-binding.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/fitting/test-binding.jl b/test/fitting/test-binding.jl index be51187f..ea05ad0c 100644 --- a/test/fitting/test-binding.jl +++ b/test/fitting/test-binding.jl @@ -80,6 +80,7 @@ _, mapping = SpectralFitting._build_parameter_mapping(prob.model, prob.bindings) # 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)