def compute(self, *args): inputNames = self.inputNames() if len(args) != len(inputNames): raise ValueError(name+'.compute requires '+str(len(inputNames))+' argument(s), '+str(len(args))+' given') # we have to make some exceptions for YamlOutput and PoolAggregator # because they expect cpp Pools if name in ('YamlOutput', 'PoolAggregator', 'SvmClassifier', 'PCA', 'GaiaTransform'): args = (args[0].cppPool,) # verify that all types match and do any necessary conversions result = [] convertedArgs = [] for i in range(len(inputNames)): goalType = _c.Edt(self.inputType(inputNames[i])) try: convertedData = _c.convertData(args[i], goalType) except TypeError: raise TypeError('Error cannot convert argument %s to %s' \ %(str(_c.determineEdt(args[i])), str(goalType))) convertedArgs.append(convertedData) results = self.__compute__(*convertedArgs) # we have to make an exceptional case for YamlInput, because we need # to wrap the Pool that it outputs w/ our python Pool from common.py if name in ('YamlInput', 'PoolAggregator', 'SvmClassifier', 'PCA', 'GaiaTransform', 'Extractor'): return _c.Pool(results) else: return results
def __init__(self, data): self.__initialized = False self.__initializedType = _c.Edt(_c.Edt.UNDEFINED) self.dataref = data self.data = _StreamConnector(None, self, 'data') # keys should be StreamConnectors (outputs) and values should be lists # of StreamConnectors/pool tuples/None (inputs) self.connections = {} self.connections[self.data] = []
def compute(self, *args): inputNames = self.inputNames() if len(args) != len(inputNames): raise ValueError(name+'.compute requires '+str(len(inputNames))+' argument(s), '+str(len(args))+' given') # we have to make some exceptions for YamlOutput and PoolAggregator # because they expect cpp Pools if name in ('YamlOutput', 'PoolAggregator', 'SvmClassifier', 'PCA', 'GaiaTransform'): args = (args[0].cppPool,) # verify that all types match and do any necessary conversions result = [] convertedArgs = [] for i in range(len(inputNames)): arg = args[i] if type(args[i]).__module__ == 'numpy': if not args[i].flags['C_CONTIGUOUS']: arg = copy(args[i]) goalType = _c.Edt(self.inputType(inputNames[i])) try: convertedData = _c.convertData(arg, goalType) except TypeError: raise TypeError('Error cannot convert argument %s to %s' \ %(str(_c.determineEdt(arg)), str(goalType))) convertedArgs.append(convertedData) results = self.__compute__(*convertedArgs) # we have to make an exceptional case for YamlInput, because we need # to wrap the Pool that it outputs w/ our python Pool from common.py if name in ('YamlInput', 'PoolAggregator', 'SvmClassifier', 'PCA', 'GaiaTransform', 'Extractor'): return _c.Pool(results) # MusicExtractor and FreesoundExtractor output two pools if name in ('MusicExtractor', 'FreesoundExtractor'): return (_c.Pool(results[0]), _c.Pool(results[1])) # In the case of MetadataReader, the 7th output is also a Pool if name in ('MetadataReader'): return results[:7] + (_c.Pool(results[7]),) + results[8:] else: return results
def __rshift__(left, right): if not isinstance(left.output_algo, VectorInput) and \ not left.output_algo.hasOutput(left.name): raise NameError( 'The \'%s\' algorithm does not have a source called \'%s\'' % (left.output_algo.name(), left.name)) # connect a source to a sink if isinstance(right, _StreamConnector): # need to make an exception if the left algorithm is a VectorInput # because its underlying c++ type is initialized at connection time if isinstance(left.output_algo, VectorInput): left.output_algo.__inner_init__( _c.Edt(right.input_algo.getInputType(right.name))) if not right.input_algo.hasInput(right.name): raise NameError( 'The \'%s\' algorithm does not have a sink called \'%s\'' % (right.input_algo.name(), right.name)) _essentia.connect(left.output_algo, left.name, right.input_algo, right.name) # update connections left.output_algo.connections[left].append(right) return right elif isinstance( right, _essentia.StreamingAlgorithm) and right.name() == 'FileOutput': _essentia.fileOutputConnect(left.output_algo, left.name, right) # update connections left.output_algo.connections[left].append(right) return right # connect a source to a pool elif isinstance(right, tuple): if not len(right) == 2 or \ not isinstance(right[0], _c.Pool) or \ not isinstance(right[1], str): raise TypeError( 'the right side tuple given should consist of a Pool and a string descriptor name' ) # make sure to lazy-initialize the VectorInput if isinstance(left.output_algo, VectorInput): left.output_algo.__inner_init_default__() # update connections left.output_algo.connections[left].append(right) return _essentia.poolConnect(left.output_algo, left.name, right[0].cppPool, right[1]) # connect a source to NOWHERE elif right is None: # still need to lazy-initialize the VectorInput if isinstance(left.output_algo, VectorInput): left.output_algo.__inner_init_default__() # update connections left.output_algo.connections[left].append(right) return _essentia.nowhereConnect(left.output_algo, left.name) # none of the above accepted types: raise an exception raise TypeError('\'%s.%s\' A source can only be connected to a sink, a pair '\ '(tuple) of pool and key name, or None' %(left.output_algo.name(), left.name))