Commit 3fc15f06 by Victor

### Changing metrics to adapt multidimensional subspace

parent f169e794
 ... @@ -52,7 +52,7 @@ If trait > 0, returns the covariance matrix, with first row geotrait and second ... @@ -52,7 +52,7 @@ If trait > 0, returns the covariance matrix, with first row geotrait and second """ """ function var(world::World;trait=1) function var(world::World;trait=1) xarray = get_x(world,trait) xarray = Float64.(get_x(world,trait)) return var(xarray,dims=1,corrected=false) return var(xarray,dims=1,corrected=false) end end """ """ ... @@ -94,12 +94,12 @@ Mean of the local variance of `trait` per patch. If trait=0, we get the geotrait ... @@ -94,12 +94,12 @@ Mean of the local variance of `trait` per patch. If trait=0, we get the geotrait # Arguments # Arguments """ """ function get_alpha_div(world::World,trait=1) function get_alpha_div(world::World,trait=1) xarray = get_xarray(world,true) g = groupby(a->a[1],agents(world)) g = groupby(x->x[1],collect(eachrow(xarray))) if trait == 0 if trait == 0 return mean([var(Float64.([x[end] for x in xp]),corrected=false) for xp in values(g)]) return mean([var(Float64.(get_geo(World(subw,space(world),parameters(world)))),corrected=false) for subw in values(g)]) else else return mean([var(Float64.([x[trait] for x in xp]),corrected=false) for xp in values(g)]) # here the second mean is here when subspace is multidimensional return mean([mean(var(Float64.(get_x(World(subw,space(world),parameters(world)),trait)),corrected=false)) for subw in values(g)]) end end end end ... @@ -109,13 +109,12 @@ Variance of the mean of `trait` per patch ... @@ -109,13 +109,12 @@ Variance of the mean of `trait` per patch # Arguments # Arguments """ """ function get_beta_div(world::World,trait=1) function get_beta_div(world::World,trait=1) xarray = get_xarray(world,true) g = groupby(a->a[1],agents(world)) g = groupby(x->x[1],collect(eachrow(xarray))) if trait == 0 if trait == 0 # need to convert to Float64, otherwise infinite variance # need to convert to Float64, otherwise infinite variance sbar_i = [mean(Float64.([x[end] for x in xp])) for xp in values(g)] sbar_i = [mean(Float64.(get_geo(World(subw,space(world),parameters(world))))) for subw in values(g)] else else sbar_i = [mean(Float64.([x[trait] for x in xp])) for xp in values(g)] sbar_i = [mean(Float64.(get_x(World(subw,space(world),parameters(world)),trait))) for subw in values(g)] end end return var(sbar_i,corrected=false) return var(sbar_i,corrected=false) end end ... ...
 ... @@ -94,10 +94,10 @@ import DataFrames.groupby ... @@ -94,10 +94,10 @@ import DataFrames.groupby function groupby(f, list::Array) function groupby(f, list::Array) returns a dictionary that group `list` elements by value of function `f` returns a dictionary that group `list` elements by value of function `f` """ """ groupby(f, list::Array) = begin groupby(f, list::Array{T}) where {T}= begin groups = Dict() groups = Dict() for v in list for v in list push!(get!(groups, f(v), []), v) push!(get!(groups, f(v), T[]), v) end end groups groups end end
 ... @@ -58,13 +58,21 @@ If `trait = 0` , we return the geotrait. ... @@ -58,13 +58,21 @@ If `trait = 0` , we return the geotrait. # Warning # Warning This works only for linear subspace, indexed with a single value This works only for linear subspace, indexed with a single value """ """ get_x(w::World,trait) = hcat(collect.(getindex.(agents(w),trait))...)' function get_x(w::World,trait) if ndims(space(w)[trait]) > 1 hcat(collect.(getindex.(agents(w),trait))...)' else collect(getindex.(agents(w),trait)) end end """ """ \$(SIGNATURES) \$(SIGNATURES) Returns every traits of every agents of `world` in the form **of a one dimensional array** (in contrast to `get_x`). Returns every traits of every agents of `world` in the form **of a one dimensional array** (in contrast to `get_x`). If `geotrait=true` the geotrait is also added to the set of trait, in the last column. If `geotrait=true` the geotrait is also added to the set of trait, in the last column. If you do not want to specify `t` (only useful for geotrait), it is also possible to use `get_xarray(world::Array{T,1}) where {T <: Agent}`. If you do not want to specify `t` (only useful for geotrait), it is also possible to use `get_xarray(world::Array{T,1}) where {T <: Agent}`. # Warning It does not work with subspace where ndims(subspace) > 1. """ """ function get_xarray(world::World,geotrait::Bool=false) function get_xarray(world::World,geotrait::Bool=false) xarray = get_x(world,Colon()) xarray = get_x(world,Colon()) ... ...
 ... @@ -64,3 +64,19 @@ end ... @@ -64,3 +64,19 @@ end end end # TODO needs to test hamming distance # TODO needs to test hamming distance # testing for real space of dimension d>1 multispace = (DiscreteSegment{Int8}(1,9),RealSpace{3,Float64}(),) K0 = 10000; multia = [Agent(multispace, (rand(Int8(1):Int8(10)),tuple(randn(3)...),),ancestors=true) for i in 1:K0] D = (Int8(1),tuple(fill(1.,3)...),) mu = [1] NMax = 1000 multip = Dict{String,Any}();@pack! multip = D,mu,NMax multiw = World(multia,multispace,multip) a = multia[10] inc = get_inc(1,D[1],multispace[2]) @test inc[1] != inc[2] # checking that we have independent increments in each direction @test isapprox(mean(var(multiw,trait = 2)),1.,atol=1e-2) @test isapprox(get_alpha_div(multiw,2),1.,atol = 1e-2) @test isapprox(get_beta_div(multiw,2),0.,atol = 1e-2)
 ... @@ -11,42 +11,44 @@ mycontinuoussegment = ContinuousSegment(-1.,1.) ... @@ -11,42 +11,44 @@ mycontinuoussegment = ContinuousSegment(-1.,1.) myspace = (mysegment,mygraph,myline) myspace = (mysegment,mygraph,myline) myspace2 = (mysegment,mycontinuoussegment,real2d) myspace2 = (mysegment,mycontinuoussegment,real2d) # checking essential properties of spaces @testset "Space properties" begin @test isfinite(mysegment) ≈ true # checking essential properties of spaces @test isfinite(mygraph) ≈ true @test isfinite(mysegment) ≈ true @test isfinite(myline) ≈ false @test isfinite(mygraph) ≈ true @test ndims(real2d) ≈ 2 @test isfinite(myline) ≈ false @test isfinite(mycontinuoussegment) ≈ true @test ndims(real2d) ≈ 2 @test typeof(myspace) <: AbstractSpacesTuple @test isfinite(mycontinuoussegment) ≈ true @test eltype(myspace2) == Tuple{Int64,Float64,Tuple{Float64,Float64}} @test typeof(myspace) <: AbstractSpacesTuple @test eltype(myspace2) == Tuple{Int64,Float64,Tuple{Float64,Float64}} # increment on infinite spaces # increment on infinite spaces @test get_inc(0.,myline) ≈ (0.) @test ABMEv._get_inc(0.,myline) ≈ (0.) @test !(get_inc(1.,myline) ≈ 0.) @test !(ABMEv._get_inc(1.,myline) ≈ 0.) @test !(get_inc(1,1.,myline) ≈ 0.) @test !(get_inc(1,1.,myline) ≈ 0.) @test typeof(get_inc([1.,0.],real2d)) == Tuple{Float64,Float64} @test typeof(ABMEv._get_inc([1.,0.],real2d)) == Tuple{Float64,Float64} @test typeof(get_inc([1.,0.],[1.,0.],real2d)) == Tuple{Float64,Float64} @test typeof(get_inc([1.,0.],[1.,0.],real2d)) == Tuple{Float64,Float64} @test typeof(get_inc([1.],real2d)) == Tuple{Float64,Float64} @test typeof(ABMEv._get_inc([1.],real2d)) == Tuple{Float64,Float64} @test typeof(get_inc(1.,real2d)) == Tuple{Float64,Float64} @test typeof(ABMEv._get_inc(1.,real2d)) == Tuple{Float64,Float64} get_inc([1.],real2d) # ABMEv._get_inc([1.],real2d) ABMEv.initpos(myspace2) # ABMEv.initpos(myspace2) # increment on finite spaces # checking if reflection works @testset "Reflection" begin @test mysegment.s - eps() < get_inc(5.,100.,mysegment) + 5. < mysegment.e + eps() @test mycontinuoussegment.s < get_inc(0.,100.,mycontinuoussegment) < mycontinuoussegment.e mysegment2 = DiscreteSegment(-1,1) @test ABMEv._reflect1D(0.,2.0,mysegment2) ≈ .0 @test ABMEv._reflect1D(0.,-2.0,mysegment2) ≈ .0 @test ABMEv._reflect1D(0.,4.0,mysegment2) ≈ .0 @test ABMEv._reflect1D(0.,1.1,mysegment2) ≈ 1 - .1 end #checking if graph works # increment on finite spaces @test prod([get_inc(1,10,mygraph) + 1 ∈ vertices(mygraph.g) for i in 1:30]) # checking if reflection works @testset "Reflection" begin @test mysegment.s - eps() < get_inc(5.,100.,mysegment) + 5. < mysegment.e + eps() @test mycontinuoussegment.s < get_inc(0.,100.,mycontinuoussegment) < mycontinuoussegment.e mysegment2 = DiscreteSegment(-1,1) @test ABMEv._reflect1D(0.,2.0,mysegment2) ≈ .0 @test ABMEv._reflect1D(0.,-2.0,mysegment2) ≈ .0 @test ABMEv._reflect1D(0.,4.0,mysegment2) ≈ .0 @test ABMEv._reflect1D(0.,1.1,mysegment2) ≈ 1 - .1 end #checking if graph works @test prod([get_inc(1,10,mygraph) + 1 ∈ vertices(mygraph.g) for i in 1:30]) end ##### AGENTS ####### ##### AGENTS ####### a1 = Agent(myspace) a1 = Agent(myspace) ... @@ -55,17 +57,19 @@ a3 = Agent(myspace,(1,1,1.)) ... @@ -55,17 +57,19 @@ a3 = Agent(myspace,(1,1,1.)) a4 = Agent(myspace2,(1,1,(1.,1)),rates=true) a4 = Agent(myspace2,(1,1,(1.,1)),rates=true) a5 = Agent(myspace2,ancestors=true) a5 = Agent(myspace2,ancestors=true) # basic test @testset "Agent properties" begin @test typeof(a1) <: AbstractAgent # basic test @test eltype(a1) == eltype(myspace) @test typeof(a1) <: AbstractAgent @test eltype(a5) == eltype(myspace2) @test eltype(a1) == eltype(myspace) @test typeof(a1) <: AbstractAgent @test eltype(a5) == eltype(myspace2) @test typeof(a1) <: AbstractAgent # increment test # increment test p_myspace = Dict("D"=>[1,1,1],"mu" =>[1,1,1] ) p_myspace = Dict("D"=>[1,1,1],"mu" =>[1,1,1] ) p_myspace2 = Dict("D"=>[1,1,1],"mu" =>[1,1,1]) p_myspace2 = Dict("D"=>[1,1,1],"mu" =>[1,1,1]) old_a1 = copy(a1) old_a1 = copy(a1) @test !prod((get_x(old_a1) .≈ get_x(increment_x!(a1,myspace,p_myspace,0.)))) @test !prod((get_x(old_a1) .≈ get_x(increment_x!(a1,myspace,p_myspace,0.)))) @test nancestors(increment_x!(a2,myspace,p_myspace,2.)) > 1 @test nancestors(increment_x!(a2,myspace,p_myspace,2.)) > 1 @test !isnothing(increment_x!(a4,myspace2,p_myspace2,2.)) @test !isnothing(increment_x!(a4,myspace2,p_myspace2,2.)) @test !isnothing(increment_x!(a5,myspace2,p_myspace2,2.)) @test !isnothing(increment_x!(a5,myspace2,p_myspace2,2.)) end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment