ABMEv_plot.jl 4.7 KB
Newer Older
1
using RecipesBase
2
3
using Colors
import KernelDensity:kde,pdf
4
@recipe function plot(world::Array{U},p;what=["x","H"],trait = 1) where U <: Union{Missing,Agent}
5
    tot_dim = size(world,2)*size(world,1)
6
    # We reduce time interval if it is too big
7
    if tot_dim > 1e6 && size(world,2) >= 200
8
9
10
11
12
13
14
15
16
17
18
        p = copy(p)
        idx_reduced = floor.(Int,range(1,size(world,2),length = 200))
        p["tspan" ] = p["tspan"][idx_reduced]
        world = world[:,idx_reduced]
    end
    if count(ismissing,world) > 0
        tspan_ar = vcat([p["tspan"][i]*ones(Int(p["NMax"] - count(ismissing,world[:,i]))) for i in 1:length(p["tspan"]) ]...);
    else
        tspan_ar = repeat(p["tspan"],inner = size(world,1))
    end
    # tspan = Float64.(tspan)
19
    world_sm = collect(skipmissing(world))
20
    if "x" in what
21
22
23
24
25
        d_i = []
        for i in 1:size(world,2)
            x = get_x.(skipmissing(world[:,i]),trait)
            append!(d_i,pdf(kde(x),x))
        end
26
        @series begin
27
        xarray = get_xarray(world_sm,trait)
28
            seriestype := :scatter
29
30
            markercolor := eth_grad_small[d_i ./ maximum(d_i)]
            # markercolor := :blue
31
            markerstrokewidth := 0
32
            alpha :=1.
33
34
35
            xlabel := "time"
            ylabel := "trait value"
            label := ""
36
37
            grid := false
            # markersize := 2.3/1000*size(world_sm,1)
38
            tspan_ar[:],xarray[:]
39
40
        end
    end
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    # we use this for discrete agents
    # world should be a one dimensional vector, corresponding to one time step only
    if "xs" in what
        d_i = []
        world_df_g = groupby(world2df(world_sm),:x1)
        for world_df in world_df_g
            x = world_df.x2
            append!(d_i,pdf(kde(x),x))
        end
        # TODO: we stopped here
        @series begin
        xarray = get_xarray(world_sm,trait)
            seriestype := :scatter
            markercolor := eth_grad_small[d_i ./ maximum(d_i)]
            # markercolor := :blue
            markerstrokewidth := 0
            alpha :=1.
            xlabel := "time"
            ylabel := "trait value"
            label := ""
            grid := false
            # markersize := 2.3/1000*size(world_sm,1)
            tspan_ar[:],xarray[:]
        end
    end
66
67
    if "geo" in what
        @series begin
68
        xarray = get_geo.(world_sm)
69
70
71
72
73
74
75
            seriestype := :scatter
            markercolor := "blue"
            markerstrokewidth := 0
            alpha :=.1
            xlabel := "time"
            ylabel := "trait value"
            label := ""
76
            # markersize := 2.3/1000*size(world_sm,1)
77
            tspan_ar[:],xarray[:]
78
79
80
81
        end
    end
    if "3dgeo" in what
        @series begin
82
83
        xarray = get_geo.(world_sm)
        yarray = get_xarray(world_sm,2)
84
85
86
87
88
89
90
91
            seriestype := :scatter3d
            markercolor := "blue"
            markerstrokewidth := 0
            alpha :=.1
            xlabel := "time"
            ylabel := "geotrait"
            zlabel := "trait value"
            label := ""
92
            # markersize := 2.3/1000*size(world_sm,1)
93
            tspan_ar,xarray[:],yarray[:]
94
95
96
97
        end
    end
    if "3d" in what
        @series begin
98
99
        xarray = get_xarray(world_sm,1)
        yarray = get_xarray(world_sm,2)
100
101
102
103
104
105
106
107
            seriestype := :scatter3d
            markercolor := "blue"
            markerstrokewidth := 0
            alpha :=.1
            xlabel := "time"
            ylabel := "position"
            zlabel := "trait value"
            label := ""
108
            # markersize := 2.3/1000*size(world_sm,1)
109
            tspan_ar,xarray[:],yarray[:]
110
111
112
113
        end
    end
    # if "H" in what
    #     @series begin
114
    #         x = get_x.(world_sm,trait)
115
116
117
118
119
120
121
122
123
124
125
126
127
    #         linewidth := 2
    #         seriestype := :line
    #         label := "Interconnectedness"
    #         tspan,N^2 / 2 .* [H_discrete(x[:,i]) for i in tspan]
    #     end
    # end
    if "var" in what
        @series begin
            linewidth := 2
            seriestype := :line
            label := "Variance"
            xlabel := "Time"
            ylabel := "Variance"
128
            p["tspan"],var(world_sm,trait=trait)[:]
129
130
131
132
133
134
135
136
137
        end
    end
    if "vargeo" in what
        @series begin
            linewidth := 2
            seriestype := :line
            label := "Variance of geotrait"
            xlabel := "Time"
            ylabel := "Variance"
138
            p["tspan"],i->first(covgeo(world_sm[:,Int(i)]))
139
140
        end
    end
141
142
143
144
145
146
147
    # if "density_t" in what
    #     @series begin
    #         linewidth := 2
    #         seriestype := :plot3d
    #         label := "Variance of geotrait"
    #         xlabel := "Time"
    #         ylabel := "Variance"
148
    #         p["tspan"],i->first(covgeo(world_sm[:,Int(i)]))
149
150
    #     end
    # end
151
end