从 Matlab 调用的 Python 子进程失败

标签 python matlab ubuntu subprocess

我在尝试在 Matlab 中加入来自不同来源的一些代码时遇到了一个问题,我真的不知道如何处理它。本质上,我有一些用于从命令行调用的压缩算法的 Python 代码,它本身使用子进程来运行编译为二进制的 C++ 代码并与之通信。

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 调用子进程?

如有任何帮助,我们将不胜感激!

最佳答案

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.

关于从 Matlab 调用的 Python 子进程失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38665247/

相关文章:

python - 使用 stdout Popen 捕获 sqoop 日志

postgresql - Ubuntu 18.04 Postgresql Client not installed 错误解决方法

c++ - 从 MATLAB 到 C++ : equivalent of bwmorph with option 'remove'

matlab - 逻辑为 char

node.js - 在ubuntu 17.04上安装 Node

node.js - 使用特定用户运行 Node 脚本

python - IPython 笔记本内核在运行 Kmeans 时死机

python - 使用pyqt5制作简单的聊天机器人,无需按钮

python - 在 setdefault 之后追加到字典中的列表

python - matlab数组索引问题