changeset 297:73beb0e970c5

Modify FeatureReducers so that it compiles.
author Chris Cannam <cannam@all-day-breakfast.com>
date Wed, 10 May 2017 14:47:26 +0100
parents cda9307d9eb7
children bc67503ea8c6 fff3a4fba992
files src/app/services/feature-extraction/FeatureReducers.ts
diffstat 1 files changed, 74 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- 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 = <T>(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<Feature>(
-      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<Float32Array>(
+	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<Feature>(
+	collected,
+	incoming
       );
+      break;
+      
+    case "tracks":
+      incoming = val.features.collected as TrackFeatures;
+      collected = acc.features.collected as TrackFeatures;
+      acc.features.collected = arrayReducer<TrackFeature>(
+	collected, incoming
+      );
+      break;
+
+    default:
+      throw new Error('Invalid feature output. Aborting');
     }
-  } else {
-    throw new Error('Invalid feature output. Aborting');
-  }
-  return acc;
-};
+    
+    return acc;
+  };