forked from low-sky/cloudpca
/
pca_utils.py
222 lines (206 loc) · 8.86 KB
/
pca_utils.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
import numpy as np
import astropy.io.fits as fits
from spectral_cube import SpectralCube
import numpy.fft as fft
from scipy.interpolate import LSQUnivariateSpline,interp1d
from astropy.modeling import models, fitting
from scipy.signal import argrelmin
import pdb
import matplotlib.pyplot as plt
def Exponential1D(x, amp, scale):
return amp*np.exp(-x/scale)
def Exponential2D(x,y,x0,y0,amp,xscale,yscale,theta):
xrot = x*np.cos(theta) + y*np.sin(theta)
yrot = -x*np.sin(theta) + y*np.cos(theta)
dist = ((xrot/xscale)**2 + (yrot/yscale)**2)**0.5
return (amp*np.exp(-dist)).flatten()
def WidthEstimate2D(inList, method = 'contour', NoiseACF = 0):
scales = np.zeros(len(inList))
for idx,zraw in enumerate(inList):
z = zraw - NoiseACF
x = fft.fftfreq(z.shape[0])*z.shape[0]/2.0
y = fft.fftfreq(z.shape[1])*z.shape[1]/2.0
xmat,ymat = np.meshgrid(x,y,indexing='ij')
z = np.roll(z,z.shape[0]/2,axis=0)
z = np.roll(z,z.shape[1]/2,axis=1)
xmat = np.roll(xmat,xmat.shape[0]/2,axis=0)
xmat = np.roll(xmat,xmat.shape[1]/2,axis=1)
ymat = np.roll(ymat,ymat.shape[0]/2,axis=0)
ymat = np.roll(ymat,ymat.shape[1]/2,axis=1)
rmat = (xmat**2+ymat**2)**0.5
if method == 'fit':
g = models.Gaussian2D(x_mean=[0],y_mean=[0],
x_stddev =[1],y_stddev = [1],
amplitude = z[0,0],
theta = [0],
fixed ={'amplitude':True,
'x_mean':True,
'y_mean':True})
fit_g = fitting.LevMarLSQFitter()
output = fit_g(g,np.abs(xmat)**0.5,np.abs(ymat)**0.5,z)
scales[idx]=2**0.5*np.sqrt(output.x_stddev.value[0]**2+
output.y_stddev.value[0]**2)
if method == 'interpolate':
rvec = rmat.ravel()
zvec = z.ravel()
zvec /= zvec.max()
sortidx = np.argsort(zvec)
rvec = rvec[sortidx]
zvec = zvec[sortidx]
dz = len(zvec)/100.
spl = LSQUnivariateSpline(zvec,rvec,zvec[dz::dz])
scales[idx] = spl(np.exp(-1))
if method == 'xinterpolate':
g = models.Gaussian2D(x_mean=[0],y_mean=[0],
x_stddev =[1],y_stddev = [1],
amplitude = z[0,0],
theta = [0],
fixed ={'amplitude':True,
'x_mean':True,
'y_mean':True})
fit_g = fitting.LevMarLSQFitter()
output = fit_g(g,xmat,ymat,z)
aspect = 1/(output.x_stddev.value[0]/output.y_stddev.value[0])
theta = output.theta.value[0]
rmat = ((xmat * np.cos(theta) + ymat * np.sin(theta))**2+\
(-xmat * np.sin(theta) + ymat * np.cos(theta))**2*\
aspect**2)**0.5
rvec = rmat.ravel()
zvec = z.ravel()
zvec /= zvec.max()
sortidx = np.argsort(zvec)
rvec = rvec[sortidx]
zvec = zvec[sortidx]
dz = len(zvec)/100.
spl = LSQUnivariateSpline(zvec,rvec,zvec[dz::dz])
scales[idx] = spl(np.exp(-1))
plt.plot((((xmat**2)+(ymat**2))**0.5).ravel(),z.ravel(),'b,')
plt.plot(rmat.ravel(),z.ravel(),'r,')
plt.vlines(scales[idx],zvec.min(),zvec.max())
plt.show()
pdb.set_trace()
if method == 'contour':
znorm = z
znorm /= znorm.max()
# plt.imshow(znorm,vmin=0,vmax=1)
cs = plt.contour(xmat,ymat,znorm,levels=[np.exp(-1)])
paths = (cs.collections[0].get_paths())
# plt.show()
plt.clf()
# Only points that contain the origin
if isinstance(paths,list) and len(paths)>1:
pidx = np.where([p.contains_point((0,0)) for p in paths])
if len(pidx[0])>0:
paths = paths[pidx[0]]
scales[idx] = (np.min(np.array([np.max(p.vertices[:,0]**2+p.vertices[:,1]**2) for p in paths])))**0.5
else:
scales[idx] = np.nan
elif len(paths)>0:
scales[idx] = (np.max(paths[0].vertices[:,0]**2+paths[0].vertices[:,1]**2))**0.5
else:
scales[idx] = np.nan
return scales
def WidthEstimate1D(inList, method = 'interpolate'):
scales = np.zeros(len(inList))
for idx,y in enumerate(inList):
x = fft.fftfreq(len(y))*len(y)/2.0
if method == 'interpolate':
minima = (argrelmin(y))[0]
if minima[0]>1:
interpolator = interp1d(y[0:minima[0]],x[0:minima[0]])
scales[idx] = interpolator(np.exp(-1))
if method == 'fit':
g = models.Gaussian1D(amplitude=y[0],mean=[0],stddev = [10],
fixed={'amplitude':True,'mean':True})
fit_g = fitting.LevMarLSQFitter()
minima = (argrelmin(y))[0]
if minima[0]>1:
xtrans = (np.abs(x)**0.5)[0:minima[0]]
yfit = y[0:minima[0]]
else:
xtrans = np.abs(x)**0.5
yfit = y
output = fit_g(g,xtrans,yfit)
scales[idx]=np.abs(output.stddev.value[0])*(2**0.5)
# expmod = Model(Exponential1D)
# pars = expmod.make_params(amp=y[0],scale=5.0)
# pars['amp'].vary = False
# result = expmod.fit(y,x=x,params = pars)
# scales[idx] = result.params['scale'].value
return scales
def AutoCorrelateImages(imageList):
acorList = []
for image in imageList:
fftx = fft.fft2(image)
fftxs = np.conjugate(fftx)
acor = fft.ifft2((fftx-fftx.mean())*(fftxs-fftxs.mean()))
acorList.append(acor.real)
return(acorList)
def AutoCorrelateSpectrum(evec,nScales = 10):
acorList = []
for idx in range(nScales):
fftx = fft.fft(evec[:,idx])
fftxs = np.conjugate(fftx)
acor = fft.ifft((fftx-fftx.mean())*(fftxs-fftxs.mean()))
acorList.append(acor.real)
return(acorList)
def NoiseACF(evec, cube, nScales = 10):
if nScales == 0:
return 0
imageList = []
for idx in range(nScales):
thisImage = np.zeros((cube.shape[1],cube.shape[2]))
for channel in range(cube.shape[0]):
thisImage +=np.nan_to_num(cube[channel,:,:].value*evec[channel,-(idx+1)])
imageList.append(thisImage)
acorList = []
for image in imageList:
fftx = fft.fft2(image)
fftxs = np.conjugate(fftx)
acor = fft.ifft2((fftx-fftx.mean())*(fftxs-fftxs.mean()))
acorList.append(acor.real)
NoiseACF = np.zeros((cube.shape[1],cube.shape[2]))
for planeACF in acorList:
NoiseACF += planeACF
NoiseACF /= len(NoiseACF)
return(NoiseACF)
def EigenImages(evec,cube,nScales = 10):
imageList = []
for idx in range(nScales):
thisImage = np.zeros((cube.shape[1],cube.shape[2]))
for channel in range(cube.shape[0]):
thisImage +=np.nan_to_num(cube[channel,:,:].value*evec[channel,idx])
imageList.append(thisImage)
return imageList
def pca(cube, meanCorrection = False):
PCAMatrix = np.zeros((cube.shape[0],cube.shape[0]))
GoodCount = np.zeros((cube.shape[0],cube.shape[0]),dtype=np.float)
ChannelMeans = np.zeros((cube.shape[0]))
if meanCorrection:
for i in range(cube.shape[0]):
ChannelMeans[i] = np.nanmean(cube[i,:,:].value)
else:
ChannelMeans = np.zeros(cube.shape[0])
for i in range(cube.shape[0]):
for j in range(i):
PlaneProduct = (cube[i,:,:].value-ChannelMeans[i])*(cube[j,:,:].value-ChannelMeans[j])
PCAMatrix[i,j] = np.nanmean(PlaneProduct)
GoodCount[i,j] = np.sum(np.isfinite(PlaneProduct))
PCAMatrix[i,i] = np.nanmean((cube[i,:,:].value-ChannelMeans[i])**2)
GoodCount[i,i] = np.sum(np.isfinite(cube[i,:,:]))
PCAMatrix = PCAMatrix + np.transpose(PCAMatrix)
GoodCount = GoodCount + np.transpose(GoodCount)
# Correct elements on the diagonal for the doubling in the transpose-and-add
PCAMatrix[range(cube.shape[0]),range(cube.shape[0])] = \
PCAMatrix[range(cube.shape[0]),range(cube.shape[0])]/2
GoodCount[range(cube.shape[0]),range(cube.shape[0])] = \
GoodCount[range(cube.shape[0]),range(cube.shape[0])]/2
if meanCorrection:
N = cube.shape[1]*cube.shape[2]
PCAMatrix = PCAMatrix * GoodCount/(GoodCount-1)
PCAMatrix[~np.isfinite(PCAMatrix)]=0.0
evals,evec = np.linalg.eig(PCAMatrix)
order = (np.argsort(evals))[::-1]
evals = evals[order]
evec = evec[:,order]
return evals,evec,PCAMatrix