熊猫应用于自定义功能导致隔离错误

时间:2018-06-30 13:26:14

标签: python pandas biopython

我正在使用apply方法在数据框上应用自定义方法。传递超过两行(元组)的数据帧时,将导致内核在jupyter笔记本中终止(死机)。在终端上运行同一命令时,会导致分段错误

该方法适用于单个行或2行,但不能超过此行。以下两个调用均与自定义函数myTrial一起使用。

myTrial(pd.ix[3,:])
newPD2 = pd.head(2).apply(myTrial, axis=1)

但这会导致以下错误。

newPD2 = pd.head(3).apply(myTrial, axis=1)
The kernel appears to have died. It will restart automatically.

方法myTrial使用pairwise2.align.globalmx中的对齐函数BioPython和其他内置的python函数。我提供以下功能:

我有一个10,000行8列的数据框。我正在起诉具有256 GB RAM的服务器。

功能如下

from Bio import pairwise2
def myTrial(pdf):
    source = pdf['source']
    targ = pdf['target']

    if source == targ:
        pdf['sourceAlign'] = source
        pdf['targetAlign'] = source
        pdf['joint'] = source

        return pdf

    alignments = pairwise2.align.globalmx(source, targ,1,-0.5)
    summaDict = dict()
    for item in alignments:
        lenList = list()
        i = 0
        while i < len(item[0]):
            con = 0
            while item[0][i] == item[1][i]:
                con += 1
                i += 1

            if con == 0:
                i += 1
            else:
                lenList.append((con,item[0][i-con:i],item))
                con =0

        summa = 0
        for thing in lenList:
            summa += (thing[0]*thing[0])
        try:
            summaDict[summa].append(lenList)
        except:
            summaDict[summa] = list()
            summaDict[summa].append(lenList)
    stuff = sorted(summaDict.keys(),reverse=True)[0]

    if len(summaDict[stuff]) > 1:
        print(source,targ,summaDict[stuff])

    words = summaDict[stuff][0][0][2]

    jointWord = ''
    for inda in range(len(words[0])):
        if words[0][inda] == words[1][inda]:
            jointWord += words[0][inda]
        else:
            if words[0][inda] != '-':
                jointWord += 'DEL('+words[0][inda]+')'
            if words[1][inda] != '-':
                jointWord += 'INS('+words[1][inda]+')'

    pdf['sourceAlign'] = words[0]
    pdf['targetAlign'] = words[1]
    pdf['joint'] = jointWord

    return pdf

数据帧如下

type |  source |    props | target |    subtype |   p0 |    p1 |    p2 |    p3 |    p4
        0 | ADJ |   najprzytulniejszy | [NEUT, INS, SG] |   najprzytulniejszym |    NaN |   NEUT |  INS |   SG |    None |  None
        1 | ADJ |   sadystyczny |   [MASC, DAT, SG] |   sadystycznemu | NaN |   MASC |  DAT |   SG |    None |  None
        2 | V | wyrzucić |  [FUT, 2, SG] |  wyrzucisz | NaN |   FUT |   2 | SG |    None |  None
        3 | N | świat | [ACC, SG] | świat | NaN |   ACC |   SG |    None |  None |  None
        4 | N | Marsjanin | [INS, PL] | Marsjanami |    NaN |   INS |   PL |    None |  None |  None

控制台输出也没有任何帮助:

[I 19:16:45.709 NotebookApp] Kernel restarted: 604e9df5-6630-4a12-9c13-e9d7a4835da2
[I 19:17:00.710 NotebookApp] KernelRestarter: restarting kernel (1/5)
WARNING:root:kernel 604e9df5-6630-4a12-9c13-e9d7a4835da2 restarted

可能是什么原因?

1 个答案:

答案 0 :(得分:1)

Bio.pairwise2.globalmx函数正在导致段错误,这已超出了熊猫的控制范围。有关如何解决基础段错误的解决方案,请参见Biopython pairwise2 for non-ASCII strings

相关问题