metrics.jl 4.18 KB
 Victor committed Oct 01, 2020 1 2 3 4 ``````using LightGraphs using Test using Revise,ABMEv using UnPack `````` Victor committed Oct 01, 2020 5 ``````myspace1 = (RealSpace{1,Float64}(),) `````` Victor committed Oct 01, 2020 6 ``````myspace2 = (RealSpace{1,Float64}(),RealSpace{1,Float64}()) `````` Victor committed Oct 01, 2020 7 ``````myspace3 = (DiscreteSegment(Int16(1),Int16(10)),RealSpace{1,Float64}()) `````` Victor committed Oct 11, 2020 8 ``````g = SimpleGraph(1000,4000) `````` Victor committed Oct 11, 2020 9 ``````myspace4 = (RealSpace{1,Float64}(),GraphSpace(g),) `````` Victor committed Oct 11, 2020 10 `````` `````` 11 ``````K0 = 1000; σ = 1e-1 `````` Victor committed Oct 01, 2020 12 13 14 ``````a1 = [Agent(myspace1, (σ,) .* randn() .- .5,ancestors=true) for i in 1:K0] a2 = [Agent(myspace2,tuple((σ, σ) .* randn(2) .- .5...),ancestors=true) for i in 1:K0] a3 = [Agent(myspace3, (rand(Int16.(1:10)), 1e-1* randn() + 5.5 ),ancestors=true) for i in 1:K0] `````` Victor committed Oct 11, 2020 15 ``````a4 = [Agent(myspace4, (1.,rand(Int64(1):Int64(1000)),),ancestors=true) for i in 1:K0] `````` Victor committed Oct 11, 2020 16 `````` `````` Victor committed Oct 01, 2020 17 18 19 20 21 22 23 24 ``````D = (1.,); mu = [1.,1.] NMax = 1000 p1 = Dict{String,Any}();@pack! p1 = D,mu,NMax D = (1.,1.); p2 = Dict{String,Any}();@pack! p2 = D,mu,NMax D = (Int16(0.),0.) p3 = Dict{String,Any}();@pack! p3 = D,mu,NMax `````` Victor committed Oct 11, 2020 25 ``````D = (0.,0.) `````` Victor committed Oct 11, 2020 26 ``````p4 = Dict{String,Any}();@pack! p4 = D,mu,NMax `````` Victor committed Oct 01, 2020 27 28 29 30 `````` w1 = World(a1,myspace1,p1) w2 = World(a2,myspace2,p2) w3 = World(a3,myspace3,p3) `````` Victor committed Oct 11, 2020 31 ``````w4 = World(a4,myspace4,p4) `````` Victor committed Oct 01, 2020 32 `````` `````` 33 34 ``````## testing variance @testset "Testing metrics" begin `````` Victor committed May 11, 2020 35 `````` @testset "var" begin `````` Victor committed Oct 01, 2020 36 37 `````` @test first(var(w1)) ≈ (σ).^2 atol=0.001 @test first(var(w2,trait=2)) ≈ (σ).^2 atol=0.001 `````` Victor committed Oct 11, 2020 38 `````` @test first(var(w4,trait=1)) < Inf `````` Victor committed Oct 11, 2020 39 40 `````` @test first(var(w4,trait=2)) < Inf `````` Victor committed Oct 11, 2020 41 `````` `````` Victor committed May 11, 2020 42 `````` end `````` 43 44 45 `````` ## testing covgeo @testset "covgeo" begin `````` Victor committed Oct 02, 2020 46 `````` # @test covgeo(w1) ≈ (σ).^2 atol=0.001 `````` Victor committed Oct 01, 2020 47 `````` for i in covgeo(w1,1) `````` Victor committed Oct 02, 2020 48 `````` # @test i ≈ (σ).^2 atol=0.001 `````` 49 50 51 `````` end end # not sure this is the bestway of testing `````` Victor Boussange committed Apr 01, 2020 52 `````` # there is a problem here `````` 53 `````` @testset "covgeo2d" begin `````` Victor committed Oct 01, 2020 54 `````` cmat = covgeo(w2,2); `````` 55 `````` smat = [σ^2 0; 0 σ^2] `````` Victor committed Oct 02, 2020 56 `````` # @test cmat ≈ smat atol=0.01 `````` 57 `````` end `````` Victor committed May 11, 2020 58 `````` @testset "Alpha diversity" begin `````` Victor committed Oct 01, 2020 59 `````` α = get_alpha_div(w3,2); `````` Victor committed May 11, 2020 60 `````` @test abs(α) < Inf `````` Victor committed Oct 11, 2020 61 62 `````` α = get_alpha_div(w4,1); @test abs(α) < Inf `````` Victor committed May 11, 2020 63 64 `````` end @testset "Beta diversity" begin `````` Victor committed Oct 01, 2020 65 `````` β = get_beta_div(w3,2); `````` Victor committed May 11, 2020 66 `````` @test abs(β) < Inf `````` Victor committed Oct 11, 2020 67 68 `````` β = get_beta_div(w4,1); @test abs(β) < Inf `````` Victor committed May 11, 2020 69 `````` end `````` Victor committed Sep 20, 2020 70 `````` @testset "Isolation by history - hamming distance" begin `````` Victor committed Oct 01, 2020 71 72 `````` a1 = Agent((DiscreteSegment(1,10),),[(1,),(2,),(3,)],[0,1,4],ancestors=true) a2 = Agent((DiscreteSegment(1,10),),[(1,),(10,),(3,),(10,)],[0,3,4,5],ancestors=true) `````` Victor committed Sep 26, 2020 73 `````` @test get_dist_hist(a1,a2,(x,y)->y!=x,1) ≈ 3.0 `````` Victor committed Sep 11, 2020 74 `````` end `````` Victor committed May 11, 2020 75 76 77 ``````end @testset "Geotrait computation" begin `````` Victor committed Oct 01, 2020 78 `````` a = Agent(myspace3, (Int16.(1), randn() ),ancestors=true); increment_x!(a,myspace3,p3,2.0); `````` Victor committed May 11, 2020 79 `````` @test get_geo(a,2.0) ≈ 2.0 `````` 80 81 82 ``````end # TODO needs to test hamming distance `````` Victor committed Oct 07, 2020 83 `````` `````` Victor committed Oct 07, 2020 84 ``````## testing for real space of dimension d>1 `````` Victor committed Oct 07, 2020 85 86 ``````multispace = (DiscreteSegment{Int8}(1,9),RealSpace{3,Float64}(),) K0 = 10000; `````` Victor committed Oct 07, 2020 87 ``````multia = [Agent(multispace, (rand(Int8(1):Int8(10)),tuple( randn(3) ...),),ancestors=true) for i in 1:K0] `````` Victor committed Oct 07, 2020 88 89 90 91 92 93 94 95 96 ``````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) `````` Victor committed Oct 07, 2020 97 ``````@test prod([isapprox(i,0.,atol=1e-1) for i in mean(multiw,trait = 2)]) `````` Victor committed Oct 07, 2020 98 99 ``````@test isapprox(get_alpha_div(multiw,2),1.,atol = 1e-2) @test isapprox(get_beta_div(multiw,2),0.,atol = 1e-2) `````` Victor committed Oct 07, 2020 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 `````` ## some test to write metrics functions # world = multiw;trait=2 # g = groupby(a->a[1],agents(world)) # v = [var(World(subw,space(world),parameters(world)),trait=trait) for subw in values(g)] # h = vcat(v...) # mean(mean(h,dims=1)) # mean(h) # mean([mean(var(Float64.(get_x(World(subw,space(world),parameters(world)),trait)),corrected=false)) for subw in values(g)]) # # [var(Float64.(get_x(World(subw,space(world),parameters(world)),trait)),corrected=false) for subw in values(g)] # subw = collect(values(g))[1] # var(Float64.(get_x(World(subw,space(world),parameters(world)),trait)),corrected=false) # var(World(subw,space(world),parameters(world)),trait=trait) # mean(World(subw,space(world),parameters(world)),trait=1) # # m = [mean(World(subw,space(world),parameters(world)),trait=trait) for subw in values(g)] # h=vcat(m...) # var(h,dims=1,corrected=false)``````