Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:faceting: maintain one dataset per each layer The issue was anticipated in the readme. Generally we need to maintain a mapping to dataset for each layer in each facet, because they may be different.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | master | trunk
Files: files | file ages | folders
SHA3-256: f99a9f88252edd16f5a1e4b1d464e4928e6af9b9efadba708b8d49af6eb9fe0c
User & Date: jack 2025-04-23 19:42:07
Context
2025-04-23
19:49
readme: add entries to news section check-in: 253b1ad517 user: jack tags: master, trunk
19:42
faceting: maintain one dataset per each layer check-in: f99a9f8825 user: jack tags: master, trunk
2025-04-22
19:14
add data generators check-in: 5c105a6ad6 user: jack tags: master, trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to Code/dataset-and-aesthetics.lisp.

︙︙
28
29
30
31
32
33
34








































35
36
37
38
39
40
41
  (:method ((dataset list) headers column-name)
    (position column-name headers :test #'equal)))

(defgeneric point-value (dataset point index)
  (:method ((dataset list) point index)
    (elt point index)))










































;;; Creates a hash table of datasets grouped by a sequence of columns.
(defun group-by (columns dataset)
  (let* ((head (dataset-head dataset))
         (result (make-hash-table :test #'equal))
         (index* (loop for col in columns
                       collect (point-index dataset head col))))







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  (:method ((dataset list) headers column-name)
    (position column-name headers :test #'equal)))

(defgeneric point-value (dataset point index)
  (:method ((dataset list) point index)
    (elt point index)))


;;; Abstraction \o/

(define-class <dataset> ()
  (dataset-head dataset-body))

(defmethod map-dataset-header (fun (dataset <dataset>))
  (map nil fun (dataset-head dataset)))

(defmethod map-dataset-points (fun (dataset <dataset>))
  (map nil fun (dataset-body dataset)))

(defmethod point-index ((self <dataset>) headers column-name)
  (position column-name headers :test #'equal))

(defmethod point-value ((self <dataset>) point index)
  (elt point index))

(defun fill-dataset (self generator n)
  (setf (dataset-body self)
        (loop repeat n collect (funcall generator))))

(defun push-dataset (self generator n)
  (setf (dataset-body self)
        (append (dataset-body self)
                (loop repeat n collect (funcall generator)))))

(define-class <datagen> (<dataset>)
  (generator))

(defmethod initialize-instance :after ((self <datagen>) &key generator (n 0))
  (push-dataset self generator n))

#+ (or)
(dataset-body
 (<datagen> :dataset-head '("x" "y")
            :dataset-body '((1 2) (3 4))
            :generator (constantly '(0 0))
            :n 4))


;;; Creates a hash table of datasets grouped by a sequence of columns.
(defun group-by (columns dataset)
  (let* ((head (dataset-head dataset))
         (result (make-hash-table :test #'equal))
         (index* (loop for col in columns
                       collect (point-index dataset head col))))
︙︙

Changes to Code/faceting.lisp.

︙︙
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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


;;; There may be more than one <facet> per panel - each distinct dataset implies
;;; a separate object for the set of variables. That means that we _can_ have
;;; many incongruent scales on the same render (and that's a good thing!).

(define-class <facet> ()
  (vars dataset scales coords
   %trained-data)
  (:default-initargs :%trained-data (make-hash-table :test #'equal)))

;;; FIXME implement the ordering of facets
(defun compute-facets (plot layer)
  (let* ((dataset (layer-dataset plot layer))
         (faceting (faceting plot))
         (facets-ht (facets faceting))
         (layer-datasets (compute-datasets faceting dataset)))
    (dohash ((vars facet-data) layer-datasets)
      (ensure-gethash (vars facets-ht)
          (<facet> :vars vars
                   :dataset facet-data
                   :scales (scales plot)
                   :coords (coords plot))))

    facets-ht))

(defun trained-dataset (facet layer)






  (gethash (list facet layer) (%trained-data facet)))

(defun (setf trained-dataset) (new-value facet layer)
  (setf (gethash (list facet layer) (%trained-data facet)) new-value))

(defun call-with-facets (cont plot)
  (let ((facets (facets (faceting plot))))
    (dohash ((vars facet) facets)
      (declare (ignore vars))
      (funcall cont facet))))

(defmacro do-facets ((facet plot) &body body)
  (let ((cont (gensym)))
    `(flet ((,cont (,facet) ,@body))
       (declare (dynamic-extent (function ,cont)))
       (call-with-facets (function ,cont) ,plot))))







|
|
|








|
|
<
|
|
>


|
>
>
>
>
>
>
|

|
|












46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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


;;; There may be more than one <facet> per panel - each distinct dataset implies
;;; a separate object for the set of variables. That means that we _can_ have
;;; many incongruent scales on the same render (and that's a good thing!).

(define-class <facet> ()
  (vars scales coords %source-data %mapped-data)
  (:default-initargs :%source-data (make-hash-table)
                     :%mapped-data (make-hash-table :test #'equal)))

;;; FIXME implement the ordering of facets
(defun compute-facets (plot layer)
  (let* ((dataset (layer-dataset plot layer))
         (faceting (faceting plot))
         (facets-ht (facets faceting))
         (layer-datasets (compute-datasets faceting dataset)))
    (dohash ((vars facet-data) layer-datasets)
      (let ((facet (ensure-gethash (vars facets-ht)
                       (<facet> :vars vars

                                :scales (scales plot)
                                :coords (coords plot)))))
        (setf (source-dataset facet layer) facet-data)))
    facets-ht))

(defun source-dataset (facet layer)
  (gethash layer (%source-data facet)))

(defun (setf source-dataset) (new-value facet layer)
  (setf (gethash layer (%source-data facet)) new-value))

(defun mapped-dataset (facet layer)
  (gethash (list facet layer) (%mapped-data facet)))

(defun (setf mapped-dataset) (new-value facet layer)
  (setf (gethash (list facet layer) (%mapped-data facet)) new-value))

(defun call-with-facets (cont plot)
  (let ((facets (facets (faceting plot))))
    (dohash ((vars facet) facets)
      (declare (ignore vars))
      (funcall cont facet))))

(defmacro do-facets ((facet plot) &body body)
  (let ((cont (gensym)))
    `(flet ((,cont (,facet) ,@body))
       (declare (dynamic-extent (function ,cont)))
       (call-with-facets (function ,cont) ,plot))))

Changes to Code/plot.lisp.

︙︙
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
      (train-layer plot layer))
    (train-layer plot layer)))

(defun train-plot (plot)
  (dolist (layer (layers plot))
    (compute-facets plot layer)
    (do-facets (facet plot)
      (let* ((dataset (dataset facet))
             (scales (scales facet))
             (aesthetics (layer-aesthetics plot layer))
             (stat-tr (statistical-transformation layer))
             (dataset/m (bind-data aesthetics dataset)))
        (scale-data scales dataset/m)
        (stat-data stat-tr dataset/m)
        (train-scales scales dataset/m)
        (setf (trained-dataset facet layer) dataset/m)))
    (do-facets (facet plot)
      (let ((scales (scales facet))
            (pos-adj (position-adjustment layer))
            (dataset/m (trained-dataset facet layer)))
        (map-scales scales dataset/m)
        (adjust-data pos-adj layer dataset/m)))))

(defmethod print-object ((plot <polyclot>) stream)
  (print-unreadable-object (plot stream :type t :identity nil)
    (format stream "~s" (title plot))))








|







|



|







27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
      (train-layer plot layer))
    (train-layer plot layer)))

(defun train-plot (plot)
  (dolist (layer (layers plot))
    (compute-facets plot layer)
    (do-facets (facet plot)
      (let* ((dataset (source-dataset facet layer))
             (scales (scales facet))
             (aesthetics (layer-aesthetics plot layer))
             (stat-tr (statistical-transformation layer))
             (dataset/m (bind-data aesthetics dataset)))
        (scale-data scales dataset/m)
        (stat-data stat-tr dataset/m)
        (train-scales scales dataset/m)
        (setf (mapped-dataset facet layer) dataset/m)))
    (do-facets (facet plot)
      (let ((scales (scales facet))
            (pos-adj (position-adjustment layer))
            (dataset/m (mapped-dataset facet layer)))
        (map-scales scales dataset/m)
        (adjust-data pos-adj layer dataset/m)))))

(defmethod print-object ((plot <polyclot>) stream)
  (print-unreadable-object (plot stream :type t :identity nil)
    (format stream "~s" (title plot))))

︙︙
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
           (let ((coords (coords facet))
                 (scales (scales facet)))
             (progn;c:formatting-cell (*standard-output*)
               (c:draw-rectangle* *standard-output* 0 0 1 1
                                  :ink c:+grey+ :filled nil)
               (with-coords (coords scales)
                 (dolist (layer (layers plot))
                   (render-geom layer coords (trained-dataset facet layer))))
               (render-guides plot coords))))
         (if (zerop offset-x)
             (incf offset-x)
             (setf offset-x 0
                   offset-y 1)))))))

(defun polyclot (*plot* *standard-output*)
  (train-plot *plot*)
  (c:present *plot* <polyclot>
             :view (<polyclot-view>)
             :sensitive nil
             :allow-sensitive-inferiors t))







|












66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
           (let ((coords (coords facet))
                 (scales (scales facet)))
             (progn;c:formatting-cell (*standard-output*)
               (c:draw-rectangle* *standard-output* 0 0 1 1
                                  :ink c:+grey+ :filled nil)
               (with-coords (coords scales)
                 (dolist (layer (layers plot))
                   (render-geom layer coords (mapped-dataset facet layer))))
               (render-guides plot coords))))
         (if (zerop offset-x)
             (incf offset-x)
             (setf offset-x 0
                   offset-y 1)))))))

(defun polyclot (*plot* *standard-output*)
  (train-plot *plot*)
  (c:present *plot* <polyclot>
             :view (<polyclot-view>)
             :sensitive nil
             :allow-sensitive-inferiors t))

Changes to Code/utilities.lisp.

︙︙
144
145
146
147
148
149
150



(defparameter *grid-background*
  (let ((array (make-array '(40 40) :initial-element 0)))
    (dotimes (i 40)
      (setf (aref array 0 i) 1
            (aref array i 0) 1))
    (c:make-rectangular-tile (c:make-pattern array (list c:+white+ c:+black+))
                             40 40)))










>
>
>
144
145
146
147
148
149
150
151
152
153
(defparameter *grid-background*
  (let ((array (make-array '(40 40) :initial-element 0)))
    (dotimes (i 40)
      (setf (aref array 0 i) 1
            (aref array i 0) 1))
    (c:make-rectangular-tile (c:make-pattern array (list c:+white+ c:+black+))
                             40 40)))

(defmacro xxx (fmt &rest args)
  `(format *debug-io* ,fmt ,@args))

Added Demo/incremental-plot.lisp.























































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
(in-package "EU.TURTLEWARE.POLYCLOT-USER")

(define-demo-plot *incremental*
  (<polyclot>
   :title "Incremental Plot"
   :width 800 :height 600
   :dataset +data/sinus+                ;base dataset
   :coords (<coord-cartesian> :ylim (make-range -200 200))
   :aesthetics (list :x "x" :y "y"
                     :line-style (make-line-style :thickness 1))
   :layers (list (<geom-line>
                  :aesthetics (list :ink c:+black+))
                 (<geom-point>
                  :dataset (make-sinus-dataset)
                  :aesthetics (list :x "x" :y "y"
                                    :ink c:+deep-pink+
                                    :line-style (make-line-style :thickness 10)))))
  "Three layers illustrating mapping and multiple layers.")

(define-application-frame incremental-plot ()
  ()
  (:pane :application :display-function (lambda (frame stream)
                                          (declare (ignore frame))
                                          (polyclot *incremental* stream))
         :width 1280 :height 720))

(find-application-frame 'incremental-plot)

Changes to Demo/test-data.lisp.

︙︙
113
114
115
116
117
118
119






120
121
122
123
124
125
  (assert (> dx 0))
  (let ((x x1))
    (lambda ()
      (incf x dx)
      (when (> x x2)
        (setf x x1))
      (list x (* height (sin (/ x 20)))))))







#+ (or)
(i::doseq (elt (i::data-array
                (stat-data (i::<stat-count>)
                           (i::bind-data '(:x "dialect") +data/lisps+))))
  (format *debug-io* "~16a: ~4d~%" (i::x elt) (i::y elt)))







>
>
>
>
>
>






113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  (assert (> dx 0))
  (let ((x x1))
    (lambda ()
      (incf x dx)
      (when (> x x2)
        (setf x x1))
      (list x (* height (sin (/ x 20)))))))

(defun make-sinus-dataset ()
  (i::<datagen>
   :dataset-head '("x" "y")
   :generator (make-sinus-generator -400 800 25 10)
   :n 120))

#+ (or)
(i::doseq (elt (i::data-array
                (stat-data (i::<stat-count>)
                           (i::bind-data '(:x "dialect") +data/lisps+))))
  (format *debug-io* "~16a: ~4d~%" (i::x elt) (i::y elt)))