2016-07-29 42 views
1

我試圖在Matlab中加入一些來自不同源代碼的問題,我不知道如何處理它。從本質上講,我有一些Python代碼用於從命令行調用的壓縮算法,該代碼本身使用子進程運行並與編譯爲二進制文件的C++代碼進行通信。從Matlab調用的Python子進程失敗

Python中的功能(這是一個較大的對象的一部分)看起來像這樣:

def __extractRepeats(self, repeatClass): 
    process = subprocess.Popen(["./repeats1/repeats11", "-i", "-r"+repeatClass, "-n2", "-psol"],stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT) 
    process.stdin.write(' '.join(map(str,self.__concatenatedDAG))) 
    text_file = '' 
    while process.poll() is None: 
     output = process.communicate()[0].rstrip() 
     text_file += output 
    process.wait() 
    repeats=[] 
    firstLine = False 
    for line in text_file.splitlines(): 
     if firstLine == False: 
      firstLine = True 
      continue 
     repeats.append(line.rstrip('\n')) 
    return repeats 

爲了最小化移植問題,我決定完全間接地通過系統命令來執行用Matlab的整合,通過組建一個腳本所有組件並運行它通過

system('./temp_script') 

其中temp_script是可執行的,看起來像這樣:

cd /home/ben/Documents/MATLAB/StructureDiscovery/+sd/Lexis 
python Lexis.py -f i /home/ben/Documents/MATLAB/StructureDiscovery/+sd/Lexis/aabb.txt >> /home/ben/Documents/MATLAB/StructureDiscovery/+sd/Lexis/lexis_results.txt 

現在我在Ubuntu 16.04上運行該腳本,從終端運行腳本。運行從MATLAB相同的腳本,不過,給我的錯誤

Traceback (most recent call last): 
    File "Lexis.py", line 762, in <module> 
    g.GLexis(quietLog, rFlag, functionFlag, costWeight) 
    File "Lexis.py", line 191, in GLexis 
    (maximumRepeatGainValue, selectedRepeatOccs) = self.__retreiveMaximumGainRepeat(normalRepeatType, CostFunction.EdgeCost) 
    File "Lexis.py", line 242, in __retreiveMaximumGainRepeat 
    repeats = self.__extractRepeats(repeatClass) 
    File "Lexis.py", line 302, in __extractRepeats 
    process.stdin.write(' '.join(map(str,self.__concatenatedDAG))) 
IOError: [Errno 32] Broken pipe 

或錯誤

File "Lexis.py", line 251, in __retreiveMaximumGainRepeat 
    idx = map(int,repeatStats[2][1:-1].split(','))[0] 
ValueError: invalid literal for int() with base 10: 'ersio' 

,我一直無法弄清楚當我是哪一個。

爲repeatStats相關片段是

repeats = self.__extractRepeats(repeatClass) 
    for r in repeats: #Extracting maximum repeat 
     repeatStats = r.split() 
     idx = map(int,repeatStats[2][1:-1].split(','))[0] 

我真的不知道什麼叫Matlab的通過系統的東西,直接從終端調用它之間是不同的,所以我不知道發生了什麼錯誤。在OSX 10.11上,完全相同的代碼工作。

有誰知道Matlab的系統命令的內部工作原理以及爲什麼它可能無法讓Python調用子進程?

任何幫助,將不勝感激!

回答

0
repeats = self.__extractRepeats(repeatClass) 
for r in repeats: #Extracting maximum repeat 
    repeatStats = r.split() 
    idx = map(int,repeatStats[2][1:-1].split(','))[0] 

假設你repeatStats [2]是xersiox,10

然後repeatStats [2] [1:-1]變爲ersiox,1

然後repeatStats [2] [1:-1 ] .split( ' ')的結果是列表[' ersio', '1']

當你有你路過整個列表到整個語句

idx = map(int,repeatStats[2][1:-1].split(','))[0] 
# it looks like this idx = map(int, ['ersio', '1'])[0] 
# it picks the first item from the list, which is 'ersio', which does not have any numerical in that. 

請改爲:

idx = map(int,repeatStats[2][1:-1].split(',')[1])[0] 
# or provide an index of the item in the list, which has a number in it. 
+0

如果子過程正常工作,repeatStats [2]是括號中(數字)索引的列表。 – serrapaladin

+0

你可以嘗試刪除stdin = subprocess.PIPE嗎? –

+0

這會失敗,因爲process.stdin是None,這意味着我無法使用process.stdin.write – serrapaladin