indexed.lisp 3.85 KB
Newer Older
Liam M. Healy's avatar
Liam M. Healy committed
1
2
;; Read an indexed set of data
;; Liam Healy 2011-12-30 10:10:58EST indexed.lisp
Liam M. Healy's avatar
Liam M. Healy committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
;; Time-stamp: <2013-12-25 19:37:12EST indexed.lisp>
;;
;; Copyright 2011, 2012, 2013 Liam M. Healy
;; Distributed under the terms of the GNU General Public License
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
Liam M. Healy's avatar
Liam M. Healy committed
20
21
22

(in-package :grid)

23
24
(export 'read-indexed-data)

Liam M. Healy's avatar
Liam M. Healy committed
25
26
27
28
29
30
31
32
;;; Data is presumed to be of the form: index-parameter-0 .... index-parameter-N datum,
;;; where index-parameter-i are integers related to the indices, or the indices themselves.
;;; 381   8     48      4.830

;;; Possible future improvements:
;;;  - accept make-simple-grid keyword arguments
;;;  - accept an existing grid, and fill it, rather than always creating a new one
;;;  - do not require maximum-index-parameters (hard unless input is read twice or saved when read)
Liam M. Healy's avatar
Liam M. Healy committed
33
;;;  - maybe make the input (file, stream, string) a kind of grid without random access, unless stream can be rewound; then you could just copy the grid you wanted.
Liam M. Healy's avatar
Liam M. Healy committed
34

35
36
37
(defun read-indexed-data
    (input maximum-index-parameters &rest args &key index-algorithm (default-value 0) (initial-element 0))
  "Read the stream of indexed data; each line contains one record.  The last column is the datum and the preceeding columns are either the indices, or inputs to index-algorithm, a function that should return the indices.  The grid is sized from maximum-indices, which are index parameters if index-algorithm is non-nil.  If a value read is not of the appropriate type (grid:*default-element-type*), then default-value is used instead, which should be of the appropriate type."
Liam M. Healy's avatar
Liam M. Healy committed
38
39
40
41
42
43
44
  (etypecase input
    (stream
     (flet ((mapip (ip)
	      (if index-algorithm
		  (funcall index-algorithm ip)
		  ip)))
       (let* ((dimensions (mapcar '1+ (mapip maximum-index-parameters)))
45
46
	      (grid (make-simple-grid :dimensions dimensions :initial-element initial-element))
	      (defval (coerce-value default-value (grid:element-type grid))))
Liam M. Healy's avatar
Liam M. Healy committed
47
	 (iter:iter (iter:for line :in-stream input :using #'read-line)
48
49
50
51
52
53
54
55
56
57
58
59
60
	   (let* ((ip-val
		    (with-input-from-string (lstr line)
		      (iter:iter (iter:for ip :in-stream lstr :using #'read)
			(iter:collecting ip))))
		  (ip-nocheck (butlast ip-val))
		  (indices (when (every #'<= ip-nocheck maximum-index-parameters)
			     (mapip ip-nocheck))))
	     (when indices
	       (setf (apply #'aref grid indices)
		     (let ((val (alexandria:lastcar ip-val)))
		       (if (typep val (grid:element-type grid))
			   val
			   defval))))))
Liam M. Healy's avatar
Liam M. Healy committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
	 grid)))
    (pathname
     (with-open-file (stream input)
       (apply 'read-indexed-data stream maximum-index-parameters args)))
    (string
     (with-input-from-string (stream input)
       (apply 'read-indexed-data stream maximum-index-parameters args)))))

#|
(defparameter *sample-indexed-data*
  "0     0     9.895
1     0     8.846
2     0     9.863
3     0     9.344
4     0     5.194
5     0     4.885
6     0    10.664
7     0     9.345
8     0     2.458
9     0     6.505
0     1     9.547
1     1     9.434
2     1     8.042
3     1     9.763
4     1     4.995
5     1     7.230
6     1    10.135
7     1     8.985
8     1     2.780
9     1     6.986")

(read-indexed-data *sample-indexed-data* '(9 1))
(read-indexed-data
 *sample-indexed-data*
 '(9 1)
 :index-algorithm
 (lambda (args) (list (first args) (* 2 (second args)))))
|#