3 ;;; File: data-clos.lisp
4 ;;; Author: AJ Rossini <blindglobe@gmail.com>
5 ;;; Copyright: (c)2008, AJ Rossini. BSD, LLGPL, or GPLv2, depending
7 ;;; Purpose: data package for lispstat
8 ;;; Time-stamp: <2008-09-02 18:40:53 tony>
9 ;;; Creation: <2008-03-12 17:18:42 user>
11 ;;; What is this talk of 'release'? Klingons do not make software
12 ;;; 'releases'. Our software 'escapes', leaving a bloody trail of
13 ;;; designers and quality assurance people in its wake.
15 ;;; This organization and structure is new to the 21st Century
20 ;;; redoing data structures in a CLOS based framework.
22 ;;; No real basis for work, there is a bit of new-ness and R-ness to
23 ;;; this work. In particular, the notion of relation is key and
24 ;;; integral to the analysis. Tables are related and matched
25 ;;; vectors,for example. "column" vectors are related observations
26 ;;; (by measure/recording) while "row" vectors are related readings
30 ;;; Relational structure -- can we capture a completely unnormalized
31 ;;; data strucutre to propose possible modeling approaches, and
32 ;;; propose appropriate models and inferential strategies?
35 ;; verb-driven schema for data collection. Should encode independence
36 ;; or lack of when possible.
40 :tables
(list (list t1
)
44 :stat-relation
'(t1 (:nest-within t2
) (:nest-within t3
))
51 (defpackage :lisp-stat-data-clos
54 (:export get-variable-matrix get-variable-vector
56 ;; generic container class for data -- if small enough
57 ;; could be value, otherwise might be reference.
62 statistical-dataset
;; primary class for working.
64 modifyData
;; metadata mods
65 importData
;; get it in
66 reshapeData
;; data mods
68 consistent-statistical-dataset-p
69 varNames caseNames
;; metadata explicit modifiers
72 ;; and later, we remove the following, exposing only
73 ;; through the above method.
74 extract-1 extract-row extract-col extract-idx
77 (in-package :lisp-stat-data-clos
)
79 ;; Need to figure out typed vectors. We then map a series of typed
80 ;; vectors over to tables where columns are equal typed. In a sense,
81 ;; this is a relation (1-1) of equal-typed arrays. For the most part,
82 ;; this ends up making the R data.frame into a relational building
83 ;; block (considering 1-1 mappings using row ID as a relation).
84 ;; Is this a worthwhile generalization?
86 (defclass statistical-dataset-metadata
(rdf-type)
92 (defclass statistical-dataset
()
96 :documentation
"Data storage: typed as table, array,
97 relation, or pointer/reference to such.")
98 (documentation-string :initform nil
101 :documentation
"uncomputable information
102 about statistical-dataset
105 ;; the rest of this is metadata. In particular, we should find a
106 ;; more flexible, compact way to store this.
107 (case-labels :initform nil
108 :initarg
:case-labels
109 :accessor case-labels
110 :documentation
"labels used for describing cases (doc
111 metadata), possibly used for merging.")
112 (var-labels :initform nil
115 :documentation
"Variable names."))
116 (:documentation
"Standard Cases by Variables Statistical-Dataset,
117 i.e. an S data.frame."))
120 ;; statistical-dataset is the basic cases by variables framework.
121 ;; Need to embed this within other structures which allow for
122 ;; generalized relations. Goal is to ensure that relations imply and
123 ;; drive the potential for statistical relativeness such as
124 ;; correlation, interference, and similar concepts.
126 ;; Actions on a statistical data structure.
129 (defgeneric consistent-statistical-dataset-p
(ds)
130 (:documentation
"methods to check for consistency."))
132 (defmethod consistent-statistical-dataset-p ((ds statistical-dataset
))
133 "Test that statistical-dataset is internally consistent with metadata.
134 Ensure that dims of stored data are same as case and var labels."
135 (equal (array-dimensions (dataset ds
))
136 (list (length (var-labels ds
))
137 (length (case-labels ds
)))))
141 (defun extract-1 (sds idx1 idx2
)
143 (aref (dataset sds
) idx1 idx2
))
145 (defun extract-1-as-sds (sds idx1 idx2
)
146 "Need a version which returns a dataset."
147 (make-instance 'statistical-dataset
150 :initial-contents
(extract-1 sds idx1 idx2
))
151 ;; ensure copy for this and following
152 :doc
(doc-string sds
)
153 :case-labels
(caseNames sds
)
154 :var-labels
(varNames sds
)))
156 (defun gen-seq (n &optional
(start 1))
157 "There has to be a better way -- I'm sure of it! Always count from 1."
159 (append (gen-seq (- n
1) start
) (list n
))))
168 (defun extract-col (sds index
)
169 "Returns data as sequence."
171 #'(lambda (x) (extract-1 sds index x
))
172 (gen-seq (nth 2 (array-dimensions (dataset sds
))))))
174 (defun extract-col-as-sds (sds index
)
175 "Returns data as SDS, copied."
177 #'(lambda (x) (extract-1 sds index x
))
178 (gen-seq (nth 2 (array-dimensions (dataset sds
))))))
180 (defun extract-row (sds index
)
181 "Returns row as sequence."
183 #'(lambda (x) (extract-1 sds x index
))
184 (gen-seq (nth 1 (array-dimensions (dataset sds
))))))
186 (defun extract-idx (sds idx1Lst idx2Lst
)
187 "return an array, row X col dims. FIXME TESTME"
188 (let ((my-pre-array (list)))
191 (append my-pre-array
(extract-1 sds x y
))))
192 (make-array (list (length idx1Lst
) (length idx2Lst
))
193 :initial-contents my-pre-array
)))
196 (defun extract-idx-sds (sds idx1Lst idx2Lst
)
197 "return a dataset encapsulated version of extract-idx."
198 (make-instance 'statistical-dataset
200 (list (length idx1Lst
) (length idx2Lst
))
201 :initial-contents
(dataset sds
))
202 ;; ensure copy for this and following
203 :doc
(doc-string sds
)
204 :case-labels
(caseNames sds
)
205 :var-labels
(varNames sds
)))
207 (defgeneric extract
(sds whatAndRange
)
208 (:documentation
"data extraction approach"))
210 ;;; Printing methods and support.
212 (defun print-as-row (seq)
213 "Print a sequence formated as a row in a table."
214 (format t
"~{~D~T~}" seq
))
216 ;; (print-as-row (list 1 2 3))
218 (defun print-structure-table (ds)
219 "example of what we want the methods to look like. Should be sort
220 of like a spreadsheet if the storage is a table."
221 (print-as-row (var-labels ds
))
223 (dolist (i (case-labels ds
))
224 (print-as-row (append (list i
)
225 (extract-row (dataset ds
) (incf j
)))))))
228 (defun print-structure-relational (ds)
229 "example of what we want the methods to look like. Should be sort
230 of like a graph of spreadsheets if the storage is a relational
232 (dolist (k (relations ds
))
233 (let ((currentRelationSet (getRelation ds k
)))
234 (print-as-row (var-labels currentRelationSet
))
236 (dolist (i (case-labels currentRelationSet
))
239 (extract-row (dataset currentRelationSet
)
244 ;;; Shaping for computation
246 (defgeneric reshapeData
(dataform into-form as-copy
)
247 (:documentation
"pulling data into a new form"))
249 (defmethod reshapeData ((sds statistical-dataset
) what into-form
))
251 (defmethod reshapeData ((ds array
) (sp list
) copy-p
)
252 "Array via specList specialization: similar to the common R
253 approaches to redistribution.")
255 (defclass data-format
() ())
257 (defun row-order-as-list (ary)
258 "Pull out data in row order into a list."
259 (let ((result (list))
260 (nrows (nth 0 (array-dimensions ary
)))
261 (ncols (nth 1 (array-dimensions ary
))))
264 (nappend result
(aref ary i j
))))))
266 (defun col-order-as-list (ary)
267 "Pull out data in row order into a list."
268 (let ((result (list))
269 (nrows (nth 0 (array-dimensions ary
)))
270 (ncols (nth 1 (array-dimensions ary
))))
273 (nappend result
(aref ary i j
))))))
275 (defun transpose (ary)
277 (make-array (reverse (array-dimensions ary
))
278 :initial-contents
(col-order-as-list ary
)))
281 ;;; verbs vs semantics for dt conversion -- consider the possibily of
282 ;;; how adverbs and verbs relate, where to put which semantically to
283 ;;; allow for general approach.
285 ;;; eg. Kasper's talk on the FUSION collection of parsers.
292 ;;; Need to consider modification APIs
295 ;;; - get/set row names (case names)
296 ;;; - column names (variable names)
298 ;;; - annotation/metadata
299 ;;; - make sure that we do coherency checking in the exported
302 ;;; - reshapeData/reformat/reshapr a reformed version of the dataset (no
303 ;;; additional input).
304 ;;; - either overwriting or not, i.e. with or without copy.
305 ;;; - check consistency of resulting data with metadata and related
306 ;;; data information.
309 ;;; Variable-name handling for Tables. Needs error checking.
313 (defun set-varNames (ds vN
)
314 (if (= (length (var-labels ds
))
316 (setf (var-labels ds
) vN
)
317 (error "wrong size.")))
319 (defsetf varNames set-varNames
)
321 ;;; Case-name handling for Tables. Needs error checking.
322 (defun caseNames (ds)
325 (defun set-caseNames (ds vN
)
326 (if (= (length (case-labels ds
))
328 (setf (case-labels ds
) vN
)
329 (error "wrong size.")))
331 (defsetf caseNames set-caseNames
)
333 ;;; General modification approaches.
335 (defgeneric importData
(source featureList
)
336 (:documentation
"command to get data into CLS. Specific methods
337 will need to handle pathnames, internal data structures, and
338 external services such as DBMS's. We would like to be able to do
340 (importData MyPathName '(:formattype 'csvString))
341 (importData '(sqlConnection :server host.domain.net :port 666)
346 (defun pathname-example (name)
347 (let ((my-path (parse-namestring name
)))
348 (values (pathname-name my-path
:case
:common
)
349 (pathname-name my-path
:case
:local
))))
351 (defvar sourceTypes
(list 'csv
'lisp
'tsv
'special
)
352 "list of possible symbols.
354 Thsees are used to specify source formats that might be supported for
355 input. CSV and TSV are standard, LISP refers to forms, and SPECIAL
356 refers to a FUNCTION which parses as appropriately.")
359 (defmethod importData ((fileHandle pathname
)
360 (fmt list
)) ;sourceTypes))
361 "File-based input for data.
363 (importData (parse-namestring 'path/to/file')
366 (importData myPathName (list :format 'lisp))
368 (let* ((fmtType (getf fmt
:format
))
369 (newData (getDataAsLists fileHandle fmtType
)))
374 ('special
(let ((parserFcn (getf fmt
:special-parser
)))))
375 (:default
(error "no standard default importData format")))))
377 (defmethod importData ((ds array
) (fmt list
))
378 "mapping arrays into CLS data.")
380 (defmethod importData ((dsSpec DBMSandSQLextract
)
382 "mapping DBMS into CLS data.")
386 ;;(defmacro with-dataframe (env &rest progn)
387 ;; "Compute using variable names with with.data.frame type semantics.")