Skip to content

Commit

Permalink
🚧 Upgrade toplevel NTI utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
iago-lito committed Dec 20, 2024
1 parent 07944a5 commit 11d944b
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/EcologicalNetworksDynamics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ include("./components/main.jl")
# HERE: now that all components have been upgraded, upgrade the following!
# Additional exposed utils built on top of components and methods.
include("./default_model.jl")
# include("./nontrophic_layers.jl")
include("./nontrophic_layers.jl")
# include("./simulate.jl")
# include("./topology.jl")
# include("./diversity.jl")
Expand Down
20 changes: 11 additions & 9 deletions src/components/nontrophic_layers/nontrophic_components_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,25 @@ end
# but it's useful for higher-level NTI interfaces like `L = (refuge = 5, interference = 8)`.
function fields_from_multiplex_parms(int::Symbol, d::MultiplexParametersDict)
name = Symbol(uppercasefirst(String(int)))
TopologyFromRawEdges = eval(Symbol(name, :TopologyFromRawEdges))
RandomTopology = eval(Symbol(:Random, name, :Topology))
Intensity = eval(Symbol(name, :Intensity))
mod = eval(name)
RawTopology = mod.Topology.Raw
RandomTopology = mod.Topology.Random
Intensity = mod.Intensity
res = [
if haskey(d, :A)
TopologyFromRawEdges(d[:A])
RawTopology(d[:A])
else
RandomTopology(
get(d, :L, nothing),
get(d, :C, nothing),
get(d, :sym, multiplex_defaults[:s][int]),
kw = (;
L = get(d, :L, nothing),
C = get(d, :C, nothing),
symmetry = get(d, :sym, multiplex_defaults[:s][int]),
)
RandomTopology(; filter(!isnothing, kw)...)
end,
Intensity(get(d, :intensity, multiplex_defaults[:I][int])),
]
if int != :interference
FunctionalForm = eval(Symbol(name, :FunctionalForm))
FunctionalForm = mod.FunctionalForm
push!(res, FunctionalForm(get(d, :fn, multiplex_defaults[:fn][int])))
end
res
Expand Down
16 changes: 10 additions & 6 deletions src/default_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,14 @@ function default_model(blueprints::Union{Blueprint,BlueprintSum}...; without = C
for brought in F.brought(bp)
brought isa Blueprint || continue
for B in F.componentsof(brought)
collected(B) &&
argerr("Blueprint embeds an already given sub-blueprint:\n \
- The blueprint: $bp\n \
- Already given: $(peek_collected(B))")
if collected(B)
bp = sprint(F.display_long, bp, 1)
alr = sprint(F.display_long, peek_collected(B), 1)
B = sprint(Base.show, B)
argerr("Blueprint embeds an already given sub-blueprint for $B:\n \
-> The blueprint: $bp\n \
-> Already given: $alr")
end
# 'Construct' the brought bueprints
# to also recursively collect their dependencies.
collect_needed!(brought)
Expand Down Expand Up @@ -218,7 +222,7 @@ function default_model(blueprints::Union{Blueprint,BlueprintSum}...; without = C
fr =
given(BioenergeticResponse) ? BioenergeticResponse :
given(LinearResponse) ? LinearResponse : nothing
isnothing(fr) || argerr("Temperature response is not designed for $(nameof(fr)). \
isnothing(fr) || argerr("Temperature response is not designed for $fr. \
Use ClassicResponse instead, \
or don't specify a temperature.")
end
Expand Down Expand Up @@ -344,7 +348,7 @@ function default_model(blueprints::Union{Blueprint,BlueprintSum}...; without = C
model = Model()

already = Set() # (avoid duplicated multi-provided blueprints)
for (C, bp) in final
for (_, bp) in final
isnothing(bp) && continue
bp in already && continue

Expand Down
20 changes: 1 addition & 19 deletions src/multiplex_api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,7 @@ using ..AliasingDicts
import ..Option
import ..SparseMatrix

# (reassure JuliaLS)
#! format: off
if (false)
(
local
InteractionDict,
Multiplex,
MultiplexArguments,
MultiplexDict,
MultiplexParametersDict,
TrackedMultiplexParameterDict,
parse_interaction_for_multiplex_parameter,
parse_multiplex_arguments,
parse_multiplex_parameter_for_interaction,

var""
)
end
#! format: on
include("multiplex_identifiers.jl")

@aliasing_dict(
InteractionDict,
Expand Down
24 changes: 24 additions & 0 deletions src/multiplex_identifiers.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Stupid shenanigans to trick JuliaLS into correctly finding references.
# and not clutter further code with "missing reference" warnings.
# All this code does nothing when actually executed.
# Its sole purpose is to solve these incorrect lints.


#! format: off
if (false)
(
local
InteractionDict,
Multiplex,
MultiplexArguments,
MultiplexDict,
MultiplexParametersDict,
TrackedMultiplexParameterDict,
parse_interaction_for_multiplex_parameter,
parse_multiplex_arguments,
parse_multiplex_parameter_for_interaction,

var""
)
end
#! format: on
12 changes: 7 additions & 5 deletions src/nontrophic_layers.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
include("multiplex_identifiers.jl")

# Construct several layers at once using the "2D" multiplex API.
nti_components = InteractionDict(
:trophic => TrophicLayer,
:competition => CompetitionLayer,
:facilitation => FacilitationLayer,
:interference => InterferenceLayer,
:refuge => RefugeLayer,
:competition => Competition.Layer,
:facilitation => Facilitation.Layer,
:interference => Interference.Layer,
:refuge => Refuge.Layer,
)

# Construct a sequence of blueprints from 2D kwargs.
Expand All @@ -26,7 +28,7 @@ export nontrophic_layers

# Output a blueprint sum for consistency as arguments to default_model.
NontrophicLayers(; kwargs...) =
sum(values(nontrophic_layers(kwargs)); init = ModelBlueprintSum())
sum(values(nontrophic_layers(kwargs)); init = BlueprintSum())
export NontrophicLayers

add_nontrophic_layers!(m::Model, input) = add!(m, values(nontrophic_layers(input))...)
Expand Down
22 changes: 10 additions & 12 deletions test/user/05-default_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module TestDefaultModel
using EcologicalNetworksDynamics
using Test

Value = EcologicalNetworksDynamics.InnerParms # To make @sysfails work.
Value = EcologicalNetworksDynamics.Internal # To make @sysfails work.
import ..Main: @sysfails, @argfails

@testset "Default model." begin
Expand Down Expand Up @@ -138,22 +138,20 @@ import ..Main: @sysfails, @argfails
),
)
@test has_component(m, ClassicResponse)
@test has_component(m, RefugeLayer)
@test has_component(m, FacilitationLayer)
@test !has_component(m, CompetitionLayer)
@test !has_component(m, InterferenceLayer)
@test m.facilitation_layer_intensity == 8
@test m.refuge_layer_intensity == 5
@test sum(m.refuge_links) == 4
@test sum(m.facilitation_links) == 6
@test has_component(m, Refuge.Layer)
@test has_component(m, Facilitation.Layer)
@test !has_component(m, Competition.Layer)
@test !has_component(m, Interference.Layer)
@test m.facilitation.intensity == 8
@test m.refuge.intensity == 5
@test sum(m.refuge.links.matrix) == 4
@test m.facilitation.links.number == 6

# Check input consistency.
@argfails(default_model(), "No blueprint specified for a foodweb.")
@argfails(
default_model(fw, BodyMass(2), ClassicResponse(; M = 3)),
"Blueprint for $ClassicResponse brings $BodyMass, already given:\n \
- $ClassicResponse brings: blueprint for $BodyMass(M: 3.0)\n \
- already given: blueprint for $BodyMass(M: 2.0)"
["Blueprint embeds an already given sub-blueprint for <$BodyMass>:"]
)
@argfails(
default_model(fw, Temperature(290), BioenergeticResponse()),
Expand Down

0 comments on commit 11d944b

Please sign in to comment.