-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProcessing.py
More file actions
executable file
·317 lines (265 loc) · 9.23 KB
/
Processing.py
File metadata and controls
executable file
·317 lines (265 loc) · 9.23 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
"""
Processing.py
Created by Peter Combs (peter.combs@berkeley.edu) on 2009-12-10
"""
USAGE = """python %s --specification='pattern' [options]
Options and Arguments:
-b : Specify the location for a particular background image
-B n : Generate a background image from n randomly selected images in the glob pattern
-c n : Cap the number of input images at n
-e n : Specify the image to end before
-o file : Specify a file to save the output in. The .mat suffix is optional
-p : Profiling mode supresses display of points at the end of the run.
-q : Use a queue to do processing instead of a pool (# may not work as of 01/10/2010)
-s n : Specify the image to start with
-t n : Specify a threshold value for point detection
-D : Debugging mode. Will output lots of graphs, messages, etc
-h : This help message
"""
import Analysis
from multiprocessing import Manager, Pool, cpu_count, Value, Process
from multiprocessing.managers import SyncManager
from scipy.io import savemat
import pyfits
from glob import glob
import sys, re
from os import path
from time import time, sleep, strftime
import signal
reload(Analysis)
THRESH = 1500
DEBUG = False
NORMAL_FLAG = 0
STOP_FLAG = 1
def single_processor(filequeue, pointslist, donelist, bgimg = None,
basedir = None, thresh=THRESH):
while not filequeue.empty():
fname = filequeue.get()
frame_num = int(path.splitext(path.split(fname)[-1])[0])
if basedir:
pass
if len(donelist)%100 == 0:
sys.stderr.write("Completed %d\n"%(len(donelist)))
sys.stderr.write("Found %d holes\n" %
len(pointslist))
try:
img1, img2 = Analysis.loadsplit(fname, bgimg = bgimg)
pointslist.extend(Analysis.findpairs(img1, img2, thresh=thresh,
DEBUG=DEBUG, abs_thresh=True,
frame_num = frame_num))
donelist.append(fname)
except IOError:
pass
except KeyboardInterrupt:
return
def processor(args):
"""map-able function that processes a single frame of data
Argument: a single tuple composed of the following, in order
file_name : string, required
pointslist : list, required
file_number : ignored, optional
background : array, optional
donelist : list, optional
A list of files that have already been processed
status : ??????
"""
fname = args[0]
frame_num = int(path.splitext(path.split(fname)[-1])[0])
pointslist = args[1]
if len(args) > 2: i = args[2]
if len(args) > 3:
bgimg = args[3]
else:
bgimg = None
if len(args) > 4:
donelist = args[4]
donelist.append(i)
if len(donelist)%1000 == 1:
sys.stderr.write("Completed %d\n"%(len(donelist)-1))
sys.stderr.write("Found %d holes\n" % len(pointslist))
if len(donelist)%10000 == 0 and len(pointslist) > 0:
xl, yl, varxl, varyl, el, xr, yr, varxr, varyr, er, framenum =\
zip(*list(pointslist))
savemat('tempfile.mat',
{'xl':xl, 'yl':yl, 'varxl': varxl, 'xr':xr, 'yr':yr,
'el': el, 'er': er, 'varxr': varxr, 'framenum':framenum},
oned_as = 'row')
sys.stderr.write("Saved a snapshot\n")
if len(args) > 5:
THRESH = args[5]
# Finally, load in the image
try:
img1, img2 = Analysis.loadsplit(fname, bgimg = bgimg)
except IOError:
print "Failed loading!"
return
pointslist.extend(Analysis.findpairs(img1, img2, thresh=THRESH,
DEBUG=DEBUG, abs_thresh=True,
frame_num = frame_num))
#pointslist.append((0,0,0,0))
def user_input(val):
"""docstring for user_input"""
while True:
sleep(1)
interaction = sys.stdin.readline().strip()
print "Received message from user '%s'" % interaction
if interaction == "stop":
val = STOP_FLAG
return
def makebackground(filelist, n = 300):
import numpy as np
from random import randrange as rand
filedesc = Analysis.imread(filelist[0])
[r,c] = np.shape(filedesc)
mat = np.empty((r,c,n))
for i in range(n):
mat[:,:,i] = Analysis.imread(filelist[rand(len(filelist))])
return np.median(mat, axis=2)
def inthandler(signum, frame):
val= STOP_FLAG
print frame
print signum
print "Keyboard Interrupt should stop processes gracefully"
if __name__ == '__main__':
import os
import sys
from getopt import getopt, GetoptError
from optparse import OptionParser
### Option Defaults ###
parser = OptionParser(usage="Usage: %prog -d FILES")
parser.set_defaults(multi=True, profile=False, queueing=False,
savefile=strftime('%m%d-map'), start=0, end=None, debug=0,
thresh=5000, bgname=100, dirname='./*/*/*.tif')
### Input/Output Options ###
parser.add_option('-f', '--specification', dest='dirname',
help='Glob pattern for files to process'
' (e.g. G:\\Data\\Map\\*\\*)')
parser.add_option('-o', '--output-file', dest='savefile',
help='Output file (defaults to a date-specific form'
', which is %default.mat today)')
parser.add_option('-B', '--make-bg', dest='bgname',
help="Make a background from NUM randomly selected images",
metavar="NUM", type="int")
parser.add_option('-b', '--bgimg', dest='bgname',
help="Use BGNAME as the background image")
### Parallel processing ###
parser.add_option('-1', '--single-processor', dest='multi',
help="Use Single Processor logic",
action="store_false")
parser.add_option('-m', '--multiprocessor', dest='multi',
help="Use Multiple Processors",
action="store_true")
parser.add_option('-q', '--queue', dest='queueing',
help="Use Queueing logic (probably broken!)",
action="store_true")
### Debugging-type options
parser.add_option('-D', '--debug', dest='debug',
help="Increase debug level (higher = more data)",
action="count")
parser.add_option('-p', '--profiling', dest='profile',
help='Puts program in profiling mode'
' (disables most plotting and waiting)',
action='store_true')
parser.add_option('-t', '--threshold', dest='thresh', type="int",
help="Threshold for holes above the background")
### Subsets of the image set ###
parser.add_option('-s', '--start', dest='start', type='int',
help="Start at the START-th frame")
parser.add_option('-e', '--end', dest='end', type='int',
help="End at the ENDth frame")
def cutoff_callback(option, opt_str, value, parser):
parser.values.end = parser.values.start + value
#option.end = option.start + value
parser.add_option('-c', '--cutoff', type="int", metavar="CUTOFF",
help="Process a maximum of CUTOFF frames, starting with the"
" START-th (which defaults to the first)",
action="callback", callback=cutoff_callback)
(opts, args) = parser.parse_args()
sys.stderr.write( "Setting up list\n")
val = 0
m = Manager()
pointlist = m.list()
donelist = m.list()
sys.stderr.write( "Setting up Pool\n")
p = Pool(processes=cpu_count()+2)
print "Globbing on '%s'" % opts.dirname
filelist = glob(opts.dirname)
print "Globbed"
fulllist = filelist
filelist = filelist[opts.start:opts.end]
n = len(filelist)
sys.stderr.write("Found %d files\n" % n)
if n == 0:
print "Could not find any files! Quitting..."
sys.exit(1)
if isinstance(opts.bgname, str):
bgimg = pyfits.open(opts.bgname)[0].data
else:
print "Making background image"
bgimg = makebackground(fulllist, opts.bgname)
print "Done making background image"
hdu = pyfits.PrimaryHDU(bgimg)
print "S1"
hdulist = pyfits.HDUList([hdu])
print "S2"
if os.path.exists(opts.savefile+'.fits'):
os.renames(opts.savefile+'.fits', opts.savefile+'.fitsold')
hdulist.writeto(opts.savefile+'.fits')
if opts.debug > 0:
from pylab import *
imshow(bgimg)
colorbar()
try:
print "Loaded background image"
processes = []
print r"Starting in 3 \a"
sleep(1)
print r"2 \a"
sleep(1)
print r"1 \a"
sleep(1)
print r"Go! \a\a"
tic = time()
if opts.queueing:
q = m.Queue()
for n in filelist:
q.put(n)
for n in range(cpu_count()):
processes.append(Process(target=single_processor,
args=(q, pointlist, donelist, bgimg)))
processes[n].start()
sys.stderr.write("Starting process %d\n"%n)
for n in range(cpu_count()):
processes[n].join()
else:
if opts.multi:
res = p.map(processor, zip(filelist, [pointlist]*n,
range(n), [bgimg]*n, [donelist]*n, [opts.thresh]*n))
else:
res = map(processor, zip(filelist, [pointlist]*n, range(n),
[bgimg]*n, [donelist]*n, [opts.thresh]*n))
except KeyboardInterrupt:
print "BALLS!"
from pylab import show
show()
except MemoryError:
pointlist = list(pointlist)[0:-50]
print "Memory Error! Only made it as far as %d" % len(donelist)
finally:
print "For %d frames, found %d spots, in %f seconds (%f fps)" % \
(len(filelist), len(pointlist), time() - tic,
(len(filelist))/(time() - tic + 1e-10))
if len(pointlist) != 0:
xl, yl, varxl, varyl, el, xr, yr, varxr, varyr, er, framenum = zip(*list(pointlist))
savemat(opts.savefile,
{'xl':xl, 'yl':yl, 'xr':xr, 'yr':yr,
'el': el, 'er': er, 'framenum':framenum, 'varxr' : varxr,
'varyr' : varyr, 'varxl': varxl, 'varyl' : varyl},
oned_as = 'row')
if not opts.profile:
from pylab import *
figure()
plot(xl, yl, 'bx')
figure()
plot(xr, yr, 'ro')
show()