arrays - Python segmentation fault -
arrays - Python segmentation fault -
i running script in python, read file several lines 7 columns.
i using regular expressions read trough file , create numpy array it; problem not in reading file reg ex, in loop run on lines.
at first created mock file of 1000 lines within script itself, read it, looped on , worked fine.
now i've received real info , 50 files ~8000 lines each, , if run script on 1 of these segmentation fault. have noticed different behaviours if input file extension .dat or .txt: if .txt segmentation fault, if .dat after while pc "freezes" , can't move cursor on desktop, have shut downwards forcefully. can explain that?
#!/usr/bin/env python __future__ import partition # no more "zero" integer partition bugs!:p import os import numpy np import re import gzip file_binaries = open('binaries_all.txt', "r") stars = open('star.50a.txt', "r") output = file('star_out.dat', "w") num_lines = sum(1 line in open('binaries_all.txt')) num_lines_s = sum(1 line in open('star.50a.txt')) #metallicity (z_sun=100) high = 50. med = 40. low = 5. # cluster mean mass (m_sun) m_cl = 3500. #simulations number each metallicity range n_z = 200 z_re = re.compile('^(\s+),') n_re = re.compile('^\s+\s+(\s+),') time_re = re.compile('^\s+\s+\s+\s+\s+\s+\s+\s+(\s+),') m1_re = re.compile('^\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+(\s+),') m2_re = re.compile('^\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+(\s+),') sma_re = re.compile('^\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+(\s+),') e_re= re.compile('^\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+(\s+)') binaries = np.empty((num_lines,7)) z_100=[] z_010=[] z_001=[] i=0 line in file_binaries: zz = z_re.search(line) nn = n_re.search(line) tt = time_re.search(line) m11 = m1_re.search(line) m22 = m2_re.search(line) aa = sma_re.search(line) ee = e_re.search(line) if(zz!=none): binaries[i,0] = zz.group(1) if (binaries[i,0] > high) : z_100.append(binaries[i,:]) elif (low < int(binaries[i,0]) < med): z_010.append(binaries[i,:]) else : z_001.append(binaries[i,:]) if(nn!=none): binaries[i,1] = nn.group(1) if(tt!=none): binaries[i,2] = tt.group(1) if(m11!=none): binaries[i,3] = m11.group(1) if(m22!=none): binaries[i,4] = m22.group(1) if(aa!=none): binaries[i,5] = aa.group(1) if(ee!=none): binaries[i,6] = ee.group(1) i+=1 z_100 = np.array(z_100) z_010 = np.array(z_010) z_001 = np.array(z_001) x1_re = re.compile('^(\s+)') x2_re = re.compile('^\s+\s(\s+)') x3_re= re.compile('^\s+\s+\s+\s+(\s+)') m_re= re.compile('^\s+\s+\s+\s+\s+\s+(\s+)') zcl_re= re.compile('^\s+\s+\s+\s+\s+\s+\s+\s+(\s+)') scale_re = re.compile('^\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+(\s+)') lookb_re = re.compile('^\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+\s+(\s+)') star = np.empty((num_lines_s,7)) i=0 line in stars: xx1 = x1_re.search(line) xx2 = x2_re.search(line) xx3 = x3_re.search(line) mm = m_re.search(line) zzcl = zcl_re.search(line) sscale = scale_re.search(line) llookb = lookb_re.search(line) if(xx1 != none): x1 = xx1.group(1) star[i,0] = xx1.group(1) if(xx2 != none): x2 = xx2.group(1) star[i,1] = xx2.group(1) if(xx3 != none): x3 = xx3.group(1) star[i,2] = xx3.group(1) if(mm != none): m = mm.group(1) star[i,3] = mm.group(1) if(zzcl != none): zcl = zzcl.group(1) star[i,4] = zzcl.group(1) if(sscale != none): scale = sscale.group(1) star[i,5] = sscale.group(1) if(llookb != none): lookb = llookb.group(1) star[i,6] = llookb.group(1) i+=1 updated=[] i=0 in range(star.shape[0]): n_cl = int(star[i,3]/m_cl) print n_cl zcl = star[i,4] pick = np.random.random_integers(n_z, size=(n_cl)) if(zcl <= low): k in range(len(pick)) : index_001 = np.where(z_001 == pick[k]) index001 = list(index_001[0]) j in range(len(index001)): q = [z_001[index001[j],0],z_001[index001[j],1],z_001[index001[j],2],z_001[index001[j],3],z_001[index001[j],4],z_001[index001[j],5],z_001[index001[j],6],star[i,0],star[i,1],star[i,2],star[i,3],star[i,4],star[i,5],star[i,6]] updated.append(q) if(high <= zcl < low): k in range(len(pick)) : index_010 = np.where( z_010 == pick[k]) index010 = list(index_010[0]) j in range(len(index010)): q = [z_010[index010[j],0],z_010[index010[j],1],z_010[index010[j],2],z_010[index010[j],3],z_010[index010[j],4],z_010[index010[j],5],z_010[index010[j],6],star[i,0],star[i,1],star[i,2],star[i,3],star[i,4],star[i,5],star[i,6]] updated.append(q) if(zcl > high): k in range(len(pick)) : index_100 = np.where( z_100 == pick[k]) index100 = list(index_100[0]) j in range(len(index100)): q = [z_100[index100[j],0],z_100[index100[j],1],z_100[index100[j],2],z_100[index100[j],3],z_100[index100[j],4],z_100[index100[j],5],z_100[index100[j],6],star[i,0],star[i,1],star[i,2],star[i,3],star[i,4],star[i,5],star[i,6]] updated.append(q) i+=1 updated = np.array(updated) print updated.shape print updated np.savetxt(output, updated )
python arrays numpy segmentation-fault
Comments
Post a Comment