|
12 | 12 | (define *blue-theme* (color-theme "lightblue" "blue" "navy")) |
13 | 13 | (define *green-theme* (color-theme "lightgreen" "green" "darkgreen")) |
14 | 14 |
|
15 | | -(define (double-transform) |
16 | | - ;; TODO: needs to be repr-aware |
17 | | - (define repr (get-representation* 'real)) |
| 15 | +(define (repr-transform repr) |
18 | 16 | (invertible-function |
19 | 17 | (compose (representation-repr->ordinal repr) (curryr ->flonum repr)) |
20 | 18 | (compose (representation-ordinal->repr repr) round))) |
21 | 19 |
|
22 | | -(define (double-axis) |
23 | | - (make-axis-transform (double-transform))) |
| 20 | +(define (repr-axis repr) |
| 21 | + (make-axis-transform (repr-transform repr))) |
24 | 22 |
|
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) |
27 | 24 | (if (= x 0) |
28 | 25 | '() |
29 | 26 | (reverse |
30 | 27 | (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)) |
32 | 29 | (if (= value 0) '() (cons value (loop (- power 1)))))))) |
33 | 30 |
|
34 | | -(define (possible-ticks min max) |
| 31 | +(define (possible-ticks min max repr) |
35 | 32 | ;; Either |
36 | 33 | ;; + 0 is between min and max |
37 | 34 | ;; + 0 is one of min and max (two cases) |
38 | 35 | ;; + min and max are on the same side of 0 (two cases) |
39 | 36 | (sort |
40 | 37 | (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)))]) |
46 | 43 | <)) |
47 | 44 |
|
48 | 45 | (define (pick-spaced-indices necessary possible number) |
|
82 | 79 | (define stopper (if (null? necessary) 0 (apply max necessary))) |
83 | 80 | (car (argmin (λ (x) (+ (cdr x) (sqr (- (- possible 1) (caar x))))) (drop (vector->list final) stopper)))) |
84 | 81 |
|
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)) |
88 | 84 | (cond |
89 | 85 | [(< (length possible) 12) |
90 | 86 | ;; If there aren't enough possible big ticks, we fall back to the standard method |
91 | 87 | (append |
92 | 88 | (if (<= min 1.0 max) (list (pre-tick 1.0 #t)) '()) |
93 | 89 | (if (<= min 0.0 max) (list (pre-tick 0.0 #t)) '()) |
94 | 90 | (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))] |
96 | 92 | [else |
97 | 93 | (define necessary (filter identity (map (curry index-of possible) '(1.0 0.0 -1.0)))) |
98 | 94 | (define major-indices (pick-spaced-indices necessary (length possible) 12)) |
99 | 95 | (for/list ([idx major-indices]) |
100 | 96 | (pre-tick (list-ref possible idx) #t))])) |
101 | 97 |
|
102 | | -(define (double-ticks) |
| 98 | +(define (repr-ticks repr) |
103 | 99 | (ticks |
104 | | - choose-ticks |
| 100 | + (curryr choose-ticks repr) |
105 | 101 | (λ (lft rgt pticks) |
106 | 102 | (for/list ([ptick pticks]) |
107 | 103 | (define val (pre-tick-value ptick)) |
|
155 | 151 | (y-tick-lines) |
156 | 152 | (error-points (map (const 1) pts) pts #:axis axis #:alpha 0))) |
157 | 153 |
|
158 | | -(define (with-herbie-plot #:title [title #f] thunk) |
| 154 | +(define (with-herbie-plot repr #:title [title #f] thunk) |
159 | 155 | (parameterize ([plot-width 800] [plot-height 300] |
160 | 156 | [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)] |
163 | 159 | [plot-x-tick-label-anchor 'top] |
164 | 160 | [plot-x-label #f] |
165 | 161 | [plot-x-far-axis? #t] |
|
177 | 173 | (if port |
178 | 174 | (lambda () (plot-file (cons (y-axis) renderers) port kind #:y-min 0 #:y-max bit-width)) |
179 | 175 | (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)) |
181 | 177 |
|
182 | | -(define (with-alt-plot #:title [title #f] thunk) |
| 178 | +(define (with-alt-plot repr #:title [title #f] thunk) |
183 | 179 | (parameterize ([plot-width 800] [plot-height 800] |
184 | 180 | [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)] |
187 | 183 | [plot-x-tick-label-anchor 'top] |
188 | 184 | [plot-x-label #f] |
189 | 185 | [plot-x-far-axis? #t] |
190 | 186 | [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)] |
193 | 189 | [plot-y-tick-label-anchor 'left] |
194 | 190 | [plot-y-label #f] |
195 | 191 | [plot-y-far-axis? #t] |
|
198 | 194 | [plot-y-label title]) |
199 | 195 | (thunk))) |
200 | 196 |
|
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) |
202 | 198 | (define thunk |
203 | 199 | (lambda () (plot-file renderers port kind))) |
204 | | - (with-alt-plot #:title title thunk)) |
| 200 | + (with-alt-plot repr #:title title thunk)) |
205 | 201 |
|
206 | 202 | (define (errors-by x errs pts) |
207 | 203 | (sort (map (λ (pt err) (cons (apply x pt) err)) pts errs) < #:key car)) |
|
254 | 250 | (define (avg-fun x) |
255 | 251 | (define h (histogram-f x)) |
256 | 252 | (/ (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 |
260 | 254 | (match* ((car (first eby)) (car (last eby))) |
261 | 255 | [(x x) (values #f #f)] |
262 | 256 | [(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 |
264 | 258 | (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))) |
267 | 260 |
|
268 | 261 | (define (error-mark x-val) |
269 | 262 | (inverse (const x-val) #:color "gray" #:width 3)) |
|
284 | 277 | (define info (regime-info altn)) |
285 | 278 | (and info (sp-bexpr (car info)))) |
286 | 279 |
|
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 | | - |
293 | 280 | (define (make-axis-plot result out idx) |
294 | 281 | (define var (list-ref (test-vars (test-result-test result)) idx)) |
295 | 282 | (define split-var? (equal? var (regime-var (test-success-end-alt result)))) |
296 | 283 | (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)) |
298 | 285 | (herbie-plot |
299 | 286 | #:port out #:kind 'png |
300 | 287 | repr |
|
309 | 296 | ['b (values *blue-theme* test-success-end-error)])) |
310 | 297 |
|
311 | 298 | (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)) |
313 | 300 | (define err (accessor result)) |
314 | 301 |
|
315 | 302 | (herbie-plot |
|
318 | 305 | (error-points err pts #:axis idx #:color theme) |
319 | 306 | (error-avg err pts repr #:axis idx #:color theme))) |
320 | 307 |
|
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) |
322 | 309 | (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)) |
324 | 312 |
|
325 | 313 | (define (make-point-alt-idxs result) |
326 | 314 | (define repr (get-representation (test-output-prec (test-result-test result)))) |
|
330 | 318 | (define newexacts (test-success-newexacts result)) |
331 | 319 | (oracle-error-idx all-alt-bodies newpoints newexacts repr)) |
332 | 320 |
|
333 | | -(define (make-contour-plot point-colors var-idxs title out) |
| 321 | +(define (make-contour-plot point-colors var-idxs title out result) |
334 | 322 | (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)) |
336 | 325 |
|
337 | 326 | #; |
338 | 327 | (define (make-plots result rdir profile? debug?) |
|
352 | 341 | (define alt-idxs (list i j)) |
353 | 342 | (define title (format "~a vs ~a" (list-ref vars j) (list-ref vars i))) |
354 | 343 | (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) |
356 | 345 | (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))) |
358 | 347 |
|
359 | 348 | (for ([var (test-vars (test-result-test result))] [idx (in-naturals)]) |
360 | 349 | (when (> (length (remove-duplicates (map (curryr list-ref idx) (test-success-newpoints result)))) 1) |
|
0 commit comments