Ubuntu中python程序结束时的分段错误

时间:2013-01-29 21:44:27

标签: python

我有一个似乎在Eclipse运行时配置中工作的python脚本。当我在Ubuntu命令行中运行它时,在主程序结束后出现分段错误。为什么会发生这种情况,我该如何解决它甚至调试呢?

$:~/ober/code/impute/impute/batch-beagle$ python ~/ober/code/impute/bin/ibd_segments.py -v 1 ~/ober/data/hutt/chr22/hutt.stage5.npz ~/ober/data/hutt/hutt.kinship /home/oren/ober/code/impute/impute/batch-beagle/out/node-0/node-0/ibd-segment-0.in
[[0 0]]
Pair 1/4: (0,0) (0,0)
0 3218 16484792 51156934 0 0
Pair 2/4: (0,0) (0,1)
Pair 3/4: (0,1) (0,0)
Pair 4/4: (0,1) (0,1)
0 3218 16484792 51156934 0 1
Done
Segmentation fault (core dumped)

脚本:

import os, sys, impute as im, itertools, csv, optparse, traceback, util, numpy as np

####################################################################################
if __name__ == '__main__':
    '''
    --------------------------------------------------
    Main program
    --------------------------------------------------
    '''
    # Parse and validate command-line arguments
    PROGRAM = os.path.basename(sys.argv[0])
    usage = 'Usage: %s [flags] <phased-data-file> <kinship-file> <input-file>\n\n' \
        'Locate IBD segments among a subset of sample in an NPZ phased data set.\n' \
        'Sample pairs are read from standard input. Segments are written to standard output.\n' \
        '\tphased-data-file - NPZ file containing phasing results\n' \
        '\tkinship-file - Sorted identity coefficient file\n' \
        '\tpair-list-file - Sorted identity coefficient file\n' \
        '\tout-file - File to output segments to\n' \
        '\n' \
        'Example:\n' \
        'phased-data-file = /home/oren/ober/data/hutt/chr22/hutt.stage5.npz\n' \
        'kinship-file = /home/oren/ober/data/hutt/hutt.kinship\n' \
        'pair-list-file contains the lines\n' \
        '0 1\n' \
        '...\n' \
        '0 100\n' \
        '\n' \
        'Type ''%s -h'' to display full help.' % (PROGRAM, PROGRAM)
    parser = optparse.OptionParser(usage=usage)
    parser.add_option('-v', '--debug', type='int', dest='debug', default=0,
                      help='Debug Level (0=quiet; 1=summary; 2=full debug)')
    (options, args) = parser.parse_args(sys.argv[1:])
    if len(args) != 3:
        print usage
        sys.exit(1)
    phased_data_file, kinship_file, input_file = args

    try:
        # Load data
        problem = im.io.read_npz(phased_data_file)
        params = im.PhaseParam(kinship_file=kinship_file, debug=(options.debug >= 2))

        # Read all pairs from stdin first
        # pairs = [(int(line[0]), int(line[1])) for line in csv.reader(sys.stdin, delimiter=' ', skipinitialspace=True) if line]
        pairs = np.loadtxt(input_file, dtype=np.uint)
        if len(pairs.shape) < 2: 
            pairs = pairs[np.newaxis]
        print pairs

        # Loop over pairs and output segments to output file
        num_pairs = 4 * len(pairs)
        for k, ((i, j), (a, b)) in enumerate(itertools.product(pairs, itertools.product(im.constants.ALLELES, im.constants.ALLELES))):
            if options.debug >= 1:
                print 'Pair %d/%d: (%d,%d) (%d,%d)' % (k + 1, num_pairs, i, a, j, b)
            segments = im.ih.hap_segments(problem, i, a, j, b, params)
            segments.save(sys.stdout)
        print 'Done'
    except:
        traceback.print_exc(file=sys.stdout)
        sys.exit(util.EXIT_FAILURE)

1 个答案:

答案 0 :(得分:2)

事实证明我正在从一个损坏的文件中加载一个numpy npz文件(带numpy.load()),我通过rsync从我的家用电脑转移到这个文件。我在这台机器上重新生成NPZ文件后,一切正常。感谢您的反馈。