5 (defparameter *model
* (make-instance 'sphere-model-angular
))
9 (defparameter *model
* (make-test-model)))
12 (write-pgm "/home/martin/tmp/model-cut.pgm"
13 (normalize-2-csf/ub8-realpart
(cross-section-xz (spheres *model
*))))
17 (multiple-value-bind (conv dx dz
)
18 (angular-intensity-psf-minimal-resolution :x-um
20s0
:z-um
44s0
19 :window-radius
.2 :window-x
.4 :window-y
0s0
20 :debug t
:initialize t
21 :integrand-evaluations
300)
22 (resample-3-csf conv dx dx dz
.2 .2 1.0))))
24 (write-pgm "/home/martin/tmp/psf-cut.pgm"
25 (normalize-2-csf/ub8-realpart
(cross-section-xz *psf
*)))
30 (convolve (spheres *model
*) *psf
*))
31 (write-pgm "/home/martin/tmp/conv-cut.pgm"
32 (normalize-2-csf/ub8-realpart
33 (cross-section-xz #+nil
(spheres *model
*)
35 #+nil
(.-
*conv
* (vol::s
* 1e11
(spheres *model
*))))))))
37 (write-pgm "/home/martin/tmp/conv-cut.pgm"
39 (cross-section-xz (.-
(normalize-3-csf/sf-realpart
*conv
*)
40 (normalize-3-csf/sf-realpart
(spheres *model
*))))))
45 (setf *new-tex
* (normalize-3-csf/ub8-realpart
*conv
*))
50 (draw *model
* :nucleus
0))
59 ,@(loop for i in
'(*centers
* *dims
* *spheres
* *psf
* *conv-l
* *conv-l-s
*
60 *conv-plane
* *conv-plane-s
*
63 `(defparameter ,i nil
))))
68 (write-pgm "/home/martin/tmp/fftw.pgm"
69 (normalize-2-csf/ub8-abs
71 (let ((a (ft (draw-sphere-csf 12.0 34 206 296)))
72 #+nil
(b (ft (draw-sphere-csf 5.0 34 206 296))))
76 ;; find the centers of the nuclei and store into *centers*
77 (multiple-value-bind (c ch dims
)
80 (defparameter *centers
* c
)
81 (defparameter *dims
* dims
)
84 ;; as a model of fluorophore concentration draw ovals around the
85 ;; positions in *centers* and store into *spheres*
87 (destructuring-bind (z y x
)
89 (draw-ovals 12.0 *centers
* z y x
))))
90 (defparameter *spheres
* spheres
)
91 (write-pgm "/home/martin/tmp/comp0-spheres-cut.pgm"
92 (normalize-2-csf/ub8-realpart
93 (cross-section-xz *spheres
*
94 (vec-i-y (elt *centers
* 21)))))
97 ;; store the fluorophore concentration
98 (save-stack-ub8 "/home/martin/tmp/spheres"
99 (normalize-3-csf/ub8-realpart
*spheres
*)))
107 ;; calculate intensity psf, make extend in z big enough to span the
108 ;; full fluorophore concentration even when looking at the bottom
112 (psf (destructuring-bind (z y x
)
114 (declare (ignore y x
))
116 (psf:intensity-psf
(* 2 z
) r r
(* z dz
) (* r dx
)
117 :integrand-evaluations
40)))))
118 (defparameter *psf
* psf
)
119 (write-pgm "/home/martin/tmp/comp1-psf.pgm"
120 (normalize-2-csf/ub8-realpart
(cross-section-xz psf
)))
121 (sb-ext:gc
:full t
)))
127 ;; Extract one specific plane from the fluorophore concentration
128 ;; model and convolve with intensity psf. The result is the light
129 ;; distribution in the sample.
130 (destructuring-bind (z y x
)
131 (array-dimensions *spheres
*)
133 (let* ((zz (vec-i-z (elt *centers
* 31)))
134 (current-slice-bbox (make-bbox :start
(v 0d0
0d0
(* 1d0 zz
))
135 :end
(v (* 1d0
(1- x
))
138 (current-slice (extract-bbox3-cdf *spheres
* current-slice-bbox
)))
139 (multiple-value-bind (conv conv-start
)
140 (convolve3-nocrop current-slice
*psf
*)
141 (defparameter *conv-l
* conv
)
142 (defparameter *conv-l-s
* (v--i (make-vec-i :z zz
)
144 (write-pgm "/home/martin/tmp/comp2-conv.pgm"
145 (normalize2-cdf/ub8-realpart
148 (vec-i-y (v+-i conv-start
(elt *centers
* 31)))))))
149 (sb-ext:gc
:full t
)))
151 ;; store light distribution
152 (save-stack-ub8 "/home/martin/tmp/conv-l"
153 (normalize3-cdf/ub8-realpart
*conv-l
*))
155 ;; multiply fluorophore concentration with light distribution, this
156 ;; gives the excitation pattern in the sample
158 (defparameter Lf
(.
* *conv-l
* *spheres
* *conv-l-s
*))
159 (write-pgm "/home/martin/tmp/comp3-conv-lf.pgm"
160 (normalize2-cdf/ub8-realpart
161 (cross-section-xz Lf
(vec-i-y (elt *centers
* 31))))))
163 ;; estimate in-focus and out-of-focus excitation for this particular
165 (destructuring-bind (z y x
)
166 (array-dimensions Lf
)
167 (let* ((zz (1- (floor z
2)))
168 (in-focus (extract-bbox3-cdf Lf
(make-bbox :start
(v 0d0
0d0
(* 1d0 zz
))
169 :end
(v (* 1d0
(1- x
))
172 (save-stack-ub8 "/home/martin/tmp/Lf" (normalize3-cdf/ub8-realpart in-focus
))
173 (/ (mean-realpart in-focus
)
174 (mean-realpart Lf
)))))
178 ;; convolve a plane with the psf
179 (destructuring-bind (z y x
)
180 (array-dimensions *spheres
*)
182 (let* ((zz (vec-i-z (elt *centers
* 31)))
183 (current-slice (make-array (list 1 y x
)
184 :element-type
'(complex my-float
)
185 :initial-element
(complex 1d0
))))
186 (multiple-value-bind (conv conv-start
)
187 (convolve3-nocrop current-slice
*psf
*)
188 (defparameter *conv-plane
* conv
)
189 (defparameter *conv-plane-s
* (v--i (make-vec-i :z zz
)
191 (write-pgm "/home/martin/tmp/comp4-conv-plane.pgm"
192 (normalize2-cdf/ub8-realpart
195 (vec-i-y (v+-i conv-start
(elt *centers
* 31)))))))
196 (sb-ext:gc
:full t
)))
198 ;; multiply fluorophore concentration with light distribution, this
199 ;; gives the excitation pattern in the sample
201 (defparameter L-plane
*f
(.
* *conv-plane
* *spheres
* *conv-plane-s
*))
202 (write-pgm "/home/martin/tmp/comp5-conv-plane-lf.pgm"
203 (normalize2-cdf/ub8-realpart
204 (cross-section-xz L-plane
*f
(vec-i-y (elt *centers
* 31))))))
206 ;; estimate in-focus and out-of-focus excitation for this particular
208 (destructuring-bind (z y x
)
209 (array-dimensions L-plane
*f
)
210 (let* ((zz (1- (floor z
2)))
211 (in-focus (extract-bbox3-cdf L-plane
*f
(make-bbox :start
(v 0d0
0d0
(* 1d0 zz
))
212 :end
(v (* 1d0
(1- x
))
215 #+nil
(save-stack-ub8 "/home/martin/tmp/Lf" (normalize3-cdf/ub8-realpart in-focus
))
216 (/ (mean-realpart in-focus
)
217 (mean-realpart L-plane
*f
)))))
227 (clem)) ;; 8s, result: 6.5
231 (widefield)) ;; 5.7s result: 1.8
235 cp
/home
/martin
/0519/MedianofCelegans-10-02-09-LSM700-t58.tif ~
/tmp
/med.tif
238 for i in
*.tif
; do tifftopnm $i > `basename $i .tif`.pgm;done
245 (defun ensure-even (x)
247 (values fixnum
&optional
))
255 (defparameter *merge
*
256 (let ((a (make-array (array-dimensions *stack
*)
257 :element-type
'(unsigned-byte 8))))
258 (destructuring-bind (z y x
)
259 (array-dimensions *stack
*)
260 (do-box (k j i
0 z
0 y
0 x
)
262 (clamp (if (eq 0 (aref *blobs
*
264 (* (aref *stack
* k j i
) 2)
267 (save-stack-ub "/home/martin/tmp/merge" *merge
*))
269 ;; (- z k 1) (- y j 1) (- x i 1)
273 #+nil
;; model with isotropic pixels
274 (save-stack-ub "/home/martin/tmp/iso/iso"
275 (convert-vol (draw-scaled-spheres 2d0
*centers
*)))
278 (save-stack-ub "/home/martin/tmp/blobs" *blobs
*)
281 (save-scaled-stack 100 100 "/home/martin/tmp/sca" *blobs
*)
283 #+nil
;; find maximum
284 (reduce #'max
(map 'vector
#'abs
(sb-ext:array-storage-vector
*stack
*)))
287 (destructuring-bind (z y x
)
288 (array-dimensions *stack
*)
289 (do-box (k j i
0 z
0 y
0 x
)
290 (setf (aref *stack
* k j i
)
291 (/ (aref *stack
* k j i
) 257d0
))))
294 (save-stack "/home/martin/tmp/stack" *stack
*)
297 #+nil
;; make a text image of the psf
298 (let ((numerical-aperture 1.38d0
))
299 (psf:init
:numerical-aperture numerical-aperture
)
300 (multiple-value-bind (u v
)
301 (psf:get-uv
0 1.5 3 :numerical-aperture numerical-aperture
)
304 (a (psf:integ-all nu nv
(/ u nu
) (/ v nv
)))
307 (destructuring-bind (uu vv
)
312 (format t
"~2d" (truncate (* scale
(aref a u v
)))))
319 (save-stack "/home/martin/tmp/psf"
320 (fftshift3 (ft3 (sim-psf 128 128 128 z
(* .5d0 z
))))
321 :function
#'(lambda (x) (* .01d0
(abs x
))))))
323 ;; worm stack 0.198 um in X and Y and 1 um in Z
326 (array-dimensions *blobs
*)
328 #+nil
;; write ft of object
330 (save-stack "/home/martin/tmp/kblobs" (fftshift3 (ft3 *blobs
*))
331 :function
#'(lambda (x) (* 1d-4
(abs x
)))))
333 #+nil
;; write ft of psf
335 (destructuring-bind (z y x
)
336 (array-dimensions *blobs
*)
337 (save-stack "/home/martin/tmp/otf"
338 (fftshift3 (ft3 (sim-psf z y x
341 (ceiling (* (sqrt 2d0
) (max y x
)))))))
342 :function
#'(lambda (x) (* 1d-1
(abs x
))))))
346 (destructuring-bind (z y x
)
347 (array-dimensions *blobs
*)
348 (let ((psf (sim-psf z y x
349 (* .198d0 z
) (* .198d0
(ceiling (* (sqrt 2d0
) (max y x
)))))))
350 (save-stack "/home/martin/tmp/impsf"
351 (convolve3 *blobs
* psf
)
352 :function
#'(lambda (x) (* 1d-8
(realpart x
)))))))
356 #+nil
;; compare intensity and |e-field|^2
361 (multiple-value-bind (e0 e1 e2
)
362 (psf:electric-field-psf z x y
10d0
5d0
)
363 (let ((intens (make-array (array-dimensions e0
)
364 :element-type
'(complex my-float
))))
365 (do-box (k j i
0 z
0 y
0 x
)
366 (setf (aref intens k j i
) (complex (+ (psf::abs2
(aref e0 k j i
))
367 (psf::abs2
(aref e1 k j i
))
368 (psf::abs2
(aref e2 k j i
))))))
369 (let* ((k0 (fftshift3 (ft3 intens
)))
370 (k1 (fftshift3 (ft3 (psf:intensity-psf z y x
10d0
5d0
))))
371 (k- (make-array (array-dimensions k0
)
372 :element-type
'(complex my-float
))))
373 (do-box (k j i
0 z
0 y
0 x
)
374 (setf (aref k- k j i
) (- (aref k0 k j i
)
376 (save-stack "/home/martin/tmp/intens0"
378 :function
#'(lambda (x) (* 1d-1
(abs x
))))
379 (write-pgm (convert-img (cross-section-xz k-
)
380 #'(lambda (z) (* 1e-1 (abs z
))))
381 "/home/martin/tmp/intens0xz.pgm"))))))
385 #+nil
;; find centers of nuclei 12.5s 3.1s
387 (multiple-value-bind (c ch dims
)
389 (defparameter *centers
* c
)
390 (defparameter *center-heights
* ch
)
391 (defparameter *dims
* dims
)
392 (sb-ext:gc
:full t
)))
394 #+nil
;; draw the spheres (squeezed in z) 9.7s 1.8s
397 (destructuring-bind (z y x
)
399 (draw-ovals 7d0
*centers
* z y x
))))
400 (setf *spheres
* spheres
)
401 #+nil
(save-stack-ub8 "/home/martin/tmp/spheres" (normalize-vol *spheres
*))
402 (write-pgm (normalize-img (cross-section-xz *spheres
*
403 (vec-i-y (elt *centers
* 31))))
404 "/home/martin/tmp/spheres-cut.pgm")
405 (sb-ext:gc
:full t
)))
407 #+nil
;; draw the spheres (squeezed in z) and emphasize one of them
410 (destructuring-bind (z y x
)
412 (let* ((dims (list z y x
))
413 (points (make-array dims
414 :element-type
'(complex my-float
)))
417 (n (length centers
)))
419 (let ((c (aref centers i
)))
424 (complex (if (eq i
31)
427 (convolve3-circ points
(fftshift3 (draw-oval radius z y x
)))))))
428 (save-stack-ub8 "/home/martin/tmp/spheres" (normalize-ub8 spheres
))
429 (sb-ext:gc
:full t
)))
431 #+nil
;; construct LCOS image
432 (let ((coord (aref *centers
* 31))
434 (slice (make-array (array-dimensions *spheres
*)
435 :element-type
'(complex my-float
))))
436 (destructuring-bind (z y x
)
437 (array-dimensions *spheres
*)
438 ;; draw only the center sphere
439 (let* ((xc (vec-i-x coord
))
443 (do-rectangle (j i
0 y
0 x
)
444 (let ((r (sqrt (+ (square (* 1d0
(- i xc
)))
445 (square (* 1d0
(- j yc
)))
446 (square (* 1d0
(- k zc
)))))))
447 (setf (aref slice k j i
)
451 (defparameter *slice
* slice
)
452 #+nil
(save-stack-ub8 "/home/martin/tmp/slice" (convert-vol slice
))
453 (write-pgm (normalize-img (cross-section-xz slice
(vec-i-y coord
)))
454 "/home/martin/tmp/slice-cut.pgm")
457 #+bla
(defparameter *bfp-circ-radius
* .3d0
)
458 #+bla
(defparameter *bfp-circ-center-x
* .4d0
#+nil
(- .999d0
*bfp-circ-radius
*))
463 (angular-psf :x
80 :z
90
464 :window-x
*bfp-circ-center-x
*
465 :window-y
0d0
:window-radius
*bfp-circ-radius
*
466 :numerical-aperture
1.38d0
467 :immersion-index
1.515d0
468 :pixel-size-x
.1d0
:pixel-size-z
.5d0
469 :integrand-evaluations
160
473 #+nil
;; light distribution in the specimen
474 ;; default resolution is isotropic 12 um /64 = 187.5 nm/pixel
483 (angular-psf :window-x
*bfp-circ-center-x
*
485 :window-radius
*bfp-circ-radius
*
486 :x
(* 2 xx
) :z
(* 2 zz
)
487 :pixel-size-x dx
:pixel-size-z dz
488 :integrand-evaluations
200)))
489 (dims (destructuring-bind (z y x
)
492 (write-pgm (normalize-img (cross-section-xz psf
))
493 "/home/martin/tmp/small-psf-cut.pgm")
495 (defparameter *slice-x-psf
* (convolve3 *slice
* psf
))
496 (sb-ext:gc
:full t
)))
499 (defparameter *slice-x-psf
* nil
)
503 (write-pgm (normalize-img
504 (cross-section-xz *slice-x-psf
*
505 (vec-i-y (elt *centers
* 31))))
506 "/home/martin/tmp/slice-x-psf-cut.pgm")
509 (save-stack-ub8 "/home/martin/tmp/psf" (normalize-ub8 *psf
*))
512 #+nil
;; draw lines into the light distribution in the specimen
513 (destructuring-bind (z y x
)
514 (array-dimensions *slice-x-psf
*)
515 (let ((coord (elt *centers
* 31))
516 (vol (normalize-ub8 *slice-x-psf
*))
519 #+nil
(draw-ray-into-vol (* dx
(- (floor x
2) (vec-i-x coord
)))
520 (* dx
(- (floor y
2) (vec-i-y coord
)))
522 (loop for pos in
(list (list (* (- (floor x
2) (- (vec-i-x coord
) 7)) dx
)
523 (* (- (floor y
2) (vec-i-y coord
)) dx
))
524 (list (* (- (floor x
2) (+ (vec-i-x coord
) 7)) dx
)
525 (* (- (floor y
2) (vec-i-y coord
)) dx
))) do
526 (loop for angle in
(list ;;-.010d0
528 ;;(- (- *bfp-circ-center-x* *bfp-circ-radius*))
531 (- *bfp-circ-center-x
*)
532 ;;(- (+ *bfp-circ-center-x* *bfp-circ-radius*))
534 (draw-ray-into-vol (first pos
) (second pos
)
537 :shift-z
(- (vec-i-z coord
)
540 #+nil
(write-pgm (normalize-img
541 (cross-section-xz vol
542 (vec-i-y (elt *centers
* 31))))
543 "/home/martin/tmp/slice-x-psf-lines-cut.pgm")
544 (save-stack-ub8 "/home/martin/tmp/slice-x-psf" vol
)))
547 #+nil
;; excited fluorophores
549 (setf *slice-x-psf-times-spheres
* (.
* *spheres
* *slice-x-psf
*))
550 (save-stack-ub8 "/home/martin/tmp/slice-x-psf-times-spheres"
551 (normalize-ub8 *slice-x-psf-times-spheres
*)))
554 #+nil
;; blur with detection psf
562 (psf (psf:intensity-psf zz yy xx
(* zz dx
) (* xx dx
)
563 :integrand-evaluations
100))
564 (dims (destructuring-bind (z y x
)
567 (psf-big (make-array dims
568 :element-type
'(complex my-float
))))
569 (setf *psf-big
* psf-big
)
570 (destructuring-bind (z y x
)
572 (let ((ox (- (floor x
2) (floor xx
2)))
573 (oy (- (floor y
2) (floor yy
2)))
574 (oz (- (floor z
2) (floor zz
2))))
575 (do-box (k j i
0 zz
0 yy
0 xx
)
576 (setf (aref psf-big
(+ oz k
) (+ oy j
) (+ ox i
))
578 (save-stack-ub8 "/home/martin/tmp/psf-detect-big" (normalize-ub8 psf-big
))
580 (defparameter *camera-volume
* (convolve3-circ *slice-x-psf-times-spheres
*
581 (fftshift3 psf-big
)))
582 (save-stack-ub8 "/home/martin/tmp/camera-volume"
583 (normalize-ub8 *camera-volume
*))
584 (sb-ext:gc
:full t
)))
588 #+nil
;; check convolution
590 (let ((a (make-array (list 64 64 64)
591 :element-type
'(complex my-float
)))
592 (b (psf:intensity-psf
64 64 64 20d0
20d0
) #+nil
(make-array (list 64 64 64)
593 :element-type
'(complex my-float
))))
594 (setf (aref a
12 12 12) (complex 255d0
))
595 #+nil
(setf (aref b
0 0 0) (complex 255d0
))
596 (save-stack-ub8 "/home/martin/tmp/conv-test" (normalize-ub8 (convolve3-circ a
(fftshift3 b
))))))
599 #+nil
;; output the xz cross section centered on a sphere in the middle
600 (let ((coord (aref *centers
* 30)))
601 (write-pgm (normalize-img (cross-section-xz *spheres
* (* 5 (vec-i-z coord
))))
602 "/home/martin/tmp/cut-spheres.pgm")
603 (write-pgm (normalize-img (cross-section-xz *psf-big
*))
604 "/home/martin/tmp/cut-psf-big.pgm")
605 (write-pgm (normalize-img (cross-section-xz *slice-x-psf
* (* 5 (vec-i-z coord
))))
606 "/home/martin/tmp/cut-slice-x-psf.pgm")
607 (write-pgm (normalize-img (cross-section-xz (.
* *spheres
* *slice-x-psf
*) (* 5 (vec-i-z coord
))))
608 "/home/martin/tmp/cut-exfluo.pgm"))
611 (save-stack-ub8 "/home/martin/tmp/spheres" (normalize-ub8 *spheres
*))
614 (let ((sli (make-array (array-dimensions *spheres
*)
615 :element-type
'(complex my-float
))))
616 (destructuring-bind (z y x
)
617 (array-dimensions *spheres
*)
618 (do-box (k j i
0 z
0 y
0 x
)
619 (setf (aref sli k j i
)
620 (aref *spheres
* k j i
)))
621 (let ((k (* 5 (vec-i-z (aref *centers
* 30)))))
622 (do-rectangle (j i
0 y
0 x
)
623 (setf (aref sli k j i
)
624 (* 10 (aref sli k j i
)))))
625 (defparameter *sli
* sli
))
626 (save-stack-ub8 "/home/martin/tmp/spheres" (normalize-ub8 *sli
*)))
631 (let ((a (sb-ext:array-storage-vector
*slice
*)))
632 (reduce #'max
(map 'vector
#'abs a
)))
637 #+nil
;; model with unscaled spheres
638 (defparameter *blobs
*
639 (destructuring-bind (z y x
)
641 (draw-spheres 7d0
*centers
* (* 5 z
) y x
)))
644 #+nil
;; print ft of angular psf
645 (time (let* ((radius .5d0
)
647 (psf (angular-psf x
0d0 radius
)))
648 (write-pgm (normalize-ub8 (cross-section-xz (fftshift3 (ft3 psf
))))
649 "/home/martin/tmp/cut-intens.pgm")))
651 (let* ((intens (psf:intensity-psf
64 64 64 10d0
5d0
))
652 (k0 intens
#+nil
(fftshift3 (ft3 intens
))))
653 (save-stack "/home/martin/tmp/intens1" k0
654 :function
#'(lambda (x) (* 1d-5
(abs x
))))
655 (write-pgm (convert-img
656 (cross-section-xz k0
)
657 #'(lambda (z) (* 1d-4
(abs z
))))
658 "/home/martin/tmp/intens1xz.pgm"))
662 (destructuring-bind (z y x
)
663 (array-dimensions *blobs
*)
664 (save-stack "/home/martin/tmp/blobs"
669 #+nil
;; clean up the garbage
674 (sb-vm:memory-usage
:print-spaces t
:count-spaces t
)
679 (sb-vm:instance-usage
:dynamic
)
682 (sb-vm:list-allocated-objects
:dynamic
)
685 (sb-vm:print-allocated-objects
:dynamic
)
688 (format t
"~a~%" (sb-vm::type-breakdown
:dynamic
))