-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspectr_cluster.py
283 lines (185 loc) · 6.21 KB
/
spectr_cluster.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
"""
Introducing the spectral clustering
to dendrograms!
"""
from astropy.io import fits
from astropy.io.fits import getheader
from astrodendro import Dendrogram
import os.path
from readcol import readcol
from pdb import set_trace as stop
import numpy as np
from itertools import combinations
import scipy
from scipy import linalg
from numpy import rank
from matplotlib import pyplot as p
from matplotlib import colors as color
# To compute the matrix kernel
def null(A, eps=1e-15):
u, s, vh = scipy.linalg.svd(A)
null_mask = (s <= eps)
null_space = scipy.compress(null_mask, vh, axis=0)
return scipy.transpose(null_space)
# File list and stuff
path = '/Users/Dario/Documents/dendrograms/'
dendro_file = 'compl_dendrogram'
data_file = 'PerA_Extn2MASS_F_Gal.fits'
load_file = 'compl_dendrogram.fits'
dendro_file = path+'orion_dendrogram'
data_file = path+'orion.fits'
load_file = path+'orion_dendrogram.fits'
# Control flow
do_make = False
do_load = True
do_matrix = False
do_weight = False
do_topview = True
# Make the dendrogram of the full cube/image
if do_make:
print 'Make dendrogram from the full cube'
data = fits.getdata(data_file)
if size(shape(data))==4:
data = data[0,:,:,:]
rms = 0.4
pix_beam = 14
d = Dendrogram.compute(data, min_value=2*rms, min_delta=2*rms, min_npix=10, verbose = 1)
d.save_to(dendro_file+'.fits')
d.viewer()
# Load a premade dendrogram
if do_load:
data = fits.getdata(data_file)
if size(shape(data))==4:
data = data[0,:,:,:]
print 'Load dendrogram file: '+load_file
d = Dendrogram.load_from(load_file)
#d.viewer()
# Introduction to the spectral clustering:
# make the necessary matrices and some
# experimental ones.
if do_matrix:
#Calculate the adjacency matrix
s = d.trunk[-1]
#Preparing the matrices
# Finding a number of nodes
# the last structure is necessary
# a leave, then:
num = d.leaves[-1].idx+1
#Adjacency matrix A
A = zeros((num,num), dtype=np.int)
#Graph degree matrix GD
GD = zeros((num,num), dtype=np.int)
#Antenna temperature distance matrix TD
TD = zeros((num,num))
#Pixel separation distance matrix SD
SD = zeros((num,num))
#Descendant matrix?
DM = zeros((num,num), dtype = np.int)
for i in range(num):
# Local maxima coordinates
if len(data.shape)==2:
xi, yi = d[i].get_peak()[0]
else:
xi, yi, vi = d[i].get_peak()[0]
if do_weight:
# Filling the weighted adjacent matrix
childs = d[i].children
if len(childs) > 0:
GD[i,i] = len(childs)*childs[0].level
for child in childs:
j = child.idx
A[i,j] = child.level
A[j,i] = child.level
else:
# Filling the adjacent matrix
childs = d[i].children
if len(childs) > 0:
GD[i,i] = len(childs)
for child in childs:
j = child.idx
A[i,j] = 1
A[j,i] = 1
# Filling the descendant matrix
descs = d[i].descendants
DM[i,i] = len(descs)
for desc in descs:
j = desc.idx
DM[i,j] = 1
DM[j,i] = 1
for j in range(num):
# TD so far is easy
TD[i,j] = d[i].height-d[j].height
# SD is more challenging
# no need to convert in
# physical units now
if len(data.shape)==2:
xj, yj = d[j].get_peak()[0]
else:
xj, yj, vj = d[j].get_peak()[0]
SD[i,j] = ((xj-xi)**2+(yj-yi)**2)**0.5
# Laplacian L = GD - A
L = GD - A
# Determine the eigenvectors
# and eigenvalues for the connectivity
L_eigval = np.linalg.eigvalsh(L)
#l_eigvec = np.linalg.eigvh(laplacian)
# The second lower eigenvalue of L
# gives the algebraic connectivity
# of L
conn = np.sort(L_eigval)[1]
# The dimension of L kernel gives
# the number of connected components
# of A
L_ker = null(L)
# To calculate the dim(L_ker) I use
# the rank theorem:
# dim(L_Ker) = num column(L) - rk(L)
# this gives the number of connected
# component of adjmat
A_conn_compts = L.shape[1] - rank(L)
# Now start the spectral clustering...
# Attempt to visualize the dendrogram from
# the top as in the spectral clustering
if do_topview:
num = d.leaves[-1].idx+1
xs = zeros(num, dtype = np.int)
ys = zeros(num, dtype = np.int)
levs = zeros(num, dtype = np.int)
p.clf()
p.axis([0,data.shape[0],0,data.shape[1]])
for i in range(num):
if len(data.shape)==2:
xi, yi = d[i].get_peak(subtree=True)[0]
else:
xi, yi, vi = d[i].get_peak(subtree=True)[0]
xs[i] = xi
ys[i] = yi
levs[i] = d[i].level
# Draw connections between branch and leaves
if d[i].is_branch:
childs = d[i].children
for child in childs:
if len(data.shape)==2:
xj, yj = child.get_peak(subtree=True)[0]
else:
xj, yj, vj = child.get_peak(subtree=True)[0]
#p.plot([xi,yi],[xj,yj], 'b-')
p.plot([xi,xj],[yi,yj], 'k-')
lev_max = max(levs)
lev_min = min(levs)
lev_range = lev_max - lev_min
for i in range(num):
xi = xs[i]
yi = ys[i]
lev = levs[i]
# Draw a circle for structure
# the color corresponds to the level
col = (lev - lev_min)/float(lev_range)
color = str(col)
p.plot(xi,yi,'o')
#struct = scatter(xi, yi, marker='o', c=levs)
#draw()
#p.clf()
#p.axis([0,data.shape[0],0,data.shape[1]])
#struct = scatter(xs, ys, marker='o', c=colors)
#draw()