ABMEv_plot.jl 7.72 KB
Newer Older
1
using RecipesBase
2
3
using Colors
import KernelDensity:kde,pdf
Victor's avatar
Victor committed
4
5
6
7
8
"""
    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
9
- `trait = 1`: the trait that will plotted regarding what you asked. `trait = 0` will plot the geotrait
10
- `tplot = 0` used when calling xs, as it plots a snapshot of the world at a particular time
Victor's avatar
Victor committed
11
It should correspond to an integer, as it indexes the column to plot
12
13
14
15

# Options available
- `"x"`
- `"xs"`
Victor's avatar
Victor committed
16
17
"""

18
19
@recipe function plot(sim::Simulation;what=["x","H"],trait = 1,tplot = 0)
    world = sim.agentarray
20
    tot_dim = size(world,2)*size(world,1)
21
22
    tspan = sim.tspan
    p = sim.p
23
    # We reduce time interval if it is too big
Victor's avatar
Victor committed
24
25
26
    # if tot_dim > 1e6 && size(world,2) >= 200
    #     p = copy(p)
    #     idx_reduced = floor.(Int,range(1,size(world,2),length = 200))
27
    #     p["tspan" ] = tspan[idx_reduced]
Victor's avatar
Victor committed
28
29
    #     world = world[:,idx_reduced]
    # end
30
31
    # second condition is to make sure that the world corresponds to all the time steps
    # If not, then we can not get "x"
32
33
    if count(ismissing,world) > 0 && length(tspan) == size(world,2)
        tspan_ar = vcat([tspan[i]*ones(Int(p["NMax"] - count(ismissing,world[:,i]))) for i in 1:length(tspan) ]...);
34
    else
35
        tspan_ar = repeat(tspan,inner = size(world,1))
36
37
    end
    # tspan = Float64.(tspan)
38
    tend = tspan[tplot > 0 ? tplot : length(tspan)]
39
    world_sm = clean_world(world)
40
    if "x" in what
41
        d_i = []
42
43
        for i in 1:length(tspan)
            x = get_x.(clean_world(world[:,i]),tspan[i],trait)[:]
44
45
            append!(d_i,pdf(kde(x),x))
        end
46
        @series begin
47
48
49
            if length(world_sm) !== length(tspan_ar[:])
                throw(DimensionMismatch("You want to plot a world with missing data"))
            end
50
        xarray = get_x.(world_sm,tspan_ar[:],trait)
51
            seriestype := :scatter
52
            markercolor := eth_grad_small[d_i ./ maximum(d_i)]
53
            markerstrokewidth := 0
Victor's avatar
Victor committed
54
            seriesalpha :=1.
Victor's avatar
Victor committed
55
56
            # xlabel := "time"
            # ylabel := "trait value"
57
            label := ""
58
59
            grid := false
            # markersize := 2.3/1000*size(world_sm,1)
60
            tspan_ar[:],xarray[:]
61
62
        end
    end
63
64
    # we use this for discrete agents
    # world should be a one dimensional vector, corresponding to one time step only
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    # if "xs" in what
    #     d_i = []; xt_array = []; x1_array = []
    #     world_df_all = world2df(clean_world(world[:, tplot > 0 ? tplot : size(world,2) ]),tend,true)
    #     world_df_g = groupby(world_df_all,:x1)
    #     for world_df in world_df_g
    #         if trait == 0
    #             x = Float64.(world_df.g)
    #         else
    #             # fitness occupies first spot
    #             x = world_df[:,trait+1] ;
    #         end
    #         x1 =  world_df.x1;
    #         append!(d_i,pdf(kde(x),x))
    #         append!(xt_array,x)
    #         append!(x1_array,x1)
    #     end
    #     @series begin
    #         seriestype := :scatter
    #         markercolor := eth_grad_small[d_i ./ maximum(d_i)]
    #         # markercolor := :blue
    #         markerstrokewidth := 0
    #         # seriesalpha := 1.
    #         xaxis := "geographical position"
    #         xticks :=  sort!(unique(world_df_all.x1))
    #         yaxis := "trait value"
    #         label := ""
    #         grid := false
    #         # marker := (:rect,20,1.)
    #         x1_array[:],xt_array[:]
    #     end
    # end
96
    if "gs" in what
