Advertisement
Guest User

pyqt test with matplotlib and 3D

a guest
Apr 22nd, 2010
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from __future__ import with_statement
  4. import h5py
  5. import cPickle as pickle
  6.  
  7. class h5creator:
  8.     """ Creates the mapping, pickles it and then creates a hdf5 project.
  9.    Remember to close the file after using.
  10.  
  11.    """
  12.     def create(self, *args):
  13.         arglist = []
  14.         for i, arg in enumerate(args):
  15.             arg[1] = [i, arg[1]] # insert index
  16.             arglist.append(arg)
  17.         with open('hdf_mapping','w') as f:
  18.             pickle.dump(arglist, f)
  19.  
  20.         dims = []
  21.         for k, v in args:
  22.             dims.append(len(v[1]))
  23.         dimensions = tuple(dims)
  24.         f = h5py.File('project.hdf5', 'w')
  25.         dset = f.create_dataset('Project Data', (dims))
  26.         return f, dset
  27.        
  28.     """ Reads the hdf mapping and returns it as a dictionary
  29.  
  30.    """
  31.     def read(self):
  32.         with open('hdf_mapping', 'rb') as f:
  33.             args = pickle.load(f)
  34.             hdf_mapping = {}
  35.             for k, v in args:
  36.                 hdf_mapping[k] = v
  37.  
  38.         return hdf_mapping
  39.  
  40.     """ Returns the mapped index of the key within the hdf data cube
  41.    and the index of the value within that.
  42.    
  43.    """
  44.     def index(self, key, value, mapping=None):
  45.         if not mapping:
  46.             mapping = self.read()
  47.         return mapping[key][0], mapping[key][1].index(value)
  48.  
  49.     def getdata(self, hdf_file, dset, items, mapping=None):
  50.         indices = []
  51.         for i in xrange(0, len(dset.shape)):
  52.             indices.append(slice(None, None, None))
  53.         for k, v in items.iteritems():
  54.             dim, index = self.index(k, v, mapping)
  55.             indices[dim] = index
  56.         return indices
  57.  
  58. if __name__ == "__main__":
  59.     h = h5creator()
  60.     f, dset = h.create(['first',[1, 2, 7.0]], ['second',['a', 'b', 'c']],
  61.             ['test',[1.0, 2.0, 3.0]], ['another',[1, 2.0, '3', 4]])
  62.  
  63.     indexDict = h.getdata(f, dset, {'test':3.0, 'another':1})
  64.    
  65.     print dset.__getitem__(tuple(indexDict))
  66.     f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement