visualize.js 8.95 KB
Newer Older
Ivan Bogatyy's avatar
Ivan Bogatyy committed
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

/**
 * @fileoverview This file is the main entrypoint for DRAGNN trace
 * visualization. The main function that it exposes is visualizeToDiv(), which
 * it will save to `window` (supposing `window` is defined).
 */
const cytoscape = require('cytoscape');
const _ = require('lodash');

// Legacy JS, downloaded in the Dockerfile from github. This generates
// colorblind-friendly color palettes, for a variable number of components.
//
// This library consumes 27kb minified-but-uncompressed. Currently we mostly
// serve JS to high-bandwidth clients like IPython, but if minified size is ever
// a concern, it should be fairly easy to pre-compute our color palettes.
const palette = require('exports-loader?palette!../palette.js');

// Register our custom DRAGNN layout class.
const DragnnLayout = require('./dragnn_layout.js');
cytoscape('layout', 'dragnn', DragnnLayout);

import preact from 'preact';
import InteractiveGraph from './interactive_graph.jsx';
import setupTraceInteractionHandlers from './trace_interaction_handlers';

// Helper class to build a Cytoscape graph from a DRAGNN master spec.
class DragnnCytoscapeGraphBuilder {
  /**
   * Creates a new DragnnCytoscapeGraphBuilder.
   */
  constructor() {
    this.graph = {nodes: [], edges: []};
    this.nodesWithoutCaptions = 0;
  }

  /**
   * Adds a new component node to the graph. Component nodes represent
   * components in DRAGNN, which often resemble tasks (tagging, parsing, etc.).
   *
   * @param {!Object} component Component descriptor from the master spec.
   */
  addComponentNode(component) {
    this.graph.nodes.push({
      'data': {
        'id': component.name,
        'idx': component.idx,
        'componentColor': '#' + component.color,
        'text': component.name,
        'type': 'component',
      },
      'classes': 'component',
    });
  }

  /**
   * Adds a new step node to the graph. Step nodes are generated by unrolling a
   * component on a concrete example.
   *
   * @param {!Object} component Component descriptor from the master spec.
   * @param {!Object} step Step descriptor from the master spec.
   * @return {string} ID fo the node created.
   */
  addNode(component, step) {
    const graphId = component.name + '-' + step.idx;

    this.graph.nodes.push({
      'data': {
        'id': graphId,
        'componentIdx': component.idx,
        'stepIdx': step.idx,
        'parent': component.name,
        'text': step.caption || graphId,
        'componentColor': '#' + component.color,
        'type': 'step',

        // Shown in the mouse-over (node_info.jsx).
        'stateInfo': step.html_representation,
        'fixedFeatures': step.fixed_feature_trace,
      },
      'classes': 'step',
    });
    return graphId;
  }

  /**
   * Adds a list of components from the master spec.
   *
   * This function generates colors, and calls addComponent().
   *
   * @param {!Object} masterTrace Master trace proto from DRAGNN.
   */
  addComponents(masterTrace) {
    const colors = palette('tol', masterTrace.component_trace.length);
    if (colors == null) {
      // Apparently palette.js can fail with > 12 components or such.
      window.alert('FAILURE -- YOU HAVE TOO MANY COMPONENTS FOR palette.js.');
      return;
    }
    _.each(masterTrace.component_trace, function(component, idx) {
      component.idx = idx;
      component.color = colors[idx];
      this.addComponent(component);
    }.bind(this));
    console.log('' + this.nodesWithoutCaptions + ' nodes without captions');
  }

  /**
   * Adds one component from the master spec. This generates component nodes,
   * step nodes, and edges.
   *
   * @param {!Object} component Component descriptor from the master spec.
   */
  addComponent(component) {
    this.addComponentNode(component);

    _.each(component.step_trace, (step, idx) => {
      step.idx = idx;

      if (!step.caption) {
        this.nodesWithoutCaptions += 1;
        return;
      }

      const graphId = this.addNode(component, step);

      _.each(step.linked_feature_trace, (linkedFeature) => {
        // Each feature can take multiple values.
        _.each(linkedFeature.value_trace, (linkedValue) => {
          const srcGraphId =
              linkedFeature.source_component + '-' + linkedValue.step_idx;
          this.graph.edges.push({
            'data': {
              'source': srcGraphId,
              'target': graphId,
              'curvature': 0,
              'featureName': linkedValue.feature_name,
              'featureValue': linkedValue.feature_value,
            }
          });
        });
      });
    });
  }
}

/**
 * Component for a graph and its controls. Currently this just manually
 * calls DOM methods, but we'll switch it out for something more modern in a
 * sec.
 */
class InteractiveDragnnGraph {
  /**
   * Controller for the entire DRAGNN graph element on a page.
   *
   * @param {!Object} masterTrace Master trace proto from DRAGNN.
   * @param {!Object} element Container DOM element to populate.
   */
  constructor(masterTrace, element) {
    this.masterTrace = masterTrace;
    this.element = element;
  }

  /**
   * Initializes the controls and the graph.
   */
  initDomElements() {
    this.element.style.position = 'relative';
    this.element.style.overflow = 'hidden';
    const elt = preact.h(InteractiveGraph, {
      onmount: (view, graph_elt) => {
        this.view = view;
        this.initializeGraph(view, graph_elt);
      },
      onfilter: this.onFilter.bind(this),
    });
    preact.render(elt, this.element);
  }

  /**
   * Handler for when filtering text is entered.
   *
   * Future features: Make the number of neighbors customizable.
   *
   * @param {string} text Regular expression text to filter with. Currently
   *     applied to node labels.
   */
  onFilter(text) {
    // Show relevant nodes (and parent components).
    const re = new RegExp(text);
    let sel = this.cy.$('node.step').filter(function(i, node) {
      return !text || node.data('text').match(re);
    });
    sel = sel.union(sel.neighborhood());
    sel = sel.union(sel.parents());
    sel.nodes().show();
    sel.abscomp().nodes().hide();

    // Redo layout.
    this.cy.layout({name: 'dragnn'});
  }

  /**
   * Initializes the Cytoscape graph.
   */
  initializeGraph(view, domElement) {
    const builder = new DragnnCytoscapeGraphBuilder();
    builder.addComponents(this.masterTrace);

    const cy = cytoscape({
      container: domElement,
      boxSelectionEnabled: true,
      autounselectify: true,
      // We'll do more custom layout later.
      layout: {name: 'dragnn'},
      style: [
        {
          selector: 'node',
          style: {
            'background-color': 'data(componentColor)',
            'content': 'data(text)',
            'text-halign': 'center',
            'text-opacity': 1.0,
            'text-valign': 'center',
          }
        },
        {
          selector: 'node.step',
          style: {
            'text-outline-width': 2,
            'text-outline-color': '#ffffff',
            'text-outline-opacity': 0.3,
          }
        },
        {
          selector: ':parent',
          style: {
            'background-opacity': 0.1,
            'text-halign': 'right',
            'text-margin-x': 5,
            'text-margin-y': 5,
            'text-valign': 'bottom',
          }
        },
        {
          selector: 'edge',
          style: {
            'control-point-distance': 'data(curvature)',
            'curve-style': 'unbundled-bezier',
            'line-color': '#666666',
            'opacity': 0.4,
            'target-arrow-color': '#666666',
            'target-arrow-shape': 'triangle',
            'width': 3,
          }
        },
        {selector: 'edge.faded-near', style: {'opacity': 0.2}},
        {selector: 'node.step.faded-near', style: {'opacity': 0.5}},
        {
          selector: 'node.step.faded-far, edge.faded-far',
          style: {'opacity': 0.1}
        },
        // Overall, override stuff for mouse-overs, but don't make the far edges
        // too dark (that looks jarring).
        {
          selector: 'edge.highlighted-edge',
          style: {'line-color': '#333333', 'opacity': 1.0}
        },
        {
          selector: 'edge.highlighted-edge.faded-far',
          style: {
            'opacity': 0.4,
          }
        },
      ],
      elements: builder.graph,
    });
    this.cy = cy;
    setupTraceInteractionHandlers(cy, view, this.element);
  }
}

/**
 * This is the external interface. See "index.html" for the development example,
 * which downloads graph data from a JSON file.  In most iPython notebook
 * situations, the script tag containing the graph definition will be generated
 * inline.
 *
 * @param {Object} masterTrace Master trace proto from DRAGNN.
 * @param {string} divId ID of the page element to populate with the graph.
 */
const visualizeToDiv = function(masterTrace, divId) {
  const interactiveGraph =
      new InteractiveDragnnGraph(masterTrace, document.getElementById(divId));
  interactiveGraph.initDomElements();
};

if (window !== undefined) {
  window.visualizeToDiv = visualizeToDiv;
}