python - Problems with variable referenced before assignment when using os.path.walk -


ok. have background in matlab , i'm switching python. have bit of code under pythnon 2.6.5 on 64-bit linux scrolls through directories, finds files named 'generaldata.dat', retrieves data them , stitches them new data set:

import pylab p import os, re import linecache ln  def loadgenomemeansize(arg, dirname, files):         file in files:             filepath = os.path.join(dirname, file)             if filepath == os.path.join(dirname,'generaldata.dat'):                 data = p.genfromtxt(filepath)                 if data[-1,4] != 0.0: # checking if data set ok                      data_chopped = data[1000:-1,:] # removing of data                     grand_mean = data_chopped[:,2].mean()                     grand_std = p.sqrt((sum(data_chopped[:,4]*data_chopped[:,3]**2) + sum((data_chopped[:,2]-grand_mean)**2))/sum(data_chopped[:,4]))                 else:                     break             if filepath == os.path.join(dirname,'modelparams.dat'):                 l = re.split(" ", ln.getline(filepath, 6))                 turb_param = float(l[2])                                 arg.append((grand_mean, grand_std, turb_param))  grandmeansdata = [] os.path.walk(os.getcwd(), loadgenomemeansize, grandmeansdata) grandmeansdata = sorted(grandmeansdata, key=lambda data_sort: data_sort[2])  themeans = p.zeros((len(grandmeansdata), 3 )) = 0 item in grandmeansdata:     themeans[i,0] = item[0]     themeans[i,1] = item[1]     themeans[i,2] = item[2]     += 1  print themeans # checking... # later computation on themeans in numpy 

and throws me (though swear working month ego):

traceback (most recent call last):   file "/home/user/01_pyscripts/testtest.py", line 29, in <module>     os.path.walk(os.getcwd(), loadgenomemeansize, grandmeansdata)   file "/usr/lib/python2.6/posixpath.py", line 233, in walk     walk(name, func, arg)   file "/usr/lib/python2.6/posixpath.py", line 225, in walk     func(arg, top, names)   file "/home/user/01_pyscripts/testtest.py", line 26, in loadgenomemeansize     arg.append((grand_mean, grand_std, turb_param)) unboundlocalerror: local variable 'grand_mean' referenced before assignment 

all right... went , did reading , came global variable:

import pylab p import os, re import linecache ln  grand_mean = p.nan grand_std = p.nan def loadgenomemeansize(arg, dirname, files):         file in files:             global grand_mean             global grand_std             filepath = os.path.join(dirname, file)             if filepath == os.path.join(dirname,'generaldata.dat'):                 data = p.genfromtxt(filepath)                 if data[-1,4] != 0.0: # checking if data set ok                      data_chopped = data[1000:-1,:]  # removing of data                     grand_mean = data_chopped[:,2].mean()                     grand_std = p.sqrt((sum(data_chopped[:,4]*data_chopped[:,3]**2) + sum((data_chopped[:,2]-grand_mean)**2))/sum(data_chopped[:,4]))                 else:                     break             if filepath == os.path.join(dirname,'modelparams.dat'):                 l = re.split(" ", ln.getline(filepath, 6))                 turb_param = float(l[2])                                 arg.append((grand_mean, grand_std, turb_param))  grandmeansdata = [] os.path.walk(os.getcwd(), loadgenomemeansize, grandmeansdata) grandmeansdata = sorted(grandmeansdata, key=lambda data_sort: data_sort[2])  themeans = p.zeros((len(grandmeansdata), 3 )) = 0 item in grandmeansdata:     themeans[i,0] = item[0]     themeans[i,1] = item[1]     themeans[i,2] = item[2]     += 1  print themeans # checking... # later computation on themeans in numpy 

it not give error massages. gives file data... data bloody wrong! checked of them manually running commands:

import pylab p data = p.genfromtxt(filepath) data_chopped = data[1000:-1,:] grand_mean = data_chopped[:,2].mean() grand_std = p.sqrt((sum(data_chopped[:,4]*data_chopped[:,3]**2) \ + sum((data_chopped[:,2]-grand_mean)**2))/sum(data_chopped[:,4])) 

on selected files. different :-(

1) can explain me what's wrong?

2) know solution that?

i'll grateful :-)

cheers, ptr

i condition not passing: if filepath == os.path.join(dirname,'generaldata.dat'):

which means not getting generaldata.dat before modelparams.dat. maybe need sort alphabetically or file not there.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -