# HG changeset patch # User Chris Cannam # Date 1494424046 -3600 # Node ID 73beb0e970c57f6cf3c5ab0be9476ffdbb8527bf # Parent cda9307d9eb7dc1ee15c4ae75e9e223a9954a064 Modify FeatureReducers so that it compiles. diff -r cda9307d9eb7 -r 73beb0e970c5 src/app/services/feature-extraction/FeatureReducers.ts --- a/src/app/services/feature-extraction/FeatureReducers.ts Wed May 10 13:41:18 2017 +0100 +++ b/src/app/services/feature-extraction/FeatureReducers.ts Wed May 10 14:47:26 2017 +0100 @@ -2,8 +2,11 @@ * Created by lucast on 26/04/2017. */ import {StreamingResponse} from 'piper/StreamingService'; -import {Feature} from 'piper/Feature'; +import {Feature, FeatureList} from 'piper/Feature'; import {SampleType} from 'piper'; +import { + VectorFeatures, MatrixFeatures, TrackFeature, TrackFeatures +} from "piper/HigherLevelUtilities"; export const arrayReducer = (acc: T[], val: T[]): T[] => { acc.push.apply(acc, val); @@ -24,40 +27,79 @@ export const streamingResponseReducer = (acc: StreamingResponse, val: StreamingResponse, - i: number): StreamingResponse => { - acc.processedBlockCount = val.processedBlockCount; - if (acc.features.data instanceof Array && - val.features.data instanceof Array) { - acc.features.data = arrayReducer( - acc.features.data, - val.features.data - ); - } else if (acc.features.data instanceof Float32Array && - val.features.data instanceof Float32Array) { + i: number): StreamingResponse => + { + if (acc.features.shape !== val.features.shape) { + throw new Error(`Unexpected feature shape ${val.features.shape} (expected ${acc.features.shape})`); + } + + acc.processedBlockCount = val.processedBlockCount; + const isOneSamplePerStep = acc.outputDescriptor.configured.sampleType === SampleType.OneSamplePerStep; - if (isOneSamplePerStep) { - // for one sample per step vectors we know there will be totalBlockCount - // number of samples - so pre-allocate the Float32Array when we know - // the totalBlockCount (after receiving the first feature) - if ( i === 1 ) { - const newBlock = new Float32Array(acc.totalBlockCount); - newBlock[0] = acc.features.data[0]; - acc.features.data = newBlock; + + let incoming, collected; + + console.log("i = " + i + ", shape = " + acc.features.shape + ", count = " + acc.processedBlockCount); + + switch (acc.features.shape) { + + case "vector": + incoming = val.features.collected as VectorFeatures; + collected = acc.features.collected as VectorFeatures; + if (isOneSamplePerStep) { + // for one sample per step vectors we know there will be + // totalBlockCount number of samples - so pre-allocate the + // Float32Array when we know the totalBlockCount (after + // receiving the first feature) + if (i === 1) { + const newBlock = new Float32Array(acc.totalBlockCount); + newBlock[0] = collected.data[0]; + collected.data = newBlock; + } + collected.data = inPlaceTypedArrayReducer( + collected.data, + incoming.data, + i + ); + } else { + // if not OneSamplePerStep we have to make a new array each time + collected.data = typedArrayReducer( + collected.data, + incoming.data + ); } - acc.features.data = inPlaceTypedArrayReducer( - acc.features.data, - val.features.data, - i + break; + + case "matrix": + incoming = val.features.collected as MatrixFeatures; + collected = acc.features.collected as MatrixFeatures; + collected.data = arrayReducer( + collected.data, + incoming.data ); - } else { // if not OneSamplePerStep we have to make a new array each time - acc.features.data = typedArrayReducer( - acc.features.data, - val.features.data + break; + + case "list": + incoming = val.features.collected as FeatureList; + collected = acc.features.collected as FeatureList; + acc.features.collected = arrayReducer( + collected, + incoming ); + break; + + case "tracks": + incoming = val.features.collected as TrackFeatures; + collected = acc.features.collected as TrackFeatures; + acc.features.collected = arrayReducer( + collected, incoming + ); + break; + + default: + throw new Error('Invalid feature output. Aborting'); } - } else { - throw new Error('Invalid feature output. Aborting'); - } - return acc; -}; + + return acc; + };