-
Notifications
You must be signed in to change notification settings - Fork 12
/
astrohog2d1v.py
282 lines (229 loc) · 11.6 KB
/
astrohog2d1v.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
#!/usr/bin/env python
#
# This file is part of astroHOG
#
# CONTACT: juandiegosolerp[at]gmail.com
# Copyright (C) 2017-2023 Juan Diego Soler
#
#------------------------------------------------------------------------------;
import sys
import numpy as np
from astropy.convolution import convolve_fft
from astropy.convolution import Gaussian2DKernel
from scipy import ndimage
from nose.tools import assert_equal, assert_true
import matplotlib.pyplot as plt
import collections
import multiprocessing
from astrohog2d import *
from statests import *
from tqdm import tqdm
# ------------------------------------------------------------------------------------------------------------------
CorrMapPair = collections.namedtuple('CorrMapPair', [
'map1','map2',
'pos1','pos2',
'pxsz','ksz','res',
'mask1','mask2',
'gradthres1','gradthres2',
'wd'
])
# ------------------------------------------------------------------------------------------------------------------
def process_item(item):
print('Process Item',item.pos1,item.pos2)
circstats, corrframe, sframe1, sframe2 = HOGcorr_frame(item.map1, item.map2, pxsz=item.pxsz, ksz=item.ksz, res=item.res, mask1=item.mask1, mask2=item.mask2, gradthres1=item.gradthres1, gradthres2=item.gradthres2, wd=item.wd)
return {
'circstats': circstats,
'corrframe': corrframe,
'sframe1': sframe1,
'sframe2': sframe2,
'pos1': item.pos1,
'pos2': item.pos2
}
# --------------------------------------------------------------------------------------------------------------------
def HOGppvblocks(corrcube, nbx=7, nby=7, vlims=[0.,1.,0.,1.], weight=1.):
# Uses the pre-calculated global HOG correlation to calculate the HOG correlation in block of the map
#
# INPUTS
#
# correcube -- output of HOGcorr_ppvcubes function containing relative orientation angles between gradients
#
# OUTPUTS
#
sz=np.shape(corrcube)
x=(np.arange(0,sz[2],1)/(sz[2]/nbx)).astype(int)
y=(np.arange(0,sz[3],1)/(sz[3]/nby)).astype(int)
xx, yy = np.meshgrid(x, y)
#limsx=np.linspace(0,sz[2]-1,nbx+1,dtype=int)
#limsy=np.linspace(0,sz[3]-1,nby+1,dtype=int)
zblocks=np.zeros([sz[0],sz[1],nbx,nby])
vblocks=np.zeros([sz[0],sz[1],nbx,nby])
maxvblocks=np.zeros([nbx,nby])
sigvblocks=np.zeros([nbx,nby])
# Loop over blocks
print("Block averaging ==========================")
for i in tqdm(range(0, nby)):
for k in range(0, nbx):
# Loop over velocity channels
for vi in range(0, sz[0]):
for vk in range(0, sz[1]):
phiframe=corrcube[vi,vk,:,:]
goodpos=np.logical_and(yy==i,xx==k).nonzero()
phi=np.ravel(phiframe[goodpos])
wghts=weight*np.ones_like(phi)
good=np.isfinite(phi).nonzero()
if (np.size(good) > 1):
output=HOG_PRS(2.*phi[good], weights=wghts[good])
zblocks[vi,vk,k,i]=output['Z']
vblocks[vi,vk,k,i]=output['Zx']
else:
zblocks[vi,vk,k,i]=np.nan
vblocks[vi,vk,k,i]=np.nan
tempvblocks=vblocks[:,:,k,i]
if (np.size(np.isfinite(tempvblocks).nonzero()) > 0):
maxvblocks[i,k]=np.max(tempvblocks[np.isfinite(tempvblocks).nonzero()])
sigvblocks[i,k]=np.std(tempvblocks[np.isfinite(tempvblocks).nonzero()])
else:
maxvblocks[i,k]=np.nan
sigvblocks[i,k]=np.nan
#if (np.logical_and(nbx==1,nby==1)):
# fig, ax = plt.subplots(figsize = (9.0,8.0))
# im=ax.imshow(vblocks[:,:,0,0], origin='lower', extent=vlims, vmin=0., vmax=np.max(maxvblocks), aspect='auto')
# ax.set_xlabel(r'$v_{CO}$ [km/s]')
# ax.set_ylabel(r'$v_{HI}$ [km/s]')
# cbl=plt.colorbar(im, ax=ax)
# cbl.ax.set_title(r'$V$')
# plt.show()
#else:
# fig, axs = plt.subplots(nbx,nby,figsize = (9.0,8.0))
# fig.subplots_adjust(hspace=0.001, wspace=0.005)
# for i in range(0,nbx):
# for k in range(0, nby):
# im=axs[nby-1-i,k].imshow(vblocks[:,:,i,k], origin='lower', extent=vlims, vmin=0., vmax=np.max(maxvblocks), aspect='auto')
# if(np.logical_and(i==nby-1,k==0)):
# axs[i,k].set_xlabel(r'$v_{CO}$ [km/s]')
# axs[i,k].set_ylabel(r'$v_{HI}$ [km/s]')
# cbl=plt.colorbar(im, ax=axs.ravel().tolist())
# cbl.ax.set_title(r'$V$')
# plt.show()
imaxb, jmaxb = (maxvblocks==np.nanmax(maxvblocks)).nonzero()
# Output circular statistics for the block with the highest V
circstats={'Z': zblocks[:,:,imaxb[0], jmaxb[0]],
'V': vblocks[:,:,imaxb[0], jmaxb[0]]}
#return [limsx[imaxb[0]],limsx[imaxb[0]+1],limsy[jmaxb[0]],limsy[jmaxb[0]+1]], vblocks[:,:,imaxb[0], jmaxb[0]], maxvblocks
#return circstats, maxvblocks, xx, yy
#return vblocks, maxvblocks, xx, yy
return vblocks, xx, yy
# ================================================================================================================
def HOGcorr_ppvcubes(cube1, cube2, z1min, z1max, z2min, z2max, pxsz=1., ksz=1., res=1., mask1=0, mask2=0, gradthres1=0., gradthres2=0., s_cube1=0., s_cube2=0., nruns=0, weights=None, verbose=True):
# Calculates the HOG correlation between PPV cubes
#
# INPUTS
#
# OUTPUTS
#
print('Computing HOG correlation')
print(z1max-z1min+1,z2max-z2min+1)
sz1=np.shape(cube1)
sz2=np.shape(cube2)
# Circular statistic outputs of orientation between image gradients
rplane=np.zeros([z1max+1-z1min, z2max+1-z2min]); s_rplane=np.zeros([z1max+1-z1min, z2max+1-z2min])
zplane=np.zeros([z1max+1-z1min, z2max+1-z2min]); s_zplane=np.zeros([z1max+1-z1min, z2max+1-z2min])
vplane=np.zeros([z1max+1-z1min, z2max+1-z2min]); s_vplane=np.zeros([z1max+1-z1min, z2max+1-z2min])
meanphiplane=np.zeros([z1max+1-z1min, z2max+1-z2min]); s_meanphiplane=np.zeros([z1max+1-z1min, z2max+1-z2min])
# Circular statistic outputs of directions between image gradients
rdplane=np.zeros([z1max+1-z1min, z2max+1-z2min]); s_rdplane=np.zeros([z1max+1-z1min, z2max+1-z2min])
zdplane=np.zeros([z1max+1-z1min, z2max+1-z2min]); s_zdplane=np.zeros([z1max+1-z1min, z2max+1-z2min])
vdplane=np.zeros([z1max+1-z1min, z2max+1-z2min]); s_vdplane=np.zeros([z1max+1-z1min, z2max+1-z2min])
meanphidplane=np.zeros([z1max+1-z1min, z2max+1-z2min]); s_meanphidplane=np.zeros([z1max+1-z1min, z2max+1-z2min])
# Correlation statistics
pearplane=np.zeros([z1max+1-z1min, z2max+1-z2min]); s_pearplane=np.zeros([z1max+1-z1min, z2max+1-z2min]);
ccorplane=np.zeros([z1max+1-z1min, z2max+1-z2min]); s_ccorplane=np.zeros([z1max+1-z1min, z2max+1-z2min]);
neleplane=np.zeros([z1max+1-z1min, z2max+1-z2min])
# -----------------------------------------------------------------
corrframe=np.zeros([sz1[1],sz1[2]])
scube1=np.zeros(sz1)
scube2=np.zeros(sz2)
corrcube=np.zeros([z1max+1-z1min, z2max+1-z2min,sz1[1],sz1[2]])
corrframe_temp=np.zeros([sz1[1],sz1[2]])
maskcube=np.zeros(sz1)
pbar = tqdm(total=(z1max-z1min)*(z2max-z2min))
# Loop over channel pairs
for i in tqdm(range(z1min, z1max+1)):
for k in range(z2min, z2max+1):
vprint('Channel '+str(i-z1min)+'/'+str(z1max-z1min)+' and '+str(k-z2min)+'/'+str(z2max-z2min))
frame1=cube1[i,:,:]
frame2=cube2[k,:,:]
if np.array_equal(np.shape(cube1), np.shape(mask1)):
if np.array_equal(np.shape(cube2), np.shape(mask2)):
circstats, corrframe, sframe1, sframe2 = HOGcorr_ima(frame1, frame2, pxsz=pxsz, ksz=ksz, res=res, mask1=mask1[i,:,:], mask2=mask2[k,:,:], gradthres1=gradthres1, gradthres2=gradthres2, s_ima1=s_cube1, s_ima2=s_cube2, nruns=nruns, weights=weights, verbose=verbose)
else:
circstats, corrframe, sframe1, sframe2 = HOGcorr_ima(frame1, frame2, pxsz=pxsz, ksz=ksz, res=res, mask1=mask1[i,:,:], gradthres1=gradthres1, gradthres2=gradthres2, s_ima1=s_cube1, s_ima2=s_cube2, nruns=nruns, weights=weights, verbose=verbose)
else:
circstats, corrframe, sframe1, sframe2 = HOGcorr_ima(frame1, frame2, pxsz=pxsz, ksz=ksz, res=res, gradthres1=gradthres1, gradthres2=gradthres2, s_ima1=s_cube1, s_ima2=s_cube2, nruns=nruns, weights=weights, verbose=verbose)
rplane[i-z1min,k-z2min] =circstats['RVL']; s_rplane[i-z1min,k-z2min] =circstats['s_RVL']
zplane[i-z1min,k-z2min] =circstats['Z']; s_zplane[i-z1min,k-z2min] =circstats['s_Z']
vplane[i-z1min,k-z2min] =circstats['V']; s_vplane[i-z1min,k-z2min] =circstats['s_V']
meanphiplane[i-z1min,k-z2min] =circstats['meanphi']; s_meanphiplane[i-z1min,k-z2min] =circstats['s_meanphi']
rdplane[i-z1min,k-z2min] =circstats['RVLd']; s_rdplane[i-z1min,k-z2min] =circstats['s_RVLd']
zdplane[i-z1min,k-z2min] =circstats['Zd']; s_zdplane[i-z1min,k-z2min] =circstats['s_Zd']
vdplane[i-z1min,k-z2min] =circstats['Vd']; s_vdplane[i-z1min,k-z2min] =circstats['s_Vd']
meanphidplane[i-z1min,k-z2min] =circstats['meanphid']; s_meanphidplane[i-z1min,k-z2min] =circstats['s_meanphid']
pearplane[i-z1min,k-z2min]=circstats['pearsonr']
s_pearplane[i-z1min,k-z2min]=circstats['s_pearsonr']
ccorplane[i-z1min,k-z2min]=circstats['crosscor']
s_ccorplane[i-z1min,k-z2min]=circstats['s_crosscor']
neleplane[i-z1min,k-z2min]=circstats['ngood']
corrcube[i-z1min,k-z2min,:,:]=corrframe
scube2[k,:,:]=sframe2
pbar.update()
scube1[i,:,:]=sframe1
pbar.close()
outcircstats={'RVL': rplane, 'Z': zplane, 'V': vplane, 'meanphi': meanphiplane,
's_RVL': s_rplane, 's_Z': s_zplane, 's_V': s_vplane, 's_meanphi': s_meanphiplane,
'RVLd': rdplane, 'Zd': zdplane, 'Vd': vdplane, 'meanphid': meanphidplane,
's_RVLd': s_rdplane, 's_Zd': s_zdplane, 's_Vd': s_vdplane, 's_meanphid': s_meanphidplane,
'pearsonr': pearplane, 's_pearsonr': s_pearplane,
'crosscor': ccorplane, 's_crosscor': s_ccorplane,
'ngood': neleplane}
return outcircstats, corrcube, scube1, scube2
# ================================================================================================================
def HOGcorr_cubeandpol(cube1, ex, ey, z1min, z1max, pxsz=1., ksz=1., res=1., mask1=0, mask2=0, wd=1, rotatepol=False, regrid=False, allow_huge=False):
# Calculates the correlation
#
# INPUTS
#
# OUTPUTS
#
#
print('Computing HOG correlation')
print(z1max-z1min)
sf=3. #Number of pixels per kernel FWHM
pxksz =ksz/pxsz
pxres =res/pxsz
sz1=np.shape(cube1)
sz2=np.shape(ex)
if(rotatepol):
xvec= ey
yvec=-ex
else:
xvec= ex
yvec= ey
normVec=np.sqrt(xvec*xvec+yvec*yvec)
corrvec=0.*np.arange(z1min,z1max+1)
corrframe=np.zeros([sz1[1],sz1[2]])
corrcube=np.zeros(sz1)
scube=np.zeros(sz1)
for i in range(z1min, z1max+1):
print(i-z1min)
if np.array_equal(np.shape(cube1), np.shape(mask1)):
if np.array_equal(np.shape(normVec), np.shape(mask2)):
corr, corrframe, sframe = HOGcorr_frameandvec(cube1[i,:,:], xvec, yvec, pxsz=pxsz, ksz=ksz, res=res, mask1=mask1[i,:,:], mask2=mask2, wd=wd, regrid=regrid)
else:
corr, corrframe, sframe = HOGcorr_frameandvec(cube1[i,:,:], xvec, yvec, pxsz=pxsz, ksz=ksz, res=res, mask1=mask1[i,:,:], wd=wd, regrid=regrid)
else:
corr, corrframe, sframe = HOGcorr_frameandvec(cube1[i,:,:], xvec, yvec, pxsz=pxsz, ksz=ksz, res=res, wd=wd, regrid=regrid)
corrvec[i-z1min]=corr
#corrcube[i-z1min]=corrframe
corrcube[i,:,:]=corrframe
scube[i,:,:]=sframe
return corrvec, corrcube, scube