fix sumSeries() function, closes #286

This commit is contained in:
Alexander Zobnin
2017-07-26 16:21:40 +03:00
parent ac4a7434ce
commit 122a97e482
7 changed files with 201 additions and 37 deletions

View File

@@ -0,0 +1,34 @@
// import _ from 'lodash';
import ts from '../timeseries';
describe('timeseries processing functions', () => {
describe('sumSeries()', () => {
it('should properly sum series', (done) => {
let series = [
[[0, 1], [1, 2], [1, 3]],
[[2, 1], [3, 2], [4, 3]]
];
let expected = [[2, 1], [4, 2], [5, 3]];
let result = ts.sumSeries(series);
expect(result).to.eql(expected);
done();
});
it('should properly sum series with nulls', (done) => {
// issue #286
let series = [
[[1, 1], [1, 2], [1, 3]],
[[3, 2], [4, 3]]
];
let expected = [[1, 1], [4, 2], [5, 3]];
let result = ts.sumSeries(series);
expect(result).to.eql(expected);
done();
});
});
});

View File

@@ -129,6 +129,7 @@ System.register(['lodash', './utils'], function (_export, _context) {
new_timestamps = _.sortBy(new_timestamps); new_timestamps = _.sortBy(new_timestamps);
var interpolated_timeseries = _.map(timeseries, function (series) { var interpolated_timeseries = _.map(timeseries, function (series) {
series = fillZeroes(series, new_timestamps);
var timestamps = _.map(series, function (point) { var timestamps = _.map(series, function (point) {
return point[1]; return point[1];
}); });
@@ -367,6 +368,30 @@ System.register(['lodash', './utils'], function (_export, _context) {
}); });
} }
/**
* Fill empty front and end of series by zeroes.
*
* | *** | | *** |
* |___ ___| -> |*** ***|
* @param {*} series
* @param {*} timestamps
*/
function fillZeroes(series, timestamps) {
var prepend = [];
var append = [];
var new_point = void 0;
for (var i = 0; i < timestamps.length; i++) {
if (timestamps[i] < series[0][POINT_TIMESTAMP]) {
new_point = [0, timestamps[i]];
prepend.push(new_point);
} else if (timestamps[i] > series[series.length - 1][POINT_TIMESTAMP]) {
new_point = [0, timestamps[i]];
append.push(new_point);
}
}
return _.concat(_.concat(prepend, series), append);
}
/** /**
* Interpolate series with gaps * Interpolate series with gaps
*/ */
@@ -376,8 +401,8 @@ System.register(['lodash', './utils'], function (_export, _context) {
// Interpolate series // Interpolate series
for (var i = series.length - 1; i >= 0; i--) { for (var i = series.length - 1; i >= 0; i--) {
if (!series[i][0]) { if (!series[i][0]) {
left = findNearestLeft(series, series[i]); left = findNearestLeft(series, i);
right = findNearestRight(series, series[i]); right = findNearestRight(series, i);
if (!left) { if (!left) {
left = right; left = right;
} }
@@ -398,26 +423,22 @@ System.register(['lodash', './utils'], function (_export, _context) {
} }
} }
function findNearestRight(series, point) { function findNearestRight(series, pointIndex) {
var point_index = _.indexOf(series, point); for (var i = pointIndex; i < series.length; i++) {
var nearestRight;
for (var i = point_index; i < series.length; i++) {
if (series[i][0] !== null) { if (series[i][0] !== null) {
return series[i]; return series[i];
} }
} }
return nearestRight; return null;
} }
function findNearestLeft(series, point) { function findNearestLeft(series, pointIndex) {
var point_index = _.indexOf(series, point); for (var i = pointIndex; i > 0; i--) {
var nearestLeft;
for (var i = point_index; i > 0; i--) {
if (series[i][0] !== null) { if (series[i][0] !== null) {
return series[i]; return series[i];
} }
} }
return nearestLeft; return null;
} }
//////////// ////////////

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,33 @@
'use strict';
var _timeseries = require('../timeseries');
var _timeseries2 = _interopRequireDefault(_timeseries);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
describe('timeseries processing functions', function () {
describe('sumSeries()', function () {
it('should properly sum series', function (done) {
var series = [[[0, 1], [1, 2], [1, 3]], [[2, 1], [3, 2], [4, 3]]];
var expected = [[2, 1], [4, 2], [5, 3]];
var result = _timeseries2.default.sumSeries(series);
expect(result).to.eql(expected);
done();
});
it('should properly sum series with nulls', function (done) {
// issue #286
var series = [[[1, 1], [1, 2], [1, 3]], [[3, 2], [4, 3]]];
var expected = [[1, 1], [4, 2], [5, 3]];
var result = _timeseries2.default.sumSeries(series);
expect(result).to.eql(expected);
done();
});
});
}); // import _ from 'lodash';

View File

@@ -154,6 +154,7 @@ function sumSeries(timeseries) {
new_timestamps = _lodash2.default.sortBy(new_timestamps); new_timestamps = _lodash2.default.sortBy(new_timestamps);
var interpolated_timeseries = _lodash2.default.map(timeseries, function (series) { var interpolated_timeseries = _lodash2.default.map(timeseries, function (series) {
series = fillZeroes(series, new_timestamps);
var timestamps = _lodash2.default.map(series, function (point) { var timestamps = _lodash2.default.map(series, function (point) {
return point[1]; return point[1];
}); });
@@ -392,6 +393,30 @@ function sortByTime(series) {
}); });
} }
/**
* Fill empty front and end of series by zeroes.
*
* | *** | | *** |
* |___ ___| -> |*** ***|
* @param {*} series
* @param {*} timestamps
*/
function fillZeroes(series, timestamps) {
var prepend = [];
var append = [];
var new_point = void 0;
for (var i = 0; i < timestamps.length; i++) {
if (timestamps[i] < series[0][POINT_TIMESTAMP]) {
new_point = [0, timestamps[i]];
prepend.push(new_point);
} else if (timestamps[i] > series[series.length - 1][POINT_TIMESTAMP]) {
new_point = [0, timestamps[i]];
append.push(new_point);
}
}
return _lodash2.default.concat(_lodash2.default.concat(prepend, series), append);
}
/** /**
* Interpolate series with gaps * Interpolate series with gaps
*/ */
@@ -401,8 +426,8 @@ function interpolateSeries(series) {
// Interpolate series // Interpolate series
for (var i = series.length - 1; i >= 0; i--) { for (var i = series.length - 1; i >= 0; i--) {
if (!series[i][0]) { if (!series[i][0]) {
left = findNearestLeft(series, series[i]); left = findNearestLeft(series, i);
right = findNearestRight(series, series[i]); right = findNearestRight(series, i);
if (!left) { if (!left) {
left = right; left = right;
} }
@@ -423,26 +448,22 @@ function linearInterpolation(timestamp, left, right) {
} }
} }
function findNearestRight(series, point) { function findNearestRight(series, pointIndex) {
var point_index = _lodash2.default.indexOf(series, point); for (var i = pointIndex; i < series.length; i++) {
var nearestRight;
for (var i = point_index; i < series.length; i++) {
if (series[i][0] !== null) { if (series[i][0] !== null) {
return series[i]; return series[i];
} }
} }
return nearestRight; return null;
} }
function findNearestLeft(series, point) { function findNearestLeft(series, pointIndex) {
var point_index = _lodash2.default.indexOf(series, point); for (var i = pointIndex; i > 0; i--) {
var nearestLeft;
for (var i = point_index; i > 0; i--) {
if (series[i][0] !== null) { if (series[i][0] !== null) {
return series[i]; return series[i];
} }
} }
return nearestLeft; return null;
} }
//////////// ////////////

View File

@@ -0,0 +1,34 @@
// import _ from 'lodash';
import ts from '../timeseries';
describe('timeseries processing functions', () => {
describe('sumSeries()', () => {
it('should properly sum series', (done) => {
let series = [
[[0, 1], [1, 2], [1, 3]],
[[2, 1], [3, 2], [4, 3]]
];
let expected = [[2, 1], [4, 2], [5, 3]];
let result = ts.sumSeries(series);
expect(result).to.eql(expected);
done();
});
it('should properly sum series with nulls', (done) => {
// issue #286
let series = [
[[1, 1], [1, 2], [1, 3]],
[[3, 2], [4, 3]]
];
let expected = [[1, 1], [4, 2], [5, 3]];
let result = ts.sumSeries(series);
expect(result).to.eql(expected);
done();
});
});
});

View File

@@ -141,6 +141,7 @@ function sumSeries(timeseries) {
new_timestamps = _.sortBy(new_timestamps); new_timestamps = _.sortBy(new_timestamps);
var interpolated_timeseries = _.map(timeseries, function (series) { var interpolated_timeseries = _.map(timeseries, function (series) {
series = fillZeroes(series, new_timestamps);
var timestamps = _.map(series, function (point) { var timestamps = _.map(series, function (point) {
return point[1]; return point[1];
}); });
@@ -384,6 +385,30 @@ function sortByTime(series) {
}); });
} }
/**
* Fill empty front and end of series by zeroes.
*
* | *** | | *** |
* |___ ___| -> |*** ***|
* @param {*} series
* @param {*} timestamps
*/
function fillZeroes(series, timestamps) {
let prepend = [];
let append = [];
let new_point;
for (let i = 0; i < timestamps.length; i++) {
if (timestamps[i] < series[0][POINT_TIMESTAMP]) {
new_point = [0, timestamps[i]];
prepend.push(new_point);
} else if (timestamps[i] > series[series.length - 1][POINT_TIMESTAMP]) {
new_point = [0, timestamps[i]];
append.push(new_point);
}
}
return _.concat(_.concat(prepend, series), append);
}
/** /**
* Interpolate series with gaps * Interpolate series with gaps
*/ */
@@ -393,8 +418,8 @@ function interpolateSeries(series) {
// Interpolate series // Interpolate series
for (var i = series.length - 1; i >= 0; i--) { for (var i = series.length - 1; i >= 0; i--) {
if (!series[i][0]) { if (!series[i][0]) {
left = findNearestLeft(series, series[i]); left = findNearestLeft(series, i);
right = findNearestRight(series, series[i]); right = findNearestRight(series, i);
if (!left) { if (!left) {
left = right; left = right;
} }
@@ -415,26 +440,22 @@ function linearInterpolation(timestamp, left, right) {
} }
} }
function findNearestRight(series, point) { function findNearestRight(series, pointIndex) {
var point_index = _.indexOf(series, point); for (var i = pointIndex; i < series.length; i++) {
var nearestRight;
for (var i = point_index; i < series.length; i++) {
if (series[i][0] !== null) { if (series[i][0] !== null) {
return series[i]; return series[i];
} }
} }
return nearestRight; return null;
} }
function findNearestLeft(series, point) { function findNearestLeft(series, pointIndex) {
var point_index = _.indexOf(series, point); for (var i = pointIndex; i > 0; i--) {
var nearestLeft;
for (var i = point_index; i > 0; i--) {
if (series[i][0] !== null) { if (series[i][0] !== null) {
return series[i]; return series[i];
} }
} }
return nearestLeft; return null;
} }
//////////// ////////////