Skip to content

Commit a2ccbed

Browse files
committed
Repr-ify plotter for binary32
(cherry picked from commit 41d1db5)
1 parent 6e8aaae commit a2ccbed

File tree

1 file changed

+40
-51
lines changed

1 file changed

+40
-51
lines changed

src/web/plot.rkt

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,34 @@
1212
(define *blue-theme* (color-theme "lightblue" "blue" "navy"))
1313
(define *green-theme* (color-theme "lightgreen" "green" "darkgreen"))
1414

15-
(define (double-transform)
16-
;; TODO: needs to be repr-aware
17-
(define repr (get-representation* 'real))
15+
(define (repr-transform repr)
1816
(invertible-function
1917
(compose (representation-repr->ordinal repr) (curryr ->flonum repr))
2018
(compose (representation-ordinal->repr repr) round)))
2119

22-
(define (double-axis)
23-
(make-axis-transform (double-transform)))
20+
(define (repr-axis repr)
21+
(make-axis-transform (repr-transform repr)))
2422

25-
(define (power10-upto x)
26-
(define ->repr (if (flag-set? 'precision 'double) real->double-flonum real->single-flonum))
23+
(define (power10-upto x repr)
2724
(if (= x 0)
2825
'()
2926
(reverse
3027
(let loop ([power (round (/ (log x) (log 10)))])
31-
(define value (->repr (expt 10.0 power)))
28+
(define value (fl->repr (expt 10.0 power) repr))
3229
(if (= value 0) '() (cons value (loop (- power 1))))))))
3330

34-
(define (possible-ticks min max)
31+
(define (possible-ticks min max repr)
3532
;; Either
3633
;; + 0 is between min and max
3734
;; + 0 is one of min and max (two cases)
3835
;; + min and max are on the same side of 0 (two cases)
3936
(sort
4037
(cond
41-
[(< (* min max) 0) (append (map - (power10-upto (- min))) '(0.0) (power10-upto max))]
42-
[(= min 0) (cons 0 (power10-upto max))]
43-
[(= max 0) (append (map - (power10-upto (abs min))) '(0.0))]
44-
[(> min 0) (set-subtract (power10-upto max) (power10-upto min))]
45-
[(< max 0) (map - (set-subtract (power10-upto (abs min)) (power10-upto (abs max))))])
38+
[(< (* min max) 0) (append (map - (power10-upto (- min) repr)) '(0.0) (power10-upto max repr))]
39+
[(= min 0) (cons 0 (power10-upto max repr))]
40+
[(= max 0) (append (map - (power10-upto (abs min) repr)) '(0.0))]
41+
[(> min 0) (set-subtract (power10-upto max repr) (power10-upto min repr))]
42+
[(< max 0) (map - (set-subtract (power10-upto (abs min) repr) (power10-upto (abs max) repr)))])
4643
<))
4744

4845
(define (pick-spaced-indices necessary possible number)
@@ -82,26 +79,25 @@
8279
(define stopper (if (null? necessary) 0 (apply max necessary)))
8380
(car (argmin (λ (x) (+ (cdr x) (sqr (- (- possible 1) (caar x))))) (drop (vector->list final) stopper))))
8481

85-
(define (choose-ticks min max)
86-
(define possible (possible-ticks min max))
87-
82+
(define (choose-ticks min max repr)
83+
(define possible (possible-ticks min max repr))
8884
(cond
8985
[(< (length possible) 12)
9086
;; If there aren't enough possible big ticks, we fall back to the standard method
9187
(append
9288
(if (<= min 1.0 max) (list (pre-tick 1.0 #t)) '())
9389
(if (<= min 0.0 max) (list (pre-tick 0.0 #t)) '())
9490
(if (<= min -1.0 max) (list (pre-tick -1.0 #t)) '())
95-
((ticks-layout (ticks-scale (linear-ticks #:number 6 #:base 10 #:divisors '(1 2 5)) (double-transform))) min max))]
91+
((ticks-layout (ticks-scale (linear-ticks #:number 6 #:base 10 #:divisors '(1 2 5)) (repr-transform repr))) min max))]
9692
[else
9793
(define necessary (filter identity (map (curry index-of possible) '(1.0 0.0 -1.0))))
9894
(define major-indices (pick-spaced-indices necessary (length possible) 12))
9995
(for/list ([idx major-indices])
10096
(pre-tick (list-ref possible idx) #t))]))
10197

102-
(define (double-ticks)
98+
(define (repr-ticks repr)
10399
(ticks
104-
choose-ticks
100+
(curryr choose-ticks repr)
105101
(λ (lft rgt pticks)
106102
(for/list ([ptick pticks])
107103
(define val (pre-tick-value ptick))
@@ -155,11 +151,11 @@
155151
(y-tick-lines)
156152
(error-points (map (const 1) pts) pts #:axis axis #:alpha 0)))
157153

158-
(define (with-herbie-plot #:title [title #f] thunk)
154+
(define (with-herbie-plot repr #:title [title #f] thunk)
159155
(parameterize ([plot-width 800] [plot-height 300]
160156
[plot-background-alpha 0]
161-
[plot-x-transform (double-axis)]
162-
[plot-x-ticks (double-ticks)]
157+
[plot-x-transform (repr-axis repr)]
158+
[plot-x-ticks (repr-ticks repr)]
163159
[plot-x-tick-label-anchor 'top]
164160
[plot-x-label #f]
165161
[plot-x-far-axis? #t]
@@ -177,19 +173,19 @@
177173
(if port
178174
(lambda () (plot-file (cons (y-axis) renderers) port kind #:y-min 0 #:y-max bit-width))
179175
(lambda () (plot-pict (cons (y-axis) renderers) #:y-min 0 #:y-max bit-width))))
180-
(with-herbie-plot #:title title thunk))
176+
(with-herbie-plot repr #:title title thunk))
181177

182-
(define (with-alt-plot #:title [title #f] thunk)
178+
(define (with-alt-plot repr #:title [title #f] thunk)
183179
(parameterize ([plot-width 800] [plot-height 800]
184180
[plot-background-alpha 1]
185-
[plot-x-transform (double-axis)]
186-
[plot-x-ticks (double-ticks)]
181+
[plot-x-transform (repr-axis repr)]
182+
[plot-x-ticks (repr-ticks repr)]
187183
[plot-x-tick-label-anchor 'top]
188184
[plot-x-label #f]
189185
[plot-x-far-axis? #t]
190186
[plot-x-far-ticks no-ticks]
191-
[plot-y-transform (double-axis)]
192-
[plot-y-ticks (double-ticks)]
187+
[plot-y-transform (repr-axis repr)]
188+
[plot-y-ticks (repr-ticks repr)]
193189
[plot-y-tick-label-anchor 'left]
194190
[plot-y-label #f]
195191
[plot-y-far-axis? #t]
@@ -198,10 +194,10 @@
198194
[plot-y-label title])
199195
(thunk)))
200196

201-
(define (alt-plot #:port [port #f] #:kind [kind 'auto] #:title [title #f] . renderers)
197+
(define (alt-plot repr #:port [port #f] #:kind [kind 'auto] #:title [title #f] . renderers)
202198
(define thunk
203199
(lambda () (plot-file renderers port kind)))
204-
(with-alt-plot #:title title thunk))
200+
(with-alt-plot repr #:title title thunk))
205201

206202
(define (errors-by x errs pts)
207203
(sort (map (λ (pt err) (cons (apply x pt) err)) pts errs) < #:key car))
@@ -254,16 +250,13 @@
254250
(define (avg-fun x)
255251
(define h (histogram-f x))
256252
(/ (apply + (vector->list h)) (vector-length h)))
257-
;; TODO: This is a weird hack in several ways, and ideally wouldn't exist
258-
;; TODO: This doesn't work in single-precision
259-
(define-values (min max)
253+
(define-values (min max) ; plot requires rational bounds
260254
(match* ((car (first eby)) (car (last eby)))
261255
[(x x) (values #f #f)]
262256
[(x y)
263-
(values (flmax (flnext -inf.0) (repr->fl x repr))
257+
(values (flmax (flnext -inf.0) (repr->fl x repr)) ; hence this trick
264258
(flmin (flprev +inf.0) (repr->fl y repr)))]))
265-
(function avg-fun min max
266-
#:width 2 #:color (color-theme-fit color)))
259+
(function avg-fun min max #:width 2 #:color (color-theme-fit color)))
267260

268261
(define (error-mark x-val)
269262
(inverse (const x-val) #:color "gray" #:width 3))
@@ -284,17 +277,11 @@
284277
(define info (regime-info altn))
285278
(and info (sp-bexpr (car info))))
286279

287-
(define (points->doubles pts repr)
288-
(cond
289-
[(or (real? (caar pts)) (complex? (caar pts))) pts]
290-
[else
291-
(map (curry map (curryr repr->fl repr)) pts)]))
292-
293280
(define (make-axis-plot result out idx)
294281
(define var (list-ref (test-vars (test-result-test result)) idx))
295282
(define split-var? (equal? var (regime-var (test-success-end-alt result))))
296283
(define repr (get-representation (test-output-prec (test-result-test result))))
297-
(define pts (points->doubles (test-success-newpoints result) repr))
284+
(define pts (test-success-newpoints result))
298285
(herbie-plot
299286
#:port out #:kind 'png
300287
repr
@@ -309,7 +296,7 @@
309296
['b (values *blue-theme* test-success-end-error)]))
310297

311298
(define repr (get-representation (test-output-prec (test-result-test result))))
312-
(define pts (points->doubles (test-success-newpoints result) repr))
299+
(define pts (test-success-newpoints result))
313300
(define err (accessor result))
314301

315302
(herbie-plot
@@ -318,9 +305,10 @@
318305
(error-points err pts #:axis idx #:color theme)
319306
(error-avg err pts repr #:axis idx #:color theme)))
320307

321-
(define (make-alt-plots point-alt-idxs alt-idxs title out)
308+
(define (make-alt-plots point-alt-idxs alt-idxs title out result)
322309
(define best-alt-point-renderers (best-alt-points point-alt-idxs alt-idxs))
323-
(alt-plot best-alt-point-renderers #:port out #:kind 'png #:title title))
310+
(define repr (get-representation (test-output-prec (test-result-test result))))
311+
(alt-plot best-alt-point-renderers repr #:port out #:kind 'png #:title title))
324312

325313
(define (make-point-alt-idxs result)
326314
(define repr (get-representation (test-output-prec (test-result-test result))))
@@ -330,9 +318,10 @@
330318
(define newexacts (test-success-newexacts result))
331319
(oracle-error-idx all-alt-bodies newpoints newexacts repr))
332320

333-
(define (make-contour-plot point-colors var-idxs title out)
321+
(define (make-contour-plot point-colors var-idxs title out result)
334322
(define point-renderers (herbie-ratio-point-renderers point-colors var-idxs))
335-
(alt-plot point-renderers #:port out #:kind 'png #:title title))
323+
(define repr (get-representation (test-output-prec (test-result-test result))))
324+
(alt-plot point-renderers repr #:port out #:kind 'png #:title title))
336325

337326
#;
338327
(define (make-plots result rdir profile? debug?)
@@ -352,9 +341,9 @@
352341
(define alt-idxs (list i j))
353342
(define title (format "~a vs ~a" (list-ref vars j) (list-ref vars i)))
354343
(open-file (- (+ j (* i (- (length vars)))) 1) #:type 'best-alts
355-
make-alt-plots point-alt-idxs alt-idxs title)
344+
make-alt-plots point-alt-idxs alt-idxs title result)
356345
(open-file (- (+ j (* i (- (length vars)))) 1) #:type 'contours
357-
make-contour-plot point-colors alt-idxs title)))
346+
make-contour-plot point-colors alt-idxs title result)))
358347

359348
(for ([var (test-vars (test-result-test result))] [idx (in-naturals)])
360349
(when (> (length (remove-duplicates (map (curryr list-ref idx) (test-success-newpoints result)))) 1)

0 commit comments

Comments
 (0)