Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support For 3D Scatter Plots and Line Plots (Closes #1463) #1572

Merged
merged 14 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 42 additions & 11 deletions src/common/viz/FigureExtractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,34 @@ define(['./Utils'], function (Utils) {
return desc;
};

FigureExtractor.prototype[EXTRACTORS.PLOT3D] = function(/*node*/) {
throw new Error('Not Implemented yet');
FigureExtractor.prototype[EXTRACTORS.PLOT3D] = function(node) {
const id = node.getId(),
graphId = node.getParentId(),
execId = this.getExecutionId(node);
let desc;

desc = {
id: id,
execId: execId,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nice to reduce the code duplication between 2D and 3D plots. The only gotcha is that 3D plots don't support images. Maybe they could be refactored to add a method for the shared abstract base class in the metamodel and both could call it...

type: 'plot3D',
graphId: this._client.getNode(graphId).getAttribute('id'),
subgraphId: node.getAttribute('id'),
subgraphName: node.getAttribute('name'),
title: node.getAttribute('title'),
xlim: node.getAttribute('xlim'),
ylim: node.getAttribute('ylim'),
zlim: node.getAttribute('zlim'),
xlabel: node.getAttribute('xlabel'),
ylabel: node.getAttribute('ylabel'),
zlabel: node.getAttribute('zlabel')
};

const children = node.getChildrenIds().map(id => this._client.getNode(id));
desc.lines = children.filter(node => this.getMetaType(node) === EXTRACTORS.LINE)
.map(lineNode => this.extract(lineNode));
desc.scatterPoints = children.filter(node => this.getMetaType(node) === EXTRACTORS.SCATTER_POINTS)
.map(scatterPointsNode => this.extract(scatterPointsNode));
return desc;
};

FigureExtractor.prototype[EXTRACTORS.LINE] = function (node) {
Expand All @@ -97,22 +123,21 @@ define(['./Utils'], function (Utils) {

points = node.getAttribute('points').split(';')
.filter(data => !!data) // remove any ''
.map(pair => {
const [x, y] = pair.split(',').map(num => parseFloat(num));
return {x, y};
});
.map(pair => extractPointsArray(pair));

desc = {
id: id,
execId: execId,
subgraphId: this._client.getNode(node.getParentId()).getAttribute('id'),
lineName: node.getAttribute('name'),
label: node.getAttribute('label'),
lineWidth: node.getAttribute('lineWidth'),
marker: node.getAttribute('marker'),
name: node.getAttribute('name'),
type: 'line',
points: points,
color: node.getAttribute('color')
};

return desc;
};

Expand Down Expand Up @@ -143,10 +168,7 @@ define(['./Utils'], function (Utils) {

points = node.getAttribute('points').split(';')
.filter(data => !!data) // remove any ''
.map(pair => {
const [x, y] = pair.split(',').map(num => parseFloat(num));
return {x, y};
});
.map(pair => extractPointsArray(pair));
desc = {
id: id,
execId: execId,
Expand Down Expand Up @@ -194,5 +216,14 @@ define(['./Utils'], function (Utils) {
return this._metaNodesMap[metaTypeId];
};

const extractPointsArray = function (pair) {
const pointsArr = pair.split(',').map(num => parseFloat(num));
let cartesianPoints = {x: pointsArr[0], y: pointsArr[1]};
brollb marked this conversation as resolved.
Show resolved Hide resolved
if (pointsArr.length === 3) {
cartesianPoints.z = pointsArr[2];
}
return cartesianPoints;
};

return FigureExtractor;
});
10 changes: 3 additions & 7 deletions src/plugins/ExecuteJob/metadata/Figure.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ define([
state.axes.forEach(axes => {
const axesNode = this.core.createNode({
parent: this.node,
base: Figure.is3D(axes) ? this.META.Plot3D : this.META.Plot2D
base: axes.is3D ? this.META.Plot3D : this.META.Plot2D
});
this.setAxesProperties(axesNode, axes);
this.addAxesLines(axesNode, this.node, axes);
if(!Figure.is3D(axes)){
if(!axes.is3D){
this.addAxesImage(axesNode, this.node, axes);
}
this.addAxesScatterPoints(axesNode, this.node, axes);
Expand All @@ -29,7 +29,7 @@ define([
this.core.setAttribute(axesNode, 'ylabel', axes.ylabel);
this.core.setAttribute(axesNode, 'xlim', axes.xlim);
this.core.setAttribute(axesNode, 'ylim', axes.ylim);
if(Figure.is3D(axes)){
if(axes.is3D){
this.core.setAttribute(axesNode, 'zlabel', axes.zlabel);
this.core.setAttribute(axesNode, 'zlim', axes.zlim);
}
Expand Down Expand Up @@ -93,10 +93,6 @@ define([
return 'Graph';
}

static is3D(axes) {
return !!axes.zlabel;
}

}

return Figure;
Expand Down
32 changes: 28 additions & 4 deletions src/plugins/GenerateJob/templates/backend_deepforge.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import six

import numpy as np
import numpy.ma as ma

from matplotlib._pylab_helpers import Gcf
from matplotlib.backend_bases import (
Expand All @@ -80,6 +81,8 @@
from matplotlib import transforms, collections
from matplotlib.collections import LineCollection, PathCollection
from matplotlib.path import Path
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Line3D, Path3DCollection
from matplotlib.pyplot import gcf, close
import simplejson as json

Expand Down Expand Up @@ -378,14 +381,24 @@ def figure_to_state(self):
axes_data['ylabel'] = axes.get_ylabel()
axes_data['xlim'] = axes.get_xlim()
axes_data['ylim'] = axes.get_ylim()
axes_data['is3D'] = False
if hasattr(axes, 'get_zlabel'):
axes_data['zlim'] = axes.get_zlim()
axes_data['zlabel'] = axes.get_zlabel()
axes_data['is3D'] = True

axes_data['lines'] = []
axes_data['images'] = []
axes_data['scatterPoints'] = []

# Line Data
for i, line in enumerate(axes.lines):
lineDict = {}
lineDict['points'] = line.get_xydata().tolist()
if isinstance(line, Line3D):
points = line.get_data_3d()
lineDict['points'] = np.transpose(points).tolist()
else:
lineDict['points'] = line.get_xydata().tolist()
lineDict['label'] = ''
lineDict['color'] = to_hex(line.get_color())
lineDict['marker'] = line.get_marker()
Expand All @@ -395,12 +408,12 @@ def figure_to_state(self):
if line.get_label() != default_label:
lineDict['label'] = line.get_label()
axes_data['lines'].append(lineDict)

if lineDict['marker'] is None or lineDict['marker'] == 'None':
lineDict['marker'] = ''
# Line Collections
for collection in axes.collections:
if isinstance(collection, LineCollection):
axes_data['lines'].extend(self.process_line_collection(collection))

if isinstance(collection, PathCollection):
axes_data['scatterPoints'].append(self.process_collection(axes, collection, force_pathtrans=axes.transAxes))

Expand Down Expand Up @@ -464,14 +477,25 @@ def process_collection(self, ax, collection,
offset_dict = {"data": "before",
"screen": "after"}
offset_order = offset_dict[collection.get_offset_position()]
coll_offsets = offsets
if isinstance(collection, Path3DCollection):
coll_offsets = self.get_3d_array(collection._offsets3d)

return {
'color': self.colors_to_hex(styles['facecolor'].tolist()),
'points': offsets.tolist(),
'points': coll_offsets.tolist(),
'marker': '.', #TODO: Detect markers from Paths
'label': '',
'width': self.convert_size_array(collection.get_sizes())
}

def get_3d_array(self, masked_array_tuple):
values = []
for array in masked_array_tuple:
values.append(ma.getdata(array))
return np.transpose(np.asarray(values))


def convert_size_array(self, size_array):
size = [math.sqrt(s) for s in size_array]
if len(size) == 1:
Expand Down
31 changes: 26 additions & 5 deletions src/visualizers/panels/ExecutionIndex/ExecutionIndexControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,28 +117,49 @@ define([
};

ExecutionIndexControl.prototype._combineSubGraphsDesc = function (consolidatedDesc, subGraphs, abbr) {
let currentSubGraph, imageSubGraphCopy, added=0;
let currentSubGraph, imageSubGraphCopy, added=0, subgraphCopy;
const originalLength = consolidatedDesc.subGraphs.length;
for (let i = 0; i < originalLength; i++) {
if (!subGraphs[i]) break;
currentSubGraph = consolidatedDesc.subGraphs[i+added];
subGraphs[i].abbr = abbr;
if (subGraphs[i].images.length > 0 || currentSubGraph.images.length > 0) {
imageSubGraphCopy = JSON.parse(JSON.stringify(subGraphs[i]));
imageSubGraphCopy.title = getDisplayTitle(subGraphs[i], true);
consolidatedDesc.subGraphs.splice(i+added, 0, imageSubGraphCopy);

if(subGraphs[i].type !== currentSubGraph.type){
subgraphCopy = JSON.parse(JSON.stringify(subGraphs[i]));
subgraphCopy.title = getDisplayTitle(subGraphs[i], true);
consolidatedDesc.subGraphs.splice(i+added, 0, subgraphCopy);
added++;
continue;
}
if(currentSubGraph.images && subGraphs[i].images) {
if (subGraphs[i].images.length > 0 || currentSubGraph.images.length > 0) {
imageSubGraphCopy = JSON.parse(JSON.stringify(subGraphs[i]));
imageSubGraphCopy.title = getDisplayTitle(subGraphs[i], true);
consolidatedDesc.subGraphs.splice(i+added, 0, imageSubGraphCopy);
added++;
continue;
}
}

currentSubGraph.title += ` vs. ${getDisplayTitle(subGraphs[i], true)}`;
if(currentSubGraph.xlabel !== subGraphs[i].xlabel){
currentSubGraph.xlabel += ` ${subGraphs[i].xlabel}`;
}

if(currentSubGraph.ylabel !== subGraphs[i].ylabel){
currentSubGraph.ylabel += ` ${subGraphs[i].ylabel}`;
}

if(currentSubGraph.zlabel && currentSubGraph.zlabel !== subGraphs[i].zlabel){
currentSubGraph.zlabel += ` ${subGraphs[i].zlabel}`;
}

subGraphs[i].lines.forEach((line, index) => {
let lineClone = JSON.parse(JSON.stringify(line));
lineClone.label = (lineClone.label || `line${index}`) + ` (${abbr})`;
currentSubGraph.lines.push(lineClone);
});

subGraphs[i].scatterPoints.forEach(scatterPoint => {
let scatterClone = JSON.parse(JSON.stringify(scatterPoint));
currentSubGraph.scatterPoints.push(scatterClone);
Expand Down
Loading