dataframe refactoring into core and storage-specific components.
[CommonLispStat.git] / src / data / dataframe-listoflist.lisp
blob47e6b9ead06869a911ee2e58fc50b2d1e48b26d6
1 ;;; -*- mode: lisp -*-
3 ;;; Time-stamp: <2009-09-19 23:17:25 tony>
4 ;;; Creation: <2009-03-12 17:14:56 tony>
5 ;;; File: dataframe-listoflist.lisp
6 ;;; Author: AJ Rossini <blindglobe@gmail.com>
7 ;;; Copyright: (c)2009--, AJ Rossini. BSD, LLGPL, or GPLv2, depending
8 ;;; on how it arrives.
9 ;;; Purpose: Instance of dataframe with the storage done using
10 ;;; LISTOFLIST data storage.
12 ;;; What is this talk of 'release'? Klingons do not make software
13 ;;; 'releases'. Our software 'escapes', leaving a bloody trail of
14 ;;; designers and quality assurance people in its wake.
16 ;;; This organization and structure is new to the 21st Century
17 ;;; version.. Think, "21st Century Schizoid Man".
19 ;;; DATAFRAME-LISTOFLIST
20 ;;;
21 ;;; example/implementatin of using lisp-matrix datastructures for
22 ;;; dataframe storage.
24 (defclass dataframe-listoflist (dataframe-like)
25 ((store :initform nil
26 :initarg :storage
27 :type list
28 :accessor dataset
29 :documentation "Data storage: typed as matrix-like
30 (numerical only)."))
31 (:documentation "example implementation of dataframe-like using storage
32 based on lisp-matrix structures."))
34 (defmethod nrows ((df dataframe-listoflist))
35 "specializes on inheritance from listoflist in lisp-matrix."
36 (length (dataset df)))
38 (defmethod ncols ((df dataframe-listoflist))
39 "specializes on inheritance from matrix-like in lisp-matrix."
40 (length (elt (dataset df) 0)))
42 (defmethod xref ((df dataframe-listoflist) &rest subscripts)
43 "Returns a scalar in array, in the same vein as aref, mref, vref, etc.
44 idx1/2 is row/col or case/var."
45 (elt (elt (dataset df) (elt subscripts 0)) (elt subscripts 1))) ;; ??
47 (defmethod (setf xref) (value (df dataframe-listoflist) &rest subscripts)
48 "Sets a value for df-ml."
49 ;; NEED TO CHECK TYPE!
50 ;; (check-type val (elt (vartype df) index2))
51 (setf (elt (elt (dataset df) (elt subscripts 1)) (elt subscripts 0)) value))
53 ;;;;;; IMPLEMENTATION INDEPENDENT FUNCTIONS AND METHODS
54 ;;;;;; (use only xref, nrows, ncols and similar dataframe-like
55 ;;;;;; components as core).
57 (defun xref-var (df index return-type)
58 "Returns the data in a single variable as type.
59 type = sequence, vector, vector-like (if valid numeric type) or dataframe."
60 (ecase return-type
61 (('list)
62 (map 'list
63 #'(lambda (x) (xref df index x))
64 (gen-seq (nth 2 (array-dimensions (dataset df))))))
65 (('vector) t)
66 (:vector-like t)
67 (:matrix-like t)
68 (:dataframe t)))
70 (defun xref-case (df index return-type)
71 "Returns row as sequence."
72 (ecase return-type
73 (:list
74 (map 'list
75 #'(lambda (x) (xref df x index))
76 (gen-seq (nth 1 (array-dimensions (dataset df))))))
77 (:vector t)
78 (:vector-like t)
79 (:matrix-like t)
80 (:dataframe t)))
82 ;; FIXME
83 (defun xref-2indexlist (df indexlist1 indexlist2 &key (return-type :array))
84 "return an array, row X col dims. FIXME TESTME"
85 (case return-type
86 (:array
87 (let ((my-pre-array (list)))
88 (dolist (x indexlist1)
89 (dolist (y indexlist2)
90 (append my-pre-array (xref df x y))))
91 (make-array (list (length indexlist1)
92 (length indexlist2))
93 :initial-contents my-pre-array)))
94 (:dataframe
95 (make-instance 'dataframe-array
96 :storage (make-array
97 (list (length indexlist1)
98 (length indexlist2))
99 :initial-contents (dataset df))
100 ;; ensure copy for this and following
101 :doc (doc-string df)
102 ;; the following 2 need to be subseted based on
103 ;; the values of indexlist1 and indexlist2
104 :case-labels (case-labels df)
105 :var-labels (var-labels df)))))