Skip to content

Commit

Permalink
loosen bounds (#119)
Browse files Browse the repository at this point in the history
* make sure conda installs latest version of sklearn

* update ci

* use LD_LIBRARY_PATH enviroment variable 

* cleanup try...catch block
  • Loading branch information
OkonSamuel authored Feb 23, 2023
1 parent 55ee9e6 commit ed3de02
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 39 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

env:
PYTHON: Conda
LD_LIBRARY_PATH: $LD_LIBRARY_PATH:$HOME/.julia/conda/3/lib
LD_LIBRARY_PATH: /home/runner/.julia/conda/3/x86_64/lib # used by linux os only

jobs:
test:
Expand All @@ -36,6 +36,9 @@ jobs:
arch: ${{ matrix.julia-arch }}
- uses: julia-actions/cache@v1 # https://github.com/julia-actions/cache
- uses: julia-actions/julia-buildpkg@latest
- name: "Export LD_LIBRARY_PATH envrioment variable"
if: ${{matrix.os == 'ubuntu-latest' && matrix.julia-version == '1.6'}}
run: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
- uses: julia-actions/julia-runtest@latest
- uses: julia-actions/[email protected]
continue-on-error: true
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ VersionParsing = "81def892-9a0e-5fdd-b105-ffc91e053289"

[compat]
Compat = "2.2, 3, 4"
Conda = "1.5.2, 1.6, 1.7"
Conda = "1.8"
DataFrames = "0.20, 0.21, 0.22, 1"
IterTools = "1.2, 1.3"
MacroTools = "0.5"
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ To install ScikitLearn.jl, type `]add ScikitLearn` at the REPL.

To import Python models (optional), ScikitLearn.jl requires [the scikit-learn Python library](https://cstjean.github.io/ScikitLearn.jl/dev/man/models/#Installation-and-importing-Python-models-1), which will be installed automatically when needed. Most of the examples use [PyPlot.jl](https://github.com/stevengj/PyPlot.jl)

## Known issue

On Linux builds, importing python models via `@sk_import` is known to fail for Julia v<0.8.4 when the `PYTHON` enviroment variable from `PyCall.jl` is set to `""` or `conda`. This is becuase the version libstdcxx loaded by Julia v<0.8.4 isn't compatible with the version of scikit-learn installed via Conda.
The easiest and recommended way to resolve this is to upgrade to Julia v>=1.8.4. If you must stick with your current julia version you can also resolve this issue by pre-appending your system's `LD_LIBRARY_PATH` enviroment variable as shown below
```bash
ROOT_ENV=`julia -e "using Conda; print(Conda.ROOTENV)`
export LD_LIBRARY_PATH=$ROOT_ENV"/lib":$LD_LIBRARY_PATH
```
## Documentation
See the [manual](https://cstjean.github.io/ScikitLearn.jl/dev/) and
Expand Down
93 changes: 56 additions & 37 deletions src/Skcore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,55 +166,74 @@ function import_sklearn()
global mkl_checked
global libstdcxx_solved

@static if Sys.isapple()
mod = try
mod = try
@static if Sys.isapple()
if PyCall.conda && !mkl_checked
try
# check for existence of mkl-service.
# Numpy, sklearn, etc. requires either `mkl` or `no-mkl` service to run
# By default Conda comes with mkl
# For this package to run on MacOS the `no-mkl` versions of Numpy, sklearn is needed
pyimport("mkl")
# For this package to run on MacOS, the `no-mkl` versions of Numpy, sklearn are needed
pyimport("mkl") # jumps to catch block if "mkl" doesn't exist on the users Mac

#following Code runs only if mkl-service exists otherwise jumps to catch branch
@info "Installing non-mkl versions of sci-kit learn via Conda"
#use non-mkl versions of python packages when ENV["PYTHON"]="Conda" or "" is used
#when a different non-conda local python is used everthing works fine
# following Code runs only if mkl-service exists otherwise jumps to catch branch
@info(
"Uninstalling mkl and Installing "*
"non-mkl versions of sci-kit learn via Conda"
)
# Use non-mkl versions of python packages when ENV["PYTHON"]="Conda"
# or "" is used.
# When a different non-conda local python is used everthing works fine
Conda.add("nomkl")
Conda.rm("mkl")#This also removes mkl-service
#force reinstall of scikit-learn replacing any previous mkl version
Conda.add("scikit-learn")
Conda.add("openblas")
Conda.add("llvm-openmp", channel = "conda-forge")
mkl_checked = true
catch
mkl_checked = true
end
catch err
## This block is reached when `mkl` pkg isn't installed.
@info(
"mkl not found, proceeding to installing non-mkl versions "*
"of sci-kit learn via Conda"
)
end
# force reinstall of scikit-learn replacing any previous mkl version
Conda.add("scikit-learn>=1.2,<1.3", channel="conda-forge")
#Conda.add("openblas")
#Conda.add("llvm-openmp", channel = "conda-forge")
mkl_checked = true
end
#PyCall.pyimport_conda("sklearn", "scikit-learn>=1.2,<1.3", "conda-forge")
else
@static if Sys.islinux()
if !libstdcxx_solved
version = _compatible_libstdcxx_ng_version()
Conda.add("conda", channel="anaconda")
Conda.add("libstdcxx-ng$version", channel="conda-forge")
#=
if version == ">=3.4,<12.0"
# https://github.com/scikit-learn/scikit-learn/pull/23990
Conda.add("scikit-learn>=1.2,<1.3", channel="conda-forge")
end
=#
libstdcxx_solved = true
end
end
Conda.add("llvm-openmp", channel = "conda-forge")
PyCall.pyimport_conda("sklearn", "scikit-learn")

catch
@info("scikit-learn isn't properly installed."*
"Please use PyCall default Conda or non-conda local python")
rethrow()
end

elseif Sys.islinux()
if !libstdcxx_solved
version = _compatible_libstdcxx_ng_version()
Conda.add("conda", channel="anaconda")
Conda.add("libstdcxx-ng$version", channel="conda-forge")
if version == ">=3.4,<12.0"
# https://github.com/scikit-learn/scikit-learn/pull/23990
Conda.add("scikit-learn<1.1", channel="conda-forge")
PyCall.pyimport_conda("sklearn", "scikit-learn>=1.2,<1.3", "conda-forge")
catch
INFO_MSG = "scikit-learn isn't properly installed."*
"Please make sure PyCall is using the default Conda or non-conda local python."
@static if Sys.islinux()
if libstdcxx_solved
INFO_MSG = INFO_MSG *
"\nIf you are experiencing Conda package conflicts, this may "*
"be because the version libstdcxx loaded by Julia $(Base.VERSION) "*
"isn't compatible with the version of scikit-learn installed by "*
"Conda. To resolve this, we recommend upgrading to Julia v>=1.8.4.\n"*
"If you must stick with your current julia version you can "*
"also resolve this issue by pre-appending `$(CONDA.ROOTENV)/lib` to "*
"your system's `LD_LIBRARY_PATH` enviroment variable."
end
libstdcxx_solved = true
end

mod = PyCall.pyimport_conda("sklearn", "scikit-learn")
else
mod = PyCall.pyimport_conda("sklearn", "scikit-learn")
@info(INFO_MSG)
rethrow()
end

version = VersionParsing.vparse(mod.__version__)
Expand Down

0 comments on commit ed3de02

Please sign in to comment.