forked from rflrob/YildizLabCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalysis.py
More file actions
executable file
·587 lines (463 loc) · 19 KB
/
Analysis.py
File metadata and controls
executable file
·587 lines (463 loc) · 19 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
#!/usr/bin/env python
# encoding: utf-8
"""
Analysis.py
Created by Peter Combs on 2009-11-17.
Based on Adam Cohen's Matlab code for two color reconciling.
Copyright (c) 2009 UC Berkeley. All rights reserved.
"""
#from pylab import *
import sys, os, Image
import numpy as np
from numpy import array, zeros, histogram, concatenate, sqrt, shape, \
meshgrid, ravel, exp, arange, mgrid, linspace, random, mean, \
NaN, reshape, median, sum, cumsum, diff
import numpy.numarray.nd_image as ndi
import scipy.interpolate as interp
import scipy.linalg as la
import scipy.optimize as optimize
from scipy.special import jn as bessel
from Mapping import *
from fitgauss import fitgauss
from pyfits import open as pfopen
from pylab import figure, colorbar, title, plot, savefig, show, legend, \
imshow, pcolor, cm, ion
from time import time
from multiprocessing import Pool
np.seterr(invalid='ignore')
box1_default = (slice(0,256),slice(0,512))
box2_default = (slice(256, 512), slice(0, 512))
def main():
from scipy.io import loadmat
from getopt import getopt
try:
optlist, args = getopt(sys.argv[1:], 'b:e:s:n:')
print optlist, args
except:
sys.exit(2)
matfile = 'big.mat'
begin = 1
end = 36
step = 1
n = None
for o,a in optlist:
if o in ('-b'):
begin = int(a)
elif o in ('-e'):
end = int(a)+1
elif o in ('-s'):
step = int(a)
elif o in ('-n'):
n = int(a)
else:
assert False, "Unhandled option"
if len(args) > 0:
matfile = args[0]
data = loadmat(matfile, struct_as_record = False, squeeze_me = True)
xl = data['xl']
yl = data['yl']
xr = data['xr']
yr = data['yr']
xlo, xhi = middle_percent(xl, .2)
ylo, yhi = middle_percent(yl, .2)
sel = (xlo < xl) * (xl < xhi) * (ylo < yl) * (yl < yhi)
print "Starting with %d points" % len(xl)
xl = xl[sel]
yl = yl[sel]
xr = xr[sel]
yr = yr[sel]
print "Cropping down to %d points" % len(xl)
if n == None: n = 0.1 * sqrt(len(xl))
make_spline_mesh(xl, yl, xr, yr, n)
tres = zeros(end)
rmss = zeros(end)
rmsg = zeros(end)
for i in range(begin, end, step):
good, tre = sample_target_registration_error(xl, yl, xr, yr,i, n=5)
if tre < 20:
tres[i] = tre
mapping = makeLSQspline(xl, yl, xr, yr)
xn, yn = mapping(xr, yr)
rmss[i] = sqrt(mean((xn - xl)**2 + (yn - yl)**2) )
else:
tres[i] = NaN
rmss[i] = NaN
figure()
plot(arange(len(tres)), tres, 'ro-', label='TRE')
plot(arange(len(rmss)), rmss, 'gx--',label='RMS')
title("Errors on %d points" % len(xl))
legend()
savefig('sTREvsNKnots.pdf')
print "just finished ", i
show()
print "TREs\t\tRMSs\t\tgood RMSs"
for i in range(len(tres)):
print "%f\t\t%f\t\t%f" % (tres[i], rmss[i], rmsg[i])
mapping = makeLSQspline(xl[good], yl[good], xr[good], yr[good])
xn, yn = mapping(xr[good], yr[good])
rms = sqrt(mean((xn-xl[good])**2 + (yn - yl[good])**2))
print "RMS Error with best points only: ", rms
sys.stderr.write("RMS Error with best points only: %f\n" % rms)
target_registration_error(xl[good], yl[good], xr[good], yr[good])
def split(img, bgimg = None, box1 = box1_default, box2 = box2_default):
if bgimg != None:
img -= bgimg
imgl = img[box1]
imgr = img[box2]
imgl -= imgl.min()
imgr -= imgr.min()
imgl *= float(imgr.max())/imgl.max()
return imgl, imgr
def imread(fname):
ext = os.path.splitext(fname)[1]
try:
if ext == '.tif':
imf = Image.open(fname)
img = np.reshape(imf.getdata(), imf.size)
elif ext == ".fits":
img = pfopen(fname)[0].data
else:
sys.stderr.write("Unrecognized file type '%s'!\n" % ext)
sys.exit(10)
except:
print "Had a problem with ", fname
return img
def loadsplit(fname, bgimg = None, box1 = box1_default, box2 = box2_default):
"""function [imgl imgr] = loadsplit(fname, imstart,imstop, box1, box2)
loads a single FITS image and splits it into two pieces.
fname: full path and filename for the input.
box1: 2 element tuple of slices.
box2: vector specifying the second sub-image.
The boxes shouldn't contain dark borders around the edges of the images.
Typically:
box1 = (slice(18,247),slice(7,506))
box2 = (slice(276, 505), slice(7, 506))
"""
img = imread(fname)
return split(img, bgimg, box1, box2)
def stretch(image):
""" Rescales an image to the range [0, 1]"""
m = image.min()
return (image - m)/(image.max() - float(m))
def otsu_threshold(image):
""" Returns the optimal threshold, as calculated using Otsu's Algorithm on
the data squeezed into 8 bits
"""
tick = time()
m = image.min()
M = image.max()
image = 255 * (image - m)/(M-m)
hgram, edges = histogram(image.flat, bins = arange(256))
num_b = 0
num_a = len(image.flat)
total_b = 0
total_a = float(image.sum())
varBetween = zeros(len(hgram))
for t in range(len(hgram)):
num_b += hgram[t]
num_a -= hgram[t]
total_b += t * hgram[t]
total_a -= t * hgram[t]
mu_b = total_b/(num_b+1e-13)
mu_a = total_a/(num_a+1e-13)
varBetween[t] = (num_b * num_a) * (mu_b - mu_a)**2
threshold = varBetween.argmax()
return threshold * (M - m)/256.0 + m
def strel():
"""Creates a disk-shaped structuring element. """
return array([[0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0] ])
def mpt(im_in, thresh = 1.0, im_start=0, im_end = None, **kwargs):
"""Multiple Particle tracking.
Using the stacked image from im_in, finds particles and localizes them
with the fitting2d function
Return
------
a list of dictionaries, each of which contains keys whose values are:
'numspots': Number of spots detected
'x2': array of x coordinates of the spots
'y2': array of y coordinates of the spots
'varx': array of width of the fit in x
'vary': array of width of the fit in y
'offset': array of level of the background
'amp': array of height of the fit above background
'exits': array of return values from the fitting
Optional Keyword Arguments
--------------------------
DEBUG : Outputs debugging information, especially graphs; Default is False
smooth : use a gaussian blur to smooth the data (?why); Default is False
abs_thresh :
"""
smooth = False
abs_thresh = False
DEBUG = False
padding = 7
for key in kwargs:
if key == "smooth": smooth = kwargs[key]
elif key == "abs_thresh": abs_thresh = kwargs[key]
elif key == "frame_num" : frame_num = kwargs[key]
elif key == "DEBUG": DEBUG = kwargs[key]
elif key == "padding": padding = kwargs[key]
if(im_end == None):
if len(shape(im_in)) == 3:
im_end = shape(im_in)[2]
else:
# Reshape to a stack if only a 1-D image
im_end = 1
im_in = reshape(im_in, (shape(im_in)[0], shape(im_in)[1], 1))
coords = [None]*(im_end - im_start)
for i in range(im_start, im_end):
if DEBUG > 0: print i, "of ", im_end - im_start
A = im_in[:,:,i]
if smooth: bA = ndi.gaussian_filter(A, 2)
else: bA = A
if abs_thresh:
T = 1
else:
T = otsu_threshold(bA)
Amask = bA > (T * thresh)
l, n = ndi.label(Amask)
if DEBUG > 1:
ion()
figure(1138)
imshow(Amask*bA, cmap = cm.gray)
colorbar()
title('Mask: Foo > %f' % (T * thresh))
print T
print thresh
print "Number of spots found: ", n
if n == 0:
x2 = []
y2 = []
varx = []
vary = []
amp = []
offset = []
exits = []
else:
D = ndi.find_objects(l) # Generates a list of slices
# Set up empty variables
x2 = zeros(n)
y2 = zeros(n)
varx = zeros(n)
vary = zeros(n)
amp = zeros(n)
offset = zeros(n)
exits = zeros(n)
for j in range(n):
row, col = D[j]
# Make sure fitbox is legal
rstart = max(0, row.start - padding)
rend = min(shape(im_in)[0]-1, row.stop + padding)
cstart = max(0, col.start - padding)
cend = min(shape(im_in)[1]-1, col.stop + padding)
# Take fitbox and subtract background
B = im_in[rstart:rend, cstart:cend, i]
B = B - B.min()
# Coordinates for fitting
dy, dx = mgrid[rstart:rend, cstart:cend]
if DEBUG > 2:
figure()
pcolor(dx, dy, B)
colorbar()
try:
fit, exit = fit2d(dx, dy, B)
x2[j] = fit[0]
y2[j] = fit[1]
varx[j] = fit[2]
vary[j] = fit[3]
offset[j] = fit[4]
amp[j] = fit[5]
exits[j] = exit
# If fit is too close to the edge of the frame, then ignore it
if (not (padding < x2[j] < shape(im_in)[1] - padding)) \
or (not (padding < y2[j] < shape(im_in)[0] - padding)):
x2[j] = float("nan")
y2[j] = float("nan")
exits[j] = -1
if DEBUG > 1:
print "Fitted a point at:", fit
except ValueError, err:
print "Value error", err
exits[j] = -1138
coords[i] = {'x2': x2, 'y2': y2, 'varx': varx, 'vary': vary,
'offset': offset, 'amp': amp, 'exits': exits, 'numspots': n}
if DEBUG > 1:
figure(1138)
plot(x2, y2, 'rx')
sys.stderr.write(str(coords[i]) + '\n')
return coords
def gaussian(center_x, center_y, width_x, width_y, offset, height):
"""Returns a gaussian function with the given parameters"""
width_x = float(width_x)
width_y = float(width_y)
return lambda x,y: height*exp(-(((center_x-x)/width_x)**2 \
+ ((center_y-y)/width_y)**2)/2) + offset
def airy(center_x, center_y, widthx, widthy, offset, height):
"""Returns an airy disc function with the given parameters"""
def airy_func(x,y):
"A function to fit the Airy Disk for the given input"
X = sqrt((x - center_x)**2/widthx + (y - center_y)**2/widthy)
return offset + height * (bessel(1,X)/(X+1e-6)) **2
return airy_func
def fit2d(xs, ys, data, estimate = None, fcn = None):
"""Finds the best parameters for a gaussian distribution. Parameters are
in the order:
center_x, center_y, width_x, width_y, offset, height
"""
if estimate == None:
estimate = zeros(6)
estimate[0] = sum(xs * data)/ float(sum(data)) # Centroid
estimate[1] = sum(ys * data) / float(sum(data))
estimate[2] = 1.3 # Empirically determined
estimate[3] = 1.3
estimate[4] = float(data.min())
estimate[5] = float(data.max() - data.min())
if fcn == None:
fcn = gaussian
errorfunction = lambda p: ravel(fcn(*p)(*(xs, ys)) - data)
p, c,d, mesg, success = optimize.leastsq(errorfunction, estimate, full_output = True, ftol=1e-3)
return p, success
def findpairs(imleft, imright, thresh=1.0, radius=15, DEBUG = False, abs_thresh = False, frame_num = 0):
""" finds pairs of particles in a stack of images from the dual-viewer
Returns
-------
pairs: list
a list containing tuples of (xl, yl, xr, yr)
Parameters
----------
imleft : array-like
the left hand image stored in a numpy array
imright : array-like
the right hand image stored in a numpy array
thresh : double, optional
The threshold level for finding particles.
radius : double, optional
The radius around a point to consider another point a "match".
DEBUG : boolean, optional
Print debugging statments/show images along the way.
"""
# Find points on the two images
coordsl = mpt(imleft, thresh, abs_thresh = abs_thresh, smooth=False, DEBUG=DEBUG)[0]
coordsr = mpt(imright, thresh, abs_thresh = abs_thresh, smooth=False, DEBUG=DEBUG)[0]
pairs = []
if DEBUG > 0:
sys.stderr.write('%d spots on left\n' %coordsl['numspots'])
sys.stderr.write('%d spots on right\n' %coordsr['numspots'])
for i in range(coordsl['numspots']):
for j in range(coordsr['numspots']):
xl = coordsl['x2'][i]
yl = coordsl['y2'][i]
varxl = coordsl['varx'][i]
varyl = coordsl['vary'][i]
ampl = coordsl['amp'][i]
xr = coordsr['x2'][j]
yr = coordsr['y2'][j]
varxr = coordsr['varx'][j]
varyr = coordsr['vary'][j]
ampr = coordsr['amp'][j]
# If the distance is within tolerance, add data to the list of pairs
delta = sqrt((xl - xr)**2 + (yl - yr)**2)
if delta < radius:
# Calculate a (possibly bullshit) estimate of the error.
el = sqrt((varxl**2 + varyl**2)/ampl)
er = sqrt((varyr**2 + varyr**2)/ampr)
pairs.append((xl + box1_default[1].start+1, 512-(yl + box1_default[0].start),
varxl, varyl, el,
xr + box2_default[1].start+1, 512-(yr + box2_default[0].start),
varxr, varyr, er, frame_num))
# The massaging to xl, yl, xr, and yr is to make the output match WHTrack
if DEBUG > 0:
sys.stderr.write('Paired spots %d and %d'%(i,j))
return pairs
def make_spline_mesh(xl, yl, xr, yr, num = 30, sampling=5, mapping = None):
"""Makes spline fit of the given order, then draws a mesh representing
that spline fit
"""
xs, ys = meshgrid(linspace(xl.min()+10, xl.max() - 10, num = sampling*num),
linspace(yl.min()+10, yl.max()-10, num = sampling*num))
if mapping == None:
mapping = makeLSQspline(xl, yl, xr, yr, num)
xn, yn = mapping(xs.ravel(), ys.ravel())
xn = reshape(xn, shape(xs))
yn = reshape(yn, shape(ys))
for i in range(num):
plot(xn[:,i], yn[:,i], 'r-')
plot(xn[i,:], yn[i,:], 'r-')
savefig('foobar.png')
show()
def make_reg_mesh(xl, yl, xr, yr, order=2):
""" One way of attempting to visualize a regression between the two sets"""
xs, ys = meshgrid(linspace(xl.min(), xl.max()),
linspace(yl.min(), yl.max()))
mapping = makeregression(xl, yl, xr, yr, order=order)
xn, yn = mapping(xs.flatten(), ys.flatten())
plot(xn.flatten(), yn.flatten(), ',g', label='Regression')
savefig('foobar.png')
def target_registration_error(xl, yl, xr, yr, param=None):
"""calculates the Target Registration Error
by setting aside each coordinate pair, calculating a 2nd order polynomial
fit from the right channel onto the left channel, then finding the error
of the un-set point. The mean of these errors is calculated and returned.
WARNING: Runs in O(n^3) time.
"""
from time import time
tick = time()
elist = zeros(len(xl))
for i in range(len(xl)):
xl_d = concatenate((xl[0:i], xl[i+1:]))
yl_d = concatenate((yl[0:i], yl[i+1:]))
xr_d = concatenate((xr[0:i], xr[i+1:]))
yr_d = concatenate((yr[0:i], yr[i+1:]))
mapping = makeLSQspline(xl, yl, xr, yr, param)
xn, yn = mapping(xr[i], yr[i])
#print "xn\t",xn, "yn\t",yn,
elist[i] = sqrt((xl[i] - xn)**2 + (yl[i] - yn)**2)
#print "e\t", elist[i]
if (i % 100 == 0): sys.stderr.write('%d\n'%i)
print "\n\ntime: ", time()-tick
print "TRE: ", elist.mean()
return elist < (4*median(elist)), elist.mean()
def sample_target_registration_error(xl, yl, xr, yr,
param=None, frac=.1, n = 100):
"""Samples the Target Registration Error
by setting aside frac*len(xl) pairs, calculating a 2nd order
polynomial fit from the right channel onto the left channel,
then finding the rms error of the un-set points. This
process is repeated n times, and the mean of these errors
is calculated and returned
"""
from time import time
tick = time()
elist = zeros(n)
elist2 = zeros(len(xl))
for i in range(n):
sel = random.random(len(xl)) > frac
xl_d = xl[sel]
yl_d = yl[sel]
xr_d = xr[sel]
yr_d = yr[sel]
mapping = makeLSQspline(xl_d, yl_d, xr_d, yr_d, param)
xn, yn = mapping(xr[~sel], yr[~sel])
elist[i] = mean(sqrt((xl[~sel] - xn)**2 + (yl[~sel] - yn)**2))
elist2[~sel]+= sqrt((xl[~sel] - xn)**2 + (yl[~sel] - yn)**2)
if (i % 100 == 0): sys.stderr.write('%d\n'%i)
print "\n\ntime: ", time()-tick
print "TRE: ", elist.mean()
print "Avg Error: ", (elist2/(frac*n)).mean()
return elist2 < (4*median(elist2)), elist.mean()
def middle_percent(points, frac):
"""Returns the values between which 1-frac of the data lies."""
sorted_points = sorted(points)
hi = 1 - frac/2.0
lo = frac/2.0
return sorted_points[int(lo*len(points))],sorted_points[int(hi*len(points))-1]
if __name__ == '__main__':
main()