#!/usr/bin/env python import tables, numpy # Our id type. Used often. IdType = tables.IntCol # # Classes representing metadata # class Namespace(tables.IsDescription): """Map from short string (e.g. OME) to versioned namespace (e.g. http://...)""" ns = tables.StringCol(255) namespace = tables.StringCol(16000) class Lsid(tables.IsDescription): """Map from integer to LSID string""" id = IdType() value = tables.StringCol(16000) class Group(tables.IsDescription): id = IdType() name = tables.StringCol(255) # etc. class Experimenter(tables.IsDescription): id = IdType() omeName = tables.StringCol(255) firstName = tables.StringCol(255) lastName = tables.StringCol(255) # etc. class Project(tables.IsDescription): """A description that has several nested columns""" id = IdType() owner = IdType() group = IdType() name = tables.StringCol(255) description = tables.StringCol(255) class Dataset(tables.IsDescription): """A description that has several nested columns""" id = IdType() owner = IdType() group = IdType() name = tables.StringCol(255) description = tables.StringCol(255) class Image(tables.IsDescription): """A description that has several nested columns""" id = IdType() owner = IdType() group = IdType() name = tables.StringCol(255) description = tables.StringCol(255) creationDate = tables.IntCol() class Pixels(tables.IsDescription): """A sub-structure of Test""" id = IdType() owner = IdType() group = IdType() sizex = tables.IntCol() sizey = tables.IntCol() sizez = tables.IntCol() sizet = tables.IntCol() sizec = tables.IntCol() order = tables.StringCol(16) # Should be enum def insert(table, **kwargs): """Simple function to insert values into tables""" for k,v in kwargs.iteritems(): table.row[k] = v table.row.append() if __name__ == "__main__": filename = "example.h5" h5file = tables.openFile(filename, mode = "w", title = "Example OME-HDF File") try: root = h5file.root ns = h5file.createTable(root, "Namespace", Namespace) insert(ns, ns="OME", namespace="http://www.openmicroscopy.org/Schemas/OME/2007-06") insert(ns, ns="Vendor1", namespace="http://www.example1.com") insert(ns, ns="Vendor2", namespace="http://www.example2.com") # Spaces for other vendors vr1 = h5file.createGroup(root, 'Vendor1', 'Data for Vendor1') vr2 = h5file.createGroup(root, 'Vendor2', 'Data for Vendor2') # ... # Main OME namespace ome = h5file.createGroup(root, 'OME', 'OME Group') lsid = h5file.createTable(ome,'Lsid', Lsid) grp = h5file.createTable(ome,'Group', Group) exp = h5file.createTable(ome,'Experimenter', Experimenter) dst = h5file.createTable(ome,'Project', Project) prj = h5file.createTable(ome,'Dataset', Dataset) img = h5file.createTable(ome,'Image', Image) pix = h5file.createTable(ome,'Pixel', Pixels) insert(lsid, id=0, value="urn:lsid:Pixels:0") insert(lsid, id=1, value="urn:lsid:Pixels:2") insert(pix, id=0, sizex=10, sizey=10, sizez=3, sizec=1, sizet=1, order="XYZCT") insert(pix, id=1, sizex=5, sizey=5, sizez=3, sizec=1, sizet=1, order="XYZCT") # Data data = h5file.createGroup(ome, 'Data', 'Binary Data') pixels = h5file.createGroup(data, 'Pixels', 'Pixels Binary Data') acquired = h5file.createCArray(pixels, 'id_0', tables.IntAtom(), [1,1,3,10,10], title="Original, acquired (example) data") subsample = h5file.createCArray(pixels, 'id_1', tables.IntAtom(), [1,1,3,5,5], title="Subsample") subsample.attrs.relatedTo = 0 # Actually setting values. Done elsewhere. subsample[0][0][0][0][0] = 5 finally: h5file.close() # # Working with the data # import exceptions h5file = tables.openFile(filename) Root = h5file.root try: Namespace = Root.Namespace except tables.exceptions.NoSuchNodeException: raise exceptions.Exception("No /Namespace group. Invalid OME-HDF file.") ns = None for row in Namespace: if row["namespace"] == "http://www.openmicroscopy.org/Schemas/OME/2007-06": ns = row["ns"] if ns == None: raise exceptions.Exception("No OME namespace found. Invalid OME-HDF file. Exiting") OME = Root.__getattr__(ns) pixId = OME.Pixel[0]["id"] # Default because first in array binary = OME.Data.Pixels.__getattr__("id_"+str(pixId)) # Could look nicer print binary[0][0][0]