-
Notifications
You must be signed in to change notification settings - Fork 0
/
radial_profile.py
158 lines (125 loc) · 4.83 KB
/
radial_profile.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
import pdb
def radial_data(data,annulus_width=1,working_mask=None,x=None,y=None,rmax=None):
"""
r = radial_data(data,annulus_width,working_mask,x,y)
A function to reduce an image to a radial cross-section.
:INPUT:
data - whatever data you are radially averaging. Data is
binned into a series of annuli of width 'annulus_width'
pixels.
annulus_width - width of each annulus. Default is 1.
working_mask - array of same size as 'data', with zeros at
whichever 'data' points you don't want included
in the radial data computations.
x,y - coordinate system in which the data exists (used to set
the center of the data). By default, these are set to
integer meshgrids
rmax -- maximum radial value over which to compute statistics
:OUTPUT:
r - a data structure containing the following
statistics, computed across each annulus:
.r - the radial coordinate used (outer edge of annulus)
.mean - mean of the data in the annulus
.sum - the sum of all enclosed values at the given radius
.std - standard deviation of the data in the annulus
.median - median value in the annulus
.max - maximum value in the annulus
.min - minimum value in the annulus
.numel - number of elements in the annulus
:EXAMPLE:
::
import numpy as np
import pylab as py
import radial_data as rad
# Create coordinate grid
npix = 50.
x = np.arange(npix) - npix/2.
xx, yy = np.meshgrid(x, x)
r = np.sqrt(xx**2 + yy**2)
fake_psf = np.exp(-(r/5.)**2)
noise = 0.1 * np.random.normal(0, 1, r.size).reshape(r.shape)
simulation = fake_psf + noise
rad_stats = rad.radial_data(simulation, x=xx, y=yy)
py.figure()
py.plot(rad_stats.r, rad_stats.mean / rad_stats.std)
py.xlabel('Radial coordinate')
py.ylabel('Signal to Noise')
"""
# 2012-02-25 20:40 IJMC: Empty bins now have numel=0, not nan.
# 2012-02-04 17:41 IJMC: Added "SUM" flag
# 2010-11-19 16:36 IJC: Updated documentation for Sphinx
# 2010-03-10 19:22 IJC: Ported to python from Matlab
# 2005/12/19 Added 'working_region' option (IJC)
# 2005/12/15 Switched order of outputs (IJC)
# 2005/12/12 IJC: Removed decifact, changed name, wrote comments.
# 2005/11/04 by Ian Crossfield at the Jet Propulsion Laboratory
import numpy as ny
class radialDat:
"""Empty object container.
"""
def __init__(self):
self.mean = None
self.std = None
self.median = None
self.numel = None
self.max = None
self.min = None
self.r = None
#---------------------
# Set up input parameters
#---------------------
data = ny.array(data)
if working_mask==None:
working_mask = ny.ones(data.shape,bool)
npix, npiy = data.shape
if x==None or y==None:
x1 = ny.arange(-npix/2.,npix/2.)
y1 = ny.arange(-npiy/2.,npiy/2.)
x,y = ny.meshgrid(y1,x1)
r = abs(x+1j*y)
if rmax==None:
rmax = r[working_mask].max()
#---------------------
# Prepare the data container
#---------------------
dr = ny.abs([x[0,0] - x[0,1]]) * annulus_width
radial = ny.arange(rmax/dr)*dr + dr/2.
nrad = len(radial)
radialdata = radialDat()
radialdata.mean = ny.zeros(nrad)
radialdata.sum = ny.zeros(nrad)
radialdata.std = ny.zeros(nrad)
radialdata.median = ny.zeros(nrad)
radialdata.numel = ny.zeros(nrad, dtype=int)
radialdata.max = ny.zeros(nrad)
radialdata.min = ny.zeros(nrad)
radialdata.r = radial
#---------------------
# Loop through the bins
#---------------------
for irad in range(nrad): #= 1:numel(radial)
minrad = irad*dr
maxrad = minrad + dr
thisindex = (r>=minrad) * (r<maxrad) * working_mask
#import pylab as py
#pdb.set_trace()
if not thisindex.ravel().any():
radialdata.mean[irad] = ny.nan
radialdata.sum[irad] = ny.nan
radialdata.std[irad] = ny.nan
radialdata.median[irad] = ny.nan
radialdata.numel[irad] = 0
radialdata.max[irad] = ny.nan
radialdata.min[irad] = ny.nan
else:
radialdata.mean[irad] = data[thisindex].mean()
radialdata.sum[irad] = data[r<maxrad].sum()
radialdata.std[irad] = data[thisindex].std()
radialdata.median[irad] = ny.median(data[thisindex])
radialdata.numel[irad] = data[thisindex].size
radialdata.max[irad] = data[thisindex].max()
radialdata.min[irad] = data[thisindex].min()
#---------------------
# Return with data
#---------------------
return radialdata