Skip to content

Commit a58dda2

Browse files
authored
Merge pull request #56 from awllee/master
Fixes for Julia 0.7/1.0
2 parents a0a0859 + b9b8502 commit a58dda2

File tree

9 files changed

+41
-38
lines changed

9 files changed

+41
-38
lines changed

.travis.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ os:
33
- linux
44
- osx
55
julia:
6-
- 0.6
6+
- 0.7
7+
- 1.0
78
- nightly
9+
matrix:
10+
allow_failures:
11+
- julia: 1.0
12+
- julia: nightly
813
notifications:
914
email: false
1015
# Uncomment the following lines to override the default test script
1116
#script:
1217
# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
1318
# - julia -e 'Pkg.clone(pwd()); Pkg.build("KernelDensity"); Pkg.test("KernelDensity"; coverage=true)'
1419
after_success:
15-
- julia -e 'cd(Pkg.dir("KernelDensity")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
20+
- julia -e 'import Pkg, KernelDensity; cd(joinpath(dirname(pathof(KernelDensity)), "..")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ least-squares cross validation. It accepts the above keyword arguments, except
4141

4242
There are also some slightly more advanced interfaces:
4343
```
44-
kde(data, midpoints::Range)
44+
kde(data, midpoints::R) where R<:AbstractRange
4545
```
4646
allows specifying the internal grid to use. Optional keyword arguments are
4747
`kernel` and `bandwidth`.
@@ -53,7 +53,7 @@ allows specifying the exact distribution to use as the kernel. Optional
5353
keyword arguments are `boundary` and `npoints`.
5454

5555
```
56-
kde(data, midpoints::Range, dist::Distribution)
56+
kde(data, midpoints::R, dist::Distribution) where R<:AbstractRange
5757
```
5858
allows specifying both the distribution and grid.
5959

@@ -95,4 +95,3 @@ pdf(ik, x)
9595
```
9696

9797
`InterpKDE` will pass any extra arguments to `interpolate`.
98-

REQUIRE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
julia 0.6
2-
Compat 0.17
1+
julia 0.7
32
StatsBase
43
Distributions
54
Optim
65
Interpolations
6+
FFTW

src/KernelDensity.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
__precompile__()
2-
31
module KernelDensity
42

5-
using Compat
63
using StatsBase
74
using Distributions
85
using Optim
96
using Interpolations
107

11-
import Base: conv
128
import StatsBase: RealVector, RealMatrix
139
import Distributions: twoπ, pdf
10+
import FFTW: rfft, irfft
1411

1512
export kde, kde_lscv, UnivariateKDE, BivariateKDE, InterpKDE, pdf
1613

@@ -21,4 +18,3 @@ include("bivariate.jl")
2118
include("interp.jl")
2219

2320
end # module
24-

src/bivariate.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Store both grid and density for KDE over R2
2-
mutable struct BivariateKDE{Rx<:Range,Ry<:Range} <: AbstractKDE
2+
mutable struct BivariateKDE{Rx<:AbstractRange,Ry<:AbstractRange} <: AbstractKDE
33
x::Rx
44
y::Ry
55
density::Matrix{Float64}
@@ -26,7 +26,8 @@ function default_bandwidth(data::Tuple{RealVector,RealVector})
2626
end
2727

2828
# tabulate data for kde
29-
function tabulate(data::Tuple{RealVector, RealVector}, midpoints::Tuple{Range, Range}, weights::Weights = default_weights(data))
29+
function tabulate(data::Tuple{RealVector, RealVector}, midpoints::Tuple{Rx, Ry},
30+
weights::Weights = default_weights(data)) where {Rx<:AbstractRange,Ry<:AbstractRange}
3031
xdata, ydata = data
3132
ndata = length(xdata)
3233
length(ydata) == ndata || error("data vectors must be of same length")
@@ -87,7 +88,8 @@ const BivariateDistribution = Union{MultivariateDistribution,Tuple{UnivariateDis
8788

8889
default_weights(data::Tuple{RealVector, RealVector}) = UniformWeights(length(data[1]))
8990

90-
function kde(data::Tuple{RealVector, RealVector}, weights::Weights, midpoints::Tuple{Range, Range}, dist::BivariateDistribution)
91+
function kde(data::Tuple{RealVector, RealVector}, weights::Weights, midpoints::Tuple{Rx, Ry},
92+
dist::BivariateDistribution) where {Rx<:AbstractRange,Ry<:AbstractRange}
9193
k = tabulate(data, midpoints, weights)
9294
conv(k,dist)
9395
end
@@ -104,8 +106,9 @@ function kde(data::Tuple{RealVector, RealVector}, dist::BivariateDistribution;
104106
kde(data,weights,(xmid,ymid),dist)
105107
end
106108

107-
function kde(data::Tuple{RealVector, RealVector}, midpoints::Tuple{Range, Range};
108-
bandwidth=default_bandwidth(data), kernel=Normal, weights::Weights = default_weights(data))
109+
function kde(data::Tuple{RealVector, RealVector}, midpoints::Tuple{Rx, Ry};
110+
bandwidth=default_bandwidth(data), kernel=Normal,
111+
weights::Weights = default_weights(data)) where {Rx<:AbstractRange,Ry<:AbstractRange}
109112

110113
dist = kernel_dist(kernel,bandwidth)
111114
kde(data,weights,midpoints,dist)

src/univariate.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Store both grid and density for KDE over the real line
2-
mutable struct UnivariateKDE{R<:Range} <: AbstractKDE
2+
mutable struct UnivariateKDE{R<:AbstractRange} <: AbstractKDE
33
x::R
44
density::Vector{Float64}
55
end
@@ -66,7 +66,7 @@ function kde_range(boundary::Tuple{Real,Real}, npoints::Int)
6666
lo, hi = boundary
6767
lo < hi || error("boundary (a,b) must have a < b")
6868

69-
Compat.range(lo, stop=hi, length=npoints)
69+
range(lo, stop=hi, length=npoints)
7070
end
7171

7272
struct UniformWeights{N} end
@@ -80,7 +80,7 @@ const Weights = Union{UniformWeights, RealVector, StatsBase.Weights}
8080

8181

8282
# tabulate data for kde
83-
function tabulate(data::RealVector, midpoints::Range, weights::Weights=default_weights(data))
83+
function tabulate(data::RealVector, midpoints::R, weights::Weights=default_weights(data)) where R<:AbstractRange
8484
npoints = length(midpoints)
8585
s = step(midpoints)
8686

@@ -131,7 +131,7 @@ function conv(k::UnivariateKDE, dist::UnivariateDistribution)
131131
end
132132

133133
# main kde interface methods
134-
function kde(data::RealVector, weights::Weights, midpoints::Range, dist::UnivariateDistribution)
134+
function kde(data::RealVector, weights::Weights, midpoints::R, dist::UnivariateDistribution) where R<:AbstractRange
135135
k = tabulate(data, midpoints, weights)
136136
conv(k,dist)
137137
end
@@ -143,8 +143,8 @@ function kde(data::RealVector, dist::UnivariateDistribution;
143143
kde(data,weights,midpoints,dist)
144144
end
145145

146-
function kde(data::RealVector, midpoints::Range;
147-
bandwidth=default_bandwidth(data), kernel=Normal, weights=default_weights(data))
146+
function kde(data::RealVector, midpoints::R;
147+
bandwidth=default_bandwidth(data), kernel=Normal, weights=default_weights(data)) where R<:AbstractRange
148148
bandwidth > 0.0 || error("Bandwidth must be positive")
149149
dist = kernel_dist(kernel,bandwidth)
150150
kde(data,weights,midpoints,dist)
@@ -162,10 +162,10 @@ end
162162
# B. W. Silverman (1986)
163163
# sections 3.4.3 (pp. 48-52) and 3.5 (pp. 61-66)
164164

165-
function kde_lscv(data::RealVector, midpoints::Range;
165+
function kde_lscv(data::RealVector, midpoints::R;
166166
kernel=Normal,
167167
bandwidth_range::Tuple{Real,Real}=(h=default_bandwidth(data); (0.25*h,1.5*h)),
168-
weights=default_weights(data))
168+
weights=default_weights(data)) where R<:AbstractRange
169169

170170
ndata = length(data)
171171
k = tabulate(data, midpoints, weights)

test/bivariate.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Base.Test
1+
using Test
22
using Distributions
33
using KernelDensity
44

@@ -32,7 +32,7 @@ for X in ([0.0], [0.0,0.0], [0.0,0.5], [-0.5:0.1:0.5;])
3232
@test all(k1.density .>= 0.0)
3333
@test sum(k1.density)*step(k1.x)*step(k1.y) 1.0
3434

35-
k2 = conv(k1,kernel_dist(Tuple{D,D}, (0.1,0.1)))
35+
k2 = KernelDensity.conv(k1,kernel_dist(Tuple{D,D}, (0.1,0.1)))
3636
@test isa(k2,BivariateKDE)
3737
@test size(k2.density) == (length(k2.x), length(k2.y))
3838
@test all(k2.density .>= 0.0)
@@ -56,11 +56,11 @@ for X in ([0.0], [0.0,0.0], [0.0,0.5], [-0.5:0.1:0.5;])
5656
@test all(k5.density .>= 0.0)
5757
@test sum(k5.density)*step(k5.x)*step(k5.y) 1.0
5858

59-
k6 = kde([X X],(r,r);kernel=D, weights=ones(X)/length(X))
59+
k6 = kde([X X],(r,r);kernel=D, weights=fill(1.0/length(X),length(X)))
6060
@test k4.density k6.density
6161
end
6262
end
6363

64-
k1 = kde([0.0 0.0; 1.0 1.0], (r,r), bandwidth=(1,1), weights=[0,1])
65-
k2 = kde([1.0 1.0], (r,r), bandwidth=(1,1))
66-
@test k1.density k2.density
64+
k11 = kde([0.0 0.0; 1.0 1.0], (r,r), bandwidth=(1,1), weights=[0,1])
65+
k12 = kde([1.0 1.0], (r,r), bandwidth=(1,1))
66+
@test k11.density k12.density

test/interp.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Base.Test
1+
using Test
22
using KernelDensity
33

44
X = randn(100)

test/univariate.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Base.Test
1+
using Test
22
using Distributions
33
using KernelDensity
44

@@ -31,7 +31,7 @@ for X in ([0.0], [0.0,0.0], [0.0,0.5], [-0.5:0.1:0.5;])
3131
@test all(k1.density .>= 0.0)
3232
@test sum(k1.density)*step(k1.x) 1.0
3333

34-
k2 = conv(k1,kernel_dist(D,0.1))
34+
k2 = KernelDensity.conv(k1,kernel_dist(D,0.1))
3535
@test isa(k2,UnivariateKDE)
3636
@test length(k2.density) == length(k2.x)
3737
@test all(k2.density .>= 0.0)
@@ -55,11 +55,11 @@ for X in ([0.0], [0.0,0.0], [0.0,0.5], [-0.5:0.1:0.5;])
5555
@test all(k5.density .>= 0.0)
5656
@test sum(k5.density)*step(k5.x) 1.0
5757

58-
k6 = kde(X,r;kernel=D, weights=ones(X)/length(X))
58+
k6 = kde(X,r;kernel=D, weights=fill(1.0/length(X),length(X)))
5959
@test k4.density k6.density
6060
end
6161
end
6262

63-
k1 = kde([0.0, 1.], r, bandwidth=1, weights=[0,1])
64-
k2 = kde([1.], r, bandwidth=1)
65-
@test k1.density k2.density
63+
k11 = kde([0.0, 1.], r, bandwidth=1, weights=[0,1])
64+
k12 = kde([1.], r, bandwidth=1)
65+
@test k11.density k12.density

0 commit comments

Comments
 (0)