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