Merge branch 'master' into staging
[jackhill/guix/guix.git] / gnu / packages / machine-learning.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2016 Ricardo Wurmus <rekado@elephly.net>
3 ;;; Copyright © 2016 Efraim Flashner <efraim@flashner.co.il>
4 ;;; Copyright © 2016 Marius Bakke <mbakke@fastmail.com>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
12 ;;;
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21 (define-module (gnu packages machine-learning)
22 #:use-module ((guix licenses) #:prefix license:)
23 #:use-module (guix packages)
24 #:use-module (guix utils)
25 #:use-module (guix download)
26 #:use-module (guix svn-download)
27 #:use-module (guix build-system cmake)
28 #:use-module (guix build-system gnu)
29 #:use-module (guix build-system ocaml)
30 #:use-module (guix build-system r)
31 #:use-module (gnu packages)
32 #:use-module (gnu packages autotools)
33 #:use-module (gnu packages boost)
34 #:use-module (gnu packages compression)
35 #:use-module (gnu packages cran)
36 #:use-module (gnu packages dejagnu)
37 #:use-module (gnu packages gcc)
38 #:use-module (gnu packages image)
39 #:use-module (gnu packages maths)
40 #:use-module (gnu packages ocaml)
41 #:use-module (gnu packages perl)
42 #:use-module (gnu packages pkg-config)
43 #:use-module (gnu packages python)
44 #:use-module (gnu packages statistics)
45 #:use-module (gnu packages swig)
46 #:use-module (gnu packages xml)
47 #:use-module (gnu packages xorg))
48
49 (define-public libsvm
50 (package
51 (name "libsvm")
52 (version "3.20")
53 (source
54 (origin
55 (method url-fetch)
56 (uri (string-append
57 "https://github.com/cjlin1/libsvm/archive/v"
58 (string-delete #\. version) ".tar.gz"))
59 (file-name (string-append name "-" version ".tar.gz"))
60 (sha256
61 (base32
62 "1jpjlql3frjza7zxzrqqr2firh44fjb8fqsdmvz6bjz7sb47zgp4"))))
63 (build-system gnu-build-system)
64 (arguments
65 `(#:tests? #f ;no "check" target
66 #:phases (modify-phases %standard-phases
67 (delete 'configure)
68 (replace
69 'install
70 (lambda* (#:key outputs #:allow-other-keys)
71 (let* ((out (assoc-ref outputs "out"))
72 (bin (string-append out "/bin/")))
73 (mkdir-p bin)
74 (for-each (lambda (file)
75 (copy-file file (string-append bin file)))
76 '("svm-train"
77 "svm-predict"
78 "svm-scale")))
79 #t)))))
80 (home-page "http://www.csie.ntu.edu.tw/~cjlin/libsvm/")
81 (synopsis "Library for Support Vector Machines")
82 (description
83 "LIBSVM is a machine learning library for support vector
84 classification, (C-SVC, nu-SVC), regression (epsilon-SVR, nu-SVR) and
85 distribution estimation (one-class SVM). It supports multi-class
86 classification.")
87 (license license:bsd-3)))
88
89 (define-public python-libsvm
90 (package (inherit libsvm)
91 (name "python-libsvm")
92 (build-system gnu-build-system)
93 (arguments
94 `(#:tests? #f ;no "check" target
95 #:make-flags '("-C" "python")
96 #:phases
97 (modify-phases %standard-phases
98 (delete 'configure)
99 (replace
100 'install
101 (lambda* (#:key inputs outputs #:allow-other-keys)
102 (let ((site (string-append (assoc-ref outputs "out")
103 "/lib/python"
104 (string-take
105 (string-take-right
106 (assoc-ref inputs "python") 5) 3)
107 "/site-packages/")))
108 (substitute* "python/svm.py"
109 (("../libsvm.so.2") "libsvm.so.2"))
110 (mkdir-p site)
111 (for-each (lambda (file)
112 (copy-file file (string-append site (basename file))))
113 (find-files "python" "\\.py"))
114 (copy-file "libsvm.so.2"
115 (string-append site "libsvm.so.2")))
116 #t)))))
117 (inputs
118 `(("python" ,python)))
119 (synopsis "Python bindings of libSVM")))
120
121 (define-public ghmm
122 ;; The latest release candidate is several years and a couple of fixes have
123 ;; been published since. This is why we download the sources from the SVN
124 ;; repository.
125 (let ((svn-revision 2341))
126 (package
127 (name "ghmm")
128 (version (string-append "0.9-rc3-0." (number->string svn-revision)))
129 (source (origin
130 (method svn-fetch)
131 (uri (svn-reference
132 (url "http://svn.code.sf.net/p/ghmm/code/trunk")
133 (revision svn-revision)))
134 (file-name (string-append name "-" version))
135 (sha256
136 (base32
137 "0qbq1rqp94l530f043qzp8aw5lj7dng9wq0miffd7spd1ff638wq"))))
138 (build-system gnu-build-system)
139 (arguments
140 `(#:imported-modules (,@%gnu-build-system-modules
141 (guix build python-build-system))
142 #:phases
143 (modify-phases %standard-phases
144 (add-after 'unpack 'enter-dir
145 (lambda _ (chdir "ghmm") #t))
146 (delete 'check)
147 (add-after 'install 'check
148 (assoc-ref %standard-phases 'check))
149 (add-before 'check 'fix-PYTHONPATH
150 (lambda* (#:key inputs outputs #:allow-other-keys)
151 (let ((python-version ((@@ (guix build python-build-system)
152 get-python-version)
153 (assoc-ref inputs "python"))))
154 (setenv "PYTHONPATH"
155 (string-append (getenv "PYTHONPATH")
156 ":" (assoc-ref outputs "out")
157 "/lib/python" python-version
158 "/site-packages")))
159 #t))
160 (add-after 'enter-dir 'fix-runpath
161 (lambda* (#:key outputs #:allow-other-keys)
162 (substitute* "ghmmwrapper/setup.py"
163 (("^(.*)extra_compile_args = \\[" line indent)
164 (string-append indent
165 "extra_link_args = [\"-Wl,-rpath="
166 (assoc-ref outputs "out") "/lib\"],\n"
167 line
168 "\"-Wl,-rpath="
169 (assoc-ref outputs "out")
170 "/lib\", ")))
171 #t))
172 (add-after 'enter-dir 'disable-broken-tests
173 (lambda _
174 (substitute* "tests/Makefile.am"
175 ;; GHMM_SILENT_TESTS is assumed to be a command.
176 (("TESTS_ENVIRONMENT.*") "")
177 ;; Do not build broken tests.
178 (("chmm .*") "")
179 (("read_fa .*") "")
180 (("mcmc .*") "")
181 (("label_higher_order_test.*$")
182 "label_higher_order_test\n"))
183
184 ;; These Python unittests are broken as there is no gato.
185 ;; See https://sourceforge.net/p/ghmm/support-requests/3/
186 (substitute* "ghmmwrapper/ghmmunittests.py"
187 (("^(.*)def (testNewXML|testMultipleTransitionClasses|testNewXML)"
188 line indent)
189 (string-append indent
190 "@unittest.skip(\"Disabled by Guix\")\n"
191 line)))
192 #t))
193 (add-after 'disable-broken-tests 'autogen
194 (lambda _
195 (zero? (system* "bash" "autogen.sh")))))))
196 (inputs
197 `(("python" ,python-2) ; only Python 2 is supported
198 ("libxml2" ,libxml2)))
199 (native-inputs
200 `(("pkg-config" ,pkg-config)
201 ("dejagnu" ,dejagnu)
202 ("swig" ,swig)
203 ("autoconf" ,autoconf)
204 ("automake" ,automake)
205 ("libtool" ,libtool)))
206 (home-page "http://ghmm.org")
207 (synopsis "Hidden Markov Model library")
208 (description
209 "The General Hidden Markov Model library (GHMM) is a C library with
210 additional Python bindings implementing a wide range of types of @dfn{Hidden
211 Markov Models} (HMM) and algorithms: discrete, continuous emissions, basic
212 training, HMM clustering, HMM mixtures.")
213 (license license:lgpl2.0+))))
214
215 (define-public mcl
216 (package
217 (name "mcl")
218 (version "14.137")
219 (source (origin
220 (method url-fetch)
221 (uri (string-append
222 "http://micans.org/mcl/src/mcl-"
223 (string-replace-substring version "." "-")
224 ".tar.gz"))
225 (sha256
226 (base32
227 "15xlax3z31lsn62vlg94hkm75nm40q4679amnfg13jm8m2bnhy5m"))))
228 (build-system gnu-build-system)
229 (arguments
230 `(#:configure-flags (list "--enable-blast")))
231 (inputs
232 `(("perl" ,perl)))
233 (home-page "http://micans.org/mcl/")
234 (synopsis "Clustering algorithm for graphs")
235 (description
236 "The MCL algorithm is short for the @dfn{Markov Cluster Algorithm}, a
237 fast and scalable unsupervised cluster algorithm for graphs (also known as
238 networks) based on simulation of (stochastic) flow in graphs.")
239 ;; In the LICENCE file and web page it says "The software is licensed
240 ;; under the GNU General Public License, version 3.", but in several of
241 ;; the source code files it suggests GPL3 or later.
242 ;; http://listserver.ebi.ac.uk/pipermail/mcl-users/2016/000376.html
243 (license license:gpl3)))
244
245 (define-public ocaml-mcl
246 (package
247 (name "ocaml-mcl")
248 (version "12-068oasis4")
249 (source
250 (origin
251 (method url-fetch)
252 (uri (string-append
253 "https://github.com/fhcrc/mcl/archive/"
254 version ".tar.gz"))
255 (file-name (string-append name "-" version ".tar.gz"))
256 (sha256
257 (base32
258 "1l5jbhwjpsj38x8b9698hfpkv75h8hn3kj0gihjhn8ym2cwwv110"))))
259 (build-system ocaml-build-system)
260 (arguments
261 `(#:phases
262 (modify-phases %standard-phases
263 (add-before 'configure 'patch-paths
264 (lambda _
265 (substitute* "configure"
266 (("SHELL = /bin/sh") (string-append "SHELL = "(which "sh"))))
267 (substitute* "setup.ml"
268 (("LDFLAGS=-fPIC")
269 (string-append "LDFLAGS=-fPIC\"; \"SHELL=" (which "sh"))))
270 #t)))))
271 (home-page "https://github.com/fhcrc/mcl")
272 (synopsis "OCaml wrappers around MCL")
273 (description
274 "This package provides OCaml bindings for the MCL graph clustering
275 algorithm.")
276 (license license:gpl3)))
277
278 (define-public ocaml4.01-mcl
279 (package-with-ocaml4.01 ocaml-mcl))
280
281 (define-public randomjungle
282 (package
283 (name "randomjungle")
284 (version "2.1.0")
285 (source
286 (origin
287 (method url-fetch)
288 (uri (string-append
289 "http://www.imbs-luebeck.de/imbs/sites/default/files/u59/"
290 "randomjungle-" version ".tar_.gz"))
291 (sha256
292 (base32
293 "12c8rf30cla71swx2mf4ww9mfd8jbdw5lnxd7dxhyw1ygrvg6y4w"))))
294 (build-system gnu-build-system)
295 (arguments
296 `(#:configure-flags
297 (list (string-append "--with-boost="
298 (assoc-ref %build-inputs "boost")))
299 #:phases
300 (modify-phases %standard-phases
301 (add-before
302 'configure 'set-CXXFLAGS
303 (lambda _
304 (setenv "CXXFLAGS" "-fpermissive ")
305 #t)))))
306 (inputs
307 `(("boost" ,boost)
308 ("gsl" ,gsl)
309 ("libxml2" ,libxml2)
310 ("zlib" ,zlib)))
311 (native-inputs
312 `(("gfortran" ,gfortran)
313 ("gfortran:lib" ,gfortran "lib")))
314 ;; Non-portable assembly instructions are used so building fails on
315 ;; platforms other than x86_64 or i686.
316 (supported-systems '("x86_64-linux" "i686-linux"))
317 (home-page "http://www.imbs-luebeck.de/imbs/de/node/227/")
318 (synopsis "Implementation of the Random Forests machine learning method")
319 (description
320 "Random Jungle is an implementation of Random Forests. It is supposed to
321 analyse high dimensional data. In genetics, it can be used for analysing big
322 Genome Wide Association (GWA) data. Random Forests is a powerful machine
323 learning method. Most interesting features are variable selection, missing
324 value imputation, classifier creation, generalization error estimation and
325 sample proximities between pairs of cases.")
326 (license license:gpl3+)))
327
328 (define-public shogun
329 (package
330 (name "shogun")
331 (version "4.0.0")
332 (source
333 (origin
334 (method url-fetch)
335 (uri (string-append
336 "ftp://shogun-toolbox.org/shogun/releases/"
337 (version-major+minor version)
338 "/sources/shogun-" version ".tar.bz2"))
339 (sha256
340 (base32
341 "159nlijnb7mnrv9za80wnm1shwvy45hgrqzn51hxy7gw4z6d6fdb"))
342 (modules '((guix build utils)
343 (ice-9 rdelim)))
344 (snippet
345 '(begin
346 ;; Remove non-free sources and files referencing them
347 (for-each delete-file
348 (find-files "src/shogun/classifier/svm/"
349 "SVMLight\\.(cpp|h)"))
350 (for-each delete-file
351 (find-files "examples/undocumented/libshogun/"
352 (string-append
353 "(classifier_.*svmlight.*|"
354 "evaluation_cross_validation_locked_comparison).cpp")))
355 ;; Remove non-free functions.
356 (define (delete-ifdefs file)
357 (with-atomic-file-replacement file
358 (lambda (in out)
359 (let loop ((line (read-line in 'concat))
360 (skipping? #f))
361 (if (eof-object? line)
362 #t
363 (let ((skip-next?
364 (or (and skipping?
365 (not (string-prefix?
366 "#endif //USE_SVMLIGHT" line)))
367 (string-prefix?
368 "#ifdef USE_SVMLIGHT" line))))
369 (when (or (not skipping?)
370 (and skipping? (not skip-next?)))
371 (display line out))
372 (loop (read-line in 'concat) skip-next?)))))))
373 (for-each delete-ifdefs (find-files "src/shogun/kernel/"
374 "^Kernel\\.(cpp|h)"))))))
375 (build-system cmake-build-system)
376 (arguments
377 '(#:tests? #f ;no check target
378 #:phases
379 (modify-phases %standard-phases
380 (add-after 'unpack 'delete-broken-symlinks
381 (lambda _
382 (for-each delete-file '("applications/arts/data"
383 "applications/asp/data"
384 "applications/easysvm/data"
385 "applications/msplicer/data"
386 "applications/ocr/data"
387 "examples/documented/data"
388 "examples/documented/matlab_static"
389 "examples/documented/octave_static"
390 "examples/undocumented/data"
391 "examples/undocumented/matlab_static"
392 "examples/undocumented/octave_static"
393 "tests/integration/data"
394 "tests/integration/matlab_static"
395 "tests/integration/octave_static"
396 "tests/integration/python_modular/tests"))
397 #t))
398 (add-after 'unpack 'change-R-target-path
399 (lambda* (#:key outputs #:allow-other-keys)
400 (substitute* '("src/interfaces/r_modular/CMakeLists.txt"
401 "src/interfaces/r_static/CMakeLists.txt"
402 "examples/undocumented/r_modular/CMakeLists.txt")
403 (("\\$\\{R_COMPONENT_LIB_PATH\\}")
404 (string-append (assoc-ref outputs "out")
405 "/lib/R/library/")))
406 #t))
407 (add-after 'unpack 'fix-octave-modules
408 (lambda* (#:key outputs #:allow-other-keys)
409 (substitute* '("src/interfaces/octave_modular/CMakeLists.txt"
410 "src/interfaces/octave_static/CMakeLists.txt")
411 (("^include_directories\\(\\$\\{OCTAVE_INCLUDE_DIRS\\}")
412 "include_directories(${OCTAVE_INCLUDE_DIRS} ${OCTAVE_INCLUDE_DIRS}/octave"))
413
414 ;; change target directory
415 (substitute* "src/interfaces/octave_modular/CMakeLists.txt"
416 (("\\$\\{OCTAVE_OCT_LOCAL_API_FILE_DIR\\}")
417 (string-append (assoc-ref outputs "out")
418 "/share/octave/packages")))
419 #t))
420 (add-before 'build 'set-HOME
421 ;; $HOME needs to be set at some point during the build phase
422 (lambda _ (setenv "HOME" "/tmp") #t)))
423 #:configure-flags
424 (list "-DCMAKE_BUILD_WITH_INSTALL_RPATH=TRUE"
425 "-DUSE_SVMLIGHT=OFF" ;disable proprietary SVMLIGHT
426 ;;"-DJavaModular=ON" ;requires unpackaged jblas
427 ;;"-DRubyModular=ON" ;requires unpackaged ruby-narray
428 ;;"-DPerlModular=ON" ;"FindPerlLibs" does not exist
429 ;;"-DLuaModular=ON" ;fails because lua doesn't build pkgconfig file
430 "-DOctaveModular=ON"
431 "-DOctaveStatic=ON"
432 "-DPythonModular=ON"
433 "-DPythonStatic=ON"
434 "-DRModular=ON"
435 "-DRStatic=ON"
436 "-DCmdLineStatic=ON")))
437 (inputs
438 `(("python" ,python)
439 ("numpy" ,python-numpy)
440 ("r-minimal" ,r-minimal)
441 ("octave" ,octave)
442 ("swig" ,swig)
443 ("hdf5" ,hdf5)
444 ("atlas" ,atlas)
445 ("arpack" ,arpack-ng)
446 ("lapack" ,lapack)
447 ("glpk" ,glpk)
448 ("libxml2" ,libxml2)
449 ("lzo" ,lzo)
450 ("zlib" ,zlib)))
451 (native-inputs
452 `(("pkg-config" ,pkg-config)))
453 ;; Non-portable SSE instructions are used so building fails on platforms
454 ;; other than x86_64.
455 (supported-systems '("x86_64-linux"))
456 (home-page "http://shogun-toolbox.org/")
457 (synopsis "Machine learning toolbox")
458 (description
459 "The Shogun Machine learning toolbox provides a wide range of unified and
460 efficient Machine Learning (ML) methods. The toolbox seamlessly allows to
461 combine multiple data representations, algorithm classes, and general purpose
462 tools. This enables both rapid prototyping of data pipelines and extensibility
463 in terms of new algorithms.")
464 (license license:gpl3+)))
465
466 (define-public r-adaptivesparsity
467 (package
468 (name "r-adaptivesparsity")
469 (version "1.4")
470 (source (origin
471 (method url-fetch)
472 (uri (cran-uri "AdaptiveSparsity" version))
473 (sha256
474 (base32
475 "1az7isvalf3kmdiycrfl6s9k9xqk22k1mc6rh8v0jmcz402qyq8z"))))
476 (properties
477 `((upstream-name . "AdaptiveSparsity")))
478 (build-system r-build-system)
479 (arguments
480 `(#:phases
481 (modify-phases %standard-phases
482 (add-after 'unpack 'link-against-armadillo
483 (lambda _
484 (substitute* "src/Makevars"
485 (("PKG_LIBS=" prefix)
486 (string-append prefix "-larmadillo"))))))))
487 (propagated-inputs
488 `(("r-rcpp" ,r-rcpp)
489 ("r-rcpparmadillo" ,r-rcpparmadillo)))
490 (inputs
491 `(("armadillo" ,armadillo)))
492 (home-page "http://cran.r-project.org/web/packages/AdaptiveSparsity")
493 (synopsis "Adaptive sparsity models")
494 (description
495 "This package implements the Figueiredo machine learning algorithm for
496 adaptive sparsity and the Wong algorithm for adaptively sparse gaussian
497 geometric models.")
498 (license license:lgpl3+)))
499
500 (define-public r-kernlab
501 (package
502 (name "r-kernlab")
503 (version "0.9-25")
504 (source
505 (origin
506 (method url-fetch)
507 (uri (cran-uri "kernlab" version))
508 (sha256
509 (base32
510 "0qnaq9x3j2xc6jrmmd98wc6hkzch487s4p3a9lnc00xvahkhgpmr"))))
511 (build-system r-build-system)
512 (home-page "http://cran.r-project.org/web/packages/kernlab")
513 (synopsis "Kernel-based machine learning tools")
514 (description
515 "This package provides kernel-based machine learning methods for
516 classification, regression, clustering, novelty detection, quantile regression
517 and dimensionality reduction. Among other methods @code{kernlab} includes
518 Support Vector Machines, Spectral Clustering, Kernel PCA, Gaussian Processes
519 and a QP solver.")
520 (license license:gpl2)))
521
522 (define-public dlib
523 (package
524 (name "dlib")
525 (version "19.7")
526 (source (origin
527 (method url-fetch)
528 (uri (string-append
529 "http://dlib.net/files/dlib-" version ".tar.bz2"))
530 (sha256
531 (base32
532 "1mljz02kwkrbggyncxv5fpnyjdybw2qihaacb3js8yfkw12vwpc2"))
533 (modules '((guix build utils)))
534 (snippet
535 '(begin
536 ;; Delete ~13MB of bundled dependencies.
537 (delete-file-recursively "dlib/external")
538 (delete-file-recursively "docs/dlib/external")
539 #t))))
540 (build-system cmake-build-system)
541 (arguments
542 ;; Recent releases defaults to "lib64" on 64bit.
543 `(#:configure-flags (list (string-append "-DCMAKE_INSTALL_LIBDIR="
544 (assoc-ref %outputs "out")
545 "/lib"))
546 #:phases
547 (modify-phases %standard-phases
548 (add-after 'unpack 'disable-asserts
549 (lambda _
550 ;; config.h recommends explicitly enabling or disabling asserts
551 ;; when building as a shared library. By default neither is set.
552 (substitute* "dlib/config.h"
553 (("^//#define DLIB_DISABLE_ASSERTS") "#define DLIB_DISABLE_ASSERTS"))
554 #t))
555 (add-after 'disable-asserts 'disable-failing-tests
556 (lambda _
557 ;; One test times out on MIPS, so we need to disable it.
558 ;; Others are flaky on some platforms.
559 (let* ((system ,(or (%current-target-system)
560 (%current-system)))
561 (disabled-tests (cond
562 ((string-prefix? "mips64" system)
563 '("object_detector" ; timeout
564 "data_io"))
565 ((string-prefix? "armhf" system)
566 '("learning_to_track"))
567 ((string-prefix? "i686" system)
568 '("optimization"))
569 (else '()))))
570 (for-each
571 (lambda (test)
572 (substitute* "dlib/test/makefile"
573 (((string-append "SRC \\+= " test "\\.cpp")) "")))
574 disabled-tests)
575 #t)))
576 (replace 'check
577 (lambda _
578 ;; No test target, so we build and run the unit tests here.
579 (let ((test-dir (string-append "../dlib-" ,version "/dlib/test")))
580 (with-directory-excursion test-dir
581 (and (zero? (system* "make" "-j" (number->string (parallel-job-count))))
582 (zero? (system* "./dtest" "--runall")))))))
583 (add-after 'install 'delete-static-library
584 (lambda* (#:key outputs #:allow-other-keys)
585 (delete-file (string-append (assoc-ref outputs "out")
586 "/lib/libdlib.a"))
587 #t)))))
588 (native-inputs
589 `(("pkg-config" ,pkg-config)))
590 (inputs
591 `(("giflib" ,giflib)
592 ("lapack" ,lapack)
593 ("libjpeg" ,libjpeg)
594 ("libpng" ,libpng)
595 ("libx11" ,libx11)
596 ("openblas" ,openblas)
597 ("zlib" ,zlib)))
598 (synopsis
599 "Toolkit for making machine learning and data analysis applications in C++")
600 (description
601 "Dlib is a modern C++ toolkit containing machine learning algorithms and
602 tools. It is used in both industry and academia in a wide range of domains
603 including robotics, embedded devices, mobile phones, and large high performance
604 computing environments.")
605 (home-page "http://dlib.net")
606 (license license:boost1.0)))