changeset 95:3e5791890b65

refactor, add process_frames
author Chris Cannam
date Mon, 02 Feb 2015 17:15:15 +0000
parents c3318a95625b
children f0e005248b9a
files test/test_frames.py vamp/__init__.py vamp/collect.py vamp/frames.py vamp/load.py vamp/process.py
diffstat 6 files changed, 84 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/test/test_frames.py	Mon Feb 02 16:32:44 2015 +0000
+++ b/test/test_frames.py	Mon Feb 02 17:15:15 2015 +0000
@@ -1,5 +1,5 @@
 
-import vamp
+import vamp.frames as fr
 import numpy as np
 
 def to_lists(arrs):
@@ -7,16 +7,16 @@
 
 def test_frames_from_1d_buffer():
     buf = np.arange(6)
-    ff = to_lists(vamp.frames_from_array(buf, 2, 2))
+    ff = to_lists(fr.frames_from_array(buf, 2, 2))
     assert(ff == [[[0,1]],[[2,3]],[[4,5]]])
-    ff = to_lists(vamp.frames_from_array(buf, 1, 2))
+    ff = to_lists(fr.frames_from_array(buf, 1, 2))
     assert(ff == [[[0,1]],[[1,2]],[[2,3]],[[3,4]],[[4,5]],[[5,0]]])
 
 def test_frames_from_2d_buffer():
     buf = np.array([np.arange(6),np.arange(6,12)])
-    ff = to_lists(vamp.frames_from_array(buf, 2, 2))
+    ff = to_lists(fr.frames_from_array(buf, 2, 2))
     assert(ff == [[[0,1],[6,7]],[[2,3],[8,9]],[[4,5],[10,11]]])
-    ff = to_lists(vamp.frames_from_array(buf, 1, 2))
+    ff = to_lists(fr.frames_from_array(buf, 1, 2))
     assert(ff == [[[0,1],[6,7]],[[1,2],[7,8]],[[2,3],[8,9]],
                   [[3,4],[9,10]],[[4,5],[10,11]],[[5,0],[11,0]]])
 
--- a/vamp/__init__.py	Mon Feb 02 16:32:44 2015 +0000
+++ b/vamp/__init__.py	Mon Feb 02 17:15:15 2015 +0000
@@ -2,7 +2,7 @@
 
 import vampyhost
 
-from load import list_plugins, load_and_configure
-from frames import frames_from_array
-from process import process, process_multiple_outputs
+from load import list_plugins
+from process import process, process_frames, process_multiple_outputs
 from collect import collect
+
--- a/vamp/collect.py	Mon Feb 02 16:32:44 2015 +0000
+++ b/vamp/collect.py	Mon Feb 02 17:15:15 2015 +0000
@@ -60,20 +60,10 @@
         return "vector"
     return "matrix"
 
-def process_and_reshape(data, sample_rate, key, output, parameters = {}):
 
-    plugin, step_size, block_size = load.load_and_configure(data, sample_rate, key, parameters)
+def reshape(results, sample_rate, step_size, output_desc):
 
-    if output == "":
-        output_desc = plugin.get_output(0)
-        output = output_desc["identifier"]
-    else:
-        output_desc = plugin.get_output(output)
-
-    ff = frames.frames_from_array(data, step_size, block_size)
-
-    results = process.process_frames_with_plugin(ff, sample_rate, step_size, plugin, [output])
-
+    output = output_desc["identifier"]
     shape = deduce_shape(output_desc)
     out_step = get_feature_step_time(sample_rate, step_size, output_desc)
 
@@ -88,9 +78,51 @@
     else:
         rv = list(fill_timestamps(results, sample_rate, step_size, output_desc))
 
+    return rv
+
+        
+def collect(data, sample_rate, key, output, parameters = {}):
+
+    plugin, step_size, block_size = load.load_and_configure(data, sample_rate, key, parameters)
+
+    if output == "":
+        output_desc = plugin.get_output(0)
+        output = output_desc["identifier"]
+    else:
+        output_desc = plugin.get_output(output)
+
+    ff = frames.frames_from_array(data, step_size, block_size)
+
+    results = process.process_frames_with_plugin(ff, sample_rate, step_size, plugin, [output])
+
+    rv = reshape(results, sample_rate, step_size, output_desc)
+
     plugin.unload()
     return rv
 
