Skip to content

Transform inverse mapping #2126

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

Merged
merged 8 commits into from
Oct 27, 2017
Merged
Show file tree
Hide file tree
Changes from 6 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
20 changes: 17 additions & 3 deletions src/transforms/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,34 @@ exports.calcTransform = function(gd, trace, opts) {
var groupArray = Lib.getTargetArray(trace, {target: groups});
if(!groupArray) return;

var i, vi, groupIndex;
var i, vi, groupIndex, newGrouping;

var groupIndices = {};
var indexToPoints = {};
var groupings = [];

var prevTransforms = trace.transforms.filter(function(tr) {return tr.indexToPoints;});
var originalPointsAccessor = prevTransforms.length ?
function(i) {return prevTransforms[prevTransforms.length - 1].indexToPoints[i];} :
function(i) {return [i];};

for(i = 0; i < groupArray.length; i++) {
vi = groupArray[i];
groupIndex = groupIndices[vi];
if(groupIndex === undefined) {
groupIndices[vi] = groupings.length;
groupings.push([i]);
newGrouping = [i];
groupings.push(newGrouping);
indexToPoints[groupIndices[vi]] = originalPointsAccessor(i);
}
else {
groupings[groupIndex].push(i);
indexToPoints[groupIndices[vi]] = indexToPoints[groupIndices[vi]].concat(originalPointsAccessor(i));
}
else groupings[groupIndex].push(i);
}

opts.indexToPoints = indexToPoints;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this isn't a real attribute but we're putting it back into fullData lets give it an _ - ie opts._indexToPoints.


var aggregations = opts.aggregations;

for(i = 0; i < aggregations.length; i++) {
Expand Down
14 changes: 13 additions & 1 deletion src/transforms/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ exports.calcTransform = function(gd, trace, opts) {
var d2c = Axes.getDataToCoordFunc(gd, trace, target, targetArray);
var filterFunc = getFilterFunc(opts, d2c, targetCalendar);
var originalArrays = {};
var indexToPoints = {};
var index = 0;

function forAllAttrs(fn, index) {
for(var j = 0; j < arrayAttrs.length; j++) {
Expand Down Expand Up @@ -203,11 +205,21 @@ exports.calcTransform = function(gd, trace, opts) {
// copy all original array attribute values, and clear arrays in trace
forAllAttrs(initFn);

var prevTransforms = trace.transforms.filter(function(tr) {return tr.indexToPoints;});
var originalPointsAccessor = prevTransforms.length ?
function(i) {return prevTransforms[prevTransforms.length - 1].indexToPoints[i];} :
function(i) {return [i];};

// loop through filter array, fill trace arrays if passed
for(var i = 0; i < len; i++) {
var passed = filterFunc(targetArray[i]);
if(passed) forAllAttrs(fillFn, i);
if(passed) {
forAllAttrs(fillFn, i);
indexToPoints[index++] = originalPointsAccessor(i);
}
}

opts.indexToPoints = indexToPoints;
};

function getFilterFunc(opts, d2c, targetCalendar) {
Expand Down
20 changes: 17 additions & 3 deletions test/jasmine/tests/transform_aggregate_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ describe('aggregate', function() {
expect(traceOut.marker.opacity).toEqual([0.6, 'boo']);
expect(traceOut.marker.line.color).toEqual(['the end', 3.3]);
expect(traceOut.marker.line.width).toEqual([4, 1]);

var transform = traceOut.transforms[0];
var inverseMapping = transform.indexToPoints;
expect(inverseMapping).toEqual({0: [0, 2, 3, 4], 1: [1]});
});

it('handles all funcs except sum for date data', function() {
Expand Down Expand Up @@ -163,6 +167,10 @@ describe('aggregate', function() {
expect(traceOut.y).toEqual(['b', undefined]);
// category average: can result in fractional categories -> rounds (0.5 rounds to 1)
expect(traceOut.text).toEqual(['b', 'b']);

var transform = traceOut.transforms[0];
var inverseMapping = transform.indexToPoints;
expect(inverseMapping).toEqual({0: [0, 1], 1: [2, 3]});
});

it('can aggregate on an existing data array', function() {
Expand All @@ -185,10 +193,12 @@ describe('aggregate', function() {
expect(traceOut.x).toEqual([8, 7]);
expect(traceOut.y).toBeCloseToArray([16 / 3, 7], 5);
expect(traceOut.marker.size).toEqual([10, 20]);

var transform = traceOut.transforms[0];
var inverseMapping = transform.indexToPoints;
expect(inverseMapping).toEqual({0: [0, 1, 4], 1: [2, 3]});
});

// Regression test - throws before fix:
// https://github.com/plotly/plotly.js/issues/2024
it('can handle case where aggregation array is missing', function() {
Plotly.newPlot(gd, [{
x: [1, 2, 3, 4, 5],
Expand All @@ -205,6 +215,10 @@ describe('aggregate', function() {
expect(traceOut.x).toEqual([1, 3]);
expect(traceOut.y).toEqual([2, 6]);
expect(traceOut.marker.size).toEqual([10, 20]);

var transform = traceOut.transforms[0];
var inverseMapping = transform.indexToPoints;
expect(inverseMapping).toEqual({0: [0, 1, 4], 1: [2, 3]});
});

it('handles median, mode, rms, & stddev for numeric data', function() {
Expand Down Expand Up @@ -257,7 +271,7 @@ describe('aggregate', function() {
aggregations: [
{target: 'x', func: 'sum'},
{target: 'x', func: 'avg'},
{target: 'y', func: 'avg'},
{target: 'y', func: 'avg'}
]
}]
}]);
Expand Down
10 changes: 8 additions & 2 deletions test/jasmine/tests/transform_filter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('filter transforms defaults:', function() {
traceIn = {
x: [1, 2, 3],
transforms: [{
type: 'filter',
type: 'filter'
}, {
type: 'filter',
target: 0
Expand Down Expand Up @@ -143,6 +143,7 @@ describe('filter transforms calc:', function() {
expect(out[0].x).toEqual([0, 1]);
expect(out[0].y).toEqual([1, 2]);
expect(out[0].z).toEqual(['2016-10-21', '2016-12-02']);
expect(out[0].transforms[0].indexToPoints).toEqual({0: [3], 1: [4]});
});

it('should use the calendar from the target attribute if target is a string', function() {
Expand Down Expand Up @@ -261,13 +262,14 @@ describe('filter transforms calc:', function() {
expect(out[0].x).toEqual([-2, 2, 3]);
expect(out[0].y).toEqual([3, 3, 1]);
expect(out[0].marker.color).toEqual([0.3, 0.3, 0.4]);
expect(out[0].transforms[0].indexToPoints).toEqual({0: [2], 1: [5], 2: [6]});
});

it('filters should handle array on base trace attributes', function() {
var out = _transform([Lib.extendDeep({}, base, {
hoverinfo: ['x', 'y', 'text', 'name', 'none', 'skip', 'all'],
hoverlabel: {
bgcolor: ['red', 'green', 'blue', 'black', 'yellow', 'cyan', 'pink'],
bgcolor: ['red', 'green', 'blue', 'black', 'yellow', 'cyan', 'pink']
},
transforms: [{
type: 'filter',
Expand Down Expand Up @@ -314,6 +316,8 @@ describe('filter transforms calc:', function() {

expect(out[0].x).toEqual([1, 2]);
expect(out[0].y).toEqual([2, 3]);
expect(out[0].transforms[0].indexToPoints).toEqual({0: [4], 1: [5], 2: [6]});
expect(out[0].transforms[1].indexToPoints).toEqual({0: [4], 1: [5]});
});

it('filters should chain as AND (case 2)', function() {
Expand All @@ -339,6 +343,8 @@ describe('filter transforms calc:', function() {

expect(out[0].x).toEqual([3]);
expect(out[0].y).toEqual([1]);
expect(out[0].transforms[0].indexToPoints).toEqual({0: [4], 1: [5], 2: [6]});
expect(out[0].transforms[2].indexToPoints).toEqual({0: [6]});
});

it('should preserve gaps in data when `preservegaps` is turned on', function() {
Expand Down
54 changes: 53 additions & 1 deletion test/jasmine/tests/transform_multi_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ describe('multiple transforms:', function() {
groups: ['a', 'a', 'b', 'a', 'b', 'b', 'a'],
styles: [{
target: 'a',
value: {marker: {color: 'red'}},
value: {marker: {color: 'red'}}
}, {
target: 'b',
value: {marker: {color: 'blue'}}
Expand Down Expand Up @@ -277,8 +277,60 @@ describe('multiple transforms:', function() {
}]
}];

var mockData2 = [{
x: [1, 2, 3, 4, 5],
y: [2, 3, 1, 7, 9],
marker: {size: [10, 20, 20, 20, 10]},
transforms: [
{
type: 'filter',
operation: '>',
value: 2,
target: 'y'
},
{
type: 'aggregate',
groups: 'marker.size',
aggregations: [
{target: 'x', func: 'sum'}, // 20: 6, 10: 5
{target: 'y', func: 'avg'} // 20: 5, 10: 9
]
},
{
type: 'filter',
operation: '<',
value: 6,
target: 'x'
}
]
}];

afterEach(destroyGraphDiv);

it('Plotly.plot should plot the transform traces - filter|aggregate|filter', function(done) {
var data = Lib.extendDeep([], mockData2);

Plotly.plot(gd, data).then(function() {
expect(gd.data.length).toEqual(1);

// this would be the result if we didn't have a second filter - kept for test case overview
// expect(gd._fullData[0].x).toEqual([6, 5]);
// expect(gd._fullData[0].y).toEqual([5, 9]);
// expect(gd._fullData[0].marker.size).toEqual([20, 10]);

expect(gd._fullData[0].x).toEqual([5]);
expect(gd._fullData[0].y).toEqual([9]);
expect(gd._fullData[0].marker.size).toEqual([10]);

expect(gd._fullData[0].transforms[0].indexToPoints).toEqual({0: [1], 1: [3], 2: [4]});
expect(gd._fullData[0].transforms[1].indexToPoints).toEqual({0: [1, 3], 1: [4]});
expect(gd._fullData[0].transforms[2].indexToPoints).toEqual({0: [4]});

done();
});
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for testing this! Nice to see indexToPoints cascade through the transforms.



it('Plotly.plot should plot the transform traces', function(done) {
var data = Lib.extendDeep([], mockData0);

Expand Down