import {Transform, ingest} from 'vega-dataflow'; import {accessorName, error, inherits} from 'vega-util'; import {max, mean, median, min} from 'd3-array'; var Methods = { value: 'value', median: median, mean: mean, min: min, max: max }; var Empty = []; /** * Impute missing values. * @constructor * @param {object} params - The parameters for this operator. * @param {function(object): *} params.field - The value field to impute. * @param {Array} [params.groupby] - An array of * accessors to determine series within which to perform imputation. * @param {function(object): *} params.key - An accessor for a key value. * Each key value should be unique within a group. New tuples will be * imputed for any key values that are not found within a group. * @param {Array<*>} [params.keyvals] - Optional array of required key * values. New tuples will be imputed for any key values that are not * found within a group. In addition, these values will be automatically * augmented with the key values observed in the input data. * @param {string} [method='value'] - The imputation method to use. One of * 'value', 'mean', 'median', 'max', 'min'. * @param {*} [value=0] - The constant value to use for imputation * when using method 'value'. */ export default function Impute(params) { Transform.call(this, [], params); } Impute.Definition = { 'type': 'Impute', 'metadata': {'changes': true}, 'params': [ { 'name': 'field', 'type': 'field', 'required': true }, { 'name': 'key', 'type': 'field', 'required': true }, { 'name': 'keyvals', 'array': true }, { 'name': 'groupby', 'type': 'field', 'array': true }, { 'name': 'method', 'type': 'enum', 'default': 'value', 'values': ['value', 'mean', 'median', 'max', 'min'] }, { 'name': 'value', 'default': 0 } ] }; var prototype = inherits(Impute, Transform); function getValue(_) { var m = _.method || Methods.value, v; if (Methods[m] == null) { error('Unrecognized imputation method: ' + m); } else if (m === Methods.value) { v = _.value !== undefined ? _.value : 0; return function() { return v; }; } else { return Methods[m]; } } function getField(_) { var f = _.field; return function(t) { return t ? f(t) : NaN; }; } prototype.transform = function(_, pulse) { var out = pulse.fork(pulse.ALL), impute = getValue(_), field = getField(_), fName = accessorName(_.field), kName = accessorName(_.key), gNames = (_.groupby || []).map(accessorName), groups = partition(pulse.source, _.groupby, _.key, _.keyvals), curr = [], prev = this.value, m = groups.domain.length, group, value, gVals, kVal, g, i, j, l, n, t; for (g=0, l=groups.length; g