-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path_moment_errs.py
More file actions
524 lines (391 loc) · 14.8 KB
/
_moment_errs.py
File metadata and controls
524 lines (391 loc) · 14.8 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
import numpy as np
import astropy.units as u
from warnings import warn
from spectral_cube._moments import _moment_shp
from spectral_cube import SpectralCube
from spectral_cube.lower_dimensional_structures import Projection
from spectral_cube.cube_utils import iterator_strategy
from spectral_cube.wcs_utils import drop_axis
# from spectral_cube.np_compat import allbadtonan
'''
Functions for making moment error maps.
Borrows heavily from the functionality in _moments.py from spectral-cube.
Functions require, at minimum, a SpectralCube object and a scale value that
characterizes the noise.
'''
# convenience structures to keep track of the reversed index
# conventions between WCS and numpy
np2wcs = {2: 0, 1: 1, 0: 2}
def moment0_error(cube, scale, axis=0, how='auto'):
'''
Compute the zeroth moment error.
Parameters
----------
cube : SpectralCube
Data cube.
scale : SpectralCube or `~astropy.units.Quantity`
The noise level in the data, either as a single value (with the same
units as the cube) or a SpectralCube of noise values.
axis : int
Axis to compute moment over.
how : {'auto', 'cube', 'slice'}, optional
The computational method to use.
Returns
-------
moment0 : `~spectral_cube.lower_dimensional_structures.Projection`
'''
if how == "auto":
how = iterator_strategy(cube, 0)
if how == "ray":
warn("Automatically selected 'ray' which isn't implemented. Using"
" 'slice' instead.")
how = 'slice'
if how == "cube":
moment0_err = _cube0(cube, axis, scale)
elif how == "slice":
moment0_err = _slice0(cube, axis, scale)
else:
raise ValueError("how must be 'cube' or 'slice'.")
meta = {'moment_order': 0,
'moment_axis': axis,
'moment_method': how}
cube_meta = cube.meta.copy()
meta.update(cube_meta)
new_wcs = drop_axis(cube._wcs, np2wcs[axis])
return Projection(moment0_err, copy=False, wcs=new_wcs, meta=meta,
header=cube._nowcs_header)
def moment1_error(cube, scale, axis=0, how='auto', moment0=None, moment1=None):
'''
Compute the first moment error.
Parameters
----------
cube : SpectralCube
Data cube.
scale : SpectralCube or `~astropy.units.Quantity`
The noise level in the data, either as a single value (with the same
units as the cube) or a SpectralCube of noise values.
axis : int
Axis to compute moment over.
how : {'auto', 'cube', 'slice'}, optional
The computational method to use.
Returns
-------
moment1_err : `~spectral_cube.lower_dimensional_structures.Projection`
'''
if how == "auto":
how = iterator_strategy(cube, 0)
if how == "ray":
warn("Automatically selected 'ray' which isn't implemented. Using"
" 'slice' instead.")
how = 'slice'
# Compute moments if they aren't given.
if moment0 is None:
moment0 = cube.moment0(how=how, axis=axis)
if moment1 is None:
moment1 = cube.moment1(how=how, axis=axis)
# Remove velocity offset from centroid to match cube._pix_cen
# Requires converting to a Quantity
moment1 = u.Quantity(moment1.copy())
moment1 -= cube.spectral_axis[0]
if how == "cube":
moment1_err = _cube1(cube, axis, scale, moment0, moment1)
elif how == "slice":
moment1_err = _slice1(cube, axis, scale, moment0, moment1)
else:
raise ValueError("how must be 'cube' or 'slice'.")
meta = {'moment_order': 1,
'moment_axis': axis,
'moment_method': how}
cube_meta = cube.meta.copy()
meta.update(cube_meta)
new_wcs = drop_axis(cube._wcs, np2wcs[axis])
return Projection(moment1_err, copy=False, wcs=new_wcs, meta=meta,
header=cube._nowcs_header)
def moment2_error(cube, scale, axis=0, how='auto', moment0=None, moment1=None,
moment2=None, moment1_err=None):
'''
Compute the second moment error.
Parameters
----------
cube : SpectralCube
Data cube.
scale : SpectralCube or `~astropy.units.Quantity`
The noise level in the data, either as a single value (with the same
units as the cube) or a SpectralCube of noise values.
axis : int
Axis to compute moment over.
how : {'auto', 'cube', 'slice'}, optional
The computational method to use.
Returns
-------
moment2_err : `~spectral_cube.lower_dimensional_structures.Projection`
'''
if how == "auto":
how = iterator_strategy(cube, 0)
if how == "ray":
warn("Automatically selected 'ray' which isn't implemented. Using"
" 'slice' instead.")
how = 'slice'
# Compute moments if they aren't given.
if moment0 is None:
moment0 = cube.moment0(how='cube', axis=axis)
if moment1 is None:
moment1 = cube.moment1(how='cube', axis=axis)
# Remove velocity offset to match cube._pix_cen
# Requires converting to a Quantity
moment1 = u.Quantity(moment1.copy())
moment1 -= cube.spectral_axis[0]
if moment2 is None:
moment2 = cube.moment2(how='cube', axis=axis)
if moment1_err is None:
moment1_err = _cube1(cube, axis, scale, moment0=moment0,
moment1=moment1)
if how == "cube":
moment2_err = _cube2(cube, axis, scale, moment0, moment1, moment2,
moment1_err)
elif how == "slice":
moment2_err = _slice2(cube, axis, scale, moment0, moment1, moment2,
moment1_err)
else:
raise ValueError("how must be 'cube' or 'slice'.")
meta = {'moment_order': 2,
'moment_axis': axis,
'moment_method': how}
cube_meta = cube.meta.copy()
meta.update(cube_meta)
new_wcs = drop_axis(cube._wcs, np2wcs[axis])
return Projection(moment2_err, copy=False, wcs=new_wcs, meta=meta,
header=cube._nowcs_header)
def _slice0(cube, axis, scale):
"""
0th moment along an axis, calculated slicewise
Parameters
----------
cube : SpectralCube
axis : int
scale : float
Returns
-------
moment0_error : array
"""
if isinstance(scale, SpectralCube):
# scale should then have the same shape as the cube.
if cube.shape != scale.shape:
raise IndexError("When scale is a SpectralCube, it must have the"
" same shape as the cube.")
_scale_cube = True
else:
_scale_cube = False
shp = _moment_shp(cube, axis)
result = np.zeros(shp) * cube.unit ** 2
view = [slice(None)] * 3
valid = np.zeros(shp, dtype=np.bool)
for i in range(cube.shape[axis]):
view[axis] = i
plane = cube._mask.include(data=cube._data, wcs=cube._wcs, view=view)
valid |= plane
if _scale_cube:
noise_plane = np.nan_to_num(scale.filled_data[view])
else:
noise_plane = scale
result += plane * np.power(noise_plane, 2)
out_result = np.sqrt(result) * cube._pix_size_slice(axis)
out_result[~valid] = np.nan
return out_result
def _slice1(cube, axis, scale, moment0, moment1):
"""
1st moment along an axis, calculated slicewise
Parameters
----------
cube : SpectralCube
axis : int
scale : float or SpectralCube
moment0 : 0th moment
moment1 : 1st moment
Returns
-------
moment1_error : array
"""
if isinstance(scale, SpectralCube):
# scale should then have the same shape as the cube.
if cube.shape != scale.shape:
raise IndexError("When scale is a SpectralCube, it must have the"
" same shape as the cube.")
_scale_cube = True
else:
_scale_cube = False
# I don't think there is a way to do this with one pass.
# The first 2 moments always have to be pre-computed.
# Divide moment0 by the pixel size in the given axis so it represents the
# sum.
spec_unit = cube.spectral_axis.unit
axis_sum = u.Quantity(moment0.copy() /
(cube._pix_size_slice(axis) * spec_unit))
shp = _moment_shp(cube, axis)
result = np.zeros(shp) * spec_unit ** 2
view = [slice(None)] * 3
pix_cen = u.Quantity(cube._pix_cen()[axis] * spec_unit)
# term2 does not depend on the plane.
term2 = moment1 / axis_sum
for i in range(cube.shape[axis]):
view[axis] = i
term1 = pix_cen[view] / axis_sum
if _scale_cube:
noise_plane = \
np.nan_to_num(scale.filled_data[view])
else:
noise_plane = scale
result += np.power((term1 - term2) * noise_plane, 2)
return np.sqrt(result)
def _slice2(cube, axis, scale, moment0, moment1, moment2,
moment1_err):
"""
2nd moment error along an axis, calculated slicewise
Parameters
----------
cube : SpectralCube
axis : int
scale : float or SpectralCube
moment0 : 0th moment
moment1 : 1st moment
moment2 : 2nd moment
moment1_err : 1st moment error
Returns
-------
moment1_error : array
"""
if isinstance(scale, SpectralCube):
# scale should then have the same shape as the cube.
if cube.shape != scale.shape:
raise IndexError("When scale is a SpectralCube, it must have the"
" same shape as the cube.")
_scale_cube = True
else:
_scale_cube = False
# Divide moment0 by the pixel size in the given axis so it represents the
# sum.
spec_unit = cube.spectral_axis.unit
axis_sum = u.Quantity(moment0.copy() /
(cube._pix_size_slice(axis) * spec_unit))
shp = _moment_shp(cube, axis)
term1 = np.zeros(shp) * spec_unit ** 4
term2 = np.zeros(shp) * spec_unit * cube.unit
view = [slice(None)] * 3
pix_cen = cube._pix_cen()[axis] * spec_unit
# term12 does not depend on plane.
term12 = moment2 / axis_sum
for i in range(cube.shape[axis]):
view[axis] = i
plane = np.nan_to_num(cube.filled_data[view])
term11 = np.power((pix_cen[view] - moment1), 2) / axis_sum
if _scale_cube:
noise_plane = \
np.nan_to_num(scale.filled_data[view])
else:
noise_plane = scale
term1 += np.power((term11 - term12) * noise_plane, 2)
term2 += np.nan_to_num(plane) * (pix_cen[view] - moment1)
term2 = 4 * np.power((moment1_err * term2) / (axis_sum), 2)
return np.sqrt(term1 + term2)
def _cube0(cube, axis, scale):
'''
Moment 0 error computed cube-wise.
'''
if isinstance(scale, SpectralCube):
# scale should then have the same shape as the cube.
if cube.shape != scale.shape:
raise IndexError("When scale is a SpectralCube, it must have the"
" same shape as the cube.")
noise_plane = np.nan_to_num(scale.filled_data[:])
else:
noise_plane = scale
error_arr = \
np.sqrt(
np.sum(cube._mask.include(data=cube._data, wcs=cube._wcs) *
noise_plane**2, axis=axis)) * cube._pix_size_slice(axis)
return error_arr
def _cube1(cube, axis, scale, moment0, moment1):
'''
Moment 1 error computed cube-wise.
'''
if isinstance(scale, SpectralCube):
# scale should then have the same shape as the cube.
if cube.shape != scale.shape:
raise IndexError("When scale is a SpectralCube, it must have the"
" same shape as the cube.")
noise_plane = scale.filled_data[:]
else:
noise_plane = scale
# I don't think there is a way to do this with one pass.
# The first 2 moments always have to be pre-computed.
# Divide moment0 by the pixel size in the given axis so it represents the
# sum.
spec_unit = cube.spectral_axis.unit
axis_sum = u.Quantity(moment0.copy() /
(cube._pix_size_slice(axis) * spec_unit))
shp = _moment_shp(cube, axis)
result = np.zeros(shp) * spec_unit
pix_cen = cube._pix_cen()[axis] * spec_unit
term1 = pix_cen / axis_sum
term2 = moment1 / axis_sum
result = np.sqrt(np.sum(np.power((term1 - term2) *
np.nan_to_num(noise_plane), 2),
axis=axis))
good_pix = np.isfinite(moment0) + np.isfinite(moment1)
result[~good_pix] = np.NaN
return result
def _cube2(cube, axis, scale, moment0, moment1, moment2,
moment1_err):
'''
'''
spec_unit = cube.spectral_axis.unit
pix_cen = cube._pix_cen()[axis] * spec_unit
if isinstance(scale, SpectralCube):
# scale should then have the same shape as the cube.
if cube.shape != scale.shape:
raise IndexError("When scale is a SpectralCube, it must have the"
" same shape as the cube.")
noise_plane = np.nan_to_num(scale.filled_data[:])
else:
noise_plane = scale
# Divide moment0 by the pixel size in the given axis so it represents the
# sum.
axis_sum = u.Quantity(moment0.copy() /
(cube._pix_size_slice(axis) * spec_unit))
plane = np.nan_to_num(cube.filled_data[:])
term11 = np.power((pix_cen - moment1), 2) / axis_sum
term12 = moment2 / axis_sum
term1 = np.sum(np.power((term11 - term12) * noise_plane, 2), axis=axis)
term21 = u.Quantity(np.nan_to_num(plane) * (pix_cen - moment1))
term2 = \
4 * np.power((moment1_err * np.sum(term21, axis=axis)) / axis_sum, 2)
result = np.sqrt(term1 + term2)
good_pix = np.isfinite(moment0) + np.isfinite(moment1) + \
np.isfinite(moment2)
result[~good_pix] = np.NaN
# result[result == 0] = np.NaN
return result
def linewidth_sigma_err(cube, scale, how='auto', moment0=None, moment1=None,
moment2=None, moment1_err=None):
'''
Error on the line width.
'''
if how == "auto":
how = iterator_strategy(cube, 0)
if moment2 is None:
moment2 = cube.moment2(how=how, axis=0)
mom2_err = moment2_error(cube, scale, axis=0, how=how,
moment0=moment0,
moment1=moment1,
moment2=moment2,
moment1_err=moment1_err)
return mom2_err / (2 * np.sqrt(moment2))
def linewidth_fwhm_err(cube, scale, how='auto', moment0=None, moment1=None,
moment2=None, moment1_err=None):
'''
Error on the FWHM line width.
'''
SIGMA2FWHM = 2. * np.sqrt(2. * np.log(2.))
del_lwidth_sig = linewidth_sigma_err(cube, scale, how, moment0, moment1,
moment2, moment1_err)
return SIGMA2FWHM * del_lwidth_sig