ABMEv_utils.jl 3.4 KB
Newer Older
1
"""
2
        generalised_gaussian(x::Number,mu::Number,sigma::Number,epsilon::Number)
3
"""
4
function generalised_gaussian(x::Number,mu::Number,sigma::Number,epsilon::Number)
Victor Boussange's avatar
Victor Boussange committed
5
6
7
8
        return exp( -.5 * ((x-mu) / sigma)^epsilon)
end

"""
9
        gaussian(x::Number,mu::Number,sigma::Number) = generalised_gaussian(x,mu,sigma,2)
Victor Boussange's avatar
Victor Boussange committed
10
"""
11
gaussian(x::Number,mu::Number,sigma::Number) = generalised_gaussian(x,mu,sigma,2.)
Victor's avatar
Victor committed
12

Victor's avatar
Victor committed
13
import DSP.conv
Victor's avatar
Victor committed
14
15
16
17
18
19
20
21
22
"""
    ma(x::Array{T},f) where T <: Number
Moving average over array x, using f as the filter, i.e. the number of points to average on. Better choosing an odd number
"""
function ma(x::Array{T},f) where T <: Number
    _N = length(x)
    _s = Int((f-1)/2)
    return conv(x,ones(f)./f)[_s:_s+_N-1]
end
23

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
    function geomsmooth(x,smooth)
Geometric smoothing, cf `https://en.wikipedia.org/wiki/Exponential_smoothing`

"""
function geomsmooth(x,smooth)
    return [prod(x[i-smooth + 1:i])^(1/smooth) for i in smooth:length(x)]
end

"""
    function arithsmooth(x,smooth)
arithmetic smoothing

"""
function arithsmooth(x,smooth)
    return [sum(x[i-smooth+1:i])/smooth for i in smooth:length(x)]
end

42
# This is all about interpolations
43
import Interpolations:interpolate,Gridded,Linear
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
91
92
93
94
95
96

import DataFrames.groupby
"""
    function groupby(f, list::Array)
returns a dictionary that group `list` elements by value of function `f`
"""
97
groupby(f, list::Array{T}) where {T}= begin
98
99
  groups = Dict()
  for v in list
100
    push!(get!(groups, f(v), T[]), v)
101
102
103
  end
  groups
end