forked from tomr-stargazer/dendrogal
/
integrated_and_cartoon_viewer.py
303 lines (225 loc) · 11.1 KB
/
integrated_and_cartoon_viewer.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""
A little moduel that creates the IntegratedViewer knockoff that has a Dame cartoon.
The axes of the two views are shared.
"""
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
from astrodendro.plot import DendrogramPlotter
from wcsaxes import WCSAxes
from overlay_dame_cartoon import overlay_lb_cartoon, overlay_lv_cartoon
class CartoonDualViewer(object):
def __init__(self, dendrogram, hub, alignment='latitude', cmap=plt.cm.gray,
clip_velocity=None):
if dendrogram.data.ndim != 3:
raise ValueError(
"Only 3-dimensional arrays are supported")
if not hasattr(hub, 'select_subtree'):
raise NotImplementedError("astrodendro does not have scatter_picker enabled")
self.hub = hub
self.datacube = dendrogram.data
self.dendrogram = dendrogram
self.wcs = dendrogram.wcs
self.plotter = DendrogramPlotter(dendrogram)
self.plotter.sort(reverse=True)
# Get the lines as individual elements, and the mapping from line to structure
self.lines = self.plotter.get_lines(edgecolor='k')
self.selected_lines = {}
self.selected_contours = {} # selection_id -> (contour, contour) tuple
if alignment == 'latitude':
figsize = (15, 9)
ax_integrated_limits = [0.025, 0.05, 0.6, 0.5]
ax_cartoon_limits = [0.025, 0.55, 0.6, 0.5]
slices = ('x', 'y', 0)
self.sum_axis=0
self.overlay_cartoon = overlay_lb_cartoon
self.aspect = 1
elif alignment == 'velocity':
figsize = (15, 9)
ax_integrated_limits = [0.025, 0.05, 0.6, 0.5]
ax_cartoon_limits = [0.025, 0.55, 0.6, 0.5]
slices = ('x', 0, 'y')
self.sum_axis=1
self.overlay_cartoon = overlay_lv_cartoon
self.aspect = 2.5
# elif alignment == 'pp' or alignment == 'lb':
# figsize = (10, 4)
# ax_integrated_limits = [0.1, 0.1, 0.8, 0.8]
# ax_cartoon_limits = [0,0,0.01,0.01]
else:
raise ValueError("`alignment` must be 'latitude' or 'velocity'")
self.alignment = alignment
self.fig = plt.figure(figsize=figsize)
self.cmap = cmap
if self.dendrogram.wcs is not None:
ax_integrated = WCSAxes(self.fig, ax_integrated_limits, wcs=self.dendrogram.wcs, slices=slices)
self.ax_integrated = self.fig.add_axes(ax_integrated)
ax_cartoon = WCSAxes(self.fig, ax_cartoon_limits, wcs=self.dendrogram.wcs, slices=slices,
sharex=ax_integrated, sharey=ax_integrated)
self.ax_cartoon = self.fig.add_axes(ax_cartoon)
else:
self.ax_integrated = self.fig.add_axes(ax_integrated_limits)
self.ax_cartoon = self.fig.add_axes(ax_cartoon_limits,
sharex=ax_integrated, sharey=ax_integrated)
array_summed = np.nansum(self.datacube, axis=self.sum_axis)
array_summed[(array_summed < 0) | np.isinf(array_summed) | np.isnan(array_summed)] = 0
self.array_summed = array_summed
# all stolen from astrodendro.viewer
self.ax_dendrogram = self.fig.add_axes([0.65, 0.1, 0.35, 0.6])
self.ax_dendrogram.add_collection(self.lines)
self.selected_label = {} # map selection IDs -> text objects
self.selected_label[1] = self.fig.text(0.75, 0.9, "No structure selected", fontsize=18,
color=self.hub.colors[1])
self.selected_label[2] = self.fig.text(0.75, 0.85, "No structure selected", fontsize=18,
color=self.hub.colors[2])
self.selected_label[3] = self.fig.text(0.75, 0.8, "No structure selected", fontsize=18,
color=self.hub.colors[3])
x = [p.vertices[:, 0] for p in self.lines.get_paths()]
y = [p.vertices[:, 1] for p in self.lines.get_paths()]
xmin = np.min(x)
xmax = np.max(x)
ymin = np.min(y)
ymax = np.max(y)
self.lines.set_picker(2.)
self.lines.set_zorder(0)
dx = xmax - xmin
self.ax_dendrogram.set_xlim(xmin - dx * 0.1, xmax + dx * 0.1)
self.ax_dendrogram.set_ylim(ymin * 0.5, ymax * 2.0)
self.ax_dendrogram.set_yscale('log')
self.fig.canvas.mpl_connect('pick_event', self.line_picker)
# array_lv = np.nansum(self.datacube, axis=1)
# array_lv[(array_lv < 0) | np.isinf(array_lv) | np.isnan(array_lv)] = 0
# self.array_lv = array_lv
# if clip_velocity is None:
# if np.shape(array_lv)[0]*2.5 > np.shape(array_lb)[0]:
# self.clip_velocity = True
# else:
# self.clip_velocity = False
# else:
# self.clip_velocity = clip_velocity
self._draw_plot()
self.hub.add_callback(self.update_selection)
self.hub.add_callback(self._update_lines)
self.fig.canvas.mpl_connect('button_press_event', self.select_from_map)
# If things are already selected in the hub, go select them!
for selection_id in self.hub.selections:
self.update_selection(selection_id)
def _draw_plot(self):
""" Create an image to plot things onto. """
len_v, len_l, len_b = self.datacube.shape
self.image_cartoon = self.overlay_cartoon(self.ax_cartoon)[0]
self.image_integrated = self.ax_integrated.imshow(np.log10(self.array_summed+1), origin='lower',
interpolation='nearest', cmap=self.cmap, aspect=self.aspect)
# Trim the top and bottom of the l, v plot for cosmetic reasons
# if self.clip_velocity:
# crop_factor = np.round(len_v/5)
# self.ax_cartoon.set_ylim(crop_factor, len_v - crop_factor)
self.fig.canvas.draw()
def remove_contour(self, selection_id):
if selection_id in self.selected_contours:
for ax, contour in zip([self.ax_integrated, self.ax_cartoon], self.selected_contours[selection_id]):
for collection in contour.collections:
ax.collections.remove(collection)
del self.selected_contours[selection_id]
def select_from_map(self, event):
# Only do this if no tools are currently selected
if event.canvas.toolbar.mode != '':
return
if event.button not in self.hub.colors:
return
input_key = event.button
if event.inaxes is self.ax_integrated or event.inaxes is self.ax_cartoon:
if self.alignment == 'latitude':
# Find pixel co-ordinates of click
ix = int(round(event.xdata))
iy = int(round(event.ydata))
try:
iz = np.where(self.datacube[:, iy, ix] == np.nanmax(self.datacube[:, iy, ix]))[0][0]
except IndexError:
iz = 0
elif self.alignment == 'velocity':
# Find pixel co-ordinates of click
ix = int(round(event.xdata))
iz = int(round(event.ydata))
try:
iy = np.where(self.datacube[iz, :, ix] == np.nanmax(self.datacube[iz, :, ix]))[0][0]
except IndexError:
iy = 0
else:
return
indices = (iz, iy, ix)
# Select the structure
structure = self.dendrogram.structure_at(indices)
self.hub.select(input_key, structure)
self.update_selection(input_key)
# Re-draw
event.canvas.draw()
def update_selection(self, selection_id):
"""Highlight seleted structures"""
self.remove_contour(selection_id)
if selection_id in self.hub.selections:
structures = self.hub.selections[selection_id]
select_subtree = self.hub.select_subtree[selection_id]
struct = structures[0]
if struct is not None:
if select_subtree:
mask = struct.get_mask(subtree=True)
else:
mask = reduce(np.add, [structure.get_mask(subtree=True) for structure in structures])
# numpy magic sets nonzero values to True
mask_summed = np.sum(mask, axis=self.sum_axis).astype('bool')
self.selected_contours[selection_id] = (
self.ax_integrated.contour(
mask_summed, colors=self.hub.colors[selection_id],
linewidths=5, levels=[0.5], alpha=0.9, zorder=struct.height),
self.ax_cartoon.contour(
mask_summed, colors=self.hub.colors[selection_id],
linewidths=5, levels=[0.5], alpha=0.9, zorder=struct.height) )
self.fig.canvas.draw()
def line_picker(self, event):
# Only do this if no tools are currently selected
if event.canvas.toolbar.mode != '':
return
if event.mouseevent.button not in self.selected_label:
return
input_key = event.mouseevent.button
# event.ind gives the indices of the paths that have been selected
# Find levels of selected paths
peaks = [event.artist.structures[i].get_peak(subtree=True)[1] for i in event.ind]
# Find position of minimum level (may be duplicates, let Numpy decide)
ind = event.ind[np.argmax(peaks)]
# Extract structure
structure = event.artist.structures[ind]
# Select the structure
self.hub.select(input_key, structure)
# Re-draw
event.canvas.draw()
def _update_lines(self, selection_id):
structures = self.hub.selections[selection_id]
select_subtree = self.hub.select_subtree[selection_id]
structure = structures[0]
# Remove previously selected collection
if selection_id in self.selected_lines:
self.ax_dendrogram.collections.remove(self.selected_lines[selection_id])
del self.selected_lines[selection_id]
if structure is None:
self.selected_label[selection_id].set_text("No structure selected")
self.remove_contour(selection_id)
self.fig.canvas.draw()
return
# self.remove_all_contours()
if len(structures) <= 1:
label_text = "Selected structure: {0}".format(structure.idx)
elif len(structures) <=3:
label_text = "Selected structures: {0}".format(', '.join([str(struct.idx) for struct in structures]))
else:
label_text = "Selected structures: {0}...".format(', '.join([str(struct.idx) for struct in structures[:3]]))
self.selected_label[selection_id].set_text(label_text)
# Get collection for this substructure
self.selected_lines[selection_id] = self.plotter.get_lines(
structures=structures, subtree=select_subtree)
self.selected_lines[selection_id].set_color(self.hub.colors[selection_id])
self.selected_lines[selection_id].set_linewidth(2)
self.selected_lines[selection_id].set_zorder(structure.height)
# Add to axes
self.ax_dendrogram.add_collection(self.selected_lines[selection_id])