From: Andre' Walker-L. <wal...@gm...> - 2012-06-14 21:30:13
|
Hi Anthony, On Jun 14, 2012, at 11:30 AM, Anthony Scopatz wrote: > On Wed, Jun 13, 2012 at 8:23 PM, Andre' Walker-Loud <wal...@gm...> wrote: > Hi All, > > Still trying to sort out a recursive walk through an hdf5 file using pytables. > > I have an hdf5 file with an unknown depth of groups/nodes. > > I am trying to write a little function to walk down the tree (with user input help) until a data file is found. > > I am hoping there is some function one can use to query whether you have found simply a group/node or an actual numpy array of data. So I can do something like > > if f.getNode('/',some_path) == "data_array": > return f.getNode('/',some_path), True > else: > return f.getNode('/',some_path), False > > where I have some function that if the second returned variable is True, will recognize the file as data, where as if it is False, it will query the user for a further path down the tree. > > > I suppose I could set this up with a try: except: but was hoping there is some built in functionality to handle this. > > Yup, I think that you are looking for the File.walkNodes() method. http://pytables.github.com/usersguide/libref.html#tables.File.walkNodes I wasn't sure how to use walkNodes in an interactive search. Here is what I came up with so far (it works on test cases I have given it). Comments are welcome. One feature I would like to add to the while loop in the second function is an iterator counting the depth of the search. I want to compare this to the maximum tree/node/group depth in the file, so if the search goes over (maybe my collaborators used createTable instead of createArray) the while loop won't run forever. Is there a function to ask the deepest recursion into the hdf5 file? Cheers, Andre def is_array(file,path): data = file.getNode(path) if str(type(data)) == "<class 'tables.array.Array'>": found_array = True else: found_array = False for g in file.getNode(path): print g return data, found_array def pytable_walk(file): found_data = False path = '' while found_data == False: for g in file.getNode('/',path): print g path_new = raw_input('which node would you like?\n ') path = path+'/'+path_new data,found_data = is_array(file,path) return path,data |