97
        _world = clean_world(world[:, tplot > 0 ? tplot : length(tspan) ])
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
        y = get_x(_world,tend,2)[:]
        x = get_x(_world,tend,0)[:]
        X = hcat(x,y)
        d = kde(X)
        # by density
        d_i = diag(pdf(d,X[:,1],X[:,2]))
        # by value
        # d_i = y
        d_i = (d_i .- minimum(d_i)) ./ (maximum(d_i) .- minimum(d_i))
        # TODO: we stopped here
        @series begin
            seriestype := :scatter
            markercolor := eth_grad_small[d_i]
            # markercolor := :blue
            markerstrokewidth := 0
            # seriesalpha := 1.
            xaxis := "geotrait"
            yaxis := "trait value"
            label := ""
            grid := false
            # marker := (:rect,20,1.)
            x,y
        end
    end
122
    if "3dgeo" in what
123
124
125
        d_i = []
        for i in 1:size(world,2)
            _world = clean_world(world[:,i])
126
127
            x = get_x(_world,tspan[i],2)[:]
            y = get_x(_world,tspan[i],0)[:]
128
129
130
131
132
133
134
135
            X = hcat(x,y)
            # d = kde(X)
            # di_temp = diag(pdf(d,X[:,1],X[:,2]))
            di_temp = y
            di_temp = (di_temp .- minimum(di_temp)) ./ (maximum(di_temp) .- minimum(di_temp))
            # here we normalise with respect to maximum value at each time step
            append!(d_i,di_temp)
        end
136
        @series begin
137
138
        xarray = get_geo.(world_sm,tspan_ar)
        yarray = get_x(world_sm,2)
139
            seriestype := :scatter3d
140
            markercolor := eth_grad_std[d_i ./ 1.]
141
            markerstrokewidth := 0
Victor's avatar
Victor committed
142
            seriesalpha :=.1
143
144
145
146
            xlabel := "time"
            ylabel := "geotrait"
            zlabel := "trait value"
            label := ""
147
            # markersize := 2.3/1000*size(world_sm,1)
148
            tspan_ar,xarray[:],yarray[:]
149
150
151
        end
    end
    if "3d" in what
152
153
        d_i = []
        for i in 1:size(world,2)
154
155
            x = get_x(clean_world(world[:,i]),tspan[i],1)[:]
            y = get_x(clean_world(world[:,i]),tspan[i],2)[:]
156
157
158
159
160
161
            X = hcat(x,y)
            d = kde(X)
            di_temp = diag(pdf(d,X[:,1],X[:,2]))
            di_temp = (di_temp .- minimum(di_temp)) ./ (maximum(di_temp) .- minimum(di_temp))
            append!(d_i,di_temp)
        end
162
        @series begin
163
        xarray = get_x(world_sm,1)
164
        yarray = get_x(world_sm,2)
165
            seriestype := :scatter3d
166
            markercolor := eth_grad_small[d_i ./ maximum(d_i)]
167
            markerstrokewidth := 0
Victor's avatar
Victor committed
168
            seriesalpha :=.1
169
170
171
172
            xlabel := "time"
            ylabel := "position"
            zlabel := "trait value"
            label := ""
173
            # markersize := 2.3/1000*size(world_sm,1)
174
            tspan_ar,xarray[:],yarray[:]
175
176
177
178
        end
    end
    # if "H" in what
    #     @series begin
179
    #         x = get_x.(world_sm,trait)
180
181
182
183
184
185
186
187
188
189
190
191
192
    #         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"
193
            tspan,var(world_sm,trait=trait)[:]
194
195
196
197
198
199
200
201
202
        end
    end
    if "vargeo" in what
        @series begin
            linewidth := 2
            seriestype := :line
            label := "Variance of geotrait"
            xlabel := "Time"
            ylabel := "Variance"
203
            tspan,i->first(covgeo(world_sm[:,Int(i)]))
204
205
        end
    end
206
207
208
209
210
211
212
    # if "density_t" in what
    #     @series begin
    #         linewidth := 2
    #         seriestype := :plot3d
    #         label := "Variance of geotrait"
    #         xlabel := "Time"
    #         ylabel := "Variance"
213
    #         tspan,i->first(covgeo(world_sm[:,Int(i)]))
214
215
    #     end
    # end
216
end
Victor's avatar
Victor committed
217
218
219
220
221
222
223


import Plots:cgrad
# asymmetry towards red, blue is only a fifth of the color range
const eth_grad_small = cgrad([colorant"#1F407A", RGB(0.671,0.851,0.914),RGB(1.0,1.0,0.749), RGB(0.992,0.682,0.38),RGB(0.647,0.0,0.149),],[.0,.1])
# symmetry between red and blue
const eth_grad_std = cgrad([colorant"#1F407A", RGB(0.671,0.851,0.914),RGB(1.0,1.0,0.749), RGB(0.992,0.682,0.38),RGB(0.647,0.0,0.149),],[.0,1.])