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

enable custom compiled lightgbm package #100

Merged
merged 10 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "0.4.3"
[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
chilledgeek marked this conversation as resolved.
Show resolved Hide resolved
MLJModelInterface = "e80e1ace-859a-464e-9ed9-23947d8ae3ea"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"

Expand Down
32 changes: 29 additions & 3 deletions src/LightGBM.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
module LightGBM

using Dates

using Logging
chilledgeek marked this conversation as resolved.
Show resolved Hide resolved
import Base
import Libdl
import StatsBase
import Libdl

struct LibraryNotFoundError <: Exception
msg::String
end


function find_library(library_name::String, custom_paths::Vector{String})

# Search system filedirs first, returns empty string if not found
output = Libdl.find_library(library_name)

if output == ""
# try specified paths
@info("$(library_name) not found in system dirs, trying fallback")
output = Libdl.find_library(library_name, custom_paths)
else
@info("$(library_name) found in system dirs!")
end

if output == ""
throw(LibraryNotFoundError("$(library_name) not found. Please ensure this library is either in system dirs or the dedicated paths: $(custom_paths)"))
chilledgeek marked this conversation as resolved.
Show resolved Hide resolved
end

return output

end

# we build it so we can assert it is present...
const LGBM_library = abspath(joinpath(@__DIR__, "lib_lightgbm.$(Libdl.dlext)"))
const LGBM_library = find_library("lib_lightgbm", [@__DIR__])

include("wrapper.jl")
include("estimators.jl")
Expand Down
97 changes: 97 additions & 0 deletions test/basic/test_lightgbm.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
module TestLightGBM

import Libdl
using LightGBM
using Test

src_dir = abspath(joinpath(@__DIR__, "..", "..", "src"))

# These set of tests use common libraries of each system to test `find_library` without having to modify env variables
function setup_env()
output = Dict()

if Sys.islinux()
output["sample_lib"] = "libcrypt"
elseif Sys.isunix()
output["sample_lib"] = "libevent"
elseif Sys.iswindows()
output["sample_lib"] = "netmsg"
end

output["ref_path"] = joinpath(src_dir, "lib_lightgbm.$(Libdl.dlext)")
output["custom_fixture_path"] = joinpath(src_dir, "$(output["sample_lib"]).$(Libdl.dlext)")
output["lib_not_on_sys_fixture_path"] = joinpath(src_dir, "lib_not_on_sys.$(Libdl.dlext)")
yalwan-iqvia marked this conversation as resolved.
Show resolved Hide resolved

return output

end

function teardown(settings::Dict)

rm(settings["custom_fixture_path"], force=true)
rm(settings["lib_not_on_sys_fixture_path"], force=true)

return nothing

end

@testset "find_library" begin

@testset "find_library works with no system lib" begin

# Arrange
settings = setup_env()
cp(settings["ref_path"], settings["lib_not_on_sys_fixture_path"]) # fake file copied from lightgbm

# Act
output = LightGBM.find_library("lib_not_on_sys", [src_dir])

# Assert
@test output == joinpath(src_dir, "lib_not_on_sys") # custom path detected (without extension)

teardown(settings)
end

@testset "find_library finds system lib first" begin

# Arrange
settings = setup_env()
cp(settings["ref_path"], settings["custom_fixture_path"]) # fake file copied from lightgbm

# Act
output = LightGBM.find_library(settings["sample_lib"], [src_dir])

# Assert
@test output == settings["sample_lib"] # sys lib detected

teardown(settings)
end

@testset "find_library finds system lib" begin

# Arrange
settings = setup_env()

# Act
output = LightGBM.find_library(settings["sample_lib"], [src_dir]) # sample_lib should only exist in syspath

# Assert
@test output == settings["sample_lib"]

teardown(settings)
end

@testset "find_library throws error if cannot find lib" begin

# Arrange
settings = setup_env()

# Act and assert
@test_throws LightGBM.LibraryNotFoundError LightGBM.find_library("lib_that_simply_doesnt_exist", [src_dir])

teardown(settings)

end
end

end # module
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ end
include(joinpath("basic", "test_search_cv.jl"))
end

@testset "LightGBM" begin
include(joinpath("basic", "test_lightgbm.jl"))
end

end

@testset "Integration tests" begin
Expand Down