Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap ccalls with proper Julia functions #76

Merged
merged 4 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 19 additions & 49 deletions src/LIBSVM.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module LIBSVM


import LIBLINEAR

using SparseArrays
Expand All @@ -12,6 +11,7 @@ export svmtrain, svmpredict, fit!, predict, transform,

include("LibSVMtypes.jl")
include("constants.jl")
include("libcalls.jl")

struct SupportVectors{T<:AbstractVector,U<:AbstractMatrix}
l::Int32
Expand Down Expand Up @@ -153,9 +153,7 @@ svmnoprint(str::Ptr{UInt8})::Cvoid = nothing
const libsvm_version = Ref{Cint}(0)

function __init__()
ccall((:svm_set_print_string_function, libsvm), Cvoid,
(Ptr{Cvoid},), @cfunction(svmnoprint, Cvoid, (Ptr{UInt8},)))

libsvm_set_verbose(false)
libsvm_version[] = unsafe_load(cglobal((:libsvm_version, libsvm), Cint))
end

Expand Down Expand Up @@ -183,12 +181,10 @@ function instances2nodes(instances::AbstractMatrix{<:Real})
nodes = Array{SVMNode}(undef, nfeatures + 1, ninstances)

for i=1:ninstances
k = 1
for j=1:nfeatures
nodes[k, i] = SVMNode(Int32(j), Float64(instances[j, i]))
k += 1
nodes[j, i] = SVMNode(Int32(j), Float64(instances[j, i]))
end
nodes[k, i] = SVMNode(Int32(-1), NaN)
nodes[end, i] = SVMNode(Int32(-1), NaN)
barucden marked this conversation as resolved.
Show resolved Hide resolved
nodeptrs[i] = pointer(nodes, (i-1)*(nfeatures+1)+1)
end

Expand Down Expand Up @@ -246,29 +242,13 @@ function indices_and_weights(labels::AbstractVector{T},
end

function set_num_threads(nt::Integer)

if nt == 0
if haskey(ENV,"OMP_NUM_THREADS")
nt = parse(Int64, ENV["OMP_NUM_THREADS"])
else
nt = 1
end
nt = parse(Int64, get(ENV, "OMP_NUM_THREADS", "1"))
end

if nt < 0
nt = ccall((:svm_get_max_threads, libsvm), Cint, ())
end

ccall((:svm_set_num_threads, libsvm), Cvoid, (Cint,), nt)
end

function check_parameter(problem::SVMProblem, param::SVMParameter)
err = ccall((:svm_check_parameter, libsvm), Cstring,
(Ref{SVMProblem}, Ref{SVMParameter}),
problem, param)
if err != C_NULL
throw(ArgumentError("Incorrect parameter: $(unsafe_string(err))"))
nt = libsvm_get_max_threads()
end
libsvm_set_num_threads(nt)
end

"""
Expand Down Expand Up @@ -368,23 +348,16 @@ function svmtrain(
problem = SVMProblem(Int32(size(X, 2)), pointer(idx), pointer(nodeptrs))

# Validate the given parameters
check_parameter(problem, param)
libsvm_check_parameter(problem, param)

if verbose
# set to stdout
ccall((:svm_set_print_string_function, libsvm), Cvoid,
(Ptr{Cvoid},), C_NULL)
else
ccall((:svm_set_print_string_function, libsvm), Cvoid,
(Ptr{Cvoid},), @cfunction(svmnoprint, Cvoid, (Ptr{UInt8},)))
end
libsvm_set_verbose(verbose)

ptr_model = libsvm_train(problem, param)
svm = SVM(unsafe_load(ptr_model), y, X, wts, reverse_labels, svmtype,
kernel)

mod = ccall((:svm_train, libsvm), Ptr{SVMModel},
(Ref{SVMProblem}, Ref{SVMParameter}),
problem, param)
svm = SVM(unsafe_load(mod), y, X, wts, reverse_labels, svmtype, kernel)
libsvm_free_model(ptr_model)

ccall((:svm_free_model_content, libsvm), Cvoid, (Ptr{Cvoid},), mod)
return svm
end

Expand Down Expand Up @@ -420,19 +393,17 @@ function svmpredict(model::SVM{T}, X::AbstractMatrix{U}; nt::Integer = 0) where
decvalues = zeros(Float64, dcols, ninstances)
end

ccall((:svm_set_print_string_function, libsvm), Cvoid,
(Ptr{Cvoid},), @cfunction(svmnoprint, Cvoid, (Ptr{UInt8},)))
libsvm_set_verbose(false)

cmod, data = svmmodel(model)
ma = [cmod]

for i = 1:ninstances
barucden marked this conversation as resolved.
Show resolved Hide resolved
if model.probability
output = ccall((:svm_predict_probability, libsvm ), Float64, (Ptr{Cvoid}, Ptr{SVMNode}, Ptr{Float64}),
ma, nodeptrs[i], pointer(decvalues, nlabels*(i-1)+1))
output = libsvm_predict_probability(cmod, nodeptrs[i],
Ref(decvalues, nlabels*(i-1)+1))
else
output = ccall((:svm_predict_values, libsvm ), Float64, (Ptr{Cvoid}, Ptr{SVMNode}, Ptr{Float64}),
ma, nodeptrs[i], pointer(decvalues, nlabels*(i-1)+1))
output = libsvm_predict_values(cmod, nodeptrs[i],
Ref(decvalues, nlabels*(i-1)+1))
end
if model.SVMtype == EpsilonSVR || model.SVMtype == NuSVR
pred[i] = output
Expand All @@ -449,5 +420,4 @@ end
include("ScikitLearnTypes.jl")
include("ScikitLearnAPI.jl")


end
48 changes: 48 additions & 0 deletions src/libcalls.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function libsvm_get_max_threads()
return ccall((:svm_get_max_threads, libsvm), Cint, ())
end
barucden marked this conversation as resolved.
Show resolved Hide resolved

function libsvm_set_num_threads(n::Integer)
ccall((:svm_set_num_threads, libsvm), Cvoid, (Cint,), n)
end

noprint(str::Ptr{UInt8})::Cvoid = nothing

function libsvm_set_verbose(v::Bool)
f = ifelse(v, C_NULL, @cfunction(noprint, Cvoid, (Ptr{UInt8},)))
ccall((:svm_set_print_string_function, libsvm), Cvoid, (Ptr{Cvoid},), f)
end

function libsvm_check_parameter(problem::SVMProblem, param::SVMParameter)
err = ccall((:svm_check_parameter, libsvm), Cstring,
(Ref{SVMProblem}, Ref{SVMParameter}),
problem, param)
if err != C_NULL
throw(ArgumentError("Incorrect parameter: $(unsafe_string(err))"))
end
end

function libsvm_train(problem::SVMProblem, param::SVMParameter)
return ccall((:svm_train, libsvm), Ptr{SVMModel},
(Ref{SVMProblem}, Ref{SVMParameter}),
problem, param)
end

function libsvm_free_model(model::Ptr{SVMModel})
ccall((:svm_free_model_content, libsvm), Cvoid, (Ptr{SVMModel},),
model)
end

function libsvm_predict_probability(model::SVMModel, nodes::Ptr{SVMNode},
decisions::Ref{Float64})
return ccall((:svm_predict_probability, libsvm), Cdouble,
(Ref{SVMModel}, Ptr{SVMNode}, Ref{Float64}),
model, node, decisions)
end

function libsvm_predict_values(model::SVMModel, nodes::Ptr{SVMNode},
decisions::Ref{Float64})
return ccall((:svm_predict_values, libsvm), Float64,
(Ref{SVMModel}, Ptr{SVMNode}, Ref{Float64}),
model, nodes, decisions)
end
15 changes: 13 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ end
end
end


@testset "AbstractVector as labels" begin
@info "test AbstractVector labels"

Expand All @@ -68,7 +67,6 @@ end
@test ŷ == predict(model, Xtest')
end


@testset "JLD2 save/load" begin
@info "JLD2 save/load"

Expand Down Expand Up @@ -156,5 +154,18 @@ end
@test_throws ArgumentError svmtrain(rand(2, 5), ones(5); bad_params...)
end

@testset "Decision values" begin
X = [-2 -1 -1 1 1 2;
-1 -1 -2 1 2 1]
y = [1, 1, 1, 2, 2, 2]
d = [1.5 1.0 1.5 -1.0 -1.5 -1.5;
0.0 0.0 0.0 0.0 0.0 0.0]

model = svmtrain(X, y, kernel = Kernel.Linear)
ỹ, d̃ = svmpredict(model, X)

@test ỹ == y
@test d ≈ d̃
end

end # @testset "LIBSVM"