ABMEv_plot.jl 5.28 KB
 Victor Boussange committed Mar 30, 2020 1 ``````using RecipesBase `````` 2 3 ``````using Colors import KernelDensity:kde,pdf `````` Victor committed May 06, 2020 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) `````` Victor Boussange committed Jan 20, 2020 16 `````` # We reduce time interval if it is too big `````` Victor committed May 06, 2020 17 18 19 20 21 22 `````` # 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 `````` Victor Boussange committed Jan 20, 2020 23 24 25 26 27 28 `````` 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)) `````` Victor Boussange committed Jan 17, 2020 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 `````` Victor Boussange committed Jan 17, 2020 36 `````` @series begin `````` 37 `````` xarray = get_xarray(world_sm,trait) `````` Victor Boussange committed Jan 17, 2020 38 `````` seriestype := :scatter `````` 39 40 `````` markercolor := eth_grad_small[d_i ./ maximum(d_i)] # markercolor := :blue `````` Victor Boussange committed Jan 17, 2020 41 `````` markerstrokewidth := 0 `````` Victor committed May 06, 2020 42 `````` seriesalpha :=1. `````` Victor Boussange committed Jan 17, 2020 43 44 45 `````` xlabel := "time" ylabel := "trait value" label := "" `````` 46 47 `````` grid := false # markersize := 2.3/1000*size(world_sm,1) `````` Victor Boussange committed Jan 20, 2020 48 `````` tspan_ar[:],xarray[:] `````` Victor Boussange committed Jan 17, 2020 49 50 `````` end end `````` Victor committed May 06, 2020 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 committed May 06, 2020 54 55 `````` d_i = []; xt_array = []; x1_array = [] world_df_g = groupby(world2df(collect(skipmissing(world[:, tplot > 0 ? tplot : size(world,2) ]))),:x1) `````` Victor committed May 06, 2020 56 `````` for world_df in world_df_g `````` Victor committed May 06, 2020 57 `````` x = world_df.x2 ; x1 = world_df.x1 `````` Victor committed May 06, 2020 58 `````` append!(d_i,pdf(kde(x),x)) `````` Victor committed May 06, 2020 59 60 `````` append!(xt_array,x) append!(x1_array,x1) `````` Victor committed May 06, 2020 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 committed May 06, 2020 68 `````` seriesalpha := 1. `````` Victor committed May 06, 2020 69 70 `````` xaxis := "geographical position" yaxis := "trait value" `````` Victor committed May 06, 2020 71 72 `````` label := "" grid := false `````` Victor committed May 06, 2020 73 `````` # markersize := 10 `````` Victor committed May 06, 2020 74 `````` x1_array[:],xt_array[:] `````` Victor committed May 06, 2020 75 76 `````` end end `````` Victor Boussange committed Jan 17, 2020 77 78 `````` if "geo" in what @series begin `````` 79 `````` xarray = get_geo.(world_sm) `````` Victor Boussange committed Jan 17, 2020 80 81 82 `````` seriestype := :scatter markercolor := "blue" markerstrokewidth := 0 `````` Victor committed May 06, 2020 83 `````` seriesalpha :=.1 `````` Victor Boussange committed Jan 17, 2020 84 85 86 `````` xlabel := "time" ylabel := "trait value" label := "" `````` 87 `````` # markersize := 2.3/1000*size(world_sm,1) `````` Victor Boussange committed Jan 20, 2020 88 `````` tspan_ar[:],xarray[:] `````` Victor Boussange committed Jan 17, 2020 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) `````` Victor Boussange committed Jan 17, 2020 95 96 97 `````` seriestype := :scatter3d markercolor := "blue" markerstrokewidth := 0 `````` Victor committed May 06, 2020 98 `````` seriesalpha :=.1 `````` Victor Boussange committed Jan 17, 2020 99 100 101 102 `````` xlabel := "time" ylabel := "geotrait" zlabel := "trait value" label := "" `````` 103 `````` # markersize := 2.3/1000*size(world_sm,1) `````` Victor Boussange committed Jan 20, 2020 104 `````` tspan_ar,xarray[:],yarray[:] `````` Victor Boussange committed Jan 17, 2020 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) `````` Victor Boussange committed Jan 17, 2020 111 112 113 `````` seriestype := :scatter3d markercolor := "blue" markerstrokewidth := 0 `````` Victor committed May 06, 2020 114 `````` seriesalpha :=.1 `````` Victor Boussange committed Jan 17, 2020 115 116 117 118 `````` xlabel := "time" ylabel := "position" zlabel := "trait value" label := "" `````` 119 `````` # markersize := 2.3/1000*size(world_sm,1) `````` Victor Boussange committed Jan 20, 2020 120 `````` tspan_ar,xarray[:],yarray[:] `````` Victor Boussange committed Jan 17, 2020 121 122 123 124 `````` end end # if "H" in what # @series begin `````` 125 `````` # x = get_x.(world_sm,trait) `````` Victor Boussange committed Jan 17, 2020 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)[:] `````` Victor Boussange committed Jan 17, 2020 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)])) `````` Victor Boussange committed Jan 17, 2020 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 `````` Victor Boussange committed Jan 17, 2020 162 ``end``