Commit 6bec3154 authored by Victor's avatar Victor
Browse files

added function clean_world, and beta and alpha diversity ( to be continued)....

added function clean_world, and beta and alpha diversity ( to be continued). Modified Plots.plot, mainly for integrating the geotrait into option "x" and "xs"
parent b50bdbcd
...@@ -18,7 +18,7 @@ module ABMEv ...@@ -18,7 +18,7 @@ module ABMEv
export MixedAgent,StdAgent,Agent,get_fitness,get_x,get_dim,get_nancestors,get_xarray,get_xhist, export MixedAgent,StdAgent,Agent,get_fitness,get_x,get_dim,get_nancestors,get_xarray,get_xhist,
get_geo,get_b,get_d,increment_x!,get_inc_reflected,world2df, get_geo,get_b,get_d,increment_x!,get_inc_reflected,world2df,
split_move,split_merge_move,tin,new_world_G split_move,split_merge_move,tin,new_world_G
export copy,runWorld_store_WF,runWorld_store_G #,runWorld_G!,runWorld_WF!, export copy,runWorld_store_WF,runWorld_store_G,clean_world #,runWorld_G!,runWorld_WF!,
export H_discrete,findclusters,var,covgeo,hamming export H_discrete,findclusters,var,covgeo,hamming
export update_afterbirth_std!,update_afterdeath_std! export update_afterbirth_std!,update_afterdeath_std!
export generalised_gaussian,gaussian,ma,eth_grad_std export generalised_gaussian,gaussian,ma,eth_grad_std
......
...@@ -36,11 +36,11 @@ function new_world_G(nagents::Int,p::Dict; spread = 1., offset = 0.) ...@@ -36,11 +36,11 @@ function new_world_G(nagents::Int,p::Dict; spread = 1., offset = 0.)
end end
# returns trait i of the agent # returns trait i of the agent
get_x(a::Agent,i::Number) = a.x_history[Int(i),end]
get_x(a::Agent) = a.x_history[:,end]
get_xhist(a::Agent,i::Number) = a.x_history[Int(i),:] get_xhist(a::Agent,i::Number) = a.x_history[Int(i),:]
get_xhist(a::Agent) = a.x_history get_xhist(a::Agent) = a.x_history
get_x(a::Agent) = a.x_history[:,end]
get_geo(a::Agent) = sum(get_xhist(a,1)) get_geo(a::Agent) = sum(get_xhist(a,1))
get_x(a::Agent,i::Number) = i > 0 ? a.x_history[Int(i),end] : get_geo(a)
get_d(a::Agent) = a.d get_d(a::Agent) = a.d
get_b(a::Agent) = a.b get_b(a::Agent) = a.b
get_fitness(a::Agent) = a.b - a.d get_fitness(a::Agent) = a.b - a.d
...@@ -48,12 +48,13 @@ get_dim(a::Agent) = size(a.x_history,1) ...@@ -48,12 +48,13 @@ get_dim(a::Agent) = size(a.x_history,1)
get_nancestors(a::Agent) = size(a.x_history,2) get_nancestors(a::Agent) = size(a.x_history,2)
""" """
get_xarray(world::Array{Agent},trait::Int) get_xarray(world::Array{Agent},trait::Int)
If trait = 0 , we return the geotrait.
Mainly works for WF-type world Mainly works for WF-type world
Returns trait of every agents of world in the form of an array which dimensions corresponds to the input. Returns trait of every agents of world in the form of an array which dimensions corresponds to the input.
Particularly suited for an array world corresponding to a timeseries. Particularly suited for an array world corresponding to a timeseries.
""" """
get_xarray(world::Array{T},trait::Int) where {T <: Agent}= reshape(hcat(get_x.(world,trait)),size(world,1),size(world,2)) get_xarray(world::Array{T},trait::Int) where {T <: Agent} = trait > 0 ? reshape(hcat(get_x.(world,trait)),size(world,1),size(world,2)) : reshape(hcat(get_geo.(world)),size(world,1),size(world,2))
""" """
get_xarray(world::Array{Agent,1}) get_xarray(world::Array{Agent,1})
......
...@@ -70,3 +70,10 @@ function updateWorld_G!(world,p,update_rates!,t) ...@@ -70,3 +70,10 @@ function updateWorld_G!(world,p,update_rates!,t)
return -1. return -1.
end end
end end
"""
clean_world(world::Array{T}) where {T <: Agent}
Get rid of missing value in `world`
"""
#TODO : put some type specs here
clean_world(world) = collect(skipmissing(world))
...@@ -42,6 +42,7 @@ function findclusters(v::Vector,allextrema =true) ...@@ -42,6 +42,7 @@ function findclusters(v::Vector,allextrema =true)
end end
import Statistics.var import Statistics.var
# TODO: rename this to gamma diversity
""" """
var(world::Array{Agent};trait=:) var(world::Array{Agent};trait=:)
If trait = 0, returns the variance of the geotrait, If trait = 0, returns the variance of the geotrait,
...@@ -89,3 +90,24 @@ function hamming(world::Array{Agent,1}) where T <: Int ...@@ -89,3 +90,24 @@ function hamming(world::Array{Agent,1}) where T <: Int
end end
return H return H
end end
"""
get_alpha_div(world::Array{U,1};trait=1) where U <: Union{Missing,Agent{T}} where T
# Arguments
"""
function get_alpha_div(world::Array{U,1};trait=1) where U <: Union{Missing,Agent{T}} where T
_xall_df = world2df(world,geotrait=true)
xall_per_patch = groupby(_xall_df, :x1,sort=true)
#TODO: to be continued
end
"""
get_beta_div(world::Array{U,1};trait=1) where U <: Union{Missing,Agent{T}} where T
# Arguments
"""
function get_beta_div(world::Array{U,1};trait=1) where U <: Union{Missing,Agent{T}} where T
_xall_df = world2df(world,geotrait=true)
xall_per_patch = groupby(_xall_df, :x1,sort=true)
sbar_i = [mean(xp.x2) for xp in xall_per_patch]
#TODO: to be continued
end
...@@ -6,9 +6,13 @@ import KernelDensity:kde,pdf ...@@ -6,9 +6,13 @@ import KernelDensity:kde,pdf
# ARGS # ARGS
- `what = ["x","H"]`: the plots you want to obtain - `what = ["x","H"]`: the plots you want to obtain
- `trait = 1`: the trait that will plotted regarding what you asked - `trait = 1`: the trait that will plotted regarding what you asked. `trait = 0` will plot the geotrait
- `tplot = false` used when calling xs, as it plots a snapshot of the world at a particular time - `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 It should correspond to an integer, as it indexes the column to plot
# Options available
- `"x"`
- `"xs"`
""" """
@recipe function plot(world::Array{U},p;what=["x","H"],trait = 1,tplot = 0) where U <: Union{Missing,Agent} @recipe function plot(world::Array{U},p;what=["x","H"],trait = 1,tplot = 0) where U <: Union{Missing,Agent}
...@@ -52,9 +56,15 @@ It should correspond to an integer, as it indexes the column to plot ...@@ -52,9 +56,15 @@ It should correspond to an integer, as it indexes the column to plot
# world should be a one dimensional vector, corresponding to one time step only # world should be a one dimensional vector, corresponding to one time step only
if "xs" in what if "xs" in what
d_i = []; xt_array = []; x1_array = [] d_i = []; xt_array = []; x1_array = []
world_df_g = groupby(world2df(collect(skipmissing(world[:, tplot > 0 ? tplot : size(world,2) ]))),:x1) world_df_g = groupby(world2df(collect(skipmissing(world[:, tplot > 0 ? tplot : size(world,2) ])),geotrait=true),:x1)
for world_df in world_df_g for world_df in world_df_g
x = world_df.x2 ; x1 = world_df.x1 if trait == 0
x = 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!(d_i,pdf(kde(x),x))
append!(xt_array,x) append!(xt_array,x)
append!(x1_array,x1) append!(x1_array,x1)
...@@ -74,20 +84,6 @@ It should correspond to an integer, as it indexes the column to plot ...@@ -74,20 +84,6 @@ It should correspond to an integer, as it indexes the column to plot
x1_array[:],xt_array[:] x1_array[:],xt_array[:]
end end
end end
if "geo" in what
@series begin
xarray = get_geo.(world_sm)
seriestype := :scatter
markercolor := "blue"
markerstrokewidth := 0
seriesalpha :=.1
xlabel := "time"
ylabel := "trait value"
label := ""
# markersize := 2.3/1000*size(world_sm,1)
tspan_ar[:],xarray[:]
end
end
if "3dgeo" in what if "3dgeo" in what
@series begin @series begin
xarray = get_geo.(world_sm) xarray = get_geo.(world_sm)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment