Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from __future__ import with_statement
- import h5py
- import cPickle as pickle
- class h5creator:
- """ Creates the mapping, pickles it and then creates a hdf5 project.
- Remember to close the file after using.
- """
- def create(self, *args):
- arglist = []
- for i, arg in enumerate(args):
- arg[1] = [i, arg[1]] # insert index
- arglist.append(arg)
- with open('hdf_mapping','w') as f:
- pickle.dump(arglist, f)
- dims = []
- for k, v in args:
- dims.append(len(v[1]))
- dimensions = tuple(dims)
- f = h5py.File('project.hdf5', 'w')
- dset = f.create_dataset('Project Data', (dims))
- return f, dset
- """ Reads the hdf mapping and returns it as a dictionary
- """
- def read(self):
- with open('hdf_mapping', 'rb') as f:
- args = pickle.load(f)
- hdf_mapping = {}
- for k, v in args:
- hdf_mapping[k] = v
- return hdf_mapping
- """ Returns the mapped index of the key within the hdf data cube
- and the index of the value within that.
- """
- def index(self, key, value, mapping=None):
- if not mapping:
- mapping = self.read()
- return mapping[key][0], mapping[key][1].index(value)
- def getdata(self, hdf_file, dset, items, mapping=None):
- indices = []
- for i in xrange(0, len(dset.shape)):
- indices.append(slice(None, None, None))
- for k, v in items.iteritems():
- dim, index = self.index(k, v, mapping)
- indices[dim] = index
- return indices
- if __name__ == "__main__":
- h = h5creator()
- f, dset = h.create(['first',[1, 2, 7.0]], ['second',['a', 'b', 'c']],
- ['test',[1.0, 2.0, 3.0]], ['another',[1, 2.0, '3', 4]])
- indexDict = h.getdata(f, dset, {'test':3.0, 'another':1})
- print dset.__getitem__(tuple(indexDict))
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement