metrics.jl 4.41 KB
Newer Older
Victor's avatar
Victor committed
1
2
3
4
using LightGraphs
using Test
using Revise,ABMEv
using UnPack
5
myspace1 = (RealSpace{1,Float64}(),)
Victor's avatar
Victor committed
6
myspace2 = (RealSpace{1,Float64}(),RealSpace{1,Float64}())
7
myspace3 = (DiscreteSegment(Int16(1),Int16(10)),RealSpace{2,Float64}())
8
g = SimpleGraph(1000,4000)
Victor's avatar
Victor committed
9
myspace4 = (RealSpace{1,Float64}(),GraphSpace(g),)
10

11
K0 = 1000; σ = 1e-1
12
13
14
15
a1 = [Agent(myspace1, σ  .* randn(1) .- .5,ancestors=true) for i in 1:K0]
a2 = [Agent(myspace2,σ  .* randn(2) .- .5,ancestors=true) for i in 1:K0]
a3 = [Agent(myspace3, [rand(Int16.(1:10)), 1e-1.* randn(2) .+ 5.5 ],ancestors=true) for i in 1:K0]
a4 = [Agent(myspace4, [1.,rand(Int64(1):Int64(1000))],ancestors=true) for i in 1:K0]
16

Victor's avatar
Victor committed
17
18
19
20
21
22
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
23
D = (Int16(0.),(0.,0.))
Victor's avatar
Victor committed
24
p3 = Dict{String,Any}();@pack! p3 = D,mu,NMax
Victor's avatar
Victor committed
25
D = (0.,0.)
26
p4 = Dict{String,Any}();@pack! p4 = D,mu,NMax
Victor's avatar
Victor committed
27
28
29
30

w1 = World(a1,myspace1,p1)
w2 = World(a2,myspace2,p2)
w3 = World(a3,myspace3,p3)
31
w4 = World(a4,myspace4,p4)
Victor's avatar
Victor committed
32

33
34
## testing variance
@testset "Testing metrics" begin
35
    @testset "var" begin
36
37
        @test first(var(w1))  (σ).^2 atol=0.001
        @test first(var(w2,trait=2))  (σ).^2 atol=0.001
38
        @test first(var(w4,trait=1)) < Inf
39
        # @test first(var(w4,trait=2)) < Inf
40
    end
41
42
43

    ## testing covgeo
    @testset "covgeo" begin
44
        # @test covgeo(w1) ≈ (σ).^2 atol=0.001
45
         for i in covgeo(w1,1)
46
             # @test i ≈ (σ).^2 atol=0.001
47
48
49
         end
     end
     # not sure this is the bestway of testing
50
     # there is a problem here
51
     @testset "covgeo2d" begin
52
         cmat = covgeo(w2,2);
53
         smat = [σ^2 0; 0 σ^2]
54
         # @test cmat ≈ smat atol=0.01
55
      end
56
      @testset "Alpha diversity" begin
Victor's avatar
Victor committed
57
          α = get_alpha_div(w3,2);
58
          @test abs(α) < Inf
59
60
          α = get_alpha_div(w4,1);
          @test abs(α) < Inf
61
62
          α = get_alpha_div(w3,2,false)
          @test length(α) == 10
63
       end
64
65
66
67
68
       @testset "Abundance" begin
           @test isapprox(get_local_abundance(w3), K0/10,atol = 10)
           N = get_local_abundance(w3,false)
           @test length(N) == 10
        end
69
       @testset "Beta diversity" begin
Victor's avatar
Victor committed
70
           β = get_beta_div(w3,2);
71
           @test abs(β) < Inf
72
73
           β = get_beta_div(w4,1);
           @test abs(β) < Inf
74
       end
75
       @testset "Isolation by history - hamming distance" begin
Victor's avatar
Victor committed
76
77
           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's avatar
Victor committed
78
           @test get_dist_hist(a1,a2,(x,y)->y!=x,1)  3.0
79
       end
80
81
82
end

@testset "Geotrait computation" begin
83
    a = Agent(myspace3, [Int16.(1), randn(2) ],ancestors=true); increment_x!(a,myspace3,p3,2.0);
84
    @test get_geo(a,2.0)  2.0
85
86
87
end

# TODO needs to test hamming distance
88

Victor's avatar
Victor committed
89
## testing for real space of dimension d>1
90
91
multispace = (DiscreteSegment{Int8}(1,9),RealSpace{3,Float64}(),)
K0 = 10000;
92
93
multia = [Agent(multispace, [rand(Int8(1):Int8(10)),randn(3) ],ancestors=true) for i in 1:K0]
D = [Int8(1),fill(1.,3)]
94
95
96
97
98
99
100
101
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's avatar
Victor committed
102
@test prod([isapprox(i,0.,atol=1e-1) for i in mean(multiw,trait = 2)])
103
104
@test isapprox(get_alpha_div(multiw,2),1.,atol = 1e-2)
@test isapprox(get_beta_div(multiw,2),0.,atol = 1e-2)
Victor's avatar
Victor committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

## 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)