Skip to content

Commit

Permalink
Updates for Julia 0.6 (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan committed Jul 26, 2017
1 parent 5e1ce91 commit 42feefb
Show file tree
Hide file tree
Showing 22 changed files with 275 additions and 326 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ os:
- linux
- osx
julia:
- 0.5
- 0.6
- nightly
notifications:
Expand Down
3 changes: 1 addition & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
julia 0.5
Compat 0.18.0
julia 0.6
DataStructures 0.5.0
SpecialFunctions 0.1.0
2 changes: 0 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
environment:
matrix:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.5/julia-0.5-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.5/julia-0.5-latest-win64.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
Expand Down
47 changes: 23 additions & 24 deletions perf/sampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,62 @@
# require the BenchmarkLite package
using BenchmarkLite
using StatsBase
using Compat

import StatsBase: direct_sample!, xmultinom_sample!
import StatsBase: knuths_sample!, fisher_yates_sample!, self_avoid_sample!
import StatsBase: seqsample_a!, seqsample_c!

### generic sampling benchmarking

type SampleProc{Alg} <: Proc end
mutable struct SampleProc{Alg} <: Proc end

@compat abstract type WithRep end
@compat abstract type NoRep end
abstract type WithRep end
abstract type NoRep end

type Direct <: WithRep end
mutable struct Direct <: WithRep end
tsample!(s::Direct, a, x) = direct_sample!(a, x)

type Xmultinom <: WithRep end
mutable struct Xmultinom <: WithRep end
tsample!(s::Xmultinom, a, x) = xmultinom_sample!(a, x)

type Sample_WRep <: WithRep end
mutable struct Sample_WRep <: WithRep end
tsample!(s::Sample_WRep, a, x) = sample!(a, x; replace=true, ordered=false)

type Sample_WRep_Ord <: WithRep end
mutable struct Sample_WRep_Ord <: WithRep end
tsample!(s::Sample_WRep_Ord, a, x) = sample!(a, x; replace=true, ordered=true)

type Knuths <: NoRep end
mutable struct Knuths <: NoRep end
tsample!(s::Knuths, a, x) = knuths_sample!(a, x)

type Fisher_Yates <: NoRep end
mutable struct Fisher_Yates <: NoRep end
tsample!(s::Fisher_Yates, a, x) = fisher_yates_sample!(a, x)

type Self_Avoid <: NoRep end
mutable struct Self_Avoid <: NoRep end
tsample!(s::Self_Avoid, a, x) = self_avoid_sample!(a, x)

type Seq_A <: NoRep end
mutable struct Seq_A <: NoRep end
tsample!(s::Seq_A, a, x) = seqsample_a!(a, x)

type Seq_C <: NoRep end
mutable struct Seq_C <: NoRep end
tsample!(s::Seq_C, a, x) = seqsample_c!(a, x)

type Sample_NoRep <: NoRep end
mutable struct Sample_NoRep <: NoRep end
tsample!(s::Sample_NoRep, a, x) = sample!(a, x; replace=false, ordered=false)

type Sample_NoRep_Ord <: NoRep end
mutable struct Sample_NoRep_Ord <: NoRep end
tsample!(s::Sample_NoRep_Ord, a, x) = sample!(a, x; replace=false, ordered=true)


# config is in the form of (n, k)

Base.string{Alg}(p::SampleProc{Alg}) = lowercase(string(Alg))
Base.string(p::SampleProc{Alg}) where {Alg} = lowercase(string(Alg))

Base.length(p::SampleProc, cfg::(Int, Int)) = cfg[2]
Base.isvalid{Alg<:WithRep}(p::SampleProc{Alg}, cfg::(Int, Int)) = ((n, k) = cfg; n >= 1 && k >= 1)
Base.isvalid{Alg<:NoRep}(p::SampleProc{Alg}, cfg::(Int, Int)) = ((n, k) = cfg; n >= k >= 1)
Base.length(p::SampleProc, cfg::Tuple{Int,Int}) = cfg[2]
Base.isvalid(p::SampleProc{<:WithRep}, cfg::Tuple{Int,Int}) = ((n, k) = cfg; n >= 1 && k >= 1)
Base.isvalid(p::SampleProc{<:NoRep}, cfg::Tuple{Int,Int}) = ((n, k) = cfg; n >= k >= 1)

Base.start(p::SampleProc, cfg::(Int, Int)) = Vector{Int}(cfg[2])
Base.run{Alg}(p::SampleProc{Alg}, cfg::(Int, Int), s::Vector{Int}) = tsample!(Alg(), 1:cfg[1], s)
Base.start(p::SampleProc, cfg::Tuple{Int,Int}) = Vector{Int}(cfg[2])
Base.run(p::SampleProc{Alg}, cfg::Tuple{Int,Int}, s::Vector{Int}) where {Alg} = tsample!(Alg(), 1:cfg[1], s)
Base.done(p::SampleProc, cfg, s) = nothing


Expand All @@ -70,9 +69,9 @@ const ks = 2 .^ [1:16]

## with replacement

const procs1 = Proc[ SampleProc{Direct}(),
const procs1 = Proc[ SampleProc{Direct}(),
SampleProc{Sample_WRep}(),
SampleProc{Xmultinom}(),
SampleProc{Xmultinom}(),
SampleProc{Sample_WRep_Ord}() ]

const cfgs1 = vec([(n, k) for k in ks, n in ns])
Expand All @@ -82,7 +81,7 @@ println()

## without replacement

const procs2 = Proc[ SampleProc{Knuths}(),
const procs2 = Proc[ SampleProc{Knuths}(),
SampleProc{Fisher_Yates}(),
SampleProc{Self_Avoid}(),
SampleProc{Sample_NoRep}(),
Expand Down
33 changes: 16 additions & 17 deletions perf/wsampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,54 @@

using BenchmarkLite
using StatsBase
using Compat

import StatsBase: direct_sample!, alias_sample!, xmultinom_sample!

### procedure definition

type WSampleProc{Alg} <: Proc end
mutable struct WSampleProc{Alg} <: Proc end

@compat abstract type WithRep end
@compat abstract type NoRep end
abstract type WithRep end
abstract type NoRep end

type Direct <: WithRep end
mutable struct Direct <: WithRep end
tsample!(s::Direct, wv, x) = direct_sample!(1:length(wv), wv, x)

type Alias <: WithRep end
mutable struct Alias <: WithRep end
tsample!(s::Alias, wv, x) = alias_sample!(1:length(wv), wv, x)

type Xmultinom_S <: WithRep end
mutable struct Xmultinom_S <: WithRep end
tsample!(s::Xmultinom_S, wv, x) = shuffle!(xmultinom_sample!(1:length(wv), wv, x))

type Xmultinom <: WithRep end
mutable struct Xmultinom <: WithRep end
tsample!(s::Xmultinom, wv, x) = xmultinom_sample!(1:length(wv), wv, x)

type Direct_S <: WithRep end
mutable struct Direct_S <: WithRep end
tsample!(s::Direct_S, wv, x) = sort!(direct_sample!(1:length(wv), wv, x))

type Sample_WRep <: WithRep end
mutable struct Sample_WRep <: WithRep end
tsample!(s::Sample_WRep, wv, x) = sample!(1:length(wv), wv, x; ordered=false)

type Sample_WRep_Ord <: WithRep end
mutable struct Sample_WRep_Ord <: WithRep end
tsample!(s::Sample_WRep_Ord, wv, x) = sample!(1:length(wv), wv, x; ordered=true)


# config is in the form of (n, k)

Base.string{Alg}(p::WSampleProc{Alg}) = lowercase(string(Alg))
Base.string(p::WSampleProc{Alg}) where {Alg} = lowercase(string(Alg))

Base.length(p::WSampleProc, cfg::(Int, Int)) = cfg[2]
Base.isvalid{Alg<:WithRep}(p::WSampleProc{Alg}, cfg::(Int, Int)) = ((n, k) = cfg; n >= 1 && k >= 1)
Base.isvalid{Alg<:NoRep}(p::WSampleProc{Alg}, cfg::(Int, Int)) = ((n, k) = cfg; n >= k >= 1)
Base.length(p::WSampleProc, cfg::Tuple{Int,Int}) = cfg[2]
Base.isvalid(p::WSampleProc{<:WithRep}, cfg::Tuple{Int,Int}) = ((n, k) = cfg; n >= 1 && k >= 1)
Base.isvalid(p::WSampleProc{<:NoRep}, cfg::Tuple{Int,Int}) = ((n, k) = cfg; n >= k >= 1)

function Base.start(p::WSampleProc, cfg::(Int, Int))
function Base.start(p::WSampleProc, cfg::Tuple{Int,Int})
n, k = cfg
x = Vector{Int}(k)
w = weights(fill(1.0/n, n))
return (w, x)
end

Base.run{Alg}(p::WSampleProc{Alg}, cfg::(Int, Int), s) = tsample!(Alg(), s[1], s[2])
Base.run(p::WSampleProc{Alg}, cfg::Tuple{Int,Int}, s) where {Alg} = tsample!(Alg(), s[1], s[2])
Base.done(p::WSampleProc, cfg, s) = nothing


Expand Down
3 changes: 0 additions & 3 deletions src/StatsBase.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
__precompile__()

module StatsBase
using Compat
import Compat: String, view

import Base: length, isempty, eltype, values, sum, mean, mean!, show, quantile
import Base: rand, rand!
import Base.LinAlg: BlasReal, BlasFloat
Expand Down
20 changes: 10 additions & 10 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@
# covariant type notation, i.e. AbstractVector{<:Real}
#

@compat RealArray{T<:Real,N} = AbstractArray{T,N}
@compat RealVector{T<:Real} = AbstractArray{T,1}
@compat RealMatrix{T<:Real} = AbstractArray{T,2}
const RealArray{T<:Real,N} = AbstractArray{T,N}
const RealVector{T<:Real} = AbstractArray{T,1}
const RealMatrix{T<:Real} = AbstractArray{T,2}

@compat IntegerArray{T<:Integer,N} = AbstractArray{T,N}
@compat IntegerVector{T<:Integer} = AbstractArray{T,1}
@compat IntegerMatrix{T<:Integer} = AbstractArray{T,2}
const IntegerArray{T<:Integer,N} = AbstractArray{T,N}
const IntegerVector{T<:Integer} = AbstractArray{T,1}
const IntegerMatrix{T<:Integer} = AbstractArray{T,2}

@compat const RealFP = Union{Float32, Float64}
const RealFP = Union{Float32, Float64}

## conversion from real to fp types

@compat fptype{T<:Union{Float32,Bool,Int8,UInt8,Int16,UInt16}}(::Type{T}) = Float32
@compat fptype{T<:Union{Float64,Int32,UInt32,Int64,UInt64,Int128,UInt128}}(::Type{T}) = Float64
fptype{T<:Union{Float32,Bool,Int8,UInt8,Int16,UInt16}}(::Type{T}) = Float32
fptype{T<:Union{Float64,Int32,UInt32,Int64,UInt64,Int128,UInt128}}(::Type{T}) = Float64
fptype(::Type{Complex64}) = Complex64
fptype(::Type{Complex128}) = Complex128

# A convenient typealias for deprecating default corrected Bool
@compat const DepBool = Union{Bool, Void}
const DepBool = Union{Bool, Void}

function depcheck(fname::Symbol, b::DepBool)
if b == nothing
Expand Down
12 changes: 6 additions & 6 deletions src/counts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
#################################################

@compat IntUnitRange{T<:Integer} = UnitRange{T}
const IntUnitRange{T<:Integer} = UnitRange{T}

#### functions for counting a single list of integers (1D)
"""
Expand Down Expand Up @@ -219,7 +219,7 @@ proportions(x::IntegerArray, y::IntegerArray, wv::AbstractWeights) =

## auxiliary functions

function _normalize_countmap{T}(cm::Dict{T}, s::Real)
function _normalize_countmap(cm::Dict{T}, s::Real) where T
r = Dict{T,Float64}()
for (k, c) in cm
r[k] = c / s
Expand All @@ -237,14 +237,14 @@ Add counts based on `x` to a count map. New entries will be added if new values
If a weighting vector `wv` is specified, the sum of the weights is used rather than the
raw counts.
"""
function addcounts!{T}(cm::Dict{T}, x::AbstractArray{T})
function addcounts!(cm::Dict{T}, x::AbstractArray{T}) where T
for v in x
cm[v] = get(cm, v, 0) + 1
end
return cm
end

function addcounts!{T,W}(cm::Dict{T}, x::AbstractArray{T}, wv::AbstractWeights{W})
function addcounts!(cm::Dict{T}, x::AbstractArray{T}, wv::AbstractWeights{W}) where {T,W}
n = length(x)
length(wv) == n || throw(DimensionMismatch())
w = values(wv)
Expand All @@ -265,8 +265,8 @@ end
Return a dictionary mapping each unique value in `x` to its number
of occurrences.
"""
countmap{T}(x::AbstractArray{T}) = addcounts!(Dict{T,Int}(), x)
countmap{T,W}(x::AbstractArray{T}, wv::AbstractWeights{W}) = addcounts!(Dict{T,W}(), x, wv)
countmap(x::AbstractArray{T}) where {T} = addcounts!(Dict{T,Int}(), x)
countmap(x::AbstractArray{T}, wv::AbstractWeights{W}) where {T,W} = addcounts!(Dict{T,W}(), x, wv)


"""
Expand Down
4 changes: 2 additions & 2 deletions src/deprecates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Base.varm, Base.stdm
@deprecate adjR2(obj::StatisticalModel, variant::Symbol) adjr2(obj, variant)
@deprecate adjR²(obj::StatisticalModel, variant::Symbol) adjr²(obj, variant)

function findat!{T}(r::IntegerArray, a::AbstractArray{T}, b::AbstractArray{T})
function findat!(r::IntegerArray, a::AbstractArray{T}, b::AbstractArray{T}) where T
Base.depwarn("findat! is deprecated, use indexin instead", :findat!)
length(r) == length(b) || raise_dimerror()
d = indexmap(a)
Expand All @@ -49,7 +49,7 @@ findat(a::AbstractArray, b::AbstractArray) = findat!(Array{Int}(size(b)), a, b)

@deprecate_binding WeightVec Weights

immutable RandIntSampler # for generating Int samples in [0, K-1]
struct RandIntSampler # for generating Int samples in [0, K-1]
a::Int
Ku::UInt
U::UInt
Expand Down
22 changes: 12 additions & 10 deletions src/deviation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ end
Compute the squared L2 distance between two arrays: ``\\sum_{i=1}^n |a_i - b_i|^2``.
Efficient equivalent of `sumabs2(a - b)`.
"""
function sqL2dist{T<:Number}(a::AbstractArray{T}, b::AbstractArray{T})
function sqL2dist(a::AbstractArray{T}, b::AbstractArray{T}) where T<:Number
n = length(a)
length(b) == n || throw(DimensionMismatch("Input dimension mismatch"))
r = 0.0
Expand All @@ -64,7 +64,7 @@ end
Compute the L2 distance between two arrays: ``\\sqrt{\\sum_{i=1}^n |a_i - b_i|^2}``.
Efficient equivalent of `sqrt(sumabs2(a - b))`.
"""
L2dist{T<:Number}(a::AbstractArray{T}, b::AbstractArray{T}) = sqrt(sqL2dist(a, b))
L2dist(a::AbstractArray{T}, b::AbstractArray{T}) where {T<:Number} = sqrt(sqL2dist(a, b))


# L1 distance
Expand All @@ -74,7 +74,7 @@ L2dist{T<:Number}(a::AbstractArray{T}, b::AbstractArray{T}) = sqrt(sqL2dist(a, b
Compute the L1 distance between two arrays: ``\\sum_{i=1}^n |a_i - b_i|``.
Efficient equivalent of `sum(abs, a - b)`.
"""
function L1dist{T<:Number}(a::AbstractArray{T}, b::AbstractArray{T})
function L1dist(a::AbstractArray{T}, b::AbstractArray{T}) where T<:Number
n = length(a)
length(b) == n || throw(DimensionMismatch("Input dimension mismatch"))
r = 0.0
Expand All @@ -93,7 +93,7 @@ Compute the L∞ distance, also called the Chebyshev distance, between
two arrays: ``\\max_{i\\in1:n} |a_i - b_i|``.
Efficient equivalent of `maxabs(a - b)`.
"""
function Linfdist{T<:Number}(a::AbstractArray{T}, b::AbstractArray{T})
function Linfdist(a::AbstractArray{T}, b::AbstractArray{T}) where T<:Number
n = length(a)
length(b) == n || throw(DimensionMismatch("Input dimension mismatch"))
r = 0.0
Expand All @@ -115,7 +115,7 @@ Compute the generalized Kullback-Leibler divergence between two arrays:
``\\sum_{i=1}^n (a_i \\log(a_i/b_i) - a_i + b_i)``.
Efficient equivalent of `sum(a*log(a/b)-a+b)`.
"""
function gkldiv{T<:AbstractFloat}(a::AbstractArray{T}, b::AbstractArray{T})
function gkldiv(a::AbstractArray{T}, b::AbstractArray{T}) where T<:AbstractFloat
n = length(a)
r = 0.0
for i = 1:n
Expand All @@ -137,7 +137,8 @@ end
Return the mean absolute deviation between two arrays: `mean(abs(a - b))`.
"""
meanad{T<:Number}(a::AbstractArray{T}, b::AbstractArray{T}) = L1dist(a, b) / length(a)
meanad(a::AbstractArray{T}, b::AbstractArray{T}) where {T<:Number} =
L1dist(a, b) / length(a)


# MaxAD: maximum absolute deviation
Expand All @@ -146,7 +147,7 @@ meanad{T<:Number}(a::AbstractArray{T}, b::AbstractArray{T}) = L1dist(a, b) / len
Return the maximum absolute deviation between two arrays: `maxabs(a - b)`.
"""
maxad{T<:Number}(a::AbstractArray{T}, b::AbstractArray{T}) = Linfdist(a, b)
maxad(a::AbstractArray{T}, b::AbstractArray{T}) where {T<:Number} = Linfdist(a, b)


# MSD: mean squared deviation
Expand All @@ -155,7 +156,8 @@ maxad{T<:Number}(a::AbstractArray{T}, b::AbstractArray{T}) = Linfdist(a, b)
Return the mean squared deviation between two arrays: `mean(abs2(a - b))`.
"""
msd{T<:Number}(a::AbstractArray{T}, b::AbstractArray{T}) = sqL2dist(a, b) / length(a)
msd(a::AbstractArray{T}, b::AbstractArray{T}) where {T<:Number} =
sqL2dist(a, b) / length(a)


# RMSD: root mean squared deviation
Expand All @@ -166,7 +168,7 @@ Return the root mean squared deviation between two optionally
normalized arrays. The root mean squared deviation is computed
as `sqrt(msd(a, b))`.
"""
function rmsd{T<:Number}(a::AbstractArray{T}, b::AbstractArray{T}; normalize::Bool=false)
function rmsd(a::AbstractArray{T}, b::AbstractArray{T}; normalize::Bool=false) where T<:Number
v = sqrt(msd(a, b))
if normalize
amin, amax = extrema(a)
Expand All @@ -184,6 +186,6 @@ Compute the peak signal-to-noise ratio between two arrays `a` and `b`.
`maxv` is the maximum possible value either array can take. The PSNR
is computed as `10 * log10(maxv^2 / msd(a, b))`.
"""
function psnr{T<:Real}(a::AbstractArray{T}, b::AbstractArray{T}, maxv::Real)
function psnr(a::AbstractArray{T}, b::AbstractArray{T}, maxv::Real) where T<:Real
20. * log10(maxv) - 10. * log10(msd(a, b))
end

0 comments on commit 42feefb

Please sign in to comment.