Analyzer = {
function Analyzer() {
this.init();
return this;
}
Analyzer.prototype.init = function init() {
this.x = new Float64Array( N );
this.y = new Float64Array( N );
this.cx = new Float64Array( nStates );
this.cy = new Float64Array( nStates );
this.t = new Uint32Array( N );
this.i = -1;
this.v = vector( [ 0.0, 0.0 ] );
this.acc = incrkmeans( nStates, ndims, {
'metric': 'euclidean',
'init': [ 'kmeans++' ],
'seed': 1234 // provide seed in order to ensure reproducible k-means++ initialization
});
// Initialize a plot constructor for visualizing simulated timeseries:
this.timeseries = plot({
'title': 'Simulated Timeseries',
'xMin': 0,
'xMax': 250,
'yMin': -0.1,
'yMax': 1.1,
'width': 800,
'height': 600
});
// Initialize a plot constructor for a two-dimensional scatter plot:
this.scatter = plot({
'title': 'Cluster Analysis',
'xMin': -0.1,
'xMax': 1.1,
'yMin': -0.1,
'yMax': 1.1,
'lineStyle': 'none',
'symbols': 'closed-circle',
'symbolsOpacity': [ 0.15, 1.0 ],
'width': 600,
'height': 600
});
};
/**
* Updates an analyzer.
*
* @param {ArrayLikeObject} datum - datum
* @returns {Analyzer} Analyzer instance
*/
Analyzer.prototype.update = function update( datum ) {
var res;
var i;
// Update the "time" counter:
this.i += 1;
// Update the "time" vector:
this.t[ this.i ] = this.i;
// Cache the provided data:
this.x[ this.i ] = datum[ 0 ];
this.y[ this.i ] = datum[ 1 ];
// Update the accumulator:
this.v.set( 0, datum[ 0 ] );
this.v.set( 1, datum[ 1 ] );
res = this.acc( this.v );
// If results are returned, update the centroid positions...
if ( res ) {
for ( i = 0; i < nStates; i++ ) {
this.cx[ i ] = res.centroids.get( i, 0 );
this.cy[ i ] = res.centroids.get( i, 1 );
}
}
return this;
};
/**
* Renders analysis plots.
*
* @returns {Array<string>} plots
*/
Analyzer.prototype.render = function render() {
var t;
var x;
var y;
// Create typed array views:
t = new Uint32Array( this.t.buffer, 0, this.i+1 );
x = new Float64Array( this.x.buffer, 0, this.i+1 );
y = new Float64Array( this.y.buffer, 0, this.i+1 );
// Update the timeseries plot:
this.timeseries.x = [ t, t ];
this.timeseries.y = [ x, y ];
if ( this.i > 250 ) {
this.timeseries.xMin = t[ this.i-250 ];
this.timeseries.xMax = t[ this.i ];
}
// Update the scatter plot:
this.scatter.x = [ x, this.cx ];
this.scatter.y = [ y, this.cy ];
// Render the plots:
return [ this.timeseries.render( 'html' ), this.scatter.render( 'html' ) ];
}
return Analyzer;
}