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

Overview
Comment:fix faceting columns
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | master | trunk
Files: files | file ages | folders
SHA3-256: c572d0ad00255240360e2f939a2a8dc2f64f5636e446fda0093cae53780d0fba
User & Date: jack 2025-05-12 07:25:56
Context
2026-05-29
10:37
add fixme, rework slightly *bench* demo check-in: 7695068fee user: jack tags: master, trunk
2025-05-12
07:25
fix faceting columns check-in: c572d0ad00 user: jack tags: master, trunk
2025-04-23
19:49
readme: add entries to news section check-in: 253b1ad517 user: jack tags: master, trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to Code/faceting.lisp.

︙︙
11
12
13
14
15
16
17



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

33
34
35
36
37
38
39
   :datasets (make-hash-table)
   ;; key [vars] val [facet]
   :facets (make-hash-table :test #'equal)))

(defun facet-count (faceting)
  (hash-table-count (facets faceting)))




(defgeneric compute-datasets (faceting dataset)
  (:method :around ((faceting <faceting>) dataset)
    (ensure-gethash (dataset (datasets faceting))
        (call-next-method))))

;;; A single plot (no faceting).
(define-class <faceting-null> (<faceting>)
  ())

(defmethod compute-datasets ((faceting <faceting-null>) dataset)
  (group-by nil dataset))

;;; "Ribbon" (one variable)
(define-class <faceting-wrap> (<faceting>)
  (var nrow ncol direction))


(defmethod compute-datasets ((faceting <faceting-wrap>) dataset)
  (group-by (var faceting) dataset))

;;; "Grid" (two variables)
(define-class <faceting-grid> (<faceting>)
  (var1 var2))







>
>
>














|
>







11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
   :datasets (make-hash-table)
   ;; key [vars] val [facet]
   :facets (make-hash-table :test #'equal)))

(defun facet-count (faceting)
  (hash-table-count (facets faceting)))

(defgeneric ncol (faceting)
  (:method ((f <faceting>)) 1))

(defgeneric compute-datasets (faceting dataset)
  (:method :around ((faceting <faceting>) dataset)
    (ensure-gethash (dataset (datasets faceting))
        (call-next-method))))

;;; A single plot (no faceting).
(define-class <faceting-null> (<faceting>)
  ())

(defmethod compute-datasets ((faceting <faceting-null>) dataset)
  (group-by nil dataset))

;;; "Ribbon" (one variable)
(define-class <faceting-wrap> (<faceting>)
  (var ncol direction)
  (:default-initargs :ncol 1))

(defmethod compute-datasets ((faceting <faceting-wrap>) dataset)
  (group-by (var faceting) dataset))

;;; "Grid" (two variables)
(define-class <faceting-grid> (<faceting>)
  (var1 var2))
︙︙

Changes to Code/plot.lisp.

︙︙
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
(c:define-presentation-method c:present ((plot <polyclot>)
                                         (type <polyclot>)
                                         stream
                                         (view <polyclot-view>)
                                         &rest args)
  (declare (ignore args))
  (with-theme (plot)
    (let ((offset-x 0)
          (offset-y 0))
      (progn;c:formatting-item-list (t :x-spacing 20 :y-spacing 10 :n-columns 2)

       (do-facets (facet plot)
         (c:with-translation (stream offset-x offset-y)
           (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))







<
<
|
>
|
<
|
|
|
|
|
|
|
|
|
<
<
<
<







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
(c:define-presentation-method c:present ((plot <polyclot>)
                                         (type <polyclot>)
                                         stream
                                         (view <polyclot-view>)
                                         &rest args)
  (declare (ignore args))
  (with-theme (plot)


    (c:formatting-item-list (t :x-spacing 20 :y-spacing 10
                               :n-columns (ncol (faceting plot)))
      (do-facets (facet plot)

        (let ((coords (coords facet))
              (scales (scales facet)))
          (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)))))))





(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.

︙︙
94
95
96
97
98
99
100
101



102


103
104












105
106
107
108
109
110
111
        finally (return (values min max))))

(defun print-number (val &optional (stream t))
  (etypecase val
    (float (format stream "~,2f" val))
    (ratio (format stream "~,2f" val))
    (number (format stream "~a" val))))




(defun rescale-value (value min max &key (tmin 0) (tmax 1))


  (let ((normal (/ (- value min) (- max min))))
    (+ (* normal (- tmax tmin)) tmin)))














;;; This is like with-slots, but hides the internal representation of the
;;; row. Later we may want change the data structure to a vector.
(defmacro with-aes (aesthetics row &body body)
  `(with-slots ,aesthetics ,row ,@body))









>
>
>

>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>







94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
        finally (return (values min max))))

(defun print-number (val &optional (stream t))
  (etypecase val
    (float (format stream "~,2f" val))
    (ratio (format stream "~,2f" val))
    (number (format stream "~a" val))))

(defun clamp (value min max)
  (min (max value min) max))

(defun rescale-value (value min max &key (tmin 0) (tmax 1))
  (if (= min max)
      (clamp value min max)
      (let ((normal (/ (- value min) (- max min))))
        (+ (* normal (- tmax tmin)) tmin))))

(defmacro xor (&rest vals)
  (let ((true (gensym)))
   `(block nil
      (let ((,true ,(car vals)))
        ,@(mapcar (lambda (val)
                    `(when ,val
                       (if ,true
                           (return nil)
                           (setf ,true ,val))))
                  (rest vals))
        ,true))))


;;; This is like with-slots, but hides the internal representation of the
;;; row. Later we may want change the data structure to a vector.
(defmacro with-aes (aesthetics row &body body)
  `(with-slots ,aesthetics ,row ,@body))

︙︙

Changes to Demo/cl-bench-results.lisp.

︙︙
10
11
12
13
14
15
16

17
18

19
20
21
22
23
24
25
26
27




















































28
29
30
31
32
33

34
35
36
37
38
39
40
41
42
43
44
;;; 2. CL aesthetic is "ink"
;;; 3. CL bars are dodged on x axis

(defun read-data (output-dir)
  "Constructs a poor man's data frame."
  (flet ((process-result (form)
           (destructuring-bind (lisp . tests) form

             (loop for (test real user sys cons) in (subseq (reverse tests) 0 16) ;
                                                    ;(subseq (reverse tests) 0 8)

                   when user            ; FIXME handle gracefully n/a
                     collect (list lisp test user)))))
    (cons (list "Lisp" "Test" "Time")
          (loop with *read-eval* = nil
                for file in (directory (merge-pathnames "CL-benchmark*.*" output-dir))
                for form = (with-open-file (stream file :direction :input)
                             (read stream))
                append (process-result form)))))





















































(define-demo-plot *bench*
    (let ((dataset (read-data "~/Workshop/repo/cl-bench/output/")))
      (<polyclot> :title "Common Lisp Benchmark"
                  :width 800 :height 600
                  :dataset dataset
                  :aesthetics (list :x "Test" :y "Time" :ink "Lisp")

                  :layers (list (<geom-bar>
                                 :bin-width (/ 1 20)
                                 :statistical-transformation i::+stat-identity+
                                 :position-adjustment (i::<position-dodge>)))
                  :scales (list (make-scale :discrete :x)
                                (make-scale :continuous :y)
                                (make-scale :discrete :ink))))
  "Common Lisp Bencharks example plot")

(find-application-frame 'a-repl :width 800 :height 600)








>
|
<
>

|
|






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

|

|


>







|

|

10
11
12
13
14
15
16
17
18

19
20
21
22
23
24
25
26
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
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
94
95
96
97
98
;;; 2. CL aesthetic is "ink"
;;; 3. CL bars are dodged on x axis

(defun read-data (output-dir)
  "Constructs a poor man's data frame."
  (flet ((process-result (form)
           (destructuring-bind (lisp . tests) form
             (loop for i from 0
                   for (test real user sys cons) in tests

                   for batch = (floor i 11)
                   when user            ; FIXME handle gracefully n/a
                     collect (list lisp test user batch)))))
    (cons (list "Lisp" "Test" "Time" "Batch")
          (loop with *read-eval* = nil
                for file in (directory (merge-pathnames "CL-benchmark*.*" output-dir))
                for form = (with-open-file (stream file :direction :input)
                             (read stream))
                append (process-result form)))))

(defun normalize-results (dataset)
  (let ((results (group-by '("Test") dataset))
        (newdata '()))
    (i::dohash ((test test-data) results)
      (when (= 3 (length (rest test-data)))
       (destructuring-bind ((lisp1 test1 time1 batch1)
                            (lisp2 test2 time2 batch2)
                            (lisp0 test0 time0 batch0))
           (rest test-data)
         (assert (and (equalp test1 test2)
                      (equalp test1 test0)))
         (let ((loser (max time0 time1 time2)))
           (push (list lisp0 test (/ time0 loser) batch0) newdata)
           (push (list lisp1 test (/ time1 loser) batch1) newdata)
           (push (list lisp2 test (/ time2 loser) batch2) newdata)))))
    (list* (car dataset) (reverse newdata))))

(defun yah ()
  (let ((dataset (read-data "~/Workshop/Repositories/cl-bench/output/")))
    (normalize-results dataset)))

;;; time1 - 1
;;; time2 - ?

;;; FIXME come up with faceting so that test X is not shared among plots
;;; NCOL doesn't work etc
(defun benches ()
  (let* ((dataset (read-data "~/Workshop/Repositories/cl-bench/output/"))
         (dataset (normalize-results dataset))
         (groups (group-by '("Batch") dataset))
         (plots nil))
    (i::dohash ((batch dataset) groups)
      (declare (ignore batch))
      (push (<polyclot> :title "Common Lisp Benchmark"
                        :width 1280 :height 200
                        :dataset dataset
                        :aesthetics (list :x "Test" :y "Time" :ink "Lisp")
                                        ;:faceting (<faceting-wrap> :var '("Batch") :ncol 1)
                        :layers (list (<geom-bar>
                                       :bin-width (/ 1 20)
                                       :statistical-transformation i::+stat-identity+
                                       :position-adjustment (i::<position-dodge>)))
                        :scales (list (make-scale :discrete :x)
                                      (make-scale :continuous :y)
                                      (make-scale :discrete :ink)))
            plots))
    (reverse plots)))

(defun draw-benches ()
  (let ((plots (benches)))
    (com-draw-plots plots)))

(define-demo-plot *bench*
    (let ((dataset (read-data "~/Workshop/Repositories/cl-bench/output/")))
      (<polyclot> :title "Common Lisp Benchmark"
                  :width 1280 :height 500
                  :dataset dataset
                  :aesthetics (list :x "Test" :y "Time" :ink "Lisp")
                  ;:faceting (<faceting-wrap> :var '("Batch") :ncol 1)
                  :layers (list (<geom-bar>
                                 :bin-width (/ 1 20)
                                 :statistical-transformation i::+stat-identity+
                                 :position-adjustment (i::<position-dodge>)))
                  :scales (list (make-scale :discrete :x)
                                (make-scale :continuous :y)
                                (make-scale :discrete :ink))))
  "Common Lisp Benchmarks example plot")

;; (find-application-frame 'a-repl :width 800 :height 600)

Changes to Demo/listener.lisp.

︙︙
22
23
24
25
26
27
28











29
30
31
32
33
34
35
(define-a-repl-command (com-draw-plot :name "draw plot")
    ((plot '<polyclot> :gesture (:select :echo t)))
  (let ((stream *standard-output*))
    (window-clear stream)
    (with-room-for-graphics (stream :first-quadrant nil)
      (polyclot plot stream))
    (finish-output stream)))












(defmethod run-frame-top-level ((frame a-repl) &rest args)
  (declare (ignore args))
  (let ((*standard-input* (frame-standard-input frame))
        (*standard-output* (frame-standard-output frame))
        (*error-output* (frame-error-output frame))
        (*query-io* (frame-query-io frame))







>
>
>
>
>
>
>
>
>
>
>







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
(define-a-repl-command (com-draw-plot :name "draw plot")
    ((plot '<polyclot> :gesture (:select :echo t)))
  (let ((stream *standard-output*))
    (window-clear stream)
    (with-room-for-graphics (stream :first-quadrant nil)
      (polyclot plot stream))
    (finish-output stream)))

(define-a-repl-command (com-draw-plots :name "draw plots")
    ((plots 't))
  (let ((plots (if (listp plots) plots (list plots)))
        (stream *standard-output*))
    (window-clear stream)
    (loop for plot in plots do
      (with-room-for-graphics (stream :first-quadrant nil)
        (polyclot plot stream))
      (terpri stream))
    (finish-output stream)))

(defmethod run-frame-top-level ((frame a-repl) &rest args)
  (declare (ignore args))
  (let ((*standard-input* (frame-standard-input frame))
        (*standard-output* (frame-standard-output frame))
        (*error-output* (frame-error-output frame))
        (*query-io* (frame-query-io frame))
︙︙

Changes to README.org.

︙︙
267
268
269
270
271
272
273

274
275
276
277
278
279
280
- [2024-09-19 Thu] when mapping allow access by user function to column by name
- [2024-09-19 Thu] replace POINT-VALUES with POINT-INDEX and POINT-VALUE
- [2025-04-23 Wed]
  - fix faceting to maintain mapping to each layer dataset
  - introduce new source dataset type <DATASET> similar to list representation
  - introduce new source dataset type <DATAGEN> that generates data
  - add incomplete demo for incremental plots (for now it only uses <DATAGEN>


** Dataset and aesthetics

Currently we allow for the user to map aesthetics by name (a string), arbitrary
function (must be a function, not a function designator), and as a constant.

When the user passes a function, then it should accept as an argument raw







>







267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
- [2024-09-19 Thu] when mapping allow access by user function to column by name
- [2024-09-19 Thu] replace POINT-VALUES with POINT-INDEX and POINT-VALUE
- [2025-04-23 Wed]
  - fix faceting to maintain mapping to each layer dataset
  - introduce new source dataset type <DATASET> similar to list representation
  - introduce new source dataset type <DATAGEN> that generates data
  - add incomplete demo for incremental plots (for now it only uses <DATAGEN>
- [2025-05-12 Mon] when faceting obey the number of columns protocol

** Dataset and aesthetics

Currently we allow for the user to map aesthetics by name (a string), arbitrary
function (must be a function, not a function designator), and as a constant.

When the user passes a function, then it should accept as an argument raw
︙︙
310
311
312
313
314
315
316

















317
318
319
320
321
322
323
Faceting is an operation of splitting datasets into a few separate sets that are
rendered as separate sub-plots within a single render. Each facet features the
same set of layers as the "main" plot and has its own scales and guides.

Each layer _may_ overwrite the plot's dataset, so each facet may feature as many
distinct datasets as there are layers, although the source dataset for each
layer is the same regardless of the facet.


















** Incremental plots

Ideally we'd like to be able to add new data to the existing plot. Consider:
- adding a new data point may change scales and influence whole facet
- it should be possible to add a set of new points without retraining repeatedly
- it would be cool to have some a GC possibility (i.e "remove older than")







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







311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
Faceting is an operation of splitting datasets into a few separate sets that are
rendered as separate sub-plots within a single render. Each facet features the
same set of layers as the "main" plot and has its own scales and guides.

Each layer _may_ overwrite the plot's dataset, so each facet may feature as many
distinct datasets as there are layers, although the source dataset for each
layer is the same regardless of the facet.

All faceting instances must obey the following protocol:

(ncol faceting)                     ; how many facets fit in a row
(compute-datasets faceting dataset) ; splits the dataset into facets

*** <faceting-null>

This is a default and means "no faceting".

*** <faceting-wrap>

Ribbon faceting grouped by a single variable.

*** <faceting-grid>

Grid faceting (grouped by two variables).

** Incremental plots

Ideally we'd like to be able to add new data to the existing plot. Consider:
- adding a new data point may change scales and influence whole facet
- it should be possible to add a set of new points without retraining repeatedly
- it would be cool to have some a GC possibility (i.e "remove older than")
︙︙