I have an agent based model from which I'm attempting to aggregate data. If each of my agents has an energy
field, I can do this:
energy(a) = a.energy
adata = [(energy, mean)]
fig, = abmexploration(
model;
agent_step!,
model_step!,
adata,
)
and all works well. However, I would like to exclude some agents from the aggregation (it's a mixed agent model, and for some types the energy
field should not even be there). To do so, I thought I could use NaN
to my advantage:
energy(a) = a isa MyAgentType1 ? a.energy : NaN
nanmean(collection) = mean(filter(!isnan, collection))
adata = [(energy, nanmean)]
However, this produces a rather cryptic error:
No possible aggregation for energy using nanmean
multi_agent_agg_types!(::Vector{Vector}, ::Tuple{DataType, DataType}, ::Vector{String}, ::Agents.AgentBasedModel{Agents.ContinuousSpace{2, true, Float64, typeof(Agents.defvel)}, Union{Main.workspace#3.Blob, Main.workspace#3.Food}, Agents.Schedulers.Randomly, Dict{Symbol, Int64}, Random.TaskLocalRNG}, ::Vector{Tuple{typeof(Main.workspace#3.energy), typeof(Main.workspace#5.nanmean)}})@collect.jl:381
init_agent_dataframe(::Agents.AgentBasedModel{Agents.ContinuousSpace{2, true, Float64, typeof(Agents.defvel)}, Union{Main.workspace#3.Blob, Main.workspace#3.Food}, Agents.Schedulers.Randomly, Dict{Symbol, Int64}, Random.TaskLocalRNG}, ::Vector{Tuple{typeof(Main.workspace#3.energy), typeof(Main.workspace#5.nanmean)}})@collect.jl:317
var"#ABMObservable#27"(::Function, ::Function, ::Vector{Tuple{typeof(Main.workspace#3.energy), typeof(Main.workspace#5.nanmean)}}, ::Nothing, ::Bool, ::Type{InteractiveDynamics.ABMObservable}, ::Agents.AgentBasedModel{Agents.ContinuousSpace{2, true, Float64, typeof(Agents.defvel)}, Union{Main.workspace#3.Blob, Main.workspace#3.Food}, Agents.Schedulers.Randomly, Dict{Symbol, Int64}, Random.TaskLocalRNG})@model_observable.jl:40
var"#abmplot!#30"(::Function, ::Function, ::Vector{Tuple{typeof(Main.workspace#3.energy), typeof(Main.workspace#5.nanmean)}}, ::Nothing, ::Bool, ::Bool, ::Bool, ::Bool, ::Base.Pairs{Symbol, typeof(Main.workspace#3.agent_color), Tuple{Symbol}, NamedTuple{(:ac,), Tuple{typeof(Main.workspace#3.agent_color)}}}, ::typeof(InteractiveDynamics.abmplot!), ::Makie.MakieLayout.Axis, ::Agents.AgentBasedModel{Agents.ContinuousSpace{2, true, Float64, typeof(Agents.defvel)}, Union{Main.workspace#3.Blob, Main.workspace#3.Food}, Agents.Schedulers.Randomly, Dict{Symbol, Int64}, Random.TaskLocalRNG})@abmplot.jl:131
#abmplot#29@abmplot.jl:112[inlined]
var"#abmexploration#81"(::Nothing, ::Nothing, ::NamedTuple{(), Tuple{}}, ::Base.Pairs{Symbol, Any, NTuple{4, Symbol}, NamedTuple{(:agent_step!, :model_step!, :ac, :adata), Tuple{typeof(Main.workspace#3.agent_step!), typeof(Main.workspace#3.model_step!), typeof(Main.workspace#3.agent_color), Vector{Tuple{typeof(Main.workspace#3.energy), typeof(Main.workspace#5.nanmean)}}}}}, ::typeof(InteractiveDynamics.abmexploration), ::Agents.AgentBasedModel{Agents.ContinuousSpace{2, true, Float64, typeof(Agents.defvel)}, Union{Main.workspace#3.Blob, Main.workspace#3.Food}, Agents.Schedulers.Randomly, Dict{Symbol, Int64}, Random.TaskLocalRNG})@convenience.jl:39
I didn't manage to find anything with google... and the nanmean
method works as intended, if I call it myself. Suggestions?
I am clueless here but what about missing
?
unfortunately no, because mean([1, missing])
is missing
.
Found out how to solve the issue: use collect(collection)
first, because whatever was being passed could not be filtered... gotta learn how to check types more easily :/
Lorenzo Gaifas has marked this topic as resolved.
Last updated: Nov 22 2024 at 04:41 UTC