def test_last_novalue(self): fun = sys._getframe().f_code.co_name print("Run: %s.%s() " % (self.__class__.__name__, fun)) fname = "test.h5" nxFile = None eFile = None # gname = "testGroup" # gtype = "NXentry" # fdname = "testField" # fdtype = "int64" # file handle nxFile = FileWriter.create_file(fname, overwrite=True).root() # element file objects eFile = EFile([], None, nxFile) el = Element(self._tfname, self._fattrs2, eFile) fi = EField(self._fattrs2, el) el2 = EDimensions(self._fattrs4, fi) el3 = EDim(self._attrs3, el2) self.assertEqual(fi.tagName, "field") self.assertEqual(fi.content, []) self.assertEqual(fi._tagAttrs, self._fattrs2) self.assertEqual(fi.doc, "") self.assertEqual(fi._lastObject(), None) self.assertEqual(type(el2.last), EField) self.assertEqual(el2.last.rank, "1") self.assertEqual(el3._beforeLast().lengths, {}) self.assertEqual(fi.lengths, {}) self.assertEqual(fi.rank, "1") nxFile.close() os.remove(fname)
def test_store_default(self): fun = sys._getframe().f_code.co_name print("Run: %s.%s() " % (self.__class__.__name__, fun)) self._fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__, fun) FileWriter.writer = H5CppWriter self._nxFile = FileWriter.create_file(self._fname, overwrite=True).root() eFile = EFile({}, None, self._nxFile) el = EGroup(self._gattrs, eFile) self.assertTrue(isinstance(el, Element)) self.assertTrue(isinstance(el, FElement)) self.assertTrue(isinstance(el, FElementWithAttr)) self.assertEqual(el.tagName, "group") self.assertEqual(el.content, []) el.store() self.assertEqual(el.h5Object.name, self._gattrs["name"]) self.assertEqual(len(el.h5Object.attributes), 1) self.assertEqual(el.h5Object.attributes["NX_class"][...], self._gattrs["type"]) # self.myAssertRaise(ValueError, el.store) self._nxFile.close() os.remove(self._fname)
def test_fetchName_notype(self): fun = sys._getframe().f_code.co_name print("Run: %s.%s() " % (self.__class__.__name__, fun)) self._fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__, fun) FileWriter.writer = H5CppWriter self._nxFile = FileWriter.create_file(self._fname, overwrite=True).root() eFile = EFile({}, None, self._nxFile) gattrs = {"type": "NXentry"} el = EGroup(gattrs, eFile) self.assertTrue(isinstance(el, Element)) self.assertTrue(isinstance(el, FElement)) self.assertTrue(isinstance(el, FElementWithAttr)) self.assertEqual(el.tagName, "group") self.assertEqual(el.content, []) self.assertEqual(type(el.h5Object), H5CppWriter.H5CppGroup) self.assertEqual(el.h5Object.name, gattrs["type"][2:]) self.assertEqual(len(el.h5Object.attributes), 1) self.assertEqual(el.h5Object.attributes["NX_class"][...], gattrs["type"]) self.assertEqual(el.h5Object.attributes["NX_class"].dtype, "string") self.assertEqual(el.h5Object.attributes["NX_class"].shape, ()) # gNames = {} el._tagAttrs.pop("type") self._nxFile.close() os.remove(self._fname)
def test_store_1d_single(self): fun = sys._getframe().f_code.co_name print("Run: %s.%s() " % (self.__class__.__name__, fun)) self._fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__, fun) FileWriter.writer = H5CppWriter self._nxFile = FileWriter.create_file(self._fname, overwrite=True).root() eFile = EFile({}, None, self._nxFile) el = EGroup(self._gattrs, eFile) self.assertEqual(el.tagAttributes, {}) attrs = { # "string":["My string","NX_CHAR", "string" , (1,)], # "datetime":["12:34:34","NX_DATE_TIME", "string", (1,) ], # "iso8601":["12:34:34","ISO8601", "string", (1,)], "int": [-123, "NX_INT", "int64", (1, )], "int8": [12, "NX_INT8", "int8", (1, )], "int16": [-123, "NX_INT16", "int16", (1, )], "int32": [12345, "NX_INT32", "int32", (1, )], "int64": [-12345, "NX_INT64", "int64", (1, )], "uint": [123, "NX_UINT", "uint64", (1, )], "uint8": [12, "NX_UINT8", "uint8", (1, )], "uint16": [123, "NX_UINT16", "uint16", (1, )], "uint32": [12345, "NX_UINT32", "uint32", (1, )], "uint64": [12345, "NX_UINT64", "uint64", (1, )], "float": [-12.345, "NX_FLOAT", "float64", (1, ), 1.e-14], "number": [-12.345e+2, "NX_NUMBER", "float64", (1, ), 1.e-14], "float32": [-12.345e-1, "NX_FLOAT32", "float32", (1, ), 1.e-5], "float64": [-12.345, "NX_FLOAT64", "float64", (1, ), 1.e-14], "bool": [True, "NX_BOOLEAN", "bool", (1, )], "bool2": ["FaLse", "NX_BOOLEAN", "bool", (1, )], "bool3": ["false", "NX_BOOLEAN", "bool", (1, )], "bool4": ["true", "NX_BOOLEAN", "bool", (1, )] } for nm in attrs.keys(): if attrs[nm][2] == 'string': "writing multi-dimensional string is not supported by pninx" continue el.tagAttributes[nm] = (attrs[nm][1], str(attrs[nm][0]), attrs[nm][3]) el.store() at = el.h5Object.attributes[nm] self.assertEqual(at.dtype, attrs[nm][2]) if attrs[nm][2] == "bool": self.assertEqual(Converters.toBool(str(attrs[nm][0])), at[...]) elif len(attrs[nm]) > 4: self.assertTrue(abs(at[...] - attrs[nm][0]) <= attrs[nm][4]) else: if isinstance(at[...], numpy.ndarray): self.assertEqual( at[...], numpy.array(attrs[nm][0], dtype=attrs[nm][2])) else: self.assertEqual(at[...], attrs[nm][0]) self._nxFile.close() os.remove(self._fname)
def test_default_constructor_thesame_name(self): fun = sys._getframe().f_code.co_name print("Run: %s.%s() " % (self.__class__.__name__, fun)) self._fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__, fun) FileWriter.writer = H5CppWriter self._nxFile = FileWriter.create_file(self._fname, overwrite=True).root() eFile = EFile({}, None, self._nxFile) el = EGroup(self._gattrs, eFile) self.assertTrue(isinstance(el, Element)) self.assertTrue(isinstance(el, FElement)) self.assertTrue(isinstance(el, FElementWithAttr)) self.assertEqual(el.tagName, "group") self.assertEqual(el.content, []) self.assertEqual(type(el.h5Object), H5CppWriter.H5CppGroup) self.assertEqual(el.h5Object.name, self._gattrs["name"]) self.assertEqual(len(el.h5Object.attributes), 1) self.assertEqual(el.h5Object.attributes["NX_class"][...], self._gattrs["type"]) self.assertEqual(el.h5Object.attributes["NX_class"].dtype, "string") self.assertEqual(el.h5Object.attributes["NX_class"].shape, ()) self.assertEqual(el.h5Object.attributes["NX_class"].shape, ()) self.myAssertRaise(XMLSettingSyntaxError, EGroup, self._gattrs, eFile) self._nxFile.close() os.remove(self._fname)
def test_lastObject_hp5y(self): fun = sys._getframe().f_code.co_name print("Run: %s.%s() " % (self.__class__.__name__, fun)) self._fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__, fun) FileWriter.writer = H5CppWriter fname = self._fname nxFile = None eFile = None gname = "testGroup" gtype = "NXentry" fdname = "testField" fdtype = "int64" # file handle nxFile = FileWriter.create_file(fname, overwrite=True).root() # element file objects eFile = EFile([], None, nxFile) group = nxFile.create_group(gname, gtype) group.create_field(fdname, fdtype) el = Element(self._tfname, self._fattrs, eFile) el2 = Element(self._tfname, self._fattrs, el) self.assertEqual(el.tagName, self._tfname) self.assertEqual(el.content, []) self.assertEqual(el._tagAttrs, self._fattrs) self.assertEqual(el.doc, "") self.assertEqual(el._lastObject(), nxFile) self.assertEqual(el2._lastObject(), None) nxFile.close() os.remove(fname)
def test_last_h5cpp(self): print("Run: %s.test_last() " % self.__class__.__name__) fname = "test.h5" nxFile = None eFile = None # gname = "testGroup" # gtype = "NXentry" # fdname = "testField" # fdtype = "int64" # file handle FileWriter.writer = H5CppWriter nxFile = FileWriter.create_file(fname, overwrite=True).root() # element file objects eFile = EFile([], None, nxFile) el = Element(self._tfname, self._fattrs2, eFile) fi = EField(self._fattrs3, el) el2 = EDimensions(self._fattrs3, fi) self.assertEqual(fi.tagName, "field") self.assertEqual(fi.content, []) self.assertEqual(fi._tagAttrs, self._fattrs3) self.assertEqual(fi.doc, "") self.assertEqual(fi._lastObject(), None) self.assertEqual(type(el2.last), EField) self.assertEqual(el2.last.rank, "2") nxFile.close() os.remove(fname)
def createTree(self): # file handle FileWriter.writer = H5PYWriter self._nxFile = FileWriter.create_file(self._fname, overwrite=True).root() # element file objects self._group = self._nxFile.create_group(self._gname, self._gtype) self._field = self._group.create_field(self._fdname, self._fdtype)
def setUp(self): # file handle FileWriter.writer = H5CppWriter self._nxFile = FileWriter.create_file(self._fname, overwrite=True) self._nxroot = self._nxFile.root() # element file objects self._group = self._nxroot.create_group(self._gname, self._gtype) self._field = self._group.create_field(self._fdname, self._fdtype) print("\nsetting up...")
def test_store_last_index2(self): fun = sys._getframe().f_code.co_name print("Run: %s.%s() " % (self.__class__.__name__, fun)) fname = "test.h5" nxFile = None eFile = None # gname = "testGroup" # gtype = "NXentry" # fdname = "testField" # fdtype = "int64" # file handle nxFile = FileWriter.create_file(fname, overwrite=True).root() # element file objects eFile = EFile([], None, nxFile) el = Element(self._tfname, self._fattrs2, eFile) fi = EField(self._fattrs2, el) el2 = EDimensions(self._fattrs3, fi) el3 = EDim(self._attrs5, el2) ds = TstDataSource() ds.value0d = self.__rnd.randint(1, 10) el3.source = ds el4 = EDim(self._attrs4, el2) ds2 = TstDataSource() ds2.value0d = self.__rnd.randint(1, 10) el4.source = ds2 el3.store() el4.store() self.assertEqual(fi.tagName, "field") self.assertEqual(fi.content, []) self.assertEqual(fi._tagAttrs, self._fattrs2) self.assertEqual(fi.doc, "") self.assertEqual(fi._lastObject(), None) self.assertEqual(type(el2.last), EField) self.assertEqual(el2.last.rank, "2") self.assertEqual(el3._beforeLast().lengths, { '1': '%s' % ds.value0d, '2': '%s' % ds2.value0d }) self.assertEqual(fi.lengths, { '1': '%s' % ds.value0d, '2': '%s' % ds2.value0d }) self.assertEqual(fi.rank, "2") nxFile.close() os.remove(fname)
def test_constructor_aTn(self): fun = sys._getframe().f_code.co_name print("Run: %s.%s() " % (self.__class__.__name__, fun)) self._fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__, fun) FileWriter.writer = H5CppWriter self._nxFile = FileWriter.create_file(self._fname, overwrite=True).root() eFile = EFile({}, None, self._nxFile) gattrs = {"type": "NXentry", "name": "shortname"} # map of tag attribute types maTn = { "signal": 1, "axis": 2, "primary": 3, "offset": 4, "stride": 6, "file_time": "12:34", "file_update_time": "12:45", "restricts": 12, "ignoreExtraGroups": True, "ignoreExtraFields": False, "ignoreExtraAttributes": True, "minOccus": 1, "maxOccus": 2 } gattrs = dict(gattrs, **(maTn)) el = EGroup(gattrs, eFile) self.assertTrue(isinstance(el, Element)) self.assertTrue(isinstance(el, FElement)) self.assertTrue(isinstance(el, FElementWithAttr)) self.assertEqual(el.tagName, "group") self.assertEqual(el.content, []) self.assertEqual(type(el.h5Object), H5CppWriter.H5CppGroup) self.assertEqual(el.h5Object.name, gattrs["name"]) self.assertEqual(len(el.h5Object.attributes), 14) self.assertEqual(el.h5Object.attributes["NX_class"][...], gattrs["type"]) self.assertEqual(el.h5Object.attributes["NX_class"].dtype, "string") self.assertEqual(el.h5Object.attributes["NX_class"].shape, ()) for k in maTn.keys(): self.assertEqual(el.h5Object.attributes[k][...], gattrs[k]) self.assertEqual(el.h5Object.attributes[k].dtype, NTP.nTnp[NTP.aTn[k]]) self.assertEqual(el.h5Object.attributes[k].shape, ()) self._nxFile.close() os.remove(self._fname)
def test_constructor_notype(self): fun = sys._getframe().f_code.co_name print("Run: %s.%s() " % (self.__class__.__name__, fun)) self._fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__, fun) FileWriter.writer = H5CppWriter self._nxFile = FileWriter.create_file(self._fname, overwrite=True).root() eFile = EFile({}, None, self._nxFile) gattrs = {"short_name": "shortname"} self.myAssertRaise(XMLSettingSyntaxError, EGroup, gattrs, eFile) self._nxFile.close() os.remove(self._fname)
def test_constructor_aTnv(self): fun = sys._getframe().f_code.co_name print("Run: %s.%s() " % (self.__class__.__name__, fun)) self._fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__, fun) FileWriter.writer = H5CppWriter self._nxFile = FileWriter.create_file(self._fname, overwrite=True).root() eFile = EFile({}, None, self._nxFile) gattrs = {"type": "NXentry", "name": "shortname"} # map of tag attribute types maTnv = {"vector": "1 2 3 4 5"} raTnv = {"vector": [1, 2, 3, 4, 5]} gattrs = dict(gattrs, **(maTnv)) rattrs = dict(gattrs) rattrs = dict(rattrs, **(raTnv)) error = 1.e-14 el = EGroup(gattrs, eFile) self.assertTrue(isinstance(el, Element)) self.assertTrue(isinstance(el, FElement)) self.assertTrue(isinstance(el, FElementWithAttr)) self.assertEqual(el.tagName, "group") self.assertEqual(el.content, []) self.assertEqual(type(el.h5Object), H5CppWriter.H5CppGroup) self.assertEqual(el.h5Object.name, gattrs["name"]) self.assertEqual(len(el.h5Object.attributes), 2) self.assertEqual(el.h5Object.attributes["NX_class"][...], gattrs["type"]) self.assertEqual(el.h5Object.attributes["NX_class"].dtype, "string") self.assertEqual(el.h5Object.attributes["NX_class"].shape, ()) for k in raTnv.keys(): for i in range(len(rattrs[k])): self.assertTrue( abs(el.h5Object.attributes[k][i] - rattrs[k][i]) <= error) self.assertEqual(el.h5Object.attributes[k].dtype, NTP.nTnp[NTP.aTnv[k]]) self.assertEqual(el.h5Object.attributes[k].shape, (len(rattrs[k]), )) self._nxFile.close() os.remove(self._fname)
def test_constructor_nx(self): fun = sys._getframe().f_code.co_name print("Run: %s.%s() " % (self.__class__.__name__, fun)) self._fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__, fun) self._nxFile = FileWriter.create_file(self._fname, overwrite=True).root() el = EFile({}, None, self._nxFile) self.assertTrue(isinstance(el, Element)) self.assertTrue(isinstance(el, FElement)) self.assertEqual(el.tagName, 'file') self.assertEqual(el.content, []) self.assertEqual(el.doc, "") self.assertEqual(el.source, None) self.assertEqual(el.error, None) self.assertEqual(el._tagAttrs, {}) self.assertEqual(type(el.h5Object), H5PYWriter.H5PYGroup) self._nxFile.close() os.remove(self._fname)
def openFile(self): """ the H5 file opening :brief: It opens the H5 file """ try: self.closeFile() except Exception: self._streams.warn( "TangoDataWriter::openFile() - File cannot be closed") wrmodule = self.__setWriter(self.writer) self.__nxFile = None self.__eFile = None self.__initPool = None self.__stepPool = None self.__finalPool = None self.__triggerPools = {} self.__currentfileid = 0 self.__pars = self.__getParams(self.writer) pars = dict(self.__pars) pars["writer"] = wrmodule if os.path.isfile(self.__fileName): self.__nxFile = FileWriter.open_file(self.__fileName, False, **pars) else: self.__nxFile = FileWriter.create_file(self.__fileName, **pars) self.__fileprefix, self.__fileext = os.path.splitext( str(self.__fileName)) self.__nxRoot = self.__nxFile.root() self.__nxRoot.stepsperfile = self.stepsperfile self.__nxRoot.currentfileid = self.__currentfileid #: element file objects self.__eFile = EFile([], None, self.__nxRoot) last = self.__eFile for gname, gtype in self.__parents: last = EGroup({ "name": gname, "type": gtype }, last, reloadmode=True) self.__nxPath.append(last) if self.addingLogs: name = "nexus_logs" if not self.__nxRoot.exists(name): ngroup = self.__nxRoot.create_group(name, "NXcollection") else: ngroup = self.__nxRoot.open(name) name = "configuration" error = True counter = 1 cname = name while error: cname = name if counter == 1 else \ ("%s_%s" % (name, counter)) if not ngroup.exists(cname): error = False else: counter += 1 self.__logGroup = ngroup.create_group(cname, "NXcollection") vfield = self.__logGroup.create_field("python_version", "string") vfield.write(str(sys.version)) vfield.close()
def test_store_2d_single(self): fun = sys._getframe().f_code.co_name print("Run: %s.%s() " % (self.__class__.__name__, fun)) self._fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__, fun) FileWriter.writer = H5CppWriter self._nxFile = FileWriter.create_file(self._fname, overwrite=True).root() eFile = EFile({}, None, self._nxFile) el = EGroup(self._gattrs, eFile) self.assertEqual(el.tagAttributes, {}) attrs = { # "string":["My string","NX_CHAR", "string" , (1,)], # "datetime":["12:34:34","NX_DATE_TIME", "string", (1,) ], # "iso8601":["12:34:34","ISO8601", "string", (1,)], "int": [-123, "NX_INT", "int64", (1, )], "int8": [12, "NX_INT8", "int8", (1, )], "int16": [-123, "NX_INT16", "int16", (1, )], "int32": [12345, "NX_INT32", "int32", (1, )], "int64": [-12345, "NX_INT64", "int64", (1, )], "uint": [123, "NX_UINT", "uint64", (1, )], "uint8": [12, "NX_UINT8", "uint8", (1, )], "uint16": [123, "NX_UINT16", "uint16", (1, )], "uint32": [12345, "NX_UINT32", "uint32", (1, )], "uint64": [12345, "NX_UINT64", "uint64", (1, )], "float": [-12.345, "NX_FLOAT", "float64", (1, ), 1.e-14], "number": [-12.345e+2, "NX_NUMBER", "float64", (1, ), 1.e-14], "float32": [-12.345e-1, "NX_FLOAT32", "float32", (1, ), 1.e-5], "float64": [-12.345, "NX_FLOAT64", "float64", (1, ), 1.e-14], "bool": [True, "NX_BOOLEAN", "bool", (1, )], "bool2": ["FaLse", "NX_BOOLEAN", "bool", (1, )], "bool3": ["false", "NX_BOOLEAN", "bool", (1, )], "bool4": ["true", "NX_BOOLEAN", "bool", (1, )] } for nm in attrs.keys(): if attrs[nm][2] != "bool": mlen = [ self.__rnd.randint(1, 1), self.__rnd.randint(1, 1), (2 << numpy.dtype(attrs[nm][2]).itemsize) ] # print "SH",nm,mlen[2] attrs[nm][0] = [[ attrs[nm][0] * self.__rnd.randint(0, 3) for r in range(mlen[1]) ] for c in range(mlen[0])] else: mlen = [self.__rnd.randint(1, 1), self.__rnd.randint(1, 1)] if nm == 'bool': attrs[nm][0] = [[ bool(self.__rnd.randint(0, 1)) for c in range(mlen[1]) ] for r in range(mlen[0])] else: attrs[nm][0] = [[ ("True" if self.__rnd.randint(0, 1) else "False") for c in range(mlen[1]) ] for r in range(mlen[0])] attrs[nm][3] = (mlen[0], mlen[1]) for nm in attrs.keys(): el.tagAttributes[nm] = (attrs[nm][1], "".join([ "".join([str(it) + " " for it in sub]) + "\n" for sub in attrs[nm][0] ]), attrs[nm][3]) el.store() at = el.h5Object.attributes[nm] self.assertEqual(at.dtype, attrs[nm][2]) if attrs[nm][2] == "bool": for i in range(len(attrs[nm][0])): for j in range(len(attrs[nm][0][i])): self.assertEqual( Converters.toBool(str(attrs[nm][0][i][j])), at[...]) pass elif len(attrs[nm]) > 4: for i in range(len(attrs[nm][0])): for j in range(len(attrs[nm][0][i])): self.assertTrue( abs(at[...] - attrs[nm][0][i][j]) <= attrs[nm][4]) else: for i in range(len(attrs[nm][0])): for j in range(len(attrs[nm][0][i])): self.assertEqual(at[...], attrs[nm][0][i][j]) self._nxFile.close() os.remove(self._fname)