factoring out the make-dataframe2 methods into relevant files. We don-t know about...
[CommonLispStat.git] / src / data / dataframe-matrixlike.lisp
blobd4fd36b971e9d122d814828e3e58741493d7a1a5
1 ;;; -*- mode: lisp -*-
3 ;;; Time-stamp: <2012-10-09 03:21:09 tony>
4 ;;; Creation: <2009-03-12 17:14:56 tony>
5 ;;; File: dataframe-matrixlike.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: Uses the lisp-matrix dataframe instance for storage.
10 ;;; Useful if we only have numerical data and no missing
11 ;;; data, strings, or categorical stuff...
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
18 ;;; version.. Think, "21st Century Schizoid Man".
20 (in-package :cls-dataframe)
22 ;;; DATAFRAME-MATRIXLIKE
23 ;;;
24 ;;; example/implementatin of using lisp-matrix datastructures for
25 ;;; dataframe storage.
28 (defclass dataframe-matrixlike (dataframe-like)
29 ((store :initform nil
30 :initarg :storage
31 :type matrix-like
32 :accessor dataset
33 :documentation "Data storage: typed as matrix-like
34 (numerical only)."))
35 (:documentation "example implementation of dataframe-like using storage
36 based on lisp-matrix structures."))
38 (defmethod make-dataframe2 ((data dataframe-matrixlike)
39 &key vartypes varlabels caselabels doc)
41 (check-dataframe-params data vartypes varlabels caselabels doc)
42 (build-dataframe 'dataframe-matrixlike))
45 (defmethod nrows ((df dataframe-matrixlike))
46 "specializes on inheritance from matrix-like in lisp-matrix."
47 (matrix-dimension (dataset df) 0))
49 (defmethod ncols ((df dataframe-matrixlike))
50 "specializes on inheritance from matrix-like in lisp-matrix."
51 (matrix-dimension (dataset df) 1))
53 ;;; *** FIXME: change mref to xref when we establish lisp-matrix
54 ;;; change to use xarray access facility. Need to dummy-proof the
55 ;;; following.
56 (defmethod xref ((df dataframe-matrixlike) &rest subscripts)
57 "Returns a scalar in array, in the same vein as aref, mref, vref, etc.
58 idx1/2 is row/col or case/var."
59 (mref (dataset df) (elt subscripts 0) (elt subscripts 1)))
61 (defmethod (setf xref) (value (df dataframe-matrixlike) &rest subscripts)
62 "Sets a value for df-ml."
63 ;; NEED TO CHECK TYPE!
64 ;; (check-type val (elt (vartype df) index2))
65 (setf (mref (dataset df) (elt subscripts 0) (elt subscripts 1)) value))
68 ;;; matrix-like <-> listoflist
69 ;; See XARRAY's listoflist example for more details and support
70 ;; functions (migrated from here to there).
71 (defun listoflist->matrix-like (lol &key
72 (orientation :row-major)
73 (coerce-to 'double-float))
74 "From a listoflists structure of numbers, return a matrix-like.
76 FIXME: need to verify that the listoflists is a valid structure (same
77 size rows, typing if required, etc.
79 FIXME: need to grep special variables to make the right kind of
80 matrix-like.
82 <example>
83 (defparameter *lol-ml-test*
84 (list (list 1d0 1d0 2.1d0)
85 (list 2d0 2d0 1.1d0)))
86 (length *lol-ml-test*)
87 (length (elt *lol-ml-test* 0))
89 (defparameter *lol-ml-result* (listoflist->matrix-like *lol-ml-test*))
90 (matrix-dimensions *lol-ml-result*)
91 </example>"
92 (declare (ignorable coerce-to))
93 (let ((n (length lol))
94 (p (length (elt lol 0))))
95 (let ((result (make-matrix n p :initial-element 0d0)))
96 (dotimes (i n)
97 (dotimes (j p)
98 (if (equal orientation :row-major)
99 (setf (mref result i j) (coerce (elt (elt lol i) j) coerce-to))
100 (setf (mref result i j) (coerce (elt (elt lol j) i) coerce-to)))))
101 result)))