Merge branch 'master' into core-updates
[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-before 'configure '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 (alist-cons-after
380 '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 (alist-cons-after
399 'unpack 'change-R-target-path
400 (lambda* (#:key outputs #:allow-other-keys)
401 (substitute* '("src/interfaces/r_modular/CMakeLists.txt"
402 "src/interfaces/r_static/CMakeLists.txt"
403 "examples/undocumented/r_modular/CMakeLists.txt")
404 (("\\$\\{R_COMPONENT_LIB_PATH\\}")
405 (string-append (assoc-ref outputs "out")
406 "/lib/R/library/")))
407 #t)
408 (alist-cons-after
409 'unpack 'fix-octave-modules
410 (lambda* (#:key outputs #:allow-other-keys)
411 (substitute* '("src/interfaces/octave_modular/CMakeLists.txt"
412 "src/interfaces/octave_static/CMakeLists.txt")
413 (("^include_directories\\(\\$\\{OCTAVE_INCLUDE_DIRS\\}")
414 "include_directories(${OCTAVE_INCLUDE_DIRS} ${OCTAVE_INCLUDE_DIRS}/octave"))
415
416 ;; change target directory
417 (substitute* "src/interfaces/octave_modular/CMakeLists.txt"
418 (("\\$\\{OCTAVE_OCT_LOCAL_API_FILE_DIR\\}")
419 (string-append (assoc-ref outputs "out")
420 "/share/octave/packages")))
421 #t)
422 (alist-cons-before
423 'build 'set-HOME
424 ;; $HOME needs to be set at some point during the build phase
425 (lambda _ (setenv "HOME" "/tmp") #t)
426 %standard-phases))))
427 #:configure-flags
428 (list "-DCMAKE_BUILD_WITH_INSTALL_RPATH=TRUE"
429 "-DUSE_SVMLIGHT=OFF" ;disable proprietary SVMLIGHT
430 ;;"-DJavaModular=ON" ;requires unpackaged jblas
431 ;;"-DRubyModular=ON" ;requires unpackaged ruby-narray
432 ;;"-DPerlModular=ON" ;"FindPerlLibs" does not exist
433 ;;"-DLuaModular=ON" ;fails because lua doesn't build pkgconfig file
434 "-DOctaveModular=ON"
435 "-DOctaveStatic=ON"
436 "-DPythonModular=ON"
437 "-DPythonStatic=ON"
438 "-DRModular=ON"
439 "-DRStatic=ON"
440 "-DCmdLineStatic=ON")))
441 (inputs
442 `(("python" ,python)
443 ("numpy" ,python-numpy)
444 ("r-minimal" ,r-minimal)
445 ("octave" ,octave)
446 ("swig" ,swig)
447 ("hdf5" ,hdf5)
448 ("atlas" ,atlas)
449 ("arpack" ,arpack-ng)
450 ("lapack" ,lapack)
451 ("glpk" ,glpk)
452 ("libxml2" ,libxml2)
453 ("lzo" ,lzo)
454 ("zlib" ,zlib)))
455 (native-inputs
456 `(("pkg-config" ,pkg-config)))
457 ;; Non-portable SSE instructions are used so building fails on platforms
458 ;; other than x86_64.
459 (supported-systems '("x86_64-linux"))
460 (home-page "http://shogun-toolbox.org/")
461 (synopsis "Machine learning toolbox")
462 (description
463 "The Shogun Machine learning toolbox provides a wide range of unified and
464 efficient Machine Learning (ML) methods. The toolbox seamlessly allows to
465 combine multiple data representations, algorithm classes, and general purpose
466 tools. This enables both rapid prototyping of data pipelines and extensibility
467 in terms of new algorithms.")
468 (license license:gpl3+)))
469
470 (define-public r-adaptivesparsity
471 (package
472 (name "r-adaptivesparsity")
473 (version "1.4")
474 (source (origin
475 (method url-fetch)
476 (uri (cran-uri "AdaptiveSparsity" version))
477 (sha256
478 (base32
479 "1az7isvalf3kmdiycrfl6s9k9xqk22k1mc6rh8v0jmcz402qyq8z"))))
480 (properties
481 `((upstream-name . "AdaptiveSparsity")))
482 (build-system r-build-system)
483 (arguments
484 `(#:phases
485 (modify-phases %standard-phases
486 (add-after 'unpack 'link-against-armadillo
487 (lambda _
488 (substitute* "src/Makevars"
489 (("PKG_LIBS=" prefix)
490 (string-append prefix "-larmadillo"))))))))
491 (propagated-inputs
492 `(("r-rcpp" ,r-rcpp)
493 ("r-rcpparmadillo" ,r-rcpparmadillo)))
494 (home-page "http://cran.r-project.org/web/packages/AdaptiveSparsity")
495 (synopsis "Adaptive sparsity models")
496 (description
497 "This package implements the Figueiredo machine learning algorithm for
498 adaptive sparsity and the Wong algorithm for adaptively sparse gaussian
499 geometric models.")
500 (license license:lgpl3+)))
501
502 (define-public r-kernlab
503 (package
504 (name "r-kernlab")
505 (version "0.9-25")
506 (source
507 (origin
508 (method url-fetch)
509 (uri (cran-uri "kernlab" version))
510 (sha256
511 (base32
512 "0qnaq9x3j2xc6jrmmd98wc6hkzch487s4p3a9lnc00xvahkhgpmr"))))
513 (build-system r-build-system)
514 (home-page "http://cran.r-project.org/web/packages/kernlab")
515 (synopsis "Kernel-based machine learning tools")
516 (description
517 "This package provides kernel-based machine learning methods for
518 classification, regression, clustering, novelty detection, quantile regression
519 and dimensionality reduction. Among other methods @code{kernlab} includes
520 Support Vector Machines, Spectral Clustering, Kernel PCA, Gaussian Processes
521 and a QP solver.")
522 (license license:gpl2)))
523
524 (define-public dlib
525 (package
526 (name "dlib")
527 (version "19.3")
528 (source (origin
529 (method url-fetch)
530 (uri (string-append
531 "http://dlib.net/files/dlib-" version ".tar.bz2"))
532 (sha256
533 (base32
534 "0gfy83av717qymv53yv7ki6mgh6mdw4xcxxbjk8lrs72f8qvnrcw"))
535 (modules '((guix build utils)))
536 (snippet
537 '(begin
538 ;; Delete ~13MB of bundled dependencies.
539 (delete-file-recursively "dlib/external")
540 (delete-file-recursively "docs/dlib/external")
541 #t))))
542 (build-system cmake-build-system)
543 (arguments
544 `(#:phases
545 (modify-phases %standard-phases
546 (add-after 'unpack 'disable-asserts
547 (lambda _
548 ;; config.h recommends explicitly enabling or disabling asserts
549 ;; when building as a shared library. By default neither is set.
550 (substitute* "dlib/config.h"
551 (("^//#define DLIB_DISABLE_ASSERTS") "#define DLIB_DISABLE_ASSERTS"))
552 #t))
553 (add-after 'disable-asserts 'disable-failing-tests
554 (lambda _
555 ;; One test times out on MIPS, so we need to disable it.
556 ;; Others are flaky on some platforms.
557 (let* ((system ,(or (%current-target-system)
558 (%current-system)))
559 (disabled-tests (cond
560 ((string-prefix? "mips64" system)
561 '("object_detector" ; timeout
562 "data_io"))
563 ((string-prefix? "armhf" system)
564 '("learning_to_track"))
565 ((string-prefix? "i686" system)
566 '("optimization"))
567 (else '()))))
568 (for-each
569 (lambda (test)
570 (substitute* "dlib/test/makefile"
571 (((string-append "SRC \\+= " test "\\.cpp")) "")))
572 disabled-tests)
573 #t)))
574 (replace 'check
575 (lambda _
576 ;; No test target, so we build and run the unit tests here.
577 (let ((test-dir (string-append "../dlib-" ,version "/dlib/test")))
578 (with-directory-excursion test-dir
579 (setenv "CXXFLAGS" "-std=gnu++11")
580 (and (zero? (system* "make" "-j" (number->string (parallel-job-count))))
581 (zero? (system* "./dtest" "--runall")))))))
582 (add-after 'install 'delete-static-library
583 (lambda* (#:key outputs #:allow-other-keys)
584 (delete-file (string-append (assoc-ref outputs "out")
585 "/lib/libdlib.a"))
586 #t)))))
587 (native-inputs
588 `(("pkg-config" ,pkg-config)))
589 (inputs
590 `(("giflib" ,giflib)
591 ("lapack" ,lapack)
592 ("libjpeg" ,libjpeg)
593 ("libpng" ,libpng)
594 ("libx11" ,libx11)
595 ("openblas" ,openblas)
596 ("zlib" ,zlib)))
597 (synopsis
598 "Toolkit for making machine learning and data analysis applications in C++")
599 (description
600 "Dlib is a modern C++ toolkit containing machine learning algorithms and
601 tools. It is used in both industry and academia in a wide range of domains
602 including robotics, embedded devices, mobile phones, and large high performance
603 computing environments.")
604 (home-page "http://dlib.net")
605 (license license:boost1.0)))