Exemplo n.º 1
0
    def test_createAttributes_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)
        self.createTree()
        el = FElement(self._tfname, self._fattrs, None)
        el2 = FElementWithAttr(self._tfname, self._fattrs, el, self._group)
        self.assertEqual(el2.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
            el2.tagAttributes[nm] = (attrs[nm][1], str(attrs[nm][0]),
                                     attrs[nm][3])
            el2._createAttributes()
            at = el2.h5Attribute(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])
Exemplo n.º 2
0
 def test_default_constructor(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.createTree()
     el = FElementWithAttr(self._tfname, self._fattrs, None)
     self.assertTrue(isinstance(el, Element))
     self.assertTrue(isinstance(el, FElement))
     self.assertTrue(isinstance(el, FElementWithAttr))
     self.assertEqual(el.tagName, self._tfname)
     self.assertEqual(el.content, [])
     self.assertEqual(el.doc, "")
     self.assertEqual(el.source, None)
     self.assertEqual(el.error, None)
     self.assertEqual(el.h5Object, None)
     self.assertEqual(el.tagAttributes, {})
Exemplo n.º 3
0
    def test_createAttributes_2d_X1(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.createTree()
        el = FElement(self._tfname, self._fattrs, None)
        el2 = FElementWithAttr(self._tfname, self._fattrs, el, self._group)
        self.assertEqual(el2.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(2, 10), 1]
                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(2, 10), 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():
            el2.tagAttributes[nm] = (attrs[nm][1], "".join([
                "".join([str(it) + " " for it in sub]) + "\n"
                for sub in attrs[nm][0]
            ]), attrs[nm][3])
            el2._createAttributes()
            at = el2.h5Attribute(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[i,
                                                                           j])
                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[i, j] - 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[i, j], attrs[nm][0][i][j])