Subject: [PATCH v5 3/3] gnu: Add python-lightgbm.

From: Vinicius Monego <[email-masked]>
Date: Fri, 7 Apr 2023 15:17:17 +0000

* gnu/packages/machine-learning.scm (python-lightgbm): New variable.
---
 gnu/packages/machine-learning.scm | 55 +++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

diff --git a/gnu/packages/machine-learning.scm b/gnu/packages/machine-learning.scm
index 248e318eea..1585f09992 100644
--- a/gnu/packages/machine-learning.scm
+++ b/gnu/packages/machine-learning.scm
@@ -1511,6 +1511,61 @@ (define-public lightgbm
 @end itemize\n")
     (license license:expat)))
 
+(define-public python-lightgbm
+  (package
+    (inherit lightgbm)
+    (name "python-lightgbm")
+    (source (package-source lightgbm))
+    (build-system python-build-system)
+    (arguments
+     (list #:configure-flags #~(list "--precompile") ; use our library
+           #:phases
+           #~(modify-phases %standard-phases
+               (add-after 'unpack 'preparations
+                 (lambda _
+                   (setenv "HOME" "/tmp") ; to write a log file
+                   (rename-file "python-package/setup.py" "setup.py")
+                   (rename-file "python-package/README.rst" "README.rst")
+                   (rename-file "python-package/lightgbm" "lightgbm")
+                   ;; Write the version so the module identifies it.
+                   (with-output-to-file "lightgbm/VERSION.txt"
+                     (lambda () (display #$(package-version lightgbm))))
+                   ;; Manually install the lib.
+                   (substitute* "setup.py"
+                     (("'install_lib': CustomInstallLib,") ""))))
+               (add-after 'install 'install-liblightgbm
+                 ;; LightGBM's setup.py hardcodes LIB_PATH to look for
+                 ;; lib_lightgbm.so in this source, which we can't modify.
+                 ;; So, we symlink it in the install phase.
+                 (lambda _
+                   (let* ((pylib (string-append #$output
+                                                "/lib/python"
+                                                #$(version-major+minor
+                                                   (package-version
+                                                    python))
+                                                "/site-packages"))
+                          (lgbdir (string-append pylib "/lightgbm"))
+                          (lib-lightgbm (string-append
+                                         #$(this-package-input "lightgbm")
+                                         "/lib/lib_lightgbm.so")))
+                     ;; For whatever reason, the first symlink is required for
+                     ;; the check phase and the second for sanity-check.
+                     (symlink lib-lightgbm "lightgbm/lib_lightgbm.so")
+                     (mkdir-p (string-append lgbdir "/lib"))
+                     (symlink lib-lightgbm
+                              (string-append lgbdir
+                                             "/lib_lightgbm.so")))))
+               (replace 'check
+                 (lambda* (#:key tests? #:allow-other-keys)
+                   (when tests?
+                     (invoke "python" "-m" "pytest"
+                             "tests/python_package_test")))))))
+    (native-inputs (list python-psutil python-pytest python-wheel))
+    (inputs (list lightgbm))
+    (propagated-inputs (list python-numpy python-pandas python-scikit-learn
+                             python-scipy))
+    (synopsis "Python interface for LightGBM")))
+
 (define-public vowpal-wabbit
   ;; Language bindings not included.
   (package
-- 
2.34.1