ABMEv_plot.jl 5.23 KB
Newer Older
1
using RecipesBase
2
3
using Colors
import KernelDensity:kde,pdf
Victor's avatar
Victor committed
4
5
6
7
8
9
10
11
12
13
14
"""
    function plot(world::Array{U},p;what=["x","H"],trait = 1,tplot = false) where U <: Union{Missing,Agent}

# ARGS
- `what = ["x","H"]`: the plots you want to obtain
- `trait = 1`: the trait that will plotted regarding what you asked
- `tplot = false` used when calling xs, as it plots a snapshot of the world at a particular time
It should correspond to an integer, as it indexes the column to plot
"""

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