2020-06-28 18:55:58 +02:00
|
|
|
import { PeakDetectorSimple } from './PeakDetection';
|
|
|
|
import { List } from 'immutable';
|
|
|
|
|
|
|
|
|
|
|
|
export default class DataAnalysis {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this._resetCache(null, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
analyze(analysisParameters, sessionId, allMeasurements) {
|
|
|
|
const cacheValid = (
|
|
|
|
this.sessionId === sessionId &&
|
|
|
|
this.analyzedUpToIdx <= allMeasurements.size &&
|
|
|
|
this.analysisParameters === analysisParameters);
|
|
|
|
|
|
|
|
let newData = null;
|
|
|
|
|
|
|
|
if (cacheValid) {
|
|
|
|
newData = allMeasurements.slice(this.analyzedUpToIdx);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this._resetCache(analysisParameters, sessionId);
|
|
|
|
newData = allMeasurements;
|
2020-06-30 18:06:37 +02:00
|
|
|
console.log("cache reset");
|
2020-06-28 18:55:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// peaks
|
2020-06-30 18:06:37 +02:00
|
|
|
const newPeaks = this.peakDetectorSimple.addVector(newData.toArray());
|
2020-06-28 18:55:58 +02:00
|
|
|
this.allPeaks = this.allPeaks.concat(List(newPeaks));
|
|
|
|
|
|
|
|
// aggregated sum/max
|
|
|
|
this.aggregatedMomentum = newData.reduce((sum, x) => sum + x, this.aggregatedMomentum);
|
|
|
|
this.peakMax = newData.reduce((running, x) => Math.max(x, running), this.peakMax);
|
|
|
|
|
|
|
|
// windowed
|
|
|
|
const windowNumDataPoints = analysisParameters.windowSizeInSecs * analysisParameters.numMeasurementsPerSec;
|
|
|
|
const windowed = allMeasurements.slice(-windowNumDataPoints);
|
|
|
|
const peakMaxWindow = windowed.reduce((running, x) => Math.max(x, running), 0);
|
|
|
|
const momentumWindow = windowed.reduce((sum, x) => sum + x, 0);
|
|
|
|
|
|
|
|
this.analyzedUpToIdx = allMeasurements.size;
|
|
|
|
return {
|
|
|
|
peaks: this.allPeaks,
|
|
|
|
totalTime: allMeasurements / analysisParameters.numMeasurementsPerSec,
|
|
|
|
|
2020-06-30 18:06:37 +02:00
|
|
|
totalMomentum: this.aggregatedMomentum,
|
2020-06-28 18:55:58 +02:00
|
|
|
peakMax: this.peakMax,
|
|
|
|
|
|
|
|
momentumWindow: momentumWindow,
|
|
|
|
peakMaxWindow: peakMaxWindow,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_resetCache(analysisParameters, sessionId) {
|
|
|
|
this.peakDetectorSimple = analysisParameters ? new PeakDetectorSimple(analysisParameters.peakDetectorSimpleThreshold) : null;
|
|
|
|
this.allPeaks = List();
|
|
|
|
|
|
|
|
this.aggregatedMomentum = 0;
|
|
|
|
this.peakMax = 0;
|
|
|
|
|
|
|
|
this.sessionId = sessionId;
|
|
|
|
this.analyzedUpToIdx = 0;
|
|
|
|
this.analysisParameters = analysisParameters;
|
|
|
|
}
|
|
|
|
};
|