comparison vamp/collect.py @ 93:4bed6bf67243

Return simple array for simple data
author Chris Cannam
date Mon, 02 Feb 2015 16:08:42 +0000
parents 1a08dd72f4d2
children c3318a95625b
comparison
equal deleted inserted replaced
92:18b412a9c4d5 93:4bed6bf67243
2 2
3 import vampyhost 3 import vampyhost
4 import load 4 import load
5 import process 5 import process
6 import frames 6 import frames
7
8 import numpy as np
7 9
8 10
9 def timestamp_features(sample_rate, step_size, output_desc, features): 11 def timestamp_features(sample_rate, step_size, output_desc, features):
10 n = -1 12 n = -1
11 if output_desc["sample_type"] == vampyhost.ONE_SAMPLE_PER_STEP: 13 if output_desc["sample_type"] == vampyhost.ONE_SAMPLE_PER_STEP:
26 else: 28 else:
27 for f in features: 29 for f in features:
28 yield f 30 yield f
29 31
30 32
31 def process_and_fill_timestamps(data, sample_rate, key, output, parameters = {}): 33 def fill_timestamps(results, sample_rate, step_size, output_desc):
34
35 output = output_desc["identifier"]
36
37 selected = [ r[output] for r in results ]
38
39 stamped = timestamp_features(sample_rate, step_size, output_desc, selected)
40
41 for s in stamped:
42 yield s
43
44
45 def deduce_shape(output_desc):
46 if output_desc["has_duration"]:
47 return "individual"
48 if output_desc["sample_type"] == vampyhost.VARIABLE_SAMPLE_RATE:
49 return "individual"
50 if not output_desc["has_fixed_bin_count"]:
51 return "individual"
52 if output_desc["bin_count"] == 0:
53 return "individual"
54 if output_desc["bin_count"] > 1:
55 return "matrix"
56 return "vector"
57
58
59 def process_and_reshape(data, sample_rate, key, output, parameters = {}):
32 60
33 plugin, step_size, block_size = load.load_and_configure(data, sample_rate, key, parameters) 61 plugin, step_size, block_size = load.load_and_configure(data, sample_rate, key, parameters)
34 62
35 if output == "": 63 if output == "":
36 output_desc = plugin.get_output(0) 64 output_desc = plugin.get_output(0)
40 68
41 ff = frames.frames_from_array(data, step_size, block_size) 69 ff = frames.frames_from_array(data, step_size, block_size)
42 70
43 results = process.process_frames_with_plugin(ff, sample_rate, step_size, plugin, [output]) 71 results = process.process_frames_with_plugin(ff, sample_rate, step_size, plugin, [output])
44 72
45 selected = [ r[output] for r in results ] 73 shape = deduce_shape(output_desc)
46 74
47 stamped = timestamp_features(sample_rate, step_size, output_desc, selected) 75 if shape == "vector":
48 76 rv = np.array([r[output]["values"][0] for r in results])
49 for s in stamped: 77 elif shape == "matrix":
50 yield s 78 rv = np.array(
79 [[r[output]["values"][i] for r in results]
80 for i in range(0, output_desc["bin_count"]-1)])
81 else:
82 rv = list(fill_timestamps(results, sample_rate, step_size, output_desc))
51 83
52 plugin.unload() 84 plugin.unload()
53 85 return rv
86
54 87
55 def collect(data, sample_rate, key, output, parameters = {}): 88 def collect(data, sample_rate, key, output, parameters = {}):
56 return process_and_fill_timestamps(data, sample_rate, key, output, parameters) 89 return process_and_reshape(data, sample_rate, key, output, parameters)