2016-03-04 155 views
0

我使用蟒並行編程模塊我已返回我的陣列,但是當我打印變量包含並行化的函數的值的函數返回我「 pp._Task object at 0x04696510「,而不是矩陣的值。 下面是代碼:對象沒有LEN()錯誤

from __future__ import print_function 
import scipy, pylab 
from scipy.io.wavfile import read 
import sys 
import peakpicker as pea 
import pp 
import fingerprint as fhash 
import matplotlib 
import numpy as np 
import tdft 
import subprocess 
import time 
if __name__ == '__main__': 
     start=time.time() 
     #Peak picking dimensions 
     f_dim1 = 30 
     t_dim1 = 80 
     f_dim2 = 10 
     t_dim2 = 20 
     percentile = 80 
     base = 100 # lowest frequency bin used (peaks below are too common/not as useful for identification) 
     high_peak_threshold = 75 
     low_peak_threshold = 60 
     #TDFT parameters 
     windowsize = 0.008  #set the window size (0.008s = 64 samples) 
     windowshift = 0.004 #set the window shift (0.004s = 32 samples) 
     fftsize = 1024   #set the fft size (if srate = 8000, 1024 --> 513 freq. bins separated by 7.797 Hz from 0 to 4000Hz) 

     #Hash parameters 
     delay_time = 250  # 250*0.004 = 1 second#200 
     delta_time = 250*3 # 750*0.004 = 3 seconds#300 
     delta_freq = 128  # 128*7.797Hz = approx 1000Hz#80 
     #Time pair parameters 
     TPdelta_freq = 4 
     TPdelta_time = 2 


     #Cargando datos almacenados 
     database=np.loadtxt('database.dat') 
     songnames=np.loadtxt('songnames.dat', dtype=str, delimiter='\t') 
     separator = '.' 
     print('Please enter an audio sample file to identify: ') 
     userinput = raw_input('---> ') 
     subprocess.call(['ffmpeg','-y','-i',userinput, '-ac', '1','-ar', '8k', 'filesample.wav']) 
     sample = read('filesample.wav') 
     userinput = userinput.split(separator,1)[0] 
     print('Analyzing the audio sample: '+str(userinput)) 
     srate = sample[0] #sample rate in samples/second 
     audio = sample[1] #audio data  
     spectrogram = tdft.tdft(audio, srate, windowsize, windowshift, fftsize) 
     mytime = spectrogram.shape[0] 
     freq = spectrogram.shape[1] 

     print('The size of the spectrogram is time: '+str(mytime)+' and freq: '+str(freq)) 

     threshold = pea.find_thres(spectrogram, percentile, base) 

     peaks = pea.peak_pick(spectrogram,f_dim1,t_dim1,f_dim2,t_dim2,threshold,base) 

     print('The initial number of peaks is:'+str(len(peaks))) 
     peaks = pea.reduce_peaks(peaks, fftsize, high_peak_threshold, low_peak_threshold) 
     print('The reduced number of peaks is:'+str(len(peaks))) 

     #Store information for the spectrogram graph 
     samplePeaks = peaks 
     sampleSpectro = spectrogram 

     hashSample = fhash.hashSamplePeaks(peaks,delay_time,delta_time,delta_freq) 
     print('The dimensions of the hash matrix of the sample: '+str(hashSample.shape)) 


     # tuple of all parallel python servers to connect with 
     ppservers =() 
     #ppservers = ("10.0.0.1",) 

     if len(sys.argv) > 1: 
      ncpus = int(sys.argv[1]) 
      # Creates jobserver with ncpus workers 
      job_server = pp.Server(ncpus, ppservers=ppservers) 
     else: 
      # Creates jobserver with automatically detected number of workers 
      job_server = pp.Server(ppservers=ppservers) 

     print ("Starting pp with", job_server.get_ncpus(), "workers") 

     print('Attempting to identify the sample audio clip.') 

