2 ;;; Copyright (c) 2005--2007, 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.
7 ;;; Author: AJ Rossini <blindglobe@gmail.com>
8 ;;; Copyright: (c)2007, AJ Rossini. BSD, LLGPL, or GPLv2, depending on how it arrives.
9 ;;; Purpose: data package for lispstat
10 ;;; Time-stamp: <2006-05-19 12:33:41 rossini>
11 ;;; Creation: <2006-05-17 21:34:07 rossini>
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 ;;; This organization and structure is new to the 21st Century
20 ;;; conside that dataa has 3 genotypic chracteristrics. The first
21 ;;; would be form -- scalar, vector, array. second would be
22 ;;; datarep type. in particular integer, real, string, symbol. The last
23 ;;; would be statistical type. augmenting datarep type with use in a
24 ;;; statistical context, i.e. that would include nominal, ordinal,
25 ;;; integer, continous, interval (orderable subtypes)
29 (defpackage :lisp-stat-data
30 (:documentation
"Data I/O, management, other data technologies.")
35 :lisp-stat-object-system
37 :lisp-stat-compound-data
40 (:shadowing-import-from
:lisp-stat-object-system
41 slot-value call-method call-next-method
)
42 (:export open-file-dialog read-data-file read-data-columns load-data
43 load-example
*variables
* *ask-on-redefine
*
44 def variables savevar undef
))
46 (in-package :lisp-stat-data
)
48 ;; (deftype dataformtype (list scalar vector sequence list array relation))
49 ;; (deftype datareptype (list integer rational real complex string symbol=
50 ;; (deftype stattzpe (list
55 ;;; The purpose of this package is to manage data which will be
56 ;;; processed by LispStat. In particular, it willbe importnat to
57 ;;; register variables, datasets, relational structures, and other
58 ;;; objects which could be the target for statistical modeling and
61 (defvar *lisp-stat-data-table
* (make-hash-table)
62 "Marks up the data the could be used by.")
64 (defvar *lisp-stat-data-count
* 0
65 "number of items currently recorded.")
70 ;;; Data types are the representation of data from a computer-science
71 ;;; perspective, i.e. what it is that they contain. These types
72 ;;; include particular forms of compound types (i.e. dataframe,
73 ;;; relationdata are compounds of arrays of different types where the
74 ;;; difference is row-wise, while array is a compound of elements of
78 ;; (defun equidimensional (a)
79 ;; (or (< (array-rank a) 2)
80 ;; (apply #'= (array-dimensions a)))) => EQUIDIMENSIONAL
81 ;; (deftype square-matrix (&optional type size)
82 ;; `(and (array ,type (,size ,size))
83 ;; (satisfies equidimensional))) => SQUARE-MATRIX
85 (deftype dt-scalar
(&optional type
)
86 `(or integer double complex
))
88 (deftype dt-array
(&optional type
)
89 `(satisfies array-of-equal-type
))
91 (deftype dt-dataframe
()
92 `(satisfies array-of-equal-type-within-column
))
94 (deftype dt-relationaldata
()
95 `(satisfies (foreach unit in relationalUnit
96 (typep unit
'dt-dataframe
))))
101 ;;; Statistical Variable Classes
104 (deftype sv-nominal
(&optional length
)
107 (deftype sv-ordinal
(ordering &optional length
)
110 (deftype sv-categorical
()
111 `(satisfies (or sv-nominal sv-ordinal
)))
112 ;;(deftype sv-integer )
114 ;;(deftype sv-rational )
115 ;;(deftype sv-complex )
116 ;;(deftype sv-continuous (or 'sv-integer 'sv-real 'sv-rational 'sv-complex))
123 ;; We can read 2 types of data -- those which are pure data, and those
124 ;; which are imprue (lisp-enables).
126 (defparameter *lisp-stat-data-formats
*
129 ;; (defgeneric data-read (srce frmt)
130 ;; "read data from stream srce, in format frmt.")
132 ;; (defgeneric data-write (srce frmt)
133 ;; "read data from stream srce, in format frmt.")
135 ;; (defmacro with-data (body)
136 ;; "Stream-handling, maintaining I/O through object typing.")
138 ;; design-wise should these be replaced with a "with-data" form?
147 ;; the goal is to have 2 operations which can be used to create new
148 ;; data formats out of old ones.
150 ;; (defgeneric data-subset (ds description)
151 ;; "Take a dataset and make it smaller.")
153 ;; (defgeneric data-relate (ds description)
154 ;; "Take 2 or more datasets, and grow them into a bigger one through
155 ;; relating them (i.e. merge is one example).")
157 ;;; Data tools from "statistics.lsp"
160 ;;;; Data File Reading
163 (defun count-file-columns (fname)
165 Returns the number of lisp items on the first nonblank line of file FNAME."
166 (with-open-file (f fname
)
168 (let ((line (do ((line (read-line f
) (read-line f
)))
169 ((or (null line
) (< 0 (length line
))) line
))))
171 (with-input-from-string (s line
)
172 (do ((n 0 (+ n
1)) (eof (gensym)))
173 ((eq eof
(read s nil eof
)) n
))))))))
175 #+xlisp
(defvar *xlisptable
* *readtable
*)
177 (if (not (fboundp 'open-file-dialog
))
179 (defun open-file-dialog () ;; why?(&optional set)
180 (get-string-dialog "Enter a data file name:"))
182 (defun open-file-dialog () ;; why? (&optional set)
183 (error "You must provide a file name explicitly")))
185 (defun read-data-file (&optional
(file (open-file-dialog t
)))
187 Returns a list of all lisp objects in FILE. FILE can be a string or a symbol,
188 in which case the symbol'f print name is used."
190 (let ((eof (gensym)))
191 (with-open-file (f file
)
193 (do* ((r (read f nil eof
) (read f nil eof
))
197 (setf (cdr tail
) (list r
))))))))
199 ;;; New definition to avoid stack size limit in apply
200 (defun read-data-columns (&optional
(file (open-file-dialog t
))
202 (count-file-columns file
))))
203 "Args: (&optional file cols)
204 Reads the data in FILE as COLS columns and returns a list of lists representing the columns."
206 (transpose (split-list (read-data-file file
) cols
))))
209 ;;; FIXME:AJR: ALL THE FOLLOWING NEED TO BE SOLVED BY PLATFORM-INDEP PATHNAME WORK!
210 ;;; FIXME:AJR: use either string or pathname.
212 (defun path-string-to-path (p s
)
213 (pathname (concatenate 'string
(namestring p
) s
)))
215 (defun load-data (file)
216 "Args: (file) as string
217 Read in data file from the data examples library."
218 (if (load (path-string-to-path *lispstat-data-dir
* file
))
220 (load (path-string-to-path *lispstat-examples-dir
* file
))))
222 (defun load-example (file)
223 "Args: (file) as string
224 Read in lisp example file from the examples library."
225 (if (load (path-string-to-path *lispstat-examples-dir
* file
))
227 (load (path-string-to-path *lispstat-data-dir
* file
))))
230 ;;;; Listing and Saving Variables and Functions
233 (defvar *variables
* nil
)
234 (defvar *ask-on-redefine
* nil
)
236 (defmacro def
(symbol value
)
237 "Syntax: (def var form)
238 VAR is not evaluated and must be a symbol. Assigns the value of FORM to
239 VAR and adds VAR to the list *VARIABLES* of def'ed variables. Returns VAR.
240 If VAR is already bound and the global variable *ASK-ON-REDEFINE*
241 is not nil then you are asked if you want to redefine the variable."
242 `(unless (and *ask-on-redefine
*
244 (not (y-or-n-p "Variable has a value. Redefine?")))
245 (if (boundp ',symbol
)
246 (setf ,symbol
,value
)
247 (defvar ,symbol
,value
))
248 (pushnew ',symbol
*variables
*)
251 (defun variables-list ()
252 (mapcar #'intern
(sort-data (mapcar #'string
*variables
*))))
256 Returns a list of the names of all def'ed variables to STREAM"
258 (mapcar #'intern
(sort-data (mapcar #'string
*variables
*)))))
260 (defun savevar (vars file
)
261 "Args: (vars file-name-root)
262 VARS is a symbol or a list of symbols. FILE-NAME-ROOT is a string (or a symbol
263 whose print name is used) not endinf in .lsp. The VARS and their current values
264 are written to the file FILE-NAME-ROOT.lsp in a form suitable for use with the
266 (with-open-file (f (concatenate 'string
(namestring file
) ".lsp")
268 (let ((vars (if (consp vars
) vars
(list vars
))))
270 (let ((v (symbol-value x
)))
272 (format f
"(def ~s ~s)~%" x
(send v
:save
))
273 (format f
"(def ~s '~s)~%" x v
)))))
274 (mapcar #'save-one vars
))
279 If V is the symbol of a defined variable the variable it is unbound and
280 removed from the list of defined variables. If V is a list of variable
281 names each is unbound and removed. Returns V."
282 (dolist (s (if (listp v
) v
(list v
)))
283 (when (member s
*variables
*)
284 (setq *variables
* (delete s
*variables
*))