-
Notifications
You must be signed in to change notification settings - Fork 3
/
lmax.py
500 lines (391 loc) · 14.2 KB
/
lmax.py
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
# Local maximum class. Indended to identify, reject, and compare local
# maxima based on an associated data set and mask.
# &%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%
# IMPORTS
# &%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%
import time, copy, sys
import numpy as np
from scipy.ndimage import maximum_filter
from scipy.ndimage import grey_dilation
from scipy.ndimage import uniform_filter
from scipy.ndimage import label, find_objects
import matplotlib.pyplot as plt
from pyprops import cube, mask, noise
from struct import *
from levutils import *
# &%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%
# LOCAL MAXIMA LIST OBJECT
# &%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%
# The maxima are treated as a set held inside a single object
class Lmax():
"""
...
"""
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Attributes
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
num = 0
name = None
indices = None
val = None
merger_matrix = None
merer_levels = None
linked_data = None
linked_mask = None
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Initialize
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
def __init__(
self,
data = None,
mask = None
):
"""
Construct a new local maximum object.
"""
if data != None:
self.link_to_data(data)
if mask != None:
self.link_to_mask(mask)
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Links to data cube
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
def link_to_data(
self,
val=None
):
"""
Link the lmax object to a data object.
"""
if val != None:
self.linked_data = val
def link_to_mask(
self,
val=None
):
"""
Link the lmax object to a mask object.
"""
if val != None:
self.linked_mask = val
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Read/write
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# TBD
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Manipulate local maxima by hand
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Some of this is pretty inefficient...
def add_local_max(
self,
new_indices=None,
append=True
):
"""
"""
if new_indices == None:
return
if new_indices.ndim == 1:
new_indices = new_indices.reshape(1,self.linked_data.data.ndim)
if append and self.indices != None:
new_indices = np.append(self.indices, new_indices, 0)
else:
new_indices = new_indices
self.indices = new_indices
self.recalc_from_ind()
return self.name[-1]
def del_local_max(
self,
):
"""
"""
pass
def recalc_from_ind(
self
):
self.val = self.linked_data.data[self.as_tuple()]
self.num = self.indices.shape[0]
self.name = np.arange(self.num)+1
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Access
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
def as_tuple(
self
):
"""
Return indices as tuple.
"""
return cube.xyzarr_to_tuple(self.indices, coordaxis=1)
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Find local maxima
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# There are several ways to do this: rank filters, rolling the
# cube. The quickest numpy way seems to be a maximum filter
# followed by a uniform filter. The downside of this is that it
# only allows square search kernels. The rank filter way to do it
# is included below (commented out) but is very slow.
def all_local_max(
self,
sky_halfbox = 3,
spec_halfbox = 1,
search_kernel = None,
timer=False,
):
"""
Extract local maxima using a maximum filter.
"""
# ............................
# Start the timer if requested
# ............................
if timer:
start=time.time()
# ........................................................
# Copy the data to suppress emission outside the mask by
# setting it to a low value (less than the original minimum of
# the data).
# ........................................................
data = copy.deepcopy(self.linked_data.data)
low_value = np.min(data[self.linked_data.valid])-1.
data[self.linked_data.valid == False] = low_value
if self.linked_mask != None:
data[self.linked_mask.data == False] = low_value
# ........................................................
# Generate the filter and handle dimensions
# ........................................................
if self.linked_data.spec_axis == 0:
uniform_size = (spec_halfbox*2+1,
sky_halfbox*2+1,
sky_halfbox*2+1)
if self.linked_data.spec_axis == 1:
uniform_size = (sky_halfbox*2+1,
spec_halfbox*2+1,
sky_halfbox*2+1)
if self.linked_data.spec_axis == 2:
uniform_size = (sky_halfbox*2+1,
sky_halfbox*2+1,
spec_halfbox*2+1)
footprint = np.ones(uniform_size)
uniform_total = (sky_halfbox*2+1)* \
(sky_halfbox*2+1)* \
(spec_halfbox*2+1)*1.
# ........................................................
# Apply the filter
# ........................................................
# Is there any danger in floating point here?
max_image = maximum_filter(
data,
footprint=footprint,
mode="constant",
cval=low_value)
lmax_cube = (data == max_image)*(data != low_value)
# This next step ensures uniquness (i.e., that you are the
# *only* local maximum) at the expense of a second filter.
max_count = uniform_filter(
lmax_cube*uniform_total,
size=uniform_size,
mode="constant", cval=0.)
lmax_cube *= (max_count == 1)
# This works and is clean and arbitrary-shaped, but it's
# incredibly slow:
# max_image = rank_filter(data,
# rank=-1,
# footprint=footprint,
# mode="constant",
# cval=np.min(data))
# rank2_image = rank_filter(data,
# rank=-2,
# footprint=footprint,
# mode="constant",
# cval=np.min(data))
# lmax_cube = (data == max_image)*(max_image != rank2_image)
# Extract the maxima
# ........................................................
# Record the maxima
# ........................................................
self.indices = np.vstack(np.where(lmax_cube)).transpose()
self.recalc_from_ind()
# ........................................................
# Report on timing
# ........................................................
if timer:
stop=time.time()
print "Generating local max candidates took ", stop-start
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Calculations Involving Merger Levels
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Note the conceptual issue here - we often want to clip on a
# signal-to-noise based merger level but also want the merger
# levels in real units. That's great, except that sometimes the
# cube may have variable noise and the result will no longer be
# identical.
def calc_merger(
self,
levels=None,
corners=False,
verbose = False,
timer = False
):
# Initialization
if timer:
start=time.time()
full_start=time.time()
if self.num == 0 or self.indices == None:
print "Find candidate local maxima before calculating mergers."
return
# Copy the data to suppress emission outside the mask by
# setting it to a low value (less than the original minimum of
# the data).
data = copy.deepcopy(self.linked_data.data)
if self.linked_mask != None:
use = self.linked_mask.data*self.linked_data.valid
else:
use = self.linked_data.valid
min_use = np.min(self.linked_data.data[use])
max_use = np.max(self.linked_data.data[use])
low_value = min_use-1.
data[(use==False)] = low_value
# Initialize default levels - if we have a noise value, treat
# the RMS as a reasonable spacing (arguing that we cannot
# distinguish much more than this finely). Else space 100
# levels between the minimum and maximum value.
if levels == None:
if self.linked_data.noise != None:
print "I will default to one sigma spacing to calculate mergers."
print "... you may want finer spacing depending on your needs."
levels = contour_values(
linspace = True,
maxval = max_use,
minval = min_use,
spacing = 1.0*self.linked_data.noise.scale
)
else:
levels = contour_values(
linspace = True,
maxval = max_use,
minval = min_use,
nlev = 100
)
# Save the levels used to construct the merger matrix
self.merger_levels = levels
# Build the connectivity structure
structure = (Struct(
"simple",
ndim=data.ndim,
corners=corners)).struct
# Initialize the output
self.merger_matrix = np.zeros((self.num, self.num))*np.nan
if timer:
stop=time.time()
print "Prep took ", stop-start
start=time.time()
# Loop over levels
count = 0
nlev = len(levels)
for level in levels:
perc = count*1./nlev
sys.stdout.write('\r')
sys.stdout.write("Calculating merger for level %d out of %d" % (count, nlev))
sys.stdout.flush()
count += 1
# Label this level
thresh = (data >= level)
labels, ncolors = label(
thresh,
structure=structure)
# Get the assignments for the seeds
seed_labels = labels[self.as_tuple()]
# Get the number of discrete assignments
max_label = np.max(seed_labels)
if max_label == 0:
continue
# Histogram and look for merged cases
bins = np.arange(0.5,max_label+0.5,1)
hist_label = (np.histogram(seed_labels,bins=bins))[0]
digi_label = np.digitize(seed_labels,bins=bins)
multi_ind = ((hist_label > 1).nonzero())[0]
# Loop over merged cases
for ind in multi_ind:
shared_seeds = (((digi_label-1) == ind).nonzero())[0]
# Note the threshold in the matrix
for seed in shared_seeds:
# this step (weirdly) can take a lot of time
self.merger_matrix[seed, shared_seeds] = level
self.merger_matrix[shared_seeds, seed] = level
# Clean up diagonal
for i in range(self.num):
self.merger_matrix[i,i] = np.nan
# Finish
if timer:
stop=time.time()
print "Merger calculations took ", stop-start
start=time.time()
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Reject Local Maxima
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
def keep_lmax_subset(
self,
keep=None
):
"""
...
"""
if keep == None:
return
self.indices = self.indices[:,keep]
if self.merger_matrix == None:
new_merger = self.merger_matrix[keep]
new_merger = new_merger[:,keep]
self.merger = new_merger
self.num = len(self.pix[0])
self.name = np.arange(self.num)
def reject_on_value(
self,
thresh=3.0,
snr=True,
verbose=False
):
"""
Reject maxima on absolute value.
"""
if snr:
data = self.linked_data.snr()
else:
data = self.linked_data.data
keep = (self.num == self.num)
i = 0
for seed in self.num:
coords = (
self.pix[0][seed],
self.pix[1][seed],
self.pix[2][seed])
if data[coords] < thresh:
keep[i] = False
i += 1
self.keep_lmax_subset(keep)
def reject_on_delta(
self,
thresh=3.0,
snr=True,
verbose=False
):
"""
"""
if self.merger_matrix == None:
print "You need to calculate the merger matrix."
merge_copy = merger_matrix.copy()
low_value = np.min(self.merge_levels)-1
merge_copy[np.isfinite(merge_copy)==False] = \
low_value
# Initialize the flags to save
keep = (self.num == self.num)
# Take the max along the first axis (which axis shouldn't matter)
max_merger_level = np.argmax(self.merge_copy,axis=1)
max_merger_seed = np.argmax(self.merge_copy,axis=1)
delta = self.val - max_merger_level
# Recast as a signal-to-noise (1d)
if snr == True:
delta = delta / self.linked_data.noise.scale
# Threshold against the required delta
keep = (delta > thresh)
# Return
self.keep_lmax_subset(keep)