-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathscatter.scm
60 lines (56 loc) · 1.77 KB
/
scatter.scm
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
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
;
; scatter.scm - create scatterplot images. Write out single-channel
; "flo" images.
;
(use-modules (ice-9 format))
(use-modules (ice-9 binary-ports))
(use-modules (rnrs bytevectors))
(use-modules (rnrs io ports))
(define-public (write-flo filename LST FN)
"
write-flo - write a single-color-channel floating-point image of a
matrix. This assumes that FN is a function, taking two arguments,
returning a float. The LST is assumed to be a list of basis elements
to be passed to FN. The resulting image consists of the x and y
dimensions (in ASCII) followed by the floats. The result is saved to
`filename`.
Example usage:
(define psu (add-support-api psa))
(define long-words
(filter (lambda (word) (<= 128 (psu 'right-length word)))
(psu 'left-basis)))
(define (pair-cos A B)
(define cos-key (PredicateNode \"*-Cosine Sim Key-*\"))
(define SIM (SimilarityLink A B))
(cog-value-ref (cog-value SIM cos-key) 0))
(write-flo \"scat-cosine-big.flo\" long-words pair-cos)
"
(define xsz (length LST))
(define ysz (length LST))
(define nrow 0)
(define flo (open-file filename "w"))
; Write 23 bytes, not counting the null-terminator
(format flo "~7d ~7d" xsz ysz)
(put-u8 flo 0) ; Write a null byte.
(for-each
(lambda (row)
(define ncol 0)
; Make room for IEEE floats
(define bvrow (make-bytevector (* 4 xsz)))
(for-each
(lambda (col)
(define number (FN row col))
; (bytevector-ieee-single-set! bvrow ncol number 'little)
(bytevector-ieee-single-native-set! bvrow ncol number)
(set! ncol (+ ncol 4))
)
LST)
; Now write out the row.
(put-bytevector flo bvrow)
(force-output flo)
(set! nrow (+ nrow 1))
(format #t "Done with row ~d of ~d\n" nrow ysz)
)
LST)
(close-port flo)
)