這裏我稱之爲指紋的功能,註釋行的工作,但是當我嘗試並行不起作用:

 timepairs = job_server.submit(fhash.findTimePairs, (database, hashSample, TPdelta_freq, TPdelta_time,)) 
#  timepairs = fhash.findTimePairs(database, hashSample, TPdelta_freq, TPdelta_time) 
     print (timepairs) 


     #Compute number of matches by song id to determine a match 
     numSongs = len(songnames) 
     songbins= np.zeros(numSongs) 
     numOffsets = len(timepairs) 
     offsets = np.zeros(numOffsets) 
     index = 0 
     for i in timepairs: 
       offsets[index]=i[0]-i[1] 
       index = index+1 
       songbins[i[2]] += 1 

     # Identify the song 
     #orderarray=np.column_stack((songbins,songnames)) 
     #orderarray=orderarray[np.lexsort((songnames,songbins))] 
     q3=np.percentile(songbins, 75) 
     q1=np.percentile(songbins, 25) 
     j=0 
     for i in songbins: 
       if i>(q3+(3*(q3-q1))): 
         print("Result-> "+str(i)+":"+songnames[j]) 
       j+=1 
     end=time.time() 
     print('Tiempo: '+str(end-start)+' s') 
     print("Time elapsed: ", +time.time() - start, "s") 
     fig3 = pylab.figure(1003) 
     ax = fig3.add_subplot(111) 
     ind = np.arange(numSongs) 
     width = 0.35 
     rects1 = ax.bar(ind,songbins,width,color='blue',align='center') 
     ax.set_ylabel('Number of Matches') 
     ax.set_xticks(ind) 
     xtickNames = ax.set_xticklabels(songnames) 
     matplotlib.pyplot.setp(xtickNames) 
     pylab.title('Song Identification') 
     fig3.show() 

     pylab.show() 

     print('The sample song is: '+str(songnames[np.argmax(songbins)])) 

指紋的功能,我嘗試並行是:

def findTimePairs(hash_database,sample_hash,deltaTime,deltaFreq): 
"Find the matching pairs between sample audio file and the songs in the database" 

timePairs = [] 

for i in sample_hash: 
    for j in hash_database: 
     if(i[0] > (j[0]-deltaFreq) and i[0] < (j[0] + deltaFreq)): 
      if(i[1] > (j[1]-deltaFreq) and i[1] < (j[1] + deltaFreq)): 
       if(i[2] > (j[2]-deltaTime) and i[2] < (j[2] + deltaTime)): 
        timePairs.append((j[3],i[3],j[4])) 
       else: 
        continue 
      else: 
       continue 
     else: 
      continue 

return timePairs 

完整的錯誤是:

Traceback (most recent call last): 
File "analisisPrueba.py", line 93, in <module> 
numOffsets = len(timepairs) 
TypeError: object of type '_Task' has no len() 

回答

1

submit()方法將任務提交到服務器。你得到的是對任務的引用,而不是結果。 (怎麼可能之前任何工作已經完成的返回它的結果?submit()回報!)而應該提供一個回調函數接收結果。例如,timepairs.append是將拍攝效果,並將其附加到列表timepairs的功能。

timepairs = [] 
job_server.submit(fhash.findTimePairs, (database, hashSample, TPdelta_freq, TPdelta_time,), callback=timepairs.append) 

(每個findTimePairs調用應計算一個結果是,在並不明顯的情況下,你應該提交多個任務。否則,你是在調用並行的Python的所有機器無緣無故。請確保您撥打job_server.wait()等待所有任務完成,然後再嘗試對結果做任何事情。簡而言之,請閱讀文檔和一些示例腳本,並確保您瞭解其工作原理。)

+0

我假裝提交多個具有不同數據庫的任務,但首次測試。我試過你的代碼,但沒有在timepairs中存儲任何東西。在http://www.parallelpython.com/content/view/17/31/有些情況中,我靠。 –

+0

我找到了解決辦法,我叫timepairs的功能。 job1 = timepairs() –

相關問題