2012-08-28 14 views
2

我试图在Python 2.7上使用mac osx上的ghmm python模块。我已经成功地得到安装了一切,我可以在Python环境中导入GHMM,但有错误,当我运行这个(从GHMM“教程”)(UnfairCasino可以在这里找到http://ghmm.sourceforge.net/UnfairCasino.py):GHMM - NULL指针上的尝试m_free

from ghmm import * 
from UnfairCasino import test_seq 
sigma = IntegerRange(1,7) 
A = [[0.9, 0.1], [0.3, 0.7]] 
efair = [1.0/6] * 6 
eloaded = [3.0/13, 3.0/13, 2.0/13, 2.0/13, 2.0/13, 1.0/13] 
B = [efair, eloaded] 
pi = [0.5] * 2 
m = HMMFromMatrices(sigma, DiscreteDistribution(sigma), A, B, pi) 
v = m.viterbi(test_seq) 

具体来说,我得到这个错误:

GHMM ghmm.py:148 - sequence.c:ghmm_dseq_free(1199): Attempted m_free on NULL pointer. Bad program, BAD! No cookie for you. python(52313,0x7fff70940cc0) malloc: * error for object 0x74706d6574744120: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug Abort trap

,当我设置ghmm.py记录器“调试”,日志打印出以下之前:

GHMM ghmm.py:2333 - HMM.viterbi() -- begin

GHMM ghmm.py:849 - EmissionSequence.asSequenceSet() -- begin >

GHMM ghmm.py:862 - EmissionSequence.asSequenceSet() -- end >

Traceback (most recent call last):

File "/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/logging/init.py", line 842, in emit

msg = self.format(record)

File "/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/logging/init.py", line 719, in format

return fmt.format(record)

File "/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/logging/init.py", line 464, in format

record.message = record.getMessage()

File "/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/logging/init.py", line 328, in getMessage

msg = msg % self.args

TypeError: not all arguments converted during string formatting

Logged from file ghmm.py, line 1159

Traceback (most recent call last):

File "/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/logging/init.py", line 842, in emit

msg = self.format(record)

File "/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/logging/init.py", line 719, in format

return fmt.format(record)

File "/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/logging/init.py", line 464, in format

record.message = record.getMessage()

File "/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/logging/init.py", line 328, in getMessage

msg = msg % self.args

TypeError: not all arguments converted during string formatting

Logged from file ghmm.py, line 949

GHMM ghmm.py:2354 - HMM.viterbi() -- end

GHMM ghmm.py:1167 - del SequenceSubSet >

所以我怀疑它有有些方法会在Viterbi函数完成后删除序列,但我不确定这是否意味着我需要修改Python代码,C代码,或者如果我需要以不同的方式编译ghmm和wrappers。任何帮助/建议将非常感激,因为我一直在试图让这个图书馆在过去4天工作。

回答

3

鉴于这个问题的年龄,你可能已经走上了别的,但这似乎是我发现的唯一相关结果。问题是由于Python函数'EmissionSequence :: asSequenceSet'的执行方式有些奇怪,所以发生了一个double-free。如果你看一下ghmm.py是如何实现的(〜845线 - 863)

def asSequenceSet(self): 
    """ 
    @returns this EmissionSequence as a one element SequenceSet 
    """ 
    log.debug("EmissionSequence.asSequenceSet() -- begin " + repr(self.cseq)) 
    seq = self.sequenceAllocationFunction(1) 

    # checking for state labels in the source C sequence struct 
    if self.emissionDomain.CDataType == "int" and self.cseq.state_labels is not None: 
     log.debug("EmissionSequence.asSequenceSet() -- found labels !") 
     seq.calloc_state_labels() 
     self.cseq.copyStateLabel(0, seq, 0) 

    seq.setLength(0, self.cseq.getLength(0)) 
    seq.setSequence(0, self.cseq.getSequence(0)) 
    seq.setWeight(0, self.cseq.getWeight(0)) 

    log.debug("EmissionSequence.asSequenceSet() -- end " + repr(seq)) 
    return SequenceSetSubset(self.emissionDomain, seq, self) 

这也许应该提出一些红旗,因为它似乎是达到到C有点多(不,我知道当然,我没有深入了解它)。

不管怎么说,如果你看起来有点这个功能上面,有一个名为“sequenceSet”另一个功能:

def sequenceSet(self): 
    """ 
    @return a one-element SequenceSet with this sequence. 
    """ 

    # in order to copy the sequence in 'self', we first create an empty SequenceSet and then 
    # add 'self' 
    seqSet = SequenceSet(self.emissionDomain, []) 
    seqSet.cseq.add(self.cseq) 
    return seqSet 

它似乎有异曲同工之妙,但实现方式不同。不管怎么说,如果更换的“EmissionSequence :: asSequenceSet”身在ghmm.py,只有:

def asSequenceSet(self): 
""" 
@returns this EmissionSequence as a one element SequenceSet 
""" 
    return self.sequenceSet(); 

,然后重新生成/重新安装GHMM模块,代码将工作没有崩溃,你应该能够继续你的快乐方式。我不确定这是否可以作为解决方案提交,因为ghmm项目看起来有点死亡,但希望这很简单,可以帮助任何使用此库的人。

+0

它的工作原理!不确定结果是否正确......但是它是有效的! – Alexander