ABMEv_plot.jl 5.17 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
Victor's avatar
Victor committed
10
11
- `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
12
13
14
15

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

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