factoring out the make-dataframe2 methods into relevant files. We don-t know about...
[CommonLispStat.git] / src / data / dataframe-listoflist.lisp
blobb2e5ae7074a146989230fe216c9febf9e13c34a2
1 ;;; -*- mode: lisp -*-
3 ;;; Time-stamp: <2012-10-09 03:18:49 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--2012, 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 (in-package :cls-dataframe)
21 ;;; DATAFRAME-LISTOFLIST
22 ;;;
23 ;;; example/implementatin of using listoflist datastructures for
24 ;;; dataframe storage.
26 (defclass dataframe-listoflist (dataframe-like)
27 ((store :initform nil
28 :initarg :storage
29 :type list
30 :accessor dataset
31 :documentation "Data storage: typed as listoflist."))
32 (:documentation "example implementation of dataframe-like objects
33 using list-of-list data storage."))
36 (defmethod make-dataframe2 ((data dataframe-listoflist)
37 &key vartypes varlabels caselabels doc
38 ;; (vartypes sequence) (varlabels sequence) (caselabels sequence) (doc string)
40 (check-dataframe-params data vartypes varlabels caselabels doc)
41 (build-dataframe 'dataframe-listoflist))
44 (defmethod nrows ((df dataframe-listoflist))
45 "specializes on inheritance from listoflist in lisp-matrix."
46 (length (dataset df)))
49 (defmethod ncols ((df dataframe-listoflist))
50 "specializes on inheritance from listoflist. This approach assumes
51 that the list of list is in a coherent form, that is that it maps
52 naturally to a rectangular array."
53 (length (elt (dataset df) 0)))
55 (defmethod xref ((df dataframe-listoflist) &rest subscripts)
56 "Returns a scalar in array, in the same vein as aref, mref, vref,
57 etc. idx1/2 is row/col or case/var."
58 (elt (elt (dataset df) (elt subscripts 0)) (elt subscripts 1))) ;; ??
60 (defmethod (setf xref) (value (df dataframe-listoflist) &rest subscripts)
61 "Sets a value for df-ml."
62 ;; NEED TO CHECK TYPE!
63 ;; (check-type val (elt (vartype df) index2))
64 (setf (elt (elt (dataset df) (elt subscripts 1)) (elt subscripts 0)) value))
66 ;;;;;; IMPLEMENTATION INDEPENDENT FUNCTIONS AND METHODS
67 ;;;;;; (use only xref, nrows, ncols and similar dataframe-like
68 ;;;;;; components as core).
70 (defun xref-var (df index return-type)
71 "Returns the data in a single variable as type.
72 type = sequence, vector, vector-like (if valid numeric type) or dataframe."
73 (ecase return-type
74 (('list)
75 (map 'list
76 #'(lambda (x) (xref df index x))
77 (gen-seq (nth 2 (array-dimensions (dataset df))))))
78 (('vector) t)
79 (:vector-like t)
80 (:matrix-like t)
81 (:dataframe t)))
83 (defun xref-case (df index return-type)
84 "Returns row as sequence."
85 (ecase return-type
86 (:list
87 (map 'list
88 #'(lambda (x) (xref df x index))
89 (gen-seq (nth 1 (array-dimensions (dataset df))))))
90 (:vector t)
91 (:vector-like t)
92 (:matrix-like t)
93 (:dataframe t)))
95 ;; FIXME
96 (defun xref-2indexlist (df indexlist1 indexlist2 &key (return-type :array))
97 "return an array, row X col dims. FIXME TESTME"
98 (case return-type
99 (:array
100 (let ((my-pre-array (list)))
101 (dolist (x indexlist1)
102 (dolist (y indexlist2)
103 (append my-pre-array (xref df x y))))
104 (make-array (list (length indexlist1)
105 (length indexlist2))
106 :initial-contents my-pre-array)))
107 (:dataframe
108 (make-instance 'dataframe-array
109 :storage (make-array
110 (list (length indexlist1)
111 (length indexlist2))
112 :initial-contents (dataset df))
113 ;; ensure copy for this and following
114 :doc (doc-string df)
115 ;; the following 2 need to be subseted based on
116 ;; the values of indexlist1 and indexlist2
117 :case-labels (case-labels df)
118 :var-labels (var-labels df)))))