File: export_d3po.py

package info (click to toggle)
glueviz 0.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 17,180 kB
  • ctags: 6,728
  • sloc: python: 37,111; makefile: 134; sh: 60
file content (312 lines) | stat: -rw-r--r-- 8,913 bytes parent folder | download
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
from __future__ import absolute_import, division, print_function

import os
import json

from glue.core import Subset



DISPATCH = {}


def save_page(page, page_number, label, subset):
    """ Convert a tab of a glue session into a D3PO page

    :param page: Tuple of data viewers to save
    :param label: Tab label
    """
    result = {}

    # layout settings
    result['grid'] = {'nRows': 1, 'nColumns': len(page)}
    result['name'] = str(label)
    result['caption'] = 'Generated by Glue'

    # style settings
    d = page[0]._data[0]
    unselected = dict(opacity=d.style.alpha,
                      size=d.style.markersize / 2,
                      color=d.style.color)
    result['markerStyle'] = dict(unselected=unselected)

    if subset is not None:
        s = subset.style
        selected = dict(opacity=s.alpha, size=s.markersize / 2, color=s.color)
        result['markerStyle']['selected'] = selected
        result['selection'] = {'type': 'booleanColumn',
                               'columnName': 'selection_%i' % page_number}
    result['histogramStyle'] = result['markerStyle']

    # save each plot
    result['plots'] = list(map(save_plot, page, range(len(page))))

    return result


def save_plot_base(plot, index):
    result = {}
    result['gridPosition'] = [0, index]
    return result


def save_plot(plot, index):
    typ = type(plot)
    return DISPATCH[typ](plot, index)


def save_scatter(plot, index):
    """ Convert a single glue scatter plot to a D3PO plot

    :param plot: Glue scatter plot
    :class:`~glue.viewers.scatter.qt.ScatterWidget`
    :param index: 1D index of plot on the page
    :type index: int

    :rtype: json-serializable dict
    """
    result = save_plot_base(plot, index)
    props = plot.properties
    result['type'] = 'scatter'
    result['xAxis'] = dict(columnName=props['xatt'].label,
                           range=[props['xmin'], props['xmax']])
    result['yAxis'] = dict(columnName=props['yatt'].label,
                           range=[props['ymin'], props['ymax']])
    # XXX log scales
    return result


def save_histogram(plot, index):
    """ Convert a single histogram to a D3PO plot

    :param plot: Glue histogram
    :type plot: :class:`~glue.viewers.histogram.qt.HistogramWidget`

    :param index: 1D index of plot on the page
    :type index: int

    :rtype: json-serializable dict
    """
    result = save_plot_base(plot, index)
    props = plot.properties
    result['type'] = 'histogram'
    result['xAxis'] = dict(columnName=props['component'].label,
                           bins=props['nbins'],
                           range=[props['xmin'], props['xmax']])
    # XXX normed, cumultive, log
    return result


def stage_subsets(application):
    """
    Return a tuple of the subset to use for each stage/tab,
    or None if the tab has no subset

    If more than one subset is used per stage/tab, returns None
    """
    result = []
    for page in application.viewers:
        subset = None
        for viewer in page:
            for layer_artist in viewer.layers:
                if not layer_artist.visible:
                    continue
                s = layer_artist.layer
                if not isinstance(s, Subset):
                    continue
                if subset is not None and s is not subset:
                    return None
                if subset is None:
                    subset = s
        result.append(subset)
    return tuple(result)


def can_save_d3po(application):
    """
    Check whether an application can be exported to D3PO.

    Raises an exception if not
    """
    dc = application.session.data_collection

    if len(dc) != 1:
        raise ValueError("D3PO Export only supports a single dataset")
    data = dc[0]

    for tab in application.viewers:
        for viewer in tab:
            if not isinstance(viewer, tuple(DISPATCH.keys())):
                raise ValueError("D3PO Export only supports scatter "
                                 "and histogram plots")
    if sum(len(tab) for tab in application.viewers) == 0:
        raise ValueError("D3PO Export requires at least one scatterplot "
                         "or histogram")

    if stage_subsets(application) is None:
        raise ValueError("D3PO Export restricted to 0 or 1 subsets visible "
                         "in each tab")


