Skip to content

Commit

Permalink
Revert whitespace_in_typedefs change
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobusmmsmit committed Mar 9, 2023
1 parent 594d09d commit a4b302c
Show file tree
Hide file tree
Showing 25 changed files with 318 additions and 318 deletions.
2 changes: 1 addition & 1 deletion .JuliaFormatter.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ annotate_untyped_fields_with_any = false
join_lines_based_on_source = true
margin = 10_000
whitespace_in_kwargs = true
whitespace_typedefs = true
whitespace_typedefs = false
whitespace_ops_in_indices = false
import_to_using = false
pipe_to_function_call = false
Expand Down
14 changes: 7 additions & 7 deletions src/core/model_abstract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ Supertype of all concrete space implementations for Agents.jl.
"""
abstract type AbstractSpace end
abstract type DiscreteSpace <: AbstractSpace end
SpaceType = Union{Nothing, AbstractSpace}
SpaceType = Union{Nothing,AbstractSpace}

# This is a collection of valid position types, sometimes used for ambiguity resolution
ValidPos = Union{
Int, # graph
NTuple{N, Int}, # grid
NTuple{M, <:AbstractFloat}, # continuous
Tuple{Int, Int, Float64}, # osm
} where {N, M}
NTuple{N,Int}, # grid
NTuple{M,<:AbstractFloat}, # continuous
Tuple{Int,Int,Float64}, # osm
} where {N,M}

"""
AgentBasedModel
Expand Down Expand Up @@ -80,7 +80,7 @@ Here we the most important information on how to query an instance of `AgentBase
Many more functions exist in the API page, such as [`allagents`](@ref).
"""
abstract type AgentBasedModel{S <: SpaceType, A <: AbstractAgent} end
abstract type AgentBasedModel{S<:SpaceType,A<:AbstractAgent} end
const ABM = AgentBasedModel

function notimplemented(model)
Expand Down Expand Up @@ -243,7 +243,7 @@ end
# %% Non-public methods. Must be implemented but are not exported
###########################################################################################
agent_container(model::ABM) = getfield(model, :agents)
agenttype(::ABM{S, A}) where {S, A} = A
agenttype(::ABM{S,A}) where {S,A} = A
spacetype(::ABM{S}) where {S} = S

"""
Expand Down
52 changes: 26 additions & 26 deletions src/core/model_concrete.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export ABM, StandardABM, UnkillableABM, FixedMassABM
using StaticArrays: SizedVector

ContainerType{A} = Union{AbstractDict{Int, A}, AbstractVector{A}}
ContainerType{A} = Union{AbstractDict{Int,A},AbstractVector{A}}

# And the three implementations here are just variants with different `C` type.
struct SingleContainerABM{S <: SpaceType, A <: AbstractAgent, C <: ContainerType{A}, F, P, R <: AbstractRNG} <: AgentBasedModel{S, A}
struct SingleContainerABM{S<:SpaceType,A<:AbstractAgent,C<:ContainerType{A},F,P,R<:AbstractRNG} <: AgentBasedModel{S,A}
agents::C
space::S
scheduler::F
Expand All @@ -14,11 +14,11 @@ struct SingleContainerABM{S <: SpaceType, A <: AbstractAgent, C <: ContainerType
end

const SCABM = SingleContainerABM
const StandardABM{A, S} = SingleContainerABM{A, S, Dict{Int, A}}
const UnkillableABM{A, S} = SingleContainerABM{A, S, Vector{A}}
const FixedMassABM{A, S} = SingleContainerABM{A, S, SizedVector{A}}
const StandardABM{A,S} = SingleContainerABM{A,S,Dict{Int,A}}
const UnkillableABM{A,S} = SingleContainerABM{A,S,Vector{A}}
const FixedMassABM{A,S} = SingleContainerABM{A,S,SizedVector{A}}

containertype(::SingleContainerABM{S, A, C}) where {S, A, C} = C
containertype(::SingleContainerABM{S,A,C}) where {S,A,C} = C

"""
union_types(U::Type)
Expand All @@ -45,18 +45,18 @@ function SingleContainerABM(
properties::P = nothing,
rng::R = Random.default_rng(),
warn = true,
) where {A <: AbstractAgent, S <: SpaceType, F, P, R <: AbstractRNG}
) where {A<:AbstractAgent,S<:SpaceType,F,P,R<:AbstractRNG}
agent_validator(A, space, warn)
C = construct_agent_container(container, A)
agents = C()
return SingleContainerABM{S, A, C, F, P, R}(agents, space, scheduler, properties, rng, Ref(0))
return SingleContainerABM{S,A,C,F,P,R}(agents, space, scheduler, properties, rng, Ref(0))
end

function SingleContainerABM(agent::AbstractAgent, args...; kwargs...)
return SingleContainerABM(typeof(agent), args...; kwargs...)
end

construct_agent_container(::Type{<:Dict}, A) = Dict{Int, A}
construct_agent_container(::Type{<:Dict}, A) = Dict{Int,A}
construct_agent_container(::Type{<:Vector}, A) = Vector{A}
construct_agent_container(container, A) = throw(
"Unrecognised container $container, please specify either Dict or Vector.",
Expand Down Expand Up @@ -106,25 +106,25 @@ function FixedMassABM(
properties::P = nothing,
rng::R = Random.default_rng(),
warn = true,
) where {A <: AbstractAgent, S <: SpaceType, F, P, R <: AbstractRNG}
C = SizedVector{length(agents), A}
) where {A<:AbstractAgent,S<:SpaceType,F,P,R<:AbstractRNG}
C = SizedVector{length(agents),A}
fixed_agents = C(agents)
# Validate that agent ID is the same as its order in the vector.
for (i, a) in enumerate(agents)
i a.id && throw(ArgumentError("$(i)-th agent had ID $(a.id) instead of $i."))
end
agent_validator(A, space, warn)
return SingleContainerABM{S, A, C, F, P, R}(fixed_agents, space, scheduler, properties, rng, Ref(0))
return SingleContainerABM{S,A,C,F,P,R}(fixed_agents, space, scheduler, properties, rng, Ref(0))
end

#######################################################################################
# %% Model accessing api
#######################################################################################
nextid(model::SingleContainerABM{<:SpaceType, A, Dict{Int, A}}) where {A} = getfield(model, :maxid)[] + 1
nextid(model::SingleContainerABM{<:SpaceType, A, Vector{A}}) where {A} = nagents(model) + 1
nextid(::SingleContainerABM{<:SpaceType, A, <:SizedVector}) where {A} = error("There is no `nextid` in a `FixedMassABM`. Most likely an internal error.")
nextid(model::SingleContainerABM{<:SpaceType,A,Dict{Int,A}}) where {A} = getfield(model, :maxid)[] + 1
nextid(model::SingleContainerABM{<:SpaceType,A,Vector{A}}) where {A} = nagents(model) + 1
nextid(::SingleContainerABM{<:SpaceType,A,<:SizedVector}) where {A} = error("There is no `nextid` in a `FixedMassABM`. Most likely an internal error.")

function add_agent_to_model!(agent, model::SingleContainerABM{<:SpaceType, A, Dict{Int, A}}) where {A <: AbstractAgent}
function add_agent_to_model!(agent, model::SingleContainerABM{<:SpaceType,A,Dict{Int,A}}) where {A<:AbstractAgent}
if haskey(agent_container(model), agent.id)
error("Can't add agent to model. There is already an agent with id=$(agent.id)")
else
Expand All @@ -139,16 +139,16 @@ function add_agent_to_model!(agent, model::SingleContainerABM{<:SpaceType, A, Di
return
end

function add_agent_to_model!(agent, model::SingleContainerABM{<:SpaceType, A, Vector{A}}) where {A}
function add_agent_to_model!(agent, model::SingleContainerABM{<:SpaceType,A,Vector{A}}) where {A}
agent.id == nagents(model) + 1 || error("Cannot add agent of ID $(agent.id) in a vector ABM of $(nagents(model)) agents. Expected ID == $(nagents(model)+1).")
push!(agent_container(model), agent)
return
end

function remove_agent_from_model!(agent::A, model::SingleContainerABM{<:SpaceType, A, <:AbstractDict{Int, A}}) where {A <: AbstractAgent}
function remove_agent_from_model!(agent::A, model::SingleContainerABM{<:SpaceType,A,<:AbstractDict{Int,A}}) where {A<:AbstractAgent}
return delete!(agent_container(model), agent.id)
end
function remove_agent_from_model!(::A, model::SingleContainerABM{<:SpaceType, A, <:AbstractVector}) where {A <: AbstractAgent}
function remove_agent_from_model!(::A, model::SingleContainerABM{<:SpaceType,A,<:AbstractVector}) where {A<:AbstractAgent}
return error(
"Cannot remove agents stored in $(containertype(model)). " *
"Use the vanilla `SingleContainerABM` to be able to remove agents.",
Expand All @@ -167,7 +167,7 @@ function agent_validator(
::Type{A},
space::S,
warn::Bool,
) where {A <: AbstractAgent, S <: SpaceType}
) where {A<:AbstractAgent,S<:SpaceType}
# Check A for required properties & fields
if isconcretetype(A)
do_checks(A, space, warn)
Expand All @@ -190,7 +190,7 @@ end
do_checks(agent, space)
Helper function for `agent_validator`.
"""
function do_checks(::Type{A}, space::S, warn::Bool) where {A <: AbstractAgent, S <: SpaceType}
function do_checks(::Type{A}, space::S, warn::Bool) where {A<:AbstractAgent,S<:SpaceType}
if warn
isbitstype(A) &&
@warn "Agent type is not mutable, and most library functions assume that it is."
Expand All @@ -207,15 +207,15 @@ function do_checks(::Type{A}, space::S, warn::Bool) where {A <: AbstractAgent, S
space_type = typeof(space)
if space_type <: GraphSpace && !(pos_type <: Integer)
throw(ArgumentError("`pos` field in agent type must be of type `Int` when using GraphSpace."))
elseif space_type <: GridSpace && !(pos_type <: NTuple{D, Integer} where {D})
elseif space_type <: GridSpace && !(pos_type <: NTuple{D,Integer} where {D})
throw(ArgumentError("`pos` field in agent type must be of type `NTuple{Int}` when using GridSpace."))
elseif space_type <: ContinuousSpace || space_type <: ContinuousSpace
if !(pos_type <: NTuple{D, <:AbstractFloat} where {D})
if !(pos_type <: NTuple{D,<:AbstractFloat} where {D})
throw(ArgumentError("`pos` field in agent type must be of type `NTuple{<:AbstractFloat}` when using ContinuousSpace."))
end
if warn &&
any(isequal(:vel), fieldnames(A)) &&
!(fieldtype(A, :vel) <: NTuple{D, <:AbstractFloat} where {D})
!(fieldtype(A, :vel) <: NTuple{D,<:AbstractFloat} where {D})
@warn "`vel` field in agent type should be of type `NTuple{<:AbstractFloat}` when using ContinuousSpace."
end
end
Expand All @@ -230,7 +230,7 @@ modelname(::Dict) = "StandardABM"
modelname(::Vector) = "UnkillableABM"
modelname(::SizedVector) = "FixedMassABM"

function Base.show(io::IO, abm::SingleContainerABM{S, A}) where {S, A}
function Base.show(io::IO, abm::SingleContainerABM{S,A}) where {S,A}
n = isconcretetype(A) ? nameof(A) : string(A)
s = "$(modelname(abm)) with $(nagents(abm)) agents of type $(n)"
if abm.space === nothing
Expand All @@ -250,5 +250,5 @@ function Base.show(io::IO, abm::SingleContainerABM{S, A}) where {S, A}
end
end

schedulername(x::Union{Function, DataType}) = nameof(x)
schedulername(x::Union{Function,DataType}) = nameof(x)
schedulername(x) = Symbol(typeof(x))
14 changes: 7 additions & 7 deletions src/core/space_interaction_API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Move agent to the given position, or to a random one if a position is not given.
The agent's position is updated to match `pos` after the move.
"""
function move_agent!(agent::A, pos::ValidPos, model::ABM{<:AbstractSpace, A}) where {A <: AbstractAgent}
function move_agent!(agent::A, pos::ValidPos, model::ABM{<:AbstractSpace,A}) where {A<:AbstractAgent}
remove_agent_from_space!(agent, model)
agent.pos = pos
add_agent_to_space!(agent, model)
Expand Down Expand Up @@ -252,7 +252,7 @@ add_agent!(5, model, 0.5, true) # add at position 5, w becomes 0.5
add_agent!(model; w = 0.5) # use keywords: w becomes 0.5, k becomes false
```
"""
function add_agent!(model::ABM{S, A}, properties...; kwargs...) where {S, A <: AbstractAgent}
function add_agent!(model::ABM{S,A}, properties...; kwargs...) where {S,A<:AbstractAgent}
return add_agent!(A, model, properties...; kwargs...)
end

Expand All @@ -262,10 +262,10 @@ end

function add_agent!(
pos::ValidPos,
model::ABM{S, A},
model::ABM{S,A},
properties...;
kwargs...,
) where {S, A <: AbstractAgent}
) where {S,A<:AbstractAgent}
return add_agent!(pos, A, model, properties...; kwargs...)
end

Expand All @@ -291,7 +291,7 @@ end
Same as `nearby_ids(agent.pos, model, r)` but the iterable *excludes* the given
`agent`'s id.
"""
function nearby_ids(agent::A, model::ABM, r = 1; kwargs...) where {A <: AbstractAgent}
function nearby_ids(agent::A, model::ABM, r = 1; kwargs...) where {A<:AbstractAgent}
all = nearby_ids(agent.pos, model, r; kwargs...)
return Iterators.filter(i -> i agent.id, all)
end
Expand All @@ -303,10 +303,10 @@ Same as `nearby_positions(agent.pos, model, r)`.
"""
function nearby_positions(
agent::A,
model::ABM{S, A},
model::ABM{S,A},
r = 1;
kwargs...,
) where {S, A <: AbstractAgent}
) where {S,A<:AbstractAgent}
return nearby_positions(agent.pos, model, r; kwargs...)
end

Expand Down
20 changes: 10 additions & 10 deletions src/simulations/collect.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ is not known.
collect_agent_data!(df, model, properties::Nothing, step::Int = 0; kwargs...) = df

function init_agent_dataframe(
model::ABM{S, A},
model::ABM{S,A},
properties::AbstractArray,
) where {S, A <: AbstractAgent}
) where {S,A<:AbstractAgent}
nagents(model) < 1 &&
throw(ArgumentError("Model must have at least one agent to initialize data collection"))

Expand Down Expand Up @@ -308,16 +308,16 @@ function _add_col_data!(
property,
agent_iter;
obtainer = identity,
) where {T >: Missing}
) where {T>:Missing}
return dd[!, dataname(property)] =
collect(get_data_missing(a, property, obtainer) for a in agent_iter)
end

# Aggregating version
function init_agent_dataframe(
model::ABM{S, A},
model::ABM{S,A},
properties::Vector{<:Tuple},
) where {S, A <: AbstractAgent}
) where {S,A<:AbstractAgent}
nagents(model) < 1 && throw(ArgumentError(
"Model must have at least one agent to " * "initialize data collection",
))
Expand Down Expand Up @@ -413,7 +413,7 @@ Return the name of the column of the `i`-th collected data where `k = adata[i]`
"""
dataname(x::Tuple) =
join(vcat([dataname(x[2]), dataname(x[1])], [dataname(s) for s in x[3:end]]), "_")
dataname(x::Union{Symbol, String}) = string(x)
dataname(x::Union{Symbol,String}) = string(x)
# This takes care to include fieldnames and values in the column name to make column names unique
# if the same function is used with different values of outer scope variables.
dataname(x::Function) = join(
Expand Down Expand Up @@ -442,10 +442,10 @@ end
# Normal aggregates
function _add_col_data!(
col::AbstractVector{T},
property::Tuple{K, A},
property::Tuple{K,A},
agent_iter;
obtainer = identity,
) where {T, K, A}
) where {T,K,A}
k, agg = property
res::T = agg(get_data(a, k, obtainer) for a in agent_iter)
return push!(col, res)
Expand All @@ -454,10 +454,10 @@ end
# Conditional aggregates
function _add_col_data!(
col::AbstractVector{T},
property::Tuple{K, A, C},
property::Tuple{K,A,C},
agent_iter;
obtainer = identity,
) where {T, K, A, C}
) where {T,K,A,C}
k, agg, condition = property
res::T = agg(get_data(a, k, obtainer) for a in Iterators.filter(condition, agent_iter))
return push!(col, res)
Expand Down
2 changes: 1 addition & 1 deletion src/simulations/ensemblerun.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export ensemblerun!
Vector_or_Tuple = Union{AbstractArray, Tuple}
Vector_or_Tuple = Union{AbstractArray,Tuple}

"""
ensemblerun!(models::Vector, agent_step!, model_step!, n; kwargs...)
Expand Down
Loading

0 comments on commit a4b302c

Please sign in to comment.