Debian Codegraph @

auxil/rama/fit-rama.scm

raw symbols SCIP
1
2 (use-modules (ice-9 format))
3
4 (define dimensions 72)
5 (define base-array (make-array 0.0 dimensions dimensions))
6
7 (define read-rama-data
8 (lambda (filename)
9
10 (let ((arr (make-array 0.0 dimensions dimensions))
11 (fac (/ 360.0 dimensions))) ; degrees/step
12
13 (call-with-input-file filename
14 (lambda (port)
15
16 (let f ((s (read-line port)))
17 (if (not (eof-object? s))
18
19 ; so a line has 3 numbers
20 (let ((parts (string-split s #\space)))
21
22 (if (= 3 (length parts))
23
24 (let ((phi (string->number (list-ref parts 0)))
25 (psi (string->number (list-ref parts 1)))
26 (val (string->number (list-ref parts 2))))
27
28 (let ((phi-index (inexact->exact (/ phi fac)))
29 (psi-index (inexact->exact (/ psi fac))))
30
31 (array-set! arr val phi-index psi-index))))
32
33 (f (read-line port)))))))
34
35 arr)))
36
37 ;; return the indices of the peak in arr, look for negative peaks too.
38 ;
39 (define indices-for-peak
40 (lambda (arr)
41
42 (let f ((peak-pair (cons -1 -1))
43 (max-value -1)
44 (phi-index 0)
45 (psi-index 0))
46 (cond
47 ((= phi-index dimensions) peak-pair) ; return value
48 ((= psi-index dimensions) (f peak-pair max-value (+ phi-index 1) 0))
49 ((> (abs (array-ref arr phi-index psi-index)) max-value)
50 (f (cons phi-index psi-index)
51 (abs (array-ref arr phi-index psi-index))
52 phi-index (+ psi-index 1)))
53 (else (f peak-pair max-value phi-index (+ psi-index 1)))))))
54
55
56 ;; What is the value of the "background" of the gaussian? Estimate it
57 ;; by averaging points about the 3 sigma mark.
58 ;;
59 ;; return a number
60 (define gaussian-base-value
61 (lambda (arr peak-pair variance)
62
63 (let ((three-sigma (* (sqrt variance) 3.0)))
64
65 4.0))
66
67
68 ;;
69 (define gaussian-value
70 (lambda (centre height variance point-x point-y)
71
72 ; exp[-2(x-xo)^2/s^2]
73 (let* ((degrees/step (/ 360 dimensions))
74 (diff-y (* (- (cdr centre) point-y) degrees/step))
75 (diff-x (* (- (car centre) point-x) degrees/step))
76 (diff-y2 (* diff-y diff-y))
77 (diff-x2 (* diff-x diff-x))
78 (z2 (/ (+ diff-y2 diff-x2) variance)))
79
80 (* height (exp (* -0.5 z2))))))
81
82
83 (define remove-gaussian
84 (lambda (arr peak-pair height variance)
85
86 ; given a sigma of 10, typically, we need 6 grid points (for
87 ; 360/dimension (=5 degree spacing)) eitheir side, so a
88 ; 12x12 grid around peak.
89 ; 3 is for 3 sigma
90 (let* ((sigma-guess 10)
91 (grid-size (inexact->exact (* 3 (/ sigma-guess (/ 360 dimensions))))))
92
93 (format #t "choosing grid size ~s from sigma ~s ~%" grid-size (sqrt variance))
94
95 (let loop ((phi-index (- (car peak-pair) grid-size))
96 (psi-index (- (cdr peak-pair) grid-size)))
97
98 (cond
99 ((> phi-index (+ (car peak-pair) grid-size)) 'done)
100 ((> psi-index (+ (cdr peak-pair) grid-size)) (loop (+ phi-index 1)
101 (- (cdr peak-pair) grid-size)))
102 (else
103 (let ((g (gaussian-value peak-pair height variance phi-index psi-index)))
104
105 ; (let ((a (sqrt (+ (* (- phi-index (car peak-pair))
106 ; (- phi-index (car peak-pair)))
107 ; (* (- psi-index (cdr peak-pair))
108 ; (- psi-index (cdr peak-pair)))))))
109
110 ; (format #t "debug: gaussian-value ~s from height ~s at dist: ~s nsigma: ~s~%"
111 ; g height a (* (/ 360 dimensions) (/ a (sqrt variance)))))
112
113 (let* ((current-pair (cons phi-index psi-index))
114 (current-value (arr-value arr current-pair)))
115
116 ; (format #t "removing ~s from ~s at ~s~%"
117 ; g current-value (cons phi-index psi-index))
118
119 (arr-remove base-array current-pair (- g)) ; so add it
120 (arr-remove arr current-pair g)
121 (loop phi-index (+ psi-index 1))))))))))
122
123
124
125 ;; the engine of all this:
126 (define gaussian-fit
127 (lambda (arr peak-pair gaussian-height-scale gaussian-variance-scale)
128
129 (define variance-est
130 (lambda (peak-value neighbours x-dist)
131
132 (let ((y (average (map (lambda (x)
133 (arr-value arr x))
134 neighbours))))
135
136 (if (< (* y peak-value) 0.0)
137 (begin
138 (format #t "bad neighb avearge: peak: ~s average ~s neigb values: ~s~%"
139 peak-value y (map (lambda (x)
140 (arr-value arr x))
141 neighbours))
142
143 ;; return a variance equal to the grid spacing
144 ;; (squared because it is variance)
145 (let ((degrees/step (/ 360 dimensions)))
146 (* 0.8 degrees/step degrees/step)))
147
148 ; normal case, we have neighbours average of the same
149 ; sign as the peak:
150 (- (/ (* x-dist x-dist ) (* 2 (log (/ y peak-value)))))))))
151
152 (define neighbours-avearge
153 (lambda (neighbours)
154
155 (average (map (lambda (x)
156 (arr-value arr x))
157 neighbours))))
158
159 ;; so we have to estimate the height and the width,
160
161 ;; Let's start off with 10 degrees width and height is peak height
162 ;; take that away from the points...
163
164 ;; aim: minimize Sigma d_i_2, where
165 ;; d_i = y_i - g(x_i)
166 ;; and g(x) = A\exp((x-x_o)^2)
167 ;;
168 ;; we have a set of y_i
169
170 ;; what is the sum of the nearest neighbours?
171 ;; what is the sum of the next neest neighbours?
172 ;;
173 ;; up to 2 cells away, we have 5 different types of neighbours.
174
175 ;; So some strategy then...
176 ;; the centre of the gaussian goes at peak-pair.
177 ;; we then get the neighbours, the 5 different kinds, and average
178 ;; each kind of neighbour.
179 ;;
180 ;; each of these can give us an estimate of the standard
181 ;; deviation. Let's average these estimates to assign sigma.
182 ;;
183
184 (let* ((phi-index (car peak-pair))
185 (psi-index (cdr peak-pair))
186 (neighbours-1 (list (cons (+ phi-index 1) psi-index)
187 (cons (- phi-index 1) psi-index)
188 (cons phi-index (+ psi-index 1))
189 (cons phi-index (- psi-index 1))))
190 (neighbours-2 (list (cons (+ phi-index 1) (+ psi-index 1))
191 (cons (+ phi-index 1) (- psi-index 1))
192 (cons (- phi-index 1) (+ psi-index 1))
193 (cons (- phi-index 1) (- psi-index 1))))
194 (neighbours-3 (list (cons (+ phi-index 2) psi-index)
195 (cons (- phi-index 2) psi-index)
196 (cons phi-index (+ psi-index 2))
197 (cons phi-index (- psi-index 2))))
198 (neighbours-4 (list (cons (+ phi-index 1) (+ psi-index 2))
199 (cons (+ phi-index 1) (- psi-index 2))
200 (cons (- phi-index 1) (+ psi-index 2))
201 (cons (- phi-index 1) (- psi-index 2))
202 (cons (+ phi-index 2) (+ psi-index 1))
203 (cons (+ phi-index 2) (- psi-index 1))
204 (cons (- phi-index 2) (+ psi-index 1))
205 (cons (- phi-index 2) (- psi-index 1))))
206 (neighbours-5 (list (cons (+ phi-index 2) (+ psi-index 2))
207 (cons (+ phi-index 2) (- psi-index 2))
208 (cons (- phi-index 2) (+ psi-index 2))
209 (cons (- phi-index 2) (- psi-index 2))))
210 (n-1-dist (* 1.0 (/ 360 dimensions)))
211 (n-2-dist (* (sqrt 2.0) (/ 360 dimensions)))
212 (n-3-dist (* 2.0 (/ 360 dimensions)))
213 (n-4-dist (* (sqrt 5.0) (/ 360 dimensions)))
214 (n-5-dist (* (sqrt 8.0) (/ 360 dimensions))))
215
216 ; (format #t "peat at ~s has neighbours: ~s~%"
217 ; peak-pair neighbours-1)
218
219 (format #t "peak: ~s at ~s with neibs average: ~s ~%"
220 (arr-value arr peak-pair)
221 peak-pair
222 (neighbours-avearge neighbours-1))
223
224 (let* ((peak-value (arr-value arr peak-pair))
225 (variance-estimate-1 (variance-est peak-value neighbours-1 n-1-dist))
226 (variance-estimate-2 (variance-est peak-value neighbours-2 n-2-dist))
227 (variance-estimate-3 (variance-est peak-value neighbours-3 n-3-dist))
228 (variance-estimate-4 (variance-est peak-value neighbours-4 n-4-dist))
229 (variance-estimate-5 (variance-est peak-value neighbours-5 n-5-dist)))
230
231 ; (format #t "variance estimates: ~s ~s ~s ~s ~s~%"
232 ; variance-estimate-1 variance-estimate-2 variance-estimate-3
233 ; variance-estimate-4 variance-estimate-5)
234
235 (let ((average-var (/ (+ (* variance-estimate-1 (length neighbours-1))
236 (* variance-estimate-2 (length neighbours-2))
237 (* variance-estimate-3 (length neighbours-3))
238 (* variance-estimate-4 (length neighbours-4))
239 (* variance-estimate-5 (length neighbours-5)))
240 (+ (length neighbours-1)
241 (length neighbours-2)
242 (length neighbours-3)
243 (length neighbours-4)
244 (length neighbours-5)))))
245
246 ; (format #t "average variance: ~s from variance estimates: ~% ~s~% ~s~% ~s~% ~s~% ~s~%"
247 ; average-var variance-estimate-1 variance-estimate-2
248 ; variance-estimate-3 variance-estimate-4 variance-estimate-5)
249
250 ; Now we have a position, a height and a variance,
251 ; let's remove that gaussian from the plot
252 (remove-gaussian arr peak-pair
253 (* gaussian-height-scale (arr-value arr peak-pair))
254 (* gaussian-variance-scale average-var)))))))
255
256
257 ;;
258 (define wrapped-phi-psi-pair
259 (lambda (phi-psi-pair)
260
261 (let ((phi (car phi-psi-pair))
262 (psi (cdr phi-psi-pair)))
263 (cond
264 ((and (>= phi 0) (< phi dimensions)
265 (>= psi 0) (< psi dimensions))
266 phi-psi-pair) ; no change
267 ((and (>= phi 0) (< phi dimensions))
268 ; psi needs changing
269 (if (< psi 0)
270 (cons phi (+ psi dimensions))
271 (if (>= psi dimensions)
272 (cons phi (- psi dimensions))
273 'this-cannot-happen-psi)))
274 ((and (>= psi 0) (< psi dimensions))
275 ; phi needs changing
276 (if (< phi 0)
277 (cons (+ phi dimensions) psi)
278 (if (>= phi dimensions)
279 (cons (- phi dimensions) psi)
280 'this-cannot-happen-phi)))
281
282 ; both need changing then:
283 ((and (< phi 0) (< psi 0))
284 (cons (+ phi dimensions) (+ psi dimensions)))
285 ((and (< phi 0) (>= dimensions psi))
286 (cons (+ phi dimensions) (- psi dimensions)))
287 ((and (>= dimensions phi) (< psi 0))
288 (cons (- phi dimensions) (+ psi dimensions)))
289 ((and (>= dimensions phi) (>= dimensions psi))
290 (cons (- phi dimensions) (- psi dimensions)))
291 (else
292 'this-cannont-happen-fall-through)))))
293
294 ;;
295 (define average
296 (lambda (lsi)
297
298 (if (null? lsi)
299 0.0
300 (let loop ((sum 0.0)
301 (ls lsi))
302 (cond
303 ((null? ls) (/ sum (length lsi)))
304 (else
305 (loop (+ sum (car ls)) (cdr ls))))))))
306
307
308 ;; does wrapping, phi and psi indices can be negative or = to
309 ;; dimension (or more).
310 ;;
311 (define arr-value
312 (lambda (arr phi-psi-pair)
313
314 (let ((w (wrapped-phi-psi-pair phi-psi-pair)))
315 (array-ref arr (car w) (cdr w)))))
316
317 ;;
318 (define arr-remove
319 (lambda (arr phi-psi-pair val)
320
321 (let* ((w (wrapped-phi-psi-pair phi-psi-pair))
322 (current-value (arr-value arr w)))
323
324 (array-set! arr (- current-value val) (car w) (cdr w)))))
325
326 ;;
327 (define unfitted-sum
328 (lambda (arr)
329
330 (let f ((sum 0.0)
331 (phi-index 0)
332 (psi-index 0))
333 (cond
334 ((= phi-index dimensions) (sqrt sum)) ; return value
335 ((= psi-index dimensions) (f sum (+ phi-index 1) 0))
336 (else
337 (let ((v (array-ref arr phi-index psi-index)))
338 (f (+ sum (* v v)) phi-index (+ psi-index 1))))))))
339
340 (define print-arr
341 (lambda (arr)
342
343 (let f ((phi-index 0)
344 (psi-index 0))
345 (cond
346 ((= phi-index dimensions) 'done) ; return value
347 ((= psi-index dimensions)
348 (format #t "~%")
349 (f (+ phi-index 1) 0))
350 (else
351 (format #t "~5f " (array-ref arr phi-index psi-index))
352 (f phi-index (+ psi-index 1)))))))
353
354 (define print-arr-for-gnuplot
355 (lambda (arr filename)
356
357 (call-with-output-file filename
358 (lambda (port)
359 (let f ((phi-index 0)
360 (psi-index 0)
361 (degrees/step (/ 360 dimensions)))
362 (cond
363 ((= phi-index dimensions) 'done) ; return value
364 ((= psi-index dimensions)
365 (format port "~%")
366 (f (+ phi-index 1) 0 degrees/step))
367 (else
368 (let ((v (array-ref arr phi-index psi-index)))
369 (format port "~s ~s ~s~%"
370 (* phi-index degrees/step)
371 (* psi-index degrees/step)
372 (if (> v 0)
373 v v))
374 (f phi-index (+ psi-index 1) degrees/step)))))))))
375
376
377 ;; first read in the data into arr
378
379 (define fit-rama
380 (lambda (rounds height-scale var-scale)
381 (let* ((arr (read-rama-data "rama-data.tab"))
382 (initial-sum (unfitted-sum arr)))
383
384 (let loop ((n 0))
385
386 (cond
387 ((= n rounds ) (* 100.0 (/ (unfitted-sum arr) initial-sum)))
388 (else
389 (let ((peak-pair (indices-for-peak arr)))
390 ; let us know if that chose a negative peak:
391 (if (< (arr-value arr peak-pair) 0.0)
392 (format #t "Fitted a negative peak (~s) at: ~s~%"
393 (arr-value arr peak-pair) peak-pair))
394
395 (let ((us (unfitted-sum arr)))
396 (format #t "----- unfitted sum: ~s (~s%)~%" us (* 100.0 (/ us initial-sum)))
397 (gaussian-fit arr peak-pair height-scale var-scale) ; and remove it
398 (loop (+ n 1)))))))
399 ; arr)))
400 (* 100.0 (/ (unfitted-sum arr) initial-sum)))))
401
402
403 ; (let* ((rounds 500)
404 ; (arr (fit-rama rounds 0.7 0.55)))
405 ; (print-arr-for-gnuplot arr (string-append "post-fit-" (number->string rounds)
406 ; ".tab"))
407 ; (print-arr-for-gnuplot base-array (string-append "base-fit-" (number->string rounds)
408 ; ".tab")))
409
410
411
412 ;; now that I have corrected the estimate of variance (with a factor
413 ;; of 2), both the optimal height-scale and the optimal variance scale
414 ;; are 1.0
415
416 (let loop ((height-scale 0.4))
417
418 (cond
419 ((> height-scale 2.0) 'done)
420 (else
421 (let ((r (fit-rama 30 height-scale 1.0)))
422
423 (format #t "height: ~s final remainder: ~s~%" height-scale r)
424 (loop (+ height-scale 0.1))))))
425
426
427
428
429