To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

ABMEv_plot.jl 6.59 KB
Newer Older
1
using RecipesBase
2
3
using Colors
import KernelDensity:kde,pdf
Victor's avatar
Victor committed
4
"""
Victor's avatar
Victor committed
5
6
    function plot(sim::Simulation;trait = 1)
Plot recipe for ABMEv.jl
Victor's avatar
Victor committed
7
# ARGS
Victor's avatar
Victor committed
8
9
10
- if `length(trait) == 1` then we scatter plot `trait` along time
- if `2 <= length(trait) <= 3` then we project world of the
last time step in the two  (three) dimensional trait space define by `trait`
11

Victor's avatar
Victor committed
12
13
14
!!! warning "To be implemented"
    We might want to get a 3 dimensional scatter plot
    with time, trait1 and trait2 as axis
Victor's avatar
Victor committed
15
16
"""

Victor's avatar
Victor committed
17
18
19
20
21
22
@recipe function plot(sim::Simulation;trait = 1,time = nothing)
    # world = sim.agentarray
    # tspan = sim.tspan
    # p = sim.p
    if length(trait) == 1
        xarray,tspan = get_xnt(sim, trait=trait)
23
        d_i = []
Victor's avatar
Victor committed
24
25
        for x in xarray
            push!(d_i,pdf(kde(x),x))
26
        end
Victor's avatar
Victor committed
27
28
        # Here we normalize with respect to the max size of the whole world through time
        d_i = [ (_d_i .- minimum(_d_i)) ./ (maximum(_d_i) .- minimum(_d_i)) for _d_i in d_i ]
29
30
        @series begin
            seriestype := :scatter
Victor's avatar
Victor committed
31
            markercolor := eth_grad_std[vcat(d_i...)]
32
            markerstrokewidth := 0
Victor's avatar
Victor committed
33
            seriesalpha := .2
Victor's avatar
Victor committed
34
35
            # xlabel := "time"
            # ylabel := "trait value"
36
            label := ""
Victor's avatar
Victor committed
37
            xlabel := "time"
38
39
            grid := false
            # markersize := 2.3/1000*size(world_sm,1)
Victor's avatar
Victor committed
40
            vcat(tspan...),vcat(xarray...)
41
42
        end
    end
Victor's avatar
Victor committed
43
    if length(trait) == 2
Victor's avatar
Victor committed
44
45
46
        isnothing(time) ? w = get_world(sim[end]) :  w = get_world(sim,time)
        x = get_x(w,trait[1]);
        y = get_x(w,trait[2])
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
        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.
            label := ""
            grid := false
            # marker := (:rect,20,1.)
            x,y
        end
    end
Victor's avatar
Victor committed
67
    if length(trait) == 3
Victor's avatar
Victor committed
68
        throw(ArgumentError("Plot for three traits not yet implemented"))
69
70
    end
end
Victor's avatar
Victor committed
71
72
73
74
75
76
77


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.])
Victor's avatar
Victor committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211


########## OLD PLOT RECIPES ##############
# To get some inspiration

# 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 = []; 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
##

# if "3dgeo" in what
#     d_i = []
#     for i in 1:size(world,2)
#         _world = clean_world(world[:,i])
#         x = get_x(_world,tspan[i],2)[:]
#         y = get_x(_world,tspan[i],0)[:]
#         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
#     @series begin
#     xarray = get_geo.(world_sm,tspan_ar)
#     yarray = get_x(world_sm,2)
#         seriestype := :scatter3d
#         markercolor := eth_grad_std[d_i ./ 1.]
#         markerstrokewidth := 0
#         seriesalpha :=.1
#         xlabel := "time"
#         ylabel := "geotrait"
#         zlabel := "trait value"
#         label := ""
#         # markersize := 2.3/1000*size(world_sm,1)
#         tspan_ar,xarray[:],yarray[:]
#     end
# end
# if "3d" in what
#     d_i = []
#     for i in 1:size(world,2)
#         x = get_x(clean_world(world[:,i]),tspan[i],1)[:]
#         y = get_x(clean_world(world[:,i]),tspan[i],2)[:]
#         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
#     @series begin
#     xarray = get_x(world_sm,1)
#     yarray = get_x(world_sm,2)
#         seriestype := :scatter3d
#         markercolor := eth_grad_small[d_i ./ maximum(d_i)]
#         markerstrokewidth := 0
#         seriesalpha :=.1
#         xlabel := "time"
#         ylabel := "position"
#         zlabel := "trait value"
#         label := ""
#         # markersize := 2.3/1000*size(world_sm,1)
#         tspan_ar,xarray[:],yarray[:]
#     end
# end
# if "H" in what
#     @series begin
#         x = get_x.(world_sm,trait)
#         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"
#         tspan,var(world_sm,trait=trait)[:]
#     end
# end
# if "vargeo" in what
#     @series begin
#         linewidth := 2
#         seriestype := :line
#         label := "Variance of geotrait"
#         xlabel := "Time"
#         ylabel := "Variance"
#         tspan,i->first(covgeo(world_sm[:,Int(i)]))
#     end
# end
# if "density_t" in what
#     @series begin
#         linewidth := 2
#         seriestype := :plot3d
#         label := "Variance of geotrait"
#         xlabel := "Time"
#         ylabel := "Variance"
#         tspan,i->first(covgeo(world_sm[:,Int(i)]))
#     end
# end