def make_data_file(data, subsets, path):
    """
    Create the data.csv file, given Data and tuple of subsets
    """
    from astropy.table import Table, Column

    data_path = os.path.join(path, 'data.csv')

    t = Table([data[c] for c in data.components],
              names=[c.label for c in data.components])

    for i, subset in enumerate(subsets):
        if subset is None:
            continue
        c = Column(data=subset.to_mask().astype('i'), name='selection_%i' % i)
        t.add_column(c)

    t.write(data_path, format='ascii', delimiter=',')


def save_d3po(application, path):
    """Save a Glue session to a D3PO bundle.

    Currently, this has the following restrictions:
    - The Glue session must have only one dataset open, and 0 or 1 subsets
    - Only scatter plots or histograms are present
    - At least one plot is present

    :param application: Glue appication to save
    :param path: Path to directory to save in. Will be created if needed
    """
    if os.path.exists(path) and not os.path.isdir(path):
        os.unlink(path)

    if not os.path.exists(path):
        os.mkdir(path)

    data = application.session.data_collection[0]
    subsets = stage_subsets(application)
    viewers = application.viewers

    # data.csv
    make_data_file(data, subsets, path)

    # states.json
    result = {}
    result['filename'] = 'data.csv'  # XXX don't think this is needed?
    result['title'] = "Glue export of %s" % data.label
    result['states'] = list(map(save_page, application.viewers,
                                range(len(viewers)),
                                application.tab_names,
                                subsets))

    state_path = os.path.join(path, 'states.json')
    with open(state_path, 'w') as outfile:
        json.dump(result, outfile, indent=2)

    # index.html
    html_path = os.path.join(path, 'index.html')
    with open(html_path, 'w') as outfile:
        outfile.write(HTML)

    # show the result
    launch(path)


def launch(path):
    """Start a server to view an exported D3PO bundle, and open a browser.

    :param path: The TLD of the bundle
    """
    from glue.external.six.moves.socketserver import TCPServer
    from glue.external.six.moves.SimpleHTTPServer import SimpleHTTPRequestHandler
    from random import randrange
    from socket import error
    import webbrowser
    from threading import Thread

    os.chdir(path)

    while True:
        try:
            PORT = randrange(8000, 9000)
            server = TCPServer(("", PORT), SimpleHTTPRequestHandler, False)
            server.allow_reuse_address = True
            server.server_bind()
            break
        except error:  # port already taken
            pass

    print('Serving D3PO on port 0.0.0.0:%i' % PORT)
    server.server_activate()

    thread = Thread(target=server.serve_forever)
    thread.setDaemon(True)  # do not prevent shutdown
    thread.start()
    webbrowser.open('http://0.0.0.0:%i' % PORT)


def setup():
    from glue.config import exporters
    exporters.add('D3PO', save_d3po, can_save_d3po, outmode='directory')


HTML = """
<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8" />

<link rel="stylesheet" type="text/css" href="http://d3po.org/static/css/style.css">
<link rel="stylesheet" type="text/css" href="http://d3po.org/static/css/d3po.css">
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:100,200,300,400,700' rel='stylesheet' type='text/css'>

<style>
#footer {
position: fixed;
bottom: 0;
right: 0;
}
</style>
<!-- not to be confused with Planet Telex -->

<!-- Javscript dependencies -->
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="http://d3po.org/static/js/util.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://d3po.org/static/js/d3po.js"></script>
<script src="http://d3po.org/static/js/d3po.init.js"></script>
</head>

<body>
<div id="svg"><svg></svg></div>
<div id="controls">
<ul class="navigation">
</ul>
</div>
<div id="caption"></div>

<div id="footer">
More information: <a href="http://d3po.org">d3po.org</a>
</div>

<script type="text/javascript">
$(document).ready(function() {
initialize('states.json', 'data.csv');
}
);
</script>
</body>
</html>
"""

try:
    from glue.viewers.scatter.qt import ScatterWidget
    from glue.viewers.histogram.qt import HistogramWidget
except ImportError:
    pass
else:
    DISPATCH[ScatterWidget] = save_scatter
    DISPATCH[HistogramWidget] = save_histogram