annotate implementations/meredith/import->db.lisp @ 330:2fbff655ba47 tip

Removed cpitch-adj and cents SQL columns
author Jeremy Gow <jeremy.gow@gmail.com>
date Mon, 21 Jan 2013 11:08:11 +0000
parents 3c5bf3f7b7a0
children
rev   line source
j@286 1 (cl:in-package #:amuse-meredith)
j@286 2
j@286 3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
j@286 4 ;; Importing
j@286 5
j@286 6 (defun make-path (base-path file-prefix file-id file-suffix)
j@286 7 (pathname (concatenate 'string
j@286 8 base-path
j@286 9 file-prefix
j@286 10 (princ-to-string file-id)
j@286 11 file-suffix)))
j@286 12
j@303 13 (defun raph-c-file->list-of-lists (path)
j@286 14 (with-open-file (stream path)
j@286 15 (loop
j@286 16 for row-strings = (split-sequence:split-sequence
j@286 17 #\space (read-line stream nil)
j@286 18 :remove-empty-subseqs t)
j@286 19 while row-strings
j@286 20 collect (loop
j@286 21 for value in row-strings
j@286 22 collect (read-from-string value)
j@286 23 into row-objects
j@286 24 finally (return row-objects))
j@286 25 into results
j@286 26 finally (return (cdr results)))))
j@286 27
j@286 28 (defvar *import-file* (pathname "/tmp/raph-c.txt"))
j@286 29
j@286 30 (defun write-to-db-file (composition-id datapoints)
j@286 31 (with-open-file (stream *import-file*
j@286 32 :direction :output
j@286 33 :if-exists :supersede
j@286 34 :external-format :latin1)
j@286 35 (sequence:dosequence (datapoint datapoints)
j@286 36 (format stream
j@286 37 "~S \\N ~{~S ~}~%"
j@286 38 composition-id datapoint)))
j@286 39 t)
j@286 40
j@286 41 (defun import-meredith-composition (event-lists &key description
j@286 42 database)
j@286 43 "Import a composition into the database. A composition is a list of
j@286 44 lists (dataset in the SIA sense), where each internal list (datapoint)
j@286 45 represents an event. All datapoints are the same cardinality. The
j@286 46 description is a string of up to 255 characters."
j@286 47 #.(clsql:locally-enable-sql-reader-syntax)
j@286 48 (clsql:with-transaction
j@286 49 (:database database)
j@286 50 (let (composition-id)
j@286 51 (clsql:insert-records :into "meredith_compositions"
j@286 52 :attributes '([description])
j@286 53 :values (list description)
j@286 54 :database database)
j@286 55 #.(clsql:locally-disable-sql-reader-syntax)
j@286 56 (setf composition-id (clsql-mysql::mysql-insert-id
j@286 57 (clsql-mysql::database-mysql-ptr
j@286 58 database)))
j@286 59 (write-to-db-file composition-id event-lists)
j@286 60 (clsql:execute-command
j@286 61 (concatenate 'string
j@286 62 "LOAD DATA LOCAL INFILE '"
j@286 63 (namestring *import-file*)
j@286 64 "' INTO TABLE "
j@286 65 "meredith_events")
j@286 66 :database database)
j@286 67 (delete-file *import-file*)
j@286 68 (format t "composition added to db, id: ~A~%" composition-id)
j@286 69 (make-meredith-composition-identifier composition-id))))
j@286 70
j@286 71 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
j@286 72 ;;; Import functions for specific datasets.
j@286 73
j@286 74 (defun import-bach-inv+sin (&optional (database *amuse-database*))
j@286 75 "Imports all of Dave's encodings of Bach's Inventions and Sinfonias,
j@303 76 and assigns them all to a unique dataset."
j@286 77 (clsql:with-transaction
j@286 78 (:database database)
j@303 79 (let ((amuse-dataset-identifier (make-new-dataset
j@286 80 "Bach Inventions and Sinfonias"
j@286 81 database)))
j@303 82 (import-bach-inventions database amuse-dataset-identifier)
j@303 83 (import-bach-sinfonias database amuse-dataset-identifier)
j@303 84 amuse-dataset-identifier)))
j@286 85
j@303 86 (defun import-bach-inventions (database &optional amuse-dataset-identifier)
j@286 87 "Imports all of Dave's encodings of Bach's Inventions."
j@286 88 (let ((base-path "/home/jamie/Music/meredith-data/20060301raph-c/")
j@286 89 (file-prefix "bachrasmussinventio0")
j@286 90 (file-suffix "01.raph-c"))
j@286 91 (loop for file-id from 772 to 786
j@286 92 for composition-order from 1
j@286 93 for composition-identifier =
j@286 94 (import-meredith-composition
j@303 95 (raph-c-file->list-of-lists
j@286 96 (make-path base-path file-prefix file-id file-suffix))
j@286 97 :description (concatenate 'string
j@286 98 "Bach Invention No."
j@286 99 (princ-to-string composition-order)
j@286 100 " BWV "
j@286 101 (princ-to-string file-id))
j@286 102 :database database)
j@303 103 do (when amuse-dataset-identifier
j@286 104 (assign-composition-to-dataset
j@303 105 composition-identifier amuse-dataset-identifier database)))))
j@286 106
j@303 107 (defun import-bach-sinfonias (database &optional amuse-dataset-identifier)
j@286 108 "Imports all of Dave's encodings of Bach's Sinfonias."
j@286 109 (let ((base-path "/home/jamie/Music/meredith-data/20060301raph-c/")
j@286 110 (file-prefix "bachrasmusssinfonie0")
j@286 111 (file-suffix "01.raph-c"))
j@286 112 (loop for file-id from 787 to 801
j@286 113 for composition-order from 1
j@286 114 for composition-identifier =
j@286 115 (import-meredith-composition
j@303 116 (raph-c-file->list-of-lists
j@286 117 (make-path base-path file-prefix file-id file-suffix))
j@286 118 :description (concatenate 'string
j@286 119 "Bach Sinfonia No."
j@286 120 (princ-to-string composition-order)
j@286 121 " BWV "
j@286 122 (princ-to-string file-id))
j@286 123 :database database)
j@303 124 do (when amuse-dataset-identifier
j@286 125 (assign-composition-to-dataset
j@303 126 composition-identifier amuse-dataset-identifier database)))))