/
app.py
532 lines (448 loc) · 20.4 KB
/
app.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import os
import ipywidgets as widgets
from IPython.display import display
import ipyvue
from glue.core.application_base import Application
from glue.core.link_helpers import LinkSame
from glue.core.roi import PolygonalROI
from glue.core.subset import RoiSubsetState
from glue.core.command import ApplySubsetState
from glue.core.edit_subset_mode import (NewMode, ReplaceMode, AndMode, OrMode,
XorMode, AndNotMode)
from glue_jupyter.utils import _update_not_none, validate_data_argument
from glue_jupyter.widgets.subset_select_vuetify import SubsetSelect
from glue_jupyter.widgets.subset_mode_vuetify import SelectionModeMenu
__all__ = ['JupyterApplication']
# TODO: move this to glue-core so that the subset mode can be set ot a string
# there too
SUBSET_MODES = {'new': NewMode, 'replace': ReplaceMode, 'and': AndMode,
'or': OrMode, 'xor': XorMode, 'not': AndNotMode}
ipyvue.register_component_from_file(
None, 'glue-float-field', os.path.join(os.path.dirname(__file__), 'widgets',
'glue_float_field.vue'))
class JupyterApplication(Application):
"""
The main Glue application object for the Jupyter environment.
This is used as the primary way to interact with glue, including loading
data, creating viewers, and adding links.
Parameters
----------
data_collection : `~glue.core.data_collection.DataCollection`
A pre-existing data collection. By default, a new data collection is
created.
session : `~glue.core.session.Session`
A pre-existing session object. By default, a new session object is
created.
"""
def __init__(self, data_collection=None, session=None):
super(JupyterApplication, self).__init__(data_collection=data_collection, session=session)
try:
from glue.main import load_plugins
load_plugins()
except Exception: # Compatibility with glue <0.16
from glue.main import REQUIRED_PLUGINS
REQUIRED_PLUGINS.clear()
load_plugins()
self.output = widgets.Output()
self.widget_data_collection = widgets.SelectMultiple()
self.widget_subset_select = SubsetSelect(session=self.session)
self.widget_subset_mode = SelectionModeMenu(session=self.session)
self.widget = widgets.VBox(children=[self.widget_subset_mode, self.output])
def _ipython_display_(self):
display(self.widget)
def link(self, links):
"""
Parse and add links.
"""
from glue.qglue import parse_links
self.data_collection.add_link(parse_links(self.data_collection, links))
def add_link(self, data1, attribute1, data2, attribute2):
"""
Add a simple identity link between two attributes.
Parameters
----------
data1 : `~glue.core.data.Data`
The dataset containing the first attribute.
attribute1 : str or `~glue.core.component_id.ComponentID`
The first attribute to link.
data2 : `~glue.core.data.Data`
The dataset containing the first attribute.
attribute2 : str or `~glue.core.component_id.ComponentID`
The first attribute to link.
"""
# For now this assumes attribute1 and attribute2 are strings and single
# attributes. In future we should generalize this while keeping the
# simplest use case simple.
att1 = data1.id[attribute1]
att2 = data2.id[attribute2]
link = LinkSame(att1, att2)
self.data_collection.add_link(link)
def set_subset_mode(self, mode):
"""
Set the current subset mode.
By default, selections in viewers update the current subset by
replacing the previous selection with the new selection. However it is
also possible to combine the current selection with previous selections
using boolean operations.
Parameters
----------
mode : {'new', 'replace', 'and', 'or', 'xor', 'not'}
The selection mode to use.
"""
if mode in SUBSET_MODES:
mode = SUBSET_MODES[mode]
self.session.edit_subset_mode.mode = mode
def new_data_viewer(self, *args, **kwargs):
show = kwargs.pop('show', True)
viewer = super().new_data_viewer(*args, **kwargs)
if show:
viewer.show()
return viewer
def table(self, *, data=None, x=None, widget='ipyvuetify', viewer_state=None,
layer_state=None, show=True):
"""
Open an interactive table viewer.
Parameters
----------
data : str or `~glue.core.data.Data`, optional
The dataset to show in the viewer.
widget : {'ipyvuetify', 'matplotlib'}
Whether to use ipyvuetify or ... as table viewer
viewer_state : `~glue.viewers.common.state.ViewerState`
The initial state for the viewer (advanced).
layer_state : `~glue.viewers.common.state.LayerState`
The initial state for the data layer (advanced).
show : bool, optional
Whether to show the view immediately (`True`) or whether to only
show it later if the ``show()`` method is called explicitly
(`False`).
"""
if widget == 'ipyvuetify':
from .table import TableViewer
viewer_cls = TableViewer
else:
raise ValueError("Widget type should be 'ipyvuetify'")
data = validate_data_argument(self.data_collection, data)
viewer_state_obj = viewer_cls._state_cls()
viewer_state = viewer_state or {}
viewer_state_obj.update_from_dict(viewer_state)
view = self.new_data_viewer(viewer_cls, data=data,
state=viewer_state_obj, show=show)
layer_state = layer_state or {}
view.layers[0].state.update_from_dict(layer_state)
return view
def histogram1d(self, *, data=None, x=None, widget='bqplot', color=None,
x_min=None, x_max=None, n_bin=None, normalize=False,
cumulative=False, viewer_state=None, layer_state=None,
show=True):
"""
Open an interactive histogram viewer.
Parameters
----------
data : str or `~glue.core.data.Data`, optional
The initial dataset to show in the viewer. Additional
datasets can be added later using the ``add_data`` method on
the viewer object.
x : str or `~glue.core.component_id.ComponentID`, optional
The attribute to show on the x axis.
widget : {'bqplot', 'matplotlib'}
Whether to use bqplot or Matplotlib as the front-end.
color : str or tuple, optional
The color to use for the data. Note that this will have the
effect of setting the data color for all viewers.
x_min : float, optional
The lower value of the range to compute the histogram in.
x_max : float, optional
The upper value of the range to compute the histogram in.
n_bin : int, optional
The number of bins in the histogram.
normalize : bool, optional
Whether to normalize the histogram.
cumulative : bool, optional
Whether to show a cumulative histogram.
viewer_state : `~glue.viewers.common.state.ViewerState`
The initial state for the viewer (advanced).
layer_state : `~glue.viewers.common.state.LayerState`
The initial state for the data layer (advanced).
show : bool, optional
Whether to show the view immediately (`True`) or whether to only
show it later if the ``show()`` method is called explicitly
(`False`).
"""
if widget == 'bqplot':
from .bqplot.histogram import BqplotHistogramView
viewer_cls = BqplotHistogramView
elif widget == 'matplotlib':
from .matplotlib.histogram import HistogramJupyterViewer
viewer_cls = HistogramJupyterViewer
else:
raise ValueError("Widget type should be 'bqplot' or 'matplotlib'")
data = validate_data_argument(self.data_collection, data)
viewer_state_obj = viewer_cls._state_cls()
viewer_state_obj.x_att_helper.append_data(data)
viewer_state = viewer_state or {}
if x is not None:
viewer_state['x_att'] = data.id[x]
# x_min and x_max get set to the hist_x_min/max in
# glue.viewers.histogram.state for this API it make more sense to call
# it x_min and x_max, and for consistency with the rest
_update_not_none(viewer_state, hist_x_min=x_min, hist_x_max=x_max, hist_n_bin=n_bin,
normalize=normalize, cumulative=cumulative)
viewer_state_obj.update_from_dict(viewer_state)
view = self.new_data_viewer(viewer_cls, data=data,
state=viewer_state_obj, show=show)
layer_state = layer_state or {}
_update_not_none(layer_state, color=color)
view.layers[0].state.update_from_dict(layer_state)
return view
def scatter2d(self, *, data=None, x=None, y=None, widget='bqplot', color=None,
size=None, viewer_state=None, layer_state=None, show=True):
"""
Open an interactive 2d scatter plot viewer.
Parameters
----------
data : str or `~glue.core.data.Data`, optional
The initial dataset to show in the viewer. Additional
datasets can be added later using the ``add_data`` method on
the viewer object.
x : str or `~glue.core.component_id.ComponentID`, optional
The attribute to show on the x axis.
y : str or `~glue.core.component_id.ComponentID`, optional
The attribute to show on the y axis.
widget : {'bqplot', 'matplotlib'}
Whether to use bqplot or Matplotlib as the front-end.
color : str or tuple, optional
The color to use for the markers. Note that this will have the
effect of setting the data color for all viewers.
size : int or float
The size to use for the markers. Note that this will have the
effect of setting the marker size for all viewers.
viewer_state : `~glue.viewers.common.state.ViewerState`
The initial state for the viewer (advanced).
layer_state : `~glue.viewers.common.state.LayerState`
The initial state for the data layer (advanced).
show : bool, optional
Whether to show the view immediately (`True`) or whether to only
show it later if the ``show()`` method is called explicitly
(`False`).
"""
if widget == 'bqplot':
from .bqplot.scatter import BqplotScatterView
viewer_cls = BqplotScatterView
elif widget == 'matplotlib':
from .matplotlib.scatter import ScatterJupyterViewer
viewer_cls = ScatterJupyterViewer
else:
raise ValueError("Widget type should be 'bqplot' or 'matplotlib'")
data = validate_data_argument(self.data_collection, data)
viewer_state_obj = viewer_cls._state_cls()
viewer_state_obj.x_att_helper.append_data(data)
viewer_state_obj.y_att_helper.append_data(data)
viewer_state = viewer_state or {}
if x is not None:
viewer_state['x_att'] = data.id[x]
if y is not None:
viewer_state['y_att'] = data.id[y]
viewer_state_obj.update_from_dict(viewer_state)
view = self.new_data_viewer(viewer_cls, data=data,
state=viewer_state_obj, show=show)
layer_state = layer_state or {}
_update_not_none(layer_state, color=color, size=size)
view.layers[0].state.update_from_dict(layer_state)
return view
def scatter3d(self, *, data=None, x=None, y=None, z=None, show=True):
"""
Open an interactive 3d scatter plot viewer.
Parameters
----------
data : str or `~glue.core.data.Data`, optional
The initial dataset to show in the viewer. Additional
datasets can be added later using the ``add_data`` method on
the viewer object.
x : str or `~glue.core.component_id.ComponentID`, optional
The attribute to show on the x axis.
y : str or `~glue.core.component_id.ComponentID`, optional
The attribute to show on the y axis.
z : str or `~glue.core.component_id.ComponentID`, optional
The attribute to show on the z axis.
show : bool, optional
Whether to show the view immediately (`True`) or whether to only
show it later if the ``show()`` method is called explicitly
(`False`).
"""
from .ipyvolume import IpyvolumeScatterView
data = validate_data_argument(self.data_collection, data)
view = self.new_data_viewer(IpyvolumeScatterView, data=data, show=show)
if x is not None:
x = data.id[x]
view.state.x_att = x
if y is not None:
y = data.id[y]
view.state.y_att = y
if z is not None:
z = data.id[z]
view.state.z_att = z
return view
def imshow(self, *, data=None, x=None, y=None, widget='bqplot', show=True):
"""
Open an interactive image viewer.
Parameters
----------
data : str or `~glue.core.data.Data`, optional
The initial dataset to show in the viewer. Additional
datasets can be added later using the ``add_data`` method on
the viewer object.
x : str or `~glue.core.component_id.ComponentID`, optional
The attribute to show on the x axis. This should be one of the
pixel axis attributes.
y : str or `~glue.core.component_id.ComponentID`, optional
The attribute to show on the y axis. This should be one of the
pixel axis attributes.
widget : {'bqplot', 'matplotlib'}
Whether to use bqplot or Matplotlib as the front-end.
show : bool, optional
Whether to show the view immediately (`True`) or whether to only
show it later if the ``show()`` method is called explicitly
(`False`).
"""
if widget == 'bqplot':
from .bqplot.image import BqplotImageView
viewer_cls = BqplotImageView
elif widget == 'matplotlib':
from .matplotlib.image import ImageJupyterViewer
viewer_cls = ImageJupyterViewer
else:
raise ValueError("Widget type should be 'bqplot' or 'matplotlib'")
data = validate_data_argument(self.data_collection, data)
if len(data.pixel_component_ids) < 2:
raise ValueError('Only data with two or more dimensions can be used '
'as the initial dataset in the image viewer')
view = self.new_data_viewer(viewer_cls, data=data, show=show)
if x is not None:
x = data.id[x]
view.state.x_att = x
if y is not None:
y = data.id[y]
view.state.y_att = y
return view
def profile1d(self, *, data=None, x=None, widget='bqplot', show=True):
"""
Open an interactive 1d profile viewer.
Parameters
----------
data : str or `~glue.core.data.Data`, optional
The initial dataset to show in the viewer. Additional
datasets can be added later using the ``add_data`` method on
the viewer object.
x : str or `~glue.core.component_id.ComponentID`, optional
The attribute to show on the x axis. This should be a pixel or
world coordinate `~glue.core.component_id.ComponentID`.
widget : {'bqplot', 'matplotlib'}
Whether to use bqplot or Matplotlib as the front-end.
show : bool, optional
Whether to show the view immediately (`True`) or whether to only
show it later if the ``show()`` method is called explicitly
(`False`).
"""
if widget == 'bqplot':
from .bqplot.profile import BqplotProfileView
viewer_cls = BqplotProfileView
elif widget == 'matplotlib':
from .matplotlib.profile import ProfileJupyterViewer
viewer_cls = ProfileJupyterViewer
else:
raise ValueError("Widget type should be 'matplotlib'")
data = validate_data_argument(self.data_collection, data)
view = self.new_data_viewer(viewer_cls, data=data, show=show)
if x is not None:
x = data.id[x]
view.state.x_att = x
return view
def volshow(self, *, data=None, x=None, y=None, z=None, show=True):
"""
Open an interactive volume viewer.
Parameters
----------
data : str or `~glue.core.data.Data`, optional
The initial dataset to show in the viewer. Additional
datasets can be added later using the ``add_data`` method on
the viewer object.
x : str or `~glue.core.component_id.ComponentID`, optional
The attribute to show on the x axis. This should be one of the
pixel axis attributes.
y : str or `~glue.core.component_id.ComponentID`, optional
The attribute to show on the y axis. This should be one of the
pixel axis attributes.
z : str or `~glue.core.component_id.ComponentID`, optional
The attribute to show on the z axis. This should be one of the
pixel axis attributes.
show : bool, optional
Whether to show the view immediately (`True`) or whether to only
show it later if the ``show()`` method is called explicitly
(`False`).
"""
from .ipyvolume import IpyvolumeVolumeView
data = validate_data_argument(self.data_collection, data)
view = self.new_data_viewer(IpyvolumeVolumeView, data=data, show=show)
if x is not None:
x = data.id[x]
view.state.x_att = x
if y is not None:
y = data.id[y]
view.state.y_att = y
if z is not None:
z = data.id[z]
view.state.z_att = z
return view
def subset(self, name, subset_state):
"""
Create a new selection/subset.
Parameters
----------
name : str
The name of the new subset.
subset_state : `~glue.core.subset.SubsetState`
The definition of the subset. See the documentation at
http://docs.glueviz.org/en/stable/python_guide/data_tutorial.html#defining-new-subsets
for more information about creating subsets programmatically.
"""
return self.data_collection.new_subset_group(name, subset_state)
def subset_lasso2d(self, x_att, y_att, lasso_x, lasso_y):
"""
Create a subset from a programmatic 2d lasso selection.
Parameters
----------
x_att : `~glue.core.component_id.ComponentID`
The attribute corresponding to the x values being selected.
y_att : `~glue.core.component_id.ComponentID`
The attribute corresponding to the x values being selected.
lasso_x : iterable
The x values of the lasso.
lasso_y : iterable
The y values of the lasso.
"""
roi = PolygonalROI(lasso_x, lasso_y)
self.subset_roi([x_att, y_att], roi)
def subset_roi(self, attributes, roi):
"""
Create a subset from a region of interest.
Parameters
----------
attributes : iterable
The attributes on the x and y axis
roi : `~glue.core.roi.Roi`
The region of interest to use to create the subset.
"""
subset_state = RoiSubsetState(attributes[0], attributes[1], roi)
cmd = ApplySubsetState(data_collection=self.data_collection,
subset_state=subset_state)
self._session.command_stack.do(cmd)
# Methods that we need to override to avoid the default behavior
def _update_undo_redo_enabled(self, *args):
pass # TODO: if we want a gui for this, we need to update it here
@staticmethod
def _choose_merge(*args, **kwargs):
# Never suggest automatic merging
return None, None
def add_widget(self, widget, label=None, tab=None):
pass