Skip to content

Commit 6221277

Browse files
lsindoniasbisen
authored andcommitted
Changes for Julia 0.7/1.0 (#45)
* Modified code to support Julia 0.7 - 1.0 * added dependencies * Removed Iterators * Added version check for initial imports * Fixed deprecation warning (Iterators) * Addressed commets raised by ararslan * Removed VERSION checks and code for < v"0.7.0" * Update .travis.yml Co-authored-by: Anand Bisen <[email protected]>
1 parent 669b69b commit 6221277

File tree

13 files changed

+90
-90
lines changed

13 files changed

+90
-90
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ os:
33
- linux
44
- osx
55
julia:
6-
- 0.6
6+
- 0.7
7+
- 1.0
78
- nightly
89
notifications:
910
email: false
@@ -12,4 +13,4 @@ notifications:
1213
# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
1314
# - julia -e 'Pkg.clone(pwd()); Pkg.build("MLBase"); Pkg.test("MLBase")'
1415
after_success:
15-
- julia -e 'cd(Pkg.dir("MLBase")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
16+
- julia -e 'using Pkg; Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
julia 0.6
1+
julia 0.7
22
Reexport
33
StatsBase 0.6.9-
44
IterTools

src/MLBase.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ module MLBase
22

33
using Reexport
44
using IterTools
5+
using Random
56
@reexport using StatsBase
67

78
import Base: length, show, keys, precision, length, getindex
8-
import Base: start, next, done
9+
import Base: iterate
910
import Base.Order: lt, Ordering, ForwardOrdering, ReverseOrdering, Forward, Reverse
1011
import StatsBase: RealVector, IntegerVector, RealMatrix, IntegerMatrix, RealArray
12+
import IterTools: product
1113

1214
export
1315

@@ -77,4 +79,3 @@ module MLBase
7779

7880
include("deprecates.jl")
7981
end
80-

src/classification.jl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ end
3232

3333
classify!(r::IntegerVector, x::RealMatrix) = classify!(r, x, Forward)
3434

35-
classify(x::RealMatrix, ord::Ordering) = classify!(Array{Int}(size(x,2)), x, ord)
35+
# - this one throws a deprecation
36+
classify(x::RealMatrix, ord::Ordering) = classify!(Array{Int}(undef, size(x,2)), x, ord)
3637
classify(x::RealMatrix) = classify(x, Forward)
3738

3839
# classify with score(s)
@@ -65,13 +66,13 @@ function classify_withscores!(r::IntegerVector, s::RealVector, x::RealMatrix, or
6566
return (r, s)
6667
end
6768

68-
classify_withscores!(r::IntegerVector, s::RealVector, x::RealMatrix) =
69+
classify_withscores!(r::IntegerVector, s::RealVector, x::RealMatrix) =
6970
classify_withscores!(r, s, x, Forward)
7071

7172
function classify_withscores(x::RealMatrix{T}, ord::Ordering) where T<:Real
7273
n = size(x, 2)
73-
r = Array{Int}(n)
74-
s = Array{T}(n)
74+
r = Array{Int}(undef, n)
75+
s = Array{T}(undef, n)
7576
return classify_withscores!(r, s, x, ord)
7677
end
7778

@@ -80,7 +81,7 @@ classify_withscores(x::RealMatrix{T}) where {T<:Real} = classify_withscores(x, F
8081

8182
# classify with threshold
8283

83-
classify(x::RealVector, t::Real, ord::Ordering) =
84+
classify(x::RealVector, t::Real, ord::Ordering) =
8485
((k, v) = classify_withscore(x, ord); ifelse(lt(ord, v, t), 0, k))
8586

8687
classify(x::RealVector, t::Real) = classify(x, t, Forward)
@@ -97,8 +98,8 @@ end
9798

9899
classify!(r::IntegerVector, x::RealMatrix, t::Real) = classify!(r, x, t, Forward)
99100

100-
classify(x::RealMatrix, t::Real, ord::Ordering) = classify!(Array{Int}(size(x,2)), x, t, ord)
101-
classify(x::RealMatrix, t::Real) = classify(x, t, Forward)
101+
classify(x::RealMatrix, t::Real, ord::Ordering) = classify!(Array{Int}(undef, size(x,2)), x, t, ord)
102+
classify(x::RealMatrix, t::Real) = classify(x, t, Forward)
102103

103104

104105
## label map
@@ -109,7 +110,7 @@ struct LabelMap{K}
109110

110111
function LabelMap{K}(vs, v2i) where K
111112
length(vs) == length(v2i) || throw(DimensionMismatch("lengths of vs and v2i mismatch"))
112-
new(vs,v2i)
113+
new(vs,v2i)
113114
end
114115
end
115116

@@ -143,18 +144,18 @@ end
143144

144145
# use a map to encode discrete values into labels
145146
labelencode(lmap::LabelMap{T}, x) where {T} = lmap.v2i[convert(T, x)]
146-
labelencode(lmap::LabelMap{T}, xs::AbstractArray{T}) where {T} =
147+
labelencode(lmap::LabelMap{T}, xs::AbstractArray{T}) where {T} =
147148
reshape(Int[labelencode(lmap, x) for x in xs], size(xs))
148149

149150
# decode the label to the associated discrete value
150151
labeldecode(lmap::LabelMap{T}, y::Int) where {T} = lmap.vs[y]
151-
labeldecode(lmap::LabelMap{T}, ys::AbstractArray{Int}) where {T} =
152+
labeldecode(lmap::LabelMap{T}, ys::AbstractArray{Int}) where {T} =
152153
reshape(T[labeldecode(lmap, y) for y in ys], size(ys))
153154

154155
## group labels
155156

156157
function groupindices(k::Int, xs::IntegerVector; warning::Bool=true)
157-
gs = Array{Vector{Int}}(k)
158+
gs = Array{Vector{Int}}(undef, k)
158159
for i = 1:k
159160
gs[i] = Int[]
160161
end
@@ -176,7 +177,7 @@ end
176177

177178
function groupindices(lmap::LabelMap{T}, xs::AbstractArray{T}) where T
178179
k = length(lmap)
179-
gs = Array{Vector{Int}}(k)
180+
gs = Array{Vector{Int}}(undef, k)
180181
for i = 1:k
181182
gs[i] = Int[]
182183
end
@@ -187,4 +188,3 @@ function groupindices(lmap::LabelMap{T}, xs::AbstractArray{T}) where T
187188
end
188189
return gs
189190
end
190-

src/crossval.jl

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
abstract type CrossValGenerator end
66

7-
# K-fold
7+
# K-fold
88

99
struct Kfold <: CrossValGenerator
1010
permseq::Vector{Int}
@@ -20,15 +20,20 @@ end
2020
length(c::Kfold) = c.k
2121

2222
struct KfoldState
23-
i::Int # the i-th of the subset
23+
i::Int # the i-th of the subset
2424
s::Int # starting index
2525
e::Int # ending index
2626
end
2727

28-
start(c::Kfold) = KfoldState(1, 1, round.(Integer,c.coeff))
29-
next(c::Kfold, s::KfoldState) =
30-
(i = s.i+1; (setdiff(1:length(c.permseq), c.permseq[s.s:s.e]), KfoldState(i, s.e+1, round.(Integer,c.coeff * i))))
31-
done(c::Kfold, s::KfoldState) = (s.i > c.k)
28+
# A version check allows to maintain compatibility with earlier versions
29+
function Base.iterate(c::Kfold, state::KfoldState=KfoldState(1, 1, round.(Integer, c.coeff)))
30+
i, s, e = state.i, state.s, state.e
31+
(i > c.k) && return nothing
32+
i += 1
33+
sd = setdiff(1:length(c.permseq), c.permseq[s:e])
34+
kst = KfoldState(i, e + 1, round.(Integer, c.coeff * i))
35+
return (i; (sd, kst))
36+
end
3237

3338
# Stratified K-fold
3439

@@ -52,22 +57,21 @@ end
5257

5358
length(c::StratifiedKfold) = c.k
5459

55-
start(c::StratifiedKfold) = 1
56-
function next(c::StratifiedKfold, s::Int)
60+
function Base.iterate(c::StratifiedKfold, s::Int=1)
61+
(s > c.k) && return nothing
5762
r = Int[]
5863
for (permseq, coeff) in zip(c.permseqs, c.coeffs)
5964
a, b = round.(Integer, [s-1, s] .* coeff)
6065
append!(r, view(permseq, a+1:b))
6166
end
62-
setdiff(1:c.n, r), s+1
67+
return setdiff(1:c.n, r), s+1
6368
end
64-
done(c::StratifiedKfold, s::Int) = (s > c.k)
6569

6670
# LOOCV (Leave-one-out cross-validation)
6771

6872
function leave_one_out(n::Int, i::Int)
6973
@assert 1 <= i <= n
70-
x = Array{Int}(n-1)
74+
x = Array{Int}(undef, n - 1)
7175
for j = 1:i-1
7276
x[j] = j
7377
end
@@ -83,10 +87,10 @@ end
8387

8488
length(c::LOOCV) = c.n
8589

86-
start(c::LOOCV) = 1
87-
next(c::LOOCV, s::Int) = (leave_one_out(c.n, s), s+1)
88-
done(c::LOOCV, s::Int) = (s > c.n)
89-
90+
function iterate(c::LOOCV, s::Int=1)
91+
(s > c.n) && return nothing
92+
return (leave_one_out(c.n, s), s + 1)
93+
end
9094

9195
# Repeated random sub-sampling
9296

@@ -98,9 +102,10 @@ end
98102

99103
length(c::RandomSub) = c.k
100104

101-
start(c::RandomSub) = 1
102-
next(c::RandomSub, s::Int) = (sort!(sample(1:c.n, c.sn; replace=false)), s+1)
103-
done(c::RandomSub, s::Int) = (s > c.k)
105+
function iterate(c::RandomSub, s::Int=1)
106+
(s > c.k) && return nothing
107+
return (sort!(sample(1:c.n, c.sn; replace=false)), s+1)
108+
end
104109

105110
# Stratified repeated random sub-sampling
106111

@@ -134,24 +139,23 @@ end
134139

135140
length(c::StratifiedRandomSub) = c.k
136141

137-
start(c::StratifiedRandomSub) = 1
138-
function next(c::StratifiedRandomSub, s::Int)
139-
idxs = Array{Int}(0)
142+
function iterate(c::StratifiedRandomSub, s::Int=1)
143+
(s > c.k) && return nothing
144+
idxs = Array{Int}(undef, 0)
140145
sizehint!(idxs, c.sn)
141146
for (stratum_sn, stratum_idxs) in zip(c.sns, c.idxs)
142147
append!(idxs, sample(stratum_idxs, stratum_sn, replace=false))
143148
end
144-
(sort!(idxs), s+1)
149+
return (sort!(idxs), s + 1)
145150
end
146-
done(c::StratifiedRandomSub, s::Int) = (s > c.k)
147151

148152
## Cross validation algorithm
149153
#
150154
# estfun: model estimation function
151155
#
152156
# model = estfun(train_inds)
153157
#
154-
# it takes as input the indices of
158+
# it takes as input the indices of
155159
# the samples for training, and returns
156160
# a trained model.
157161
#
@@ -172,7 +176,7 @@ done(c::StratifiedRandomSub, s::Int) = (s > c.k)
172176
#
173177
function cross_validate(estfun::Function, evalfun::Function, n::Int, gen)
174178
best_model = nothing
175-
best_score = NaN
179+
best_score = NaN
176180
best_inds = Int[]
177181
first = true
178182

@@ -186,8 +190,5 @@ function cross_validate(estfun::Function, evalfun::Function, n::Int, gen)
186190
return scores
187191
end
188192

189-
cross_validate(estfun::Function, evalfun::Function, n::Integer, gen) =
193+
cross_validate(estfun::Function, evalfun::Function, n::Integer, gen) =
190194
cross_validate(estfun, evalfun, n, gen, Forward)
191-
192-
193-

src/modeltune.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function gridtune(estfun::Function, # model estimation
1616
t = 0
1717
first = true
1818
local best_score, best_model, best_cfg
19-
for cf in product(pvals...)
19+
for cf in Iterators.product(pvals...)
2020
t += 1
2121
m = estfun(cf...)
2222
if m == nothing

src/perfeval.jl

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ function counthits(gt::IntegerVector, rklst::IntegerMatrix, ks::IntegerVector)
6767
end
6868

6969

70-
hitrate(gt::IntegerVector, rklst::IntegerMatrix, k::Integer) =
70+
hitrate(gt::IntegerVector, rklst::IntegerMatrix, k::Integer) =
7171
(counthits(gt, rklst, k) / length(gt))::Float64
7272

7373
function hitrates(gt::IntegerVector, rklst::IntegerMatrix, ks::IntegerVector)
7474
n = length(gt)
7575
h = counthits(gt, rklst, ks)
7676
nk = length(ks)
77-
r = Array{Float64}(nk)
77+
r = Array{Float64}(undef, nk)
7878
for i = 1:nk
7979
r[i] = h[i] / n
8080
end
@@ -178,7 +178,7 @@ length(v::BinaryThresPredVec) = length(v.scores)
178178
getindex(v::BinaryThresPredVec, i::Integer) = !lt(v.ord, v.scores[i], v.thres)
179179

180180
# compute roc numbers based on scores & threshold
181-
roc(gt::IntegerVector, scores::RealVector, t::Real, ord::Ordering) =
181+
roc(gt::IntegerVector, scores::RealVector, t::Real, ord::Ordering) =
182182
_roc(gt, BinaryThresPredVec(scores, t, ord))
183183

184184
roc(gt::IntegerVector, scores::RealVector, thres::Real) =
@@ -211,7 +211,7 @@ length(v::ThresPredVec) = length(v.preds)
211211
getindex(v::ThresPredVec, i::Integer) = ifelse(lt(v.ord, v.scores[i], v.thres), 0, v.preds[i])
212212

213213
# compute roc numbers based on predictions & scores & threshold
214-
roc(gt::IntegerVector, preds::Tuple{PV,SV}, t::Real, ord::Ordering) where {PV<:IntegerVector,SV<:RealVector} =
214+
roc(gt::IntegerVector, preds::Tuple{PV,SV}, t::Real, ord::Ordering) where {PV<:IntegerVector,SV<:RealVector} =
215215
_roc(gt, ThresPredVec(preds..., t, ord))
216216

217217
roc(gt::IntegerVector, preds::Tuple{PV,SV}, thres::Real) where {PV<:IntegerVector,SV<:RealVector} =
@@ -246,10 +246,10 @@ end
246246

247247
find_thresbin(x::Real, thresholds::RealVector) = find_thresbin(x, thresholds, Forward)
248248

249-
lin_thresholds(scores::RealArray, n::Integer, ord::ForwardOrdering) =
249+
lin_thresholds(scores::RealArray, n::Integer, ord::ForwardOrdering) =
250250
((s0, s1) = extrema(scores); intv = (s1 - s0) / (n-1); s0:intv:s1)
251251

252-
lin_thresholds(scores::RealArray, n::Integer, ord::ReverseOrdering{ForwardOrdering}) =
252+
lin_thresholds(scores::RealArray, n::Integer, ord::ReverseOrdering{ForwardOrdering}) =
253253
((s0, s1) = extrema(scores); intv = (s0 - s1) / (n-1); s1:intv:s0)
254254

255255
# roc for binary predictions
@@ -278,7 +278,7 @@ function roc(gt::IntegerVector, scores::RealVector, thresholds::RealVector, ord:
278278
end
279279

280280
# produce results
281-
r = Array{ROCNums{Int}}(nt)
281+
r = Array{ROCNums{Int}}(undef, nt)
282282
fn = 0
283283
tn = 0
284284
@inbounds for i = 1:nt
@@ -293,7 +293,7 @@ end
293293

294294
roc(gt::IntegerVector, scores::RealVector, thresholds::RealVector) = roc(gt, scores, thresholds, Forward)
295295

296-
roc(gt::IntegerVector, scores::RealVector, n::Integer, ord::Ordering) =
296+
roc(gt::IntegerVector, scores::RealVector, n::Integer, ord::Ordering) =
297297
roc(gt, scores, lin_thresholds(scores, n, ord), ord)
298298

299299
roc(gt::IntegerVector, scores::RealVector, n::Integer) = roc(gt, scores, n, Forward)
@@ -341,7 +341,7 @@ function roc(
341341
end
342342

343343
# produce results
344-
r = Array{ROCNums{Int}}(nt)
344+
r = Array{ROCNums{Int}}(undef, nt)
345345
fn = 0
346346
tn = 0
347347
@inbounds for i = 1:nt
@@ -357,15 +357,14 @@ end
357357
roc(gt::IntegerVector, preds::Tuple{PV,SV}, thresholds::RealVector) where {PV<:IntegerVector, SV<:RealVector} =
358358
roc(gt, preds, thresholds, Forward)
359359

360-
roc(gt::IntegerVector, preds::Tuple{PV,SV}, n::Integer, ord::Ordering) where {PV<:IntegerVector, SV<:RealVector} =
360+
roc(gt::IntegerVector, preds::Tuple{PV,SV}, n::Integer, ord::Ordering) where {PV<:IntegerVector, SV<:RealVector} =
361361
roc(gt, preds, lin_thresholds(preds[2],n,ord), ord)
362362

363-
roc(gt::IntegerVector, preds::Tuple{PV,SV}, n::Integer) where {PV<:IntegerVector, SV<:RealVector} =
363+
roc(gt::IntegerVector, preds::Tuple{PV,SV}, n::Integer) where {PV<:IntegerVector, SV<:RealVector} =
364364
roc(gt, preds, n, Forward)
365365

366-
roc(gt::IntegerVector, preds::Tuple{PV,SV}, ord::Ordering) where {PV<:IntegerVector, SV<:RealVector} =
366+
roc(gt::IntegerVector, preds::Tuple{PV,SV}, ord::Ordering) where {PV<:IntegerVector, SV<:RealVector} =
367367
roc(gt, preds, 100, ord)
368368

369-
roc(gt::IntegerVector, preds::Tuple{PV,SV}) where {PV<:IntegerVector, SV<:RealVector} =
369+
roc(gt::IntegerVector, preds::Tuple{PV,SV}) where {PV<:IntegerVector, SV<:RealVector} =
370370
roc(gt, preds, Forward)
371-

0 commit comments

Comments
 (0)