Skip to content

Commit

Permalink
edit code for selection of param removal codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis-Titov committed Feb 26, 2024
1 parent 74a9ec9 commit 3a63e5a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 60 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ CMAEvolutionStrategy = "0.2.6"
DataFrames = "1.6"
Statistics = "1.6"
julia = "1.6"
CSV = "0.10"

[extras]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "SafeTestsets", "BenchmarkTools", "CSV"]
test = ["Test", "SafeTestsets", "BenchmarkTools"]
37 changes: 6 additions & 31 deletions src/optimal_rate_equation_selection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ function forward_selection_next_param_removal_codes(
])...,
)
end
param_removal_codes
param_removal_codes = unique(param_removal_codes)
# param_removal_codes = [
# NamedTuple{Tuple(nt_param_choice_names)}(x) for
# x in unique(param_removal_codes) if
Expand All @@ -254,14 +254,10 @@ function reverse_selection_next_param_removal_codes(
sum(param_removal_code[1:(end-num_alpha_params)] .> 0) == num_params - 1 for
param_removal_code in previous_param_removal_codes
])
# previous_param_subset_masks = unique([
# previous_param_removal_code .> 0 for
# previous_param_removal_code in previous_param_removal_codes
# ])
previous_param_subset_masks = unique([
(
mask = [
(previous_param_removal_code[1:(end-num_alpha_params)] .> 0)...,
(previous_param_removal_code[1:(end-num_alpha_params)] .== 0)...,
zeros(Int64, num_alpha_params)...,
],
non_zero_params = previous_param_removal_code .*
Expand All @@ -276,44 +272,23 @@ function reverse_selection_next_param_removal_codes(
sum(param_removal_codes[1:(end-num_alpha_params)] .> 0)
) == num_params
]
#actually choose param_removal_codes with n_removed_params number of parameters removed that also contain non-zero elements from previous_param_removal_codes
# param_removal_codes = []
# for previous_param_subset_mask in previous_param_subset_masks
# push!(
# param_removal_codes,
# unique([
# param_code_w_num_params .* previous_param_subset_mask for
# param_code_w_num_params in all_param_codes_w_num_params if (
# length(param_names) - num_alpha_params - sum(
# (param_code_w_num_params.*previous_param_subset_mask)[1:(end-num_alpha_params)] .>
# 0,
# )
# ) == num_params
# ])...,
# )
# end
#choose param_removal_codes with n_removed_params number of parameters removed that also contain non-zero elements from previous_param_removal_codes
param_removal_codes = []
for previous_param_subset_mask in previous_param_subset_masks
push!(
param_removal_codes,
unique([
param_code_w_num_params .* previous_param_subset_mask.mask .+
previous_param_subset_mask.non_zero_params for
previous_param_subset_mask.non_zero_params .*
(param_code_w_num_params .!= 0) for
param_code_w_num_params in all_param_codes_w_num_params if (
length(param_names) - num_alpha_params - sum(
(param_code_w_num_params.*previous_param_subset_mask.mask.+previous_param_subset_mask.non_zero_params)[1:(end-num_alpha_params)] .>
(previous_param_subset_mask.non_zero_params.*(param_code_w_num_params.!=0))[1:(end-num_alpha_params)] .>
0,
)
) == num_params
])...,
)
end
param_removal_codes
# param_removal_codes = [
# NamedTuple{Tuple(nt_param_choice_names)}(x) for
# x in unique(param_removal_codes) if
# sum(values(x[1:(end-num_alpha_params)]) .> 0) == num_params - min_num_params
# ]
param_removal_codes = unique(param_removal_codes)
return param_removal_codes
end
4 changes: 2 additions & 2 deletions src/rate_equation_fitting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function train_rate_equation(
end
rescaled_params = param_rescaling(xbest(best_sol), param_names)
if !isnothing(nt_param_choice)
rescaled_params = param_subset_select(rescaled_params, nt_param_choice)
rescaled_params = param_subset_select(rescaled_params, param_names, nt_param_choice)
end
return (loss=fbest(best_sol), params=NamedTuple{param_names}(rescaled_params))
end
Expand All @@ -186,7 +186,7 @@ function loss_rate_equation(
kinetic_params = param_rescaling(params, param_names)
end
if !isnothing(nt_param_choice)
kinetic_params .= param_subset_select(kinetic_params, nt_param_choice)
kinetic_params .= param_subset_select(kinetic_params, param_names, nt_param_choice)
end

#precalculate log_pred_vs_data_ratios for all points as it is expensive and reuse it for weights and loss
Expand Down
1 change: 1 addition & 0 deletions test/tests_for_general_rate_eq_fitting.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# using TestEnv
# TestEnv.activate()

##
using EnzymeFitting, Test
using BenchmarkTools, CMAEvolutionStrategy, DataFrames, CSV, Statistics

Expand Down
72 changes: 47 additions & 25 deletions test/tests_for_optimal_rate_eq_selection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,42 @@ funct_output_param_subset_codes = EnzymeFitting.forward_selection_next_param_rem
num_params,
param_names,
)
#test1
#ensure that funct_output_param_subset_codes have one less parameter than previous_param_removal_codes
@test all(
length(param_names) - n_alphas -
sum(funct_output_param_subset_code[1:(end-n_alphas)] .> 0) ==
(num_previous_step_params - 1) for
funct_output_param_subset_code in funct_output_param_subset_codes
)
#test2
#ensure that non-zero elements from previous_param_removal_codes are present in > 1 of the funct_output_param_subset_code but less than the max_matches
count_matches = Bool[]
non_zero_code_combos_per_param = ()
for param_name in param_names
if param_name == :L
global non_zero_code_combos_per_param = (non_zero_code_combos_per_param..., 1)
elseif occursin("Vmax_a", string(param_name))
global non_zero_code_combos_per_param = (non_zero_code_combos_per_param..., 2)
elseif occursin("K_a", string(param_name))
global non_zero_code_combos_per_param = (non_zero_code_combos_per_param..., 3)
elseif occursin("alpha", string(param_name))
global non_zero_code_combos_per_param = (non_zero_code_combos_per_param..., 1)
end
end
for funct_output_param_subset_code in funct_output_param_subset_codes
count = 0
max_matches = 0
for previous_param_removal_code in previous_param_removal_codes
count +=
funct_output_param_subset_code .* [previous_param_removal_code...] ==
[previous_param_removal_code...] .^ 2
funct_output_param_subset_code[1:end-n_alphas] .* [previous_param_removal_code[1:end-n_alphas]...] ==
[previous_param_removal_code[1:end-n_alphas]...] .^ 2
max_matches = sum((previous_param_removal_code[1:end-n_alphas] .== 0) .* non_zero_code_combos_per_param[1:end-n_alphas])
end
push!(count_matches, count > 0)
push!(count_matches, max_matches >= count > 0)
end
@test all(count_matches)



#test reverse_selection_next_param_removal_codes
num_metabolites = rand(4:8)
n_alphas = rand(1:4)
Expand Down Expand Up @@ -80,29 +95,36 @@ funct_output_param_subset_codes = EnzymeFitting.reverse_selection_next_param_rem
num_params,
param_names,
)

#test1
@assert all(
#ensure that funct_output_param_subset_codes have one more parameter than previous_param_removal_codes
@test all(
length(param_names) - n_alphas -
sum(funct_output_param_subset_code[1:(end-n_alphas)] .> 0) ==
(num_previous_step_params + 1) for
funct_output_param_subset_code in funct_output_param_subset_codes
)
#ensure that non-zero elements from each funct_output_param_subset_codes are present in > 1 of the previous_param_removal_codes but less than the max_matches
count_matches = []
non_zero_code_combos_per_param = ()
for param_name in param_names
if param_name == :L
global non_zero_code_combos_per_param = (non_zero_code_combos_per_param..., 1)
elseif occursin("Vmax_a", string(param_name))
global non_zero_code_combos_per_param = (non_zero_code_combos_per_param..., 2)
elseif occursin("K_a", string(param_name))
global non_zero_code_combos_per_param = (non_zero_code_combos_per_param..., 3)
elseif occursin("alpha", string(param_name))
global non_zero_code_combos_per_param = (non_zero_code_combos_per_param..., 1)
end
end
for funct_output_param_subset_code in funct_output_param_subset_codes
count = 0
for previous_param_removal_code in previous_param_removal_codes
count +=
funct_output_param_subset_code[1:end-n_alphas] == previous_param_removal_code[1:end-n_alphas] .* (funct_output_param_subset_code[1:end-n_alphas] .!= 0)

# #test2
# count_matches = []
# for previous_param_removal_code in previous_param_removal_codes
# count = 0
# for funct_output_param_subset_code in funct_output_param_subset_codes
# count +=
# funct_output_param_subset_code[1:end-n_alphas] .* [previous_param_removal_code[1:end-n_alphas]...] ==
# [funct_output_param_subset_code[1:end-n_alphas]...] .^ 2
# end
# push!(count_matches, count)
# end
# funct_output_param_subset_codes
# previous_param_removal_codes
# funct_output_param_subset_codes[2] .* [previous_param_removal_codes[1]...] ==
# [funct_output_param_subset_codes[2]...] .^ 2
# count_matches
# @assert all(count_matches)
end
max_matches = sum((funct_output_param_subset_code[1:end-n_alphas] .== 0) .* non_zero_code_combos_per_param[1:end-n_alphas])
push!(count_matches, max_matches >= count > 0)

end
@test all(count_matches)

0 comments on commit 3a63e5a

Please sign in to comment.