Search code examples
pythonpandasbiopython

Pandas apply on custom function results in segmetnation fault


I am applying a custom method on a dataframe using the apply method. When a dataframe with more than 2 rows (tuples) are passed, it results in the kernel being terminated (dead) in the jupyter notebook. When running the same on terminal, it results in a segmentation Fault.

The method works for an individual row or for 2 rows, but not more than that. Both the calls below works with the custom function myTrial.

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

But this results in the following error.

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

The method myTrial uses alignment function pairwise2.align.globalmx from BioPython and other inbuilt python functions. I am providing the function below:

I am having a dataframe with 10,000 rows and 8 columns. I am suing a server with 256 GB RAM.

The function is as follows

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

The dataframe is as follows

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

The console output isnt any helpgul either:

[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

What can be the possible reason?


Solution

  • The Bio.pairwise2.globalmx function is causing a segfault, which is out of Pandas' control. Please see Biopython pairwise2 for non-ASCII strings for the solution on how to fix the underlying segfault.