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
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,
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 update_afterbirth_std!,update_afterdeath_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.)
end
# 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) = a.x_history
get_x(a::Agent) = a.x_history[:,end]
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_b(a::Agent) = a.b
get_fitness(a::Agent) = a.b - a.d
......@@ -48,12 +48,13 @@ get_dim(a::Agent) = size(a.x_history,1)
get_nancestors(a::Agent) = size(a.x_history,2)
"""
get_xarray(world::Array{Agent},trait::Int)
If trait = 0 , we return the geotrait.
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.
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})
......
......@@ -70,3 +70,10 @@ function updateWorld_G!(world,p,update_rates!,t)
return -1.
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)
end
import Statistics.var
# TODO: rename this to gamma diversity
"""
var(world::Array{Agent};trait=:)
If trait = 0, returns the variance of the geotrait,
......@@ -89,3 +90,24 @@ function hamming(world::Array{Agent,1}) where T <: Int
end
return H
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
# ARGS
- `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
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}
......@@ -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
if "xs" in what
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
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!(xt_array,x)
append!(x1_array,x1)
......@@ -74,20 +84,6 @@ It should correspond to an integer, as it indexes the column to plot
x1_array[:],xt_array[:]
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
@series begin
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