Commit 240ef944 authored by Victor's avatar Victor
Browse files

Added interpolations function in utils.jl. Minor change on world2df, with...

Added interpolations function in utils.jl. Minor change on world2df, with argument `geotrait=false added`. Minor change on plot
parent 93e4a0a8
......@@ -116,7 +116,7 @@ end
# end
function world2df(world::Array{T,1}) where {T <: Agent}
function world2df(world::Array{T,1},geotrait=false) where {T <: Agent}
xx = get_xarray(world)
dfw = DataFrame(:f => get_fitness.(world))
for i in 1:size(xx,1)
......
......@@ -41,6 +41,9 @@ It should correspond to an integer, as it indexes the column to plot
append!(d_i,pdf(kde(x),x))
end
@series begin
if length(world_sm) !== length(tspan_ar[:])
throw(DimensionMismatch("You want to plot a world with missing data"))
end
xarray = get_x.(world_sm,tspan_ar[:],trait)
seriestype := :scatter
markercolor := eth_grad_small[d_i ./ maximum(d_i)]
......
......@@ -44,3 +44,53 @@ import Plots:cgrad
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.])
# This is all about interpolations
using Interpolations
struct DiversityFunction
x
y
itp
end
"""
function geomsmooth2D(xa,ya,itp,smooth)
Return xas,yas,A where A is an interpolated matrix with geometric smooth,
whose axis are xas, yas
# ARGS
`xa` xaxis values, `ya` yaxis values, `itp` interpolation function, `smooth` smoothing function
"""
function geomsmooth2D(xa,ya,itp,smooth)
prod(isodd.(smooth)) ? nothing : throw(ArgumentError("smoothing coefficients need to be odd"))
idx1 = Int((smooth[1]-1)/2)+1:length(xa)- Int((smooth[1]-1)/2)
idx2 = Int((smooth[2]-1)/2)+1:length(ya)- Int((smooth[2]-1)/2)
A = [prod(itp.(xa[i-smooth[1] + 1:i]', ya[j-smooth[2] + 1:j])).^(1/prod(smooth)) for i in smooth[1]:length(xa), j in smooth[2]:length(ya)]
xas = [xa[i] for i in idx1, j in idx2]; yas = [ya[j] for i in idx1,j in idx2]
return xas,yas,A
end
"""
function arithsmooth2D(xa,ya,itp,smooth)
Return xas,yas,A where A is an interpolated matrix with arithmetic smooth,
whose axis are xas, yas
# ARGS
`xa` xaxis values, `ya` yaxis values, `itp` interpolation function, `smooth` smoothing function
"""
function arithsmooth2D(xa,ya,itp,smooth)
prod(isodd.(smooth)) ? nothing : throw(ArgumentError("smoothing coefficients need to be odd"))
idx1 = Int((smooth[1]-1)/2)+1:length(xa)- Int((smooth[1]-1)/2)
idx2 = Int((smooth[2]-1)/2)+1:length(ya)- Int((smooth[2]-1)/2)
A = [sum(itp.(xa[i-smooth[1] + 1:i]', ya[j-smooth[2] + 1:j]))./prod(smooth) for i in smooth[1]:length(xa), j in smooth[2]:length(ya)]
xas = [xa[i] for i in idx1,j in idx2]; yas = [ya[j] for i in idx1,j in idx2]
return xas,yas,A
end
"""
function interpolate_df(df,xlab,ylab,zlab)
returns an interpolated function itp(x,y) -> z, as well as its axis `xa` and `ya`
"""
function interpolate_df(df,xlab,ylab,zlab)
sort!(df,[ylab,xlab])
xa = unique(df[xlab]); ya = unique(df[ylab])
A = reshape(df[zlab],length(xa),length(ya))
return DiversityFunction(xa,ya,interpolate((xa,ya),A,Gridded(Linear())))
end
......@@ -4,6 +4,8 @@ using ABMEv, Test, JLD2,Random
include("gillepsie.jl")
# include("wrightfisher.jl")
include("metrics.jl")
include("utilstest.jl")
# we might want to put this in a separate file at some point
@testset "Reflection" begin
@test get_inc_reflected(0.,2.0) .0
......
Markdown is supported
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