2 ;;; Copyright (c) 2008, by A.J. Rossini <blindglobe@gmail.com>
3 ;;; See COPYRIGHT file for any additional restrictions (BSD license).
4 ;;; Since 1991, ANSI was finally finished. Edited for ANSI Common Lisp.
6 ;;; Time-stamp: <2009-12-06 10:08:34 tony>
7 ;;; Creation: <2008-09-03 08:10:00 tony>
9 ;;; Author: AJ Rossini <blindglobe@gmail.com>
10 ;;; Copyright: (c)2007--2009, AJ Rossini. GPLv2
11 ;;; Purpose: base structures for importing data into CLS
13 ;;; What is this talk of 'release'? Klingons do not make software
14 ;;; 'releases'. Our software 'escapes', leaving a bloody trail of
15 ;;; designers and quality assurance people in its wake.
17 (in-package :lisp-stat-data
)
21 ;; We can read 2 types of data -- those which are non-lisp-native
22 ;; data, and those which are lisp-native (lisp-enabled, an extension
23 ;; of lisp-serialized, i.e. data as program as data thingy's).
25 ;; of the non-native, there could be raw sources (ascii file formats),
26 ;; xml sources (xml -> lisp, possible with some preprocessing.
28 ;;; Reading from DSV files:
30 ;;; consider either the cybertyggyr-dsv package, or the rsm.string
31 ;;; package. The latter seems to actually work a bit at what we need
32 ;;; to acccomplish, but the former is a challenge to get right when we
33 ;;; need to think abut what it is that we need to get done. The
34 ;;; latter is also better licensed. i.e. BSD-style.
37 (defparameter *lisp-stat-data-external-source-formats
*
38 '(csv tsv xml
;; ex of text-based (UTF, ASCII, or similar) formats
39 sql
;; ex of RDBMS call
40 fcs affy
)) ;; ex of binary formats
42 (defparameter *lisp-stat-data-import-referencing-type
*
43 '(lisp-data-structure reference lisp-function
))
45 (defgeneric data-import
(source source-format referencing-type
)
46 (:documentation
"read data from stream srce, in format srce-frmt;
47 return a reftype, which could be a
48 lisp-data-structure, a reference to such, or a lisp
49 function which can be evaluated to generate
52 (defgeneric data-export
(data target-format target-referencing-type
)
53 (:documentation
"write data from stream srce, in format srce-frmt;
54 return a reftype, which could be a
55 lisp-data-structure, a reference to such, or a lisp
56 function which can be evaluated to generate
62 ;;; Potentially useful functions
64 ;; the following belongs here if we are working externally, but might
65 ;; belong with data if we are working internlly
67 ;; (defmacro with-data (body)
68 ;; "Stream-handling, maintaining I/O through object typing.")
72 ;;; Related to data file reading
75 (defun count-file-columns (fname)
77 Returns the number of lisp items on the first nonblank line of file FNAME."
78 (with-open-file (f fname
)
80 (let ((line (do ((line (read-line f
) (read-line f
)))
81 ((or (null line
) (< 0 (length line
))) line
))))
83 (with-input-from-string (s line
)
84 (do ((n 0 (+ n
1)) (eof (gensym)))
85 ((eq eof
(read s nil eof
)) n
))))))))
87 (if (not (fboundp 'open-file-dialog
))
89 (defun open-file-dialog () ;; why?(&optional set)
90 (get-string-dialog "Enter a data file name:"))
92 (defun open-file-dialog () ;; why? (&optional set)
93 (error "You must provide a file name explicitly")))
95 (defun read-data-file (&optional
(file (open-file-dialog)))
97 Returns a list of all lisp objects in FILE. FILE can be a string or a symbol,
98 in which case the symbol'f print name is used."
100 (let ((eof (gensym)))
101 (with-open-file (f file
)
103 (do* ((r (read f nil eof
) (read f nil eof
))
107 (setf (cdr tail
) (list r
))))))))
109 ;;; New definition to avoid stack size limit in apply
111 (defun read-data-columns (&optional
(file (open-file-dialog))
113 (count-file-columns file
))))
114 "Args: (&optional file cols)
115 Reads the data in FILE as COLS columns and returns a list of lists representing the columns."
117 (transpose (split-list (read-data-file file
) cols
))))
120 ;;; FIXME:AJR: ALL THE FOLLOWING NEED TO BE SOLVED BY PLATFORM-INDEP PATHNAME WORK!
121 ;;; FIXME:AJR: use either string or pathname.
123 (defun path-string-to-path (p s
)
124 (pathname (concatenate 'string
(namestring p
) s
)))
126 (defun load-data (file)
127 "Args: (file) as string
128 Read in data file from the data examples library."
129 (if (load (path-string-to-path *cls-data-dir
* file
))
131 (load (path-string-to-path *cls-data-dir
* file
))))
133 (defun load-example (file)
134 "Args: (file) as string
135 Read in lisp example file from the examples library."
136 (if (load (path-string-to-path *cls-examples-dir
* file
))
138 (load (path-string-to-path *cls-examples-dir
* file
))))
141 ;;; Saving Variables and Functions
144 (defun savevar (vars file
)
145 "Args: (vars file-name-root)
146 VARS is a symbol or a list of symbols. FILE-NAME-ROOT is a string (or a symbol
147 whose print name is used) not endinf in .lsp. The VARS and their current values
148 are written to the file FILE-NAME-ROOT.lsp in a form suitable for use with the
150 (with-open-file (f (concatenate 'string
(namestring file
) ".lsp")
152 (let ((vars (if (consp vars
) vars
(list vars
))))
154 (let ((v (symbol-value x
)))
156 (format f
"(def ~s ~s)~%" x
(send v
:save
))
157 (format f
"(def ~s '~s)~%" x v
)))))
158 (mapcar #'save-one vars
))
163 ;;; General modification approaches.
165 (defgeneric importData
(source featureList
)
166 (:documentation
"command to get data into CLS. Specific methods
167 will need to handle pathnames, internal data structures, and
168 external services such as DBMS's. We would like to be able to do
170 (importData MyPathName '(:formattype 'csvString))
171 (importData '(sqlConnection :server host.domain.net :port 666)
176 (defun pathname-example (name)
177 (let ((my-path (parse-namestring name
)))
178 (values (pathname-name my-path
:case
:common
)
179 (pathname-name my-path
:case
:local
))))
181 (defvar sourceTypes
(list 'csv
'lisp
'tsv
'special
)
182 "list of possible symbols.
184 Thsees are used to specify source formats that might be supported for
185 input. CSV and TSV are standard, LISP refers to forms, and SPECIAL
186 refers to a FUNCTION which parses as appropriately.")
190 (defmethod importData ((fileHandle pathname
)
191 (fmt list
)) ;sourceTypes))
192 "File-based input for data.
194 (importData (parse-namestring 'path/to/file')
197 (importData myPathName (list :format 'lisp))
199 (let* ((fmtType (getf fmt
:format
))
200 (newData (getDataAsLists fileHandle fmtType
)))
205 ('special
(let ((parserFcn (getf fmt
:special-parser
)))))
206 (:default
(error "no standard default importData format")))))
209 (defmethod importData ((ds array
) (fmt list
))
210 "mapping arrays into CLS data.")
213 (defmethod importData ((dsSpec DBMSandSQLextract
)
215 "mapping DBMS into CLS data.")
219 ;;(defmacro with-dataframe (env &rest progn)
220 ;; "Compute using variable names with with.data.frame type semantics.")