+        
+def collect_frames(ff, channels, sample_rate, step_size, key, output, parameters = {}):
 
-def collect(data, sample_rate, key, output, parameters = {}):
-    return process_and_reshape(data, sample_rate, key, output, parameters)
+    plug = vampyhost.load_plugin(key, sample_rate,
+                                 vampyhost.ADAPT_INPUT_DOMAIN +
+                                 vampyhost.ADAPT_BUFFER_SIZE +
+                                 vampyhost.ADAPT_CHANNEL_COUNT)
+
+    plug.set_parameter_values(parameters)
+
+    if not plug.initialise(channels, step_size, block_size):
+        raise "Failed to initialise plugin"
+
+    if output == "":
+        output_desc = plugin.get_output(0)
+        output = output_desc["identifier"]
+    else:
+        output_desc = plugin.get_output(output)
+
+    results = process.process_frames_with_plugin(ff, sample_rate, step_size, plugin, [output])
+
+    rv = reshape(results, sample_rate, step_size, output_desc)
+
+    plugin.unload()
+    return rv
+
--- a/vamp/frames.py	Mon Feb 02 16:32:44 2015 +0000
+++ b/vamp/frames.py	Mon Feb 02 17:15:15 2015 +0000
@@ -2,8 +2,8 @@
 
 import numpy
 
-def frames_from_array(arr, step_size, frameSize):
-    """Generate a list of frames of size frameSize, extracted from the input array arr at step_size intervals"""
+def frames_from_array(arr, step_size, frame_size):
+    """Generate a list of frames of size frame_size, extracted from the input array arr at step_size intervals"""
     # presumably such a function exists in many places, but I need practice
     assert(step_size > 0)
     if arr.ndim == 1: # turn 1d into 2d array with 1 channel
@@ -12,10 +12,10 @@
     n = arr.shape[1]
     i = 0
     while (i < n):
-        frame = arr[:, i : i + frameSize]
+        frame = arr[:, i : i + frame_size]
         w = frame.shape[1]
-        if (w < frameSize):
-            pad = numpy.zeros((frame.shape[0], frameSize - w))
+        if (w < frame_size):
+            pad = numpy.zeros((frame.shape[0], frame_size - w))
             frame = numpy.concatenate((frame, pad), 1)
         yield frame
         i = i + step_size
--- a/vamp/load.py	Mon Feb 02 16:32:44 2015 +0000
+++ b/vamp/load.py	Mon Feb 02 17:15:15 2015 +0000
@@ -25,6 +25,8 @@
     if data.ndim > 1:
         channels = data.shape[0]
 
-    plug.initialise(channels, step_size, block_size)
-    return (plug, step_size, block_size)
+    if plug.initialise(channels, step_size, block_size):
+        return (plug, step_size, block_size)
+    else:
+        raise "Failed to initialise plugin"
 
--- a/vamp/process.py	Mon Feb 02 16:32:44 2015 +0000
+++ b/vamp/process.py	Mon Feb 02 17:15:15 2015 +0000
@@ -46,6 +46,27 @@
     plugin.unload()
 
 
+def process_frames(ff, channels, sample_rate, step_size, key, output = "", parameters = {}):
+
+    plug = vampyhost.load_plugin(key, sample_rate,
+                                 vampyhost.ADAPT_INPUT_DOMAIN +
+                                 vampyhost.ADAPT_BUFFER_SIZE +
+                                 vampyhost.ADAPT_CHANNEL_COUNT)
+
+    plug.set_parameter_values(parameters)
+
+    if not plug.initialise(channels, step_size, block_size):
+        raise "Failed to initialise plugin"
+    
+    if output == "":
+        output = plugin.get_output(0)["identifier"]
+
+    for r in process_frames_with_plugin(ff, sample_rate, step_size, plugin, [output]):
+        yield r[output]
+    
+    plugin.unload()
+    
+
 def process_multiple_outputs(data, sample_rate, key, outputs, parameters = {}):
 #!!! docstring