Skip to content

Commit

Permalink
refactoring the dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
gpogudin committed Feb 5, 2024
1 parent dab25d5 commit 762c568
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 62 deletions.
23 changes: 18 additions & 5 deletions src/StructuralIdentifiability.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ Input:
- `ode` - the ODE model
- `funcs_to_check` - list of functions to check identifiability for; if empty, all parameters
and states are taken
- `known_ic`: a list of functions whose initial conditions are assumed to be known,
then the returned identifiable functions will be functions of parameters and
initial conditions, not states (this is an experimental functionality).
- `prob_threshold` - probability of correctness.
- `loglevel` - the minimal level of log messages to display (`Logging.Info` by default)
Expand All @@ -99,17 +102,27 @@ The function returns an (ordered) dictionary from the functions to check to thei
function assess_identifiability(
ode::ODE{P};
funcs_to_check = Vector(),
known_ic::Vector{<:Union{P, Generic.Frac{P}}} = Vector{Union{P, Generic.Frac{P}}}(),
prob_threshold::Float64 = 0.99,
loglevel = Logging.Info,
) where {P <: MPolyRingElem{QQFieldElem}}
restart_logging(loglevel = loglevel)
reset_timings()
with_logger(_si_logger[]) do
return _assess_identifiability(
ode,
funcs_to_check = funcs_to_check,
prob_threshold = prob_threshold,
)
if isempty(known_ic)
return _assess_identifiability(
ode,
funcs_to_check = funcs_to_check,
prob_threshold = prob_threshold,
)
else
return _assess_identifiability_kic(

Check warning on line 119 in src/StructuralIdentifiability.jl

View check run for this annotation

Codecov / codecov/patch

src/StructuralIdentifiability.jl#L119

Added line #L119 was not covered by tests
ode,
known_ic,
funcs_to_check = funcs_to_check,
prob_threshold = prob_threshold,
)
end
end
end

Expand Down
31 changes: 23 additions & 8 deletions src/identifiable_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ This functions takes the following optional arguments:
- `:strong`: Strong simplification. This option is the slowest, but the output
functions are nice and simple.
- `:absent`: No simplification.
- `known_ic`: a list of functions whose initial conditions are assumed to be known,
then the returned identifiable functions will be functions of parameters and
initial conditions, not states (this is an experimental functionality).
- `prob_threshold`: A float in the range from 0 to 1, the probability of correctness. Default
is `0.99`.
- `seed`: The rng seed. Default value is `42`.
Expand Down Expand Up @@ -45,6 +48,7 @@ find_identifiable_functions(ode)
"""
function find_identifiable_functions(
ode::ODE{T};
known_ic::Vector{<:Union{T, Generic.Frac{T}}} = Vector{Union{T, Generic.Frac{T}}}(),
prob_threshold::Float64 = 0.99,
seed = 42,
with_states = false,
Expand All @@ -55,14 +59,25 @@ function find_identifiable_functions(
restart_logging(loglevel = loglevel)
reset_timings()
with_logger(_si_logger[]) do
return _find_identifiable_functions(
ode,
prob_threshold = prob_threshold,
seed = seed,
with_states = with_states,
simplify = simplify,
rational_interpolator = rational_interpolator,
)
if isempty(known_ic)
return _find_identifiable_functions(
ode,
prob_threshold = prob_threshold,
seed = seed,
with_states = with_states,
simplify = simplify,
rational_interpolator = rational_interpolator,
)
else
return _find_identifiable_functions_kic(

Check warning on line 72 in src/identifiable_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/identifiable_functions.jl#L72

Added line #L72 was not covered by tests
ode,
known_ic,
prob_threshold = prob_threshold,
seed = seed,
simplify = simplify,
rational_interpolator = rational_interpolator,
)
end
end
end

Expand Down
48 changes: 3 additions & 45 deletions src/known_ic.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
find_identifiable_functions_kic(ode::ODE, known_ic; options...)
_find_identifiable_functions_kic(ode::ODE, known_ic; options...)
Finds all functions of parameters/initial conditions that are identifiable in the given ODE
system under assumptions that the initial conditions for functions in the list
Expand All @@ -26,29 +26,6 @@ This functions takes the following optional arguments:
```
"""
function find_identifiable_functions_kic(
ode::ODE{T},
known_ic::Vector{<:Union{T, Generic.Frac{T}}};
prob_threshold::Float64 = 0.99,
seed = 42,
simplify = :standard,
rational_interpolator = :VanDerHoevenLecerf,
loglevel = Logging.Info,
) where {T <: MPolyElem{fmpq}}
restart_logging(loglevel = loglevel)
reset_timings()
with_logger(_si_logger[]) do
return _find_identifiable_functions_kic(
ode,
known_ic,
prob_threshold = prob_threshold,
seed = seed,
simplify = simplify,
rational_interpolator = rational_interpolator,
)
end
end

function _find_identifiable_functions_kic(

Check warning on line 29 in src/known_ic.jl

View check run for this annotation

Codecov / codecov/patch

src/known_ic.jl#L29

Added line #L29 was not covered by tests
ode::ODE{T},
known_ic::Vector{<:Union{T, Generic.Frac{T}}};
Expand All @@ -61,7 +38,7 @@ function _find_identifiable_functions_kic(
@assert simplify in (:standard, :weak, :strong, :absent)
half_p = 0.5 + prob_threshold / 2
runtime_start = time_ns()
id_funcs_general = find_identifiable_functions(
id_funcs_general = _find_identifiable_functions(

Check warning on line 41 in src/known_ic.jl

View check run for this annotation

Codecov / codecov/patch

src/known_ic.jl#L37-L41

Added lines #L37 - L41 were not covered by tests
ode,
prob_threshold = half_p,
with_states = true,
Expand All @@ -86,7 +63,7 @@ function _find_identifiable_functions_kic(
end

"""
assess_identifiability_kic(ode, known_ic; funcs_to_check = [], prob_threshold=0.99, loglevel=Logging.Info)
_assess_identifiability_kic(ode; known_ic, funcs_to_check = [], prob_threshold=0.99, loglevel=Logging.Info)
Input:
- `ode` - the ODE model
Expand All @@ -101,25 +78,6 @@ The result is guaranteed to be correct with the probability at least `prob_thres
The function returns an (ordered) dictionary from the functions to check to their identifiability properties
(one of `:nonidentifiable`, `:locally`, `:globally`).
"""
function assess_identifiability_kic(
ode::ODE{P},
known_ic::Vector{<:Union{P, Generic.Frac{P}}};
funcs_to_check = Vector(),
prob_threshold::Float64 = 0.99,
loglevel = Logging.Info,
) where {P <: MPolyElem{fmpq}}
restart_logging(loglevel = loglevel)
reset_timings()
with_logger(_si_logger[]) do
return _assess_identifiability_kic(
ode,
known_ic,
prob_threshold = prob_threshold,
funcs_to_check = funcs_to_check,
)
end
end

function _assess_identifiability_kic(

Check warning on line 81 in src/known_ic.jl

View check run for this annotation

Codecov / codecov/patch

src/known_ic.jl#L81

Added line #L81 was not covered by tests
ode::ODE{P},
known_ic::Vector{<:Union{P, Generic.Frac{P}}};
Expand Down
17 changes: 15 additions & 2 deletions test/known_ic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,31 @@ push!(
),
)

ode = @ODEmodel(x1'(t) = a * x1(t), x2'(t) = x1(t) + 1 / x1(t), y(t) = x2(t))

push!(
cases,
Dict(
:ode => ode,
:known => [x1],
:to_check => [],
:correct_funcs => [a, x1, x2],
:correct_ident => OrderedDict(x1 => :globally, x2 => :globally, a => :globally),
),
)

@testset "Identifiable functions with known generic initial conditions" begin
for case in cases
ode = case[:ode]
known = case[:known]

result_funcs = find_identifiable_functions_kic(ode, known)
result_funcs = find_identifiable_functions(ode, known_ic = known)
correct_funcs =
replace_with_ic(ode, [f // one(parent(ode)) for f in case[:correct_funcs]])
@test Set(result_funcs) == Set(correct_funcs)

result_ident =
assess_identifiability_kic(ode, known, funcs_to_check = case[:to_check])
assess_identifiability(ode, known_ic = known, funcs_to_check = case[:to_check])
@test OrderedDict(
replace_with_ic(ode, [k])[1] => v for (k, v) in case[:correct_ident]
) == result_ident
Expand Down
2 changes: 0 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ using StructuralIdentifiability:
lie_derivative,
states_generators,
RationalFunctionField,
find_identifiable_functions_kic,
assess_identifiability_kic,
replace_with_ic,
x_vars,
y_vars,
Expand Down

0 comments on commit 762c568

Please sign in to comment.