Mercurial > hg > ugly-duckling
comparison src/app/services/feature-extraction/FeatureReducers.ts @ 302:c6dd5752f7f7
Change to new StreamingResponse type.
author | Lucas Thompson <dev@lucas.im> |
---|---|
date | Fri, 12 May 2017 01:40:21 +0100 |
parents | 73beb0e970c5 |
children | dc415a620b15 |
comparison
equal
deleted
inserted
replaced
301:fff3a4fba992 | 302:c6dd5752f7f7 |
---|---|
1 /** | 1 /** |
2 * Created by lucast on 26/04/2017. | 2 * Created by lucast on 26/04/2017. |
3 */ | 3 */ |
4 import {StreamingResponse} from 'piper/StreamingService'; | 4 import {StreamingResponse} from "piper/StreamingService"; |
5 import {Feature, FeatureList} from 'piper/Feature'; | |
6 import {SampleType} from 'piper'; | |
7 import { | |
8 VectorFeatures, MatrixFeatures, TrackFeature, TrackFeatures | |
9 } from "piper/HigherLevelUtilities"; | |
10 | 5 |
11 export const arrayReducer = <T>(acc: T[], val: T[]): T[] => { | 6 export const arrayReducer = <T>(acc: T[], val: T[]): T[] => { |
12 acc.push.apply(acc, val); | 7 for (let i = 0, len = val.length; i < len; ++i) { |
8 acc.push(val[i]); | |
9 } | |
13 return acc; | 10 return acc; |
14 }; | 11 }; |
15 | 12 |
16 export const typedArrayReducer = (acc: Float32Array, | 13 export const typedArrayReducer = (acc: Float32Array, |
17 val: Float32Array): Float32Array => { | 14 val: Float32Array): Float32Array => { |
24 acc.set(val, i); | 21 acc.set(val, i); |
25 return acc; | 22 return acc; |
26 }; | 23 }; |
27 | 24 |
28 export const streamingResponseReducer = (acc: StreamingResponse, | 25 export const streamingResponseReducer = (acc: StreamingResponse, |
29 val: StreamingResponse, | 26 val: StreamingResponse): |
30 i: number): StreamingResponse => | 27 StreamingResponse => { |
31 { | 28 acc.progress = val.progress; |
32 if (acc.features.shape !== val.features.shape) { | 29 if (val.configuration) { |
33 throw new Error(`Unexpected feature shape ${val.features.shape} (expected ${acc.features.shape})`); | 30 acc.configuration = val.configuration; |
34 } | 31 } |
35 | 32 arrayReducer(acc.features, val.features); |
36 acc.processedBlockCount = val.processedBlockCount; | 33 return acc; |
37 | 34 }; |
38 const isOneSamplePerStep = acc.outputDescriptor.configured.sampleType === | |
39 SampleType.OneSamplePerStep; | |
40 | |
41 let incoming, collected; | |
42 | |
43 console.log("i = " + i + ", shape = " + acc.features.shape + ", count = " + acc.processedBlockCount); | |
44 | |
45 switch (acc.features.shape) { | |
46 | |
47 case "vector": | |
48 incoming = val.features.collected as VectorFeatures; | |
49 collected = acc.features.collected as VectorFeatures; | |
50 if (isOneSamplePerStep) { | |
51 // for one sample per step vectors we know there will be | |
52 // totalBlockCount number of samples - so pre-allocate the | |
53 // Float32Array when we know the totalBlockCount (after | |
54 // receiving the first feature) | |
55 if (i === 1) { | |
56 const newBlock = new Float32Array(acc.totalBlockCount); | |
57 newBlock[0] = collected.data[0]; | |
58 collected.data = newBlock; | |
59 } | |
60 collected.data = inPlaceTypedArrayReducer( | |
61 collected.data, | |
62 incoming.data, | |
63 i | |
64 ); | |
65 } else { | |
66 // if not OneSamplePerStep we have to make a new array each time | |
67 collected.data = typedArrayReducer( | |
68 collected.data, | |
69 incoming.data | |
70 ); | |
71 } | |
72 break; | |
73 | |
74 case "matrix": | |
75 incoming = val.features.collected as MatrixFeatures; | |
76 collected = acc.features.collected as MatrixFeatures; | |
77 collected.data = arrayReducer<Float32Array>( | |
78 collected.data, | |
79 incoming.data | |
80 ); | |
81 break; | |
82 | |
83 case "list": | |
84 incoming = val.features.collected as FeatureList; | |
85 collected = acc.features.collected as FeatureList; | |
86 acc.features.collected = arrayReducer<Feature>( | |
87 collected, | |
88 incoming | |
89 ); | |
90 break; | |
91 | |
92 case "tracks": | |
93 incoming = val.features.collected as TrackFeatures; | |
94 collected = acc.features.collected as TrackFeatures; | |
95 acc.features.collected = arrayReducer<TrackFeature>( | |
96 collected, incoming | |
97 ); | |
98 break; | |
99 | |
100 default: | |
101 throw new Error('Invalid feature output. Aborting'); | |
102 } | |
103 | |
104 return acc; | |
105 }; |