Example #1
0
    def setUp(self):
        self.temp = TempFile()

        ad.init_noxml()

        ad.allocate_buffer(ad.BUFFER_ALLOC_WHEN.NOW, 10)
        g = ad.declare_group("temperature", "", ad.FLAG.YES)
        ad.define_var(g, "NX", "", ad.DATATYPE.integer, "", "", "")
        ad.define_var(g, "size", "", ad.DATATYPE.integer, "", "", "")
        ad.define_var(g, "temperature", "", ad.DATATYPE.double, "size,NX",
                      "size,NX", "0,0")
        self.msg = "this is a test"
        self.unit = "C"
        ## attr must be <varpath> + / + something without / (line 857, common_read.c)
        ad.define_attribute(g, "temperature/desc", "", ad.DATATYPE.string,
                            self.msg, "")
        ad.define_attribute(g, "temperature/unit", "", ad.DATATYPE.string,
                            self.unit, "")
        ad.select_method(g, "POSIX1", "verbose=3", "")

        fd = ad.open("temperature", self.temp.path, "w")
        self.NX = 10
        self.size = 2
        groupsize = 4 + 4 + 8 * self.size * self.NX
        t = np.array(range(self.NX * self.size), dtype=np.float64)
        self.tt = t.reshape((self.size, self.NX))
        ad.set_group_size(fd, groupsize)
        ad.write_int(fd, "NX", self.NX)
        ad.write_int(fd, "size", self.size)
        ad.write(fd, "temperature", self.tt)
        ad.close(fd)

        ad.finalize()

        self.f = ad.file(self.temp.path)
Example #2
0
    def test_writer_default_group(self):
        self.temp = TempFile()
        fw = ad.writer(self.temp.path, method="POSIX1")
        fw.close()

        f = ad.file(self.temp.path)
        f.close()
Example #3
0
    def setUp(self):
        self.temp = TempFile()

        ad.init_noxml()

        g = ad.declare_group("temperature", "", ad.FLAG.YES)
        ad.define_var(g, "NX", "", ad.DATATYPE.integer, "", "", "")
        ad.define_var(g, "size", "", ad.DATATYPE.integer, "", "", "")
        ad.define_var(g, "temperature", "", ad.DATATYPE.double, "size,NX",
                      "size,NX", "0,0")
        self.msg = "this is a test"
        ad.define_attribute(g, "desc", "", ad.DATATYPE.string, self.msg, "")
        ad.select_method(g, "POSIX1", "verbose=3", "")

        for i in range(5):
            mode = "a"
            if i == 0: mode = "w"
            fd = ad.open("temperature", self.temp.path, mode)
            self.NX = 10
            self.size = 2
            ##groupsize =  4 + 4 + 8 * self.size * self.NX
            t = np.array(list(range(self.NX * self.size)),
                         dtype=np.float64) + 100 * i
            self.tt = t.reshape((self.size, self.NX))
            ##ad.set_group_size(fd, groupsize)
            ad.write_int(fd, "NX", self.NX)
            ad.write_int(fd, "size", self.size)
            ad.write(fd, "temperature", self.tt)
            ad.close(fd)

        ad.finalize()

        self.f = ad.file(self.temp.path)
    def test_simple1(self):
        self.temp = TempFile()

        NX = 10
        size = 2
        t = np.array(list(range(NX * size)), dtype=np.float64)
        tt = t.reshape((size, NX))

        fw = ad.writer(self.temp.path)
        fw.declare_group('group', method='POSIX1')

        fw['NX'] = NX
        fw['size'] = size
        fw['temperature'] = tt
        fw.attrs[
            '/temperature/description'] = "Global array written from 'size' processes"
        fw.attrs["/someSubGroup/anOtherGroup/anOtherAttribute"] = 99
        fw["/someSubGroup/anOtherGroup/anOtherVariable"] = 77
        fw.close()

        f = ad.file(self.temp.path)
        t = f["temperature"]
        # here t could have a member dictionary attr(s) again
        # which looks up all attributes starting with t.name

        # now match: f.attrs["/temperature/description"]
        self.assertEqual(t.attrs["description"],
                         f.attrs["/temperature/description"])

        # the same should be possible for groups
        g = f["/someSubGroup/anOtherGroup"]

        # now match: f.attrs["/someSubGroup/anOtherGroup/anOtherAttribute"]
        self.assertEqual(
            g.attrs["anOtherAttribute"],
            f.attrs["/someSubGroup/anOtherGroup/anOtherAttribute"])

        # now match: f["/someSubGroup/anOtherGroup/anOtherVariable"]
        self.assertEqual(g["anOtherVariable"],
                         f["/someSubGroup/anOtherGroup/anOtherVariable"])

        # Missing '/'
        g = f["someSubGroup/anOtherGroup"]

        # now match: f.attrs["/someSubGroup/anOtherGroup/anOtherAttribute"]
        self.assertEqual(
            g.attrs["anOtherAttribute"],
            f.attrs["/someSubGroup/anOtherGroup/anOtherAttribute"])

        # now match: f["/someSubGroup/anOtherGroup/anOtherVariable"]
        self.assertEqual(g["anOtherVariable"],
                         f["/someSubGroup/anOtherGroup/anOtherVariable"])

        ## Testing name acess
        self.assertEqual(g["anOtherVariable"][...], g.anOtherVariable[...])
        self.assertEqual(g["anOtherAttribute"].value, g.anOtherAttribute.value)
    def test_simple2(self):
        self.temp = TempFile()

        NX = 10
        size = 2
        t = np.array(list(range(NX*size)), dtype=np.float64)
        tt = t.reshape((size, NX))

        fw = ad.writer(self.temp.path)
        fw.declare_group('mygroup', method='POSIX1')

        fw['/data/0/fields/FieldE/x'] = tt
        fw['/data/0/fields/FieldE/y'] = tt*2

        fw['/data/0/particles/i/globalCellIdx/x'] = t
        fw['/data/0/particles/i/globalCellIdx/y'] = t*2

        fw.attrs['/data/0/fields/FieldE/x/sim_unit'] = 77
        fw.attrs['/data/0/fields/FieldE/y/sim_unit'] = 99
        fw.attrs['/data/0/fields/e_chargeDensity/sim_unit'] = 88

        fw.attrs['/data/0/iteration'] = 33
        fw.attrs['/data/0/sim_slides'] = 55
        fw.close()

        f = ad.file(self.temp.path)
        g = f["data/0"]
        self.assertTrue((g['fields/FieldE/x'][...] == tt).all())
        self.assertEqual(g['fields/FieldE/y/sim_unit'][...], 99)
        self.assertEqual(g['iteration'][...], 33)

        g1 = f["/data/0"]
        self.assertTrue((g1['fields/FieldE/x'][...] == tt).all())
        self.assertEqual(g1['fields/FieldE/y/sim_unit'][...], 99)
        self.assertEqual(g1['iteration'][...], 33)

        g = f["data"]
        g = f["/data"]
        g = f["/data/"]
        g = f["data/0/"]
        g = f["/data/0"]

        g2 = g["fields"]
        self.assertTrue((g2['FieldE/x'][...] == tt).all())
        self.assertTrue((g2['FieldE/y'][...] == tt*2).all())
        self.assertEqual(g2['FieldE/x/sim_unit'][...], 77)
        self.assertEqual(g2['FieldE/y/sim_unit'][...], 99)

        ## Check dirs()
        self.assertEqual(sorted(g.dirs()), sorted(['particles', 'fields']))
        self.assertEqual(sorted(g['fields'].dirs()), sorted(['e_chargeDensity', 'FieldE']))
        self.assertEqual(f.dirs(), ['data'])
    def test_softdict2(self):
        self.temp = TempFile()

        fw = ad.writer(self.temp.path)
        fw.vars['a1'] = 12
        fw.vars['/a2'] = 42
        fw.close()

        f = ad.file(self.temp.path)
        self.assertEqual(f.vars['/a1'][...], f['/a1'][...])
        self.assertEqual(f.vars['a1'][...], f['a1'][...])
        self.assertEqual(f.vars['/a2'][...], f['/a2'][...])
        self.assertEqual(f.vars['a2'][...], f['a2'][...])
Example #7
0
    def test_writer_varname(self):
        self.temp = TempFile()

        fw = ad.writer(self.temp.path, method="POSIX1")

        NVARS = 99
        fw.vars['nvars'] = NVARS
        fw.vars['/nvars'] = NVARS
        fw.vars['_nvars'] = NVARS
        fw.close()

        f = ad.file(self.temp.path)
        self.assertEqual(f.nvars, 3)
        self.assertEqual(f._nvars[...], NVARS)
Example #8
0
    def test_writer_attr(self):
        self.temp = TempFile()

        NX = 10
        val1 = np.array(list(range(NX)), dtype=np.int32)
        val2 = np.array(list(range(5)), dtype='f8')

        single_string = "ABCD"
        three_string = ("AA", "BBB", "CCCC")
        single_int = 10
        five_int = np.array(list(range(5)), dtype=np.int32)
        single_double = 1.1
        five_double = np.array(list(range(5)), dtype='double') * 1.1
        unicode_string = u"unicode"
        bytes_string = u"bytes"

        fw = ad.writer(self.temp.path)
        fw.declare_group("group", method="POSIX1")

        fw.define_attr("single_string")
        fw.define_attr("three_string")
        fw.define_attr("single_int")
        fw.define_attr("five_int")
        fw.define_attr("single_double")
        fw.define_attr("five_double")
        fw.define_attr("unicode_string")
        fw.define_attr("bytes_string")

        fw['single_string'] = single_string
        fw['three_string'] = three_string
        fw['single_int'] = single_int
        fw['five_int'] = five_int
        fw['single_double'] = single_double
        fw['five_double'] = five_double
        fw['unicode_string'] = unicode_string
        fw['bytes_string'] = bytes_string
        fw.close()

        f = ad.file(self.temp.path)
        self.assertEqual(f['single_string'].value, single_string.encode())
        ##self.assertTrue((f['three_string'].value == three_string).all())
        self.assertTrue(f['three_string'].value[0], three_string[0].encode())
        self.assertTrue(f['three_string'].value[1], three_string[1].encode())
        self.assertTrue(f['three_string'].value[2], three_string[2].encode())
        self.assertEqual(f['single_int'].value, single_int)
        self.assertTrue((f['five_int'].value == five_int).all())
        self.assertEqual(f['single_double'].value, single_double)
        self.assertTrue((f['five_double'].value == five_double).all())
        self.assertTrue(f['unicode_string'].value, unicode_string.encode())
        self.assertTrue(f['bytes_string'].value, bytes_string)
Example #9
0
    def test_writer_undefined_var(self):
        self.temp = TempFile()

        NX = 10
        val1 = np.array(list(range(NX)), dtype=np.int32)
        val2 = np.array(list(range(5)), dtype='f8')

        fw = ad.writer(self.temp.path, method="POSIX1")

        fw['NX'] = NX
        fw['val1'] = val1
        fw['val2'] = val2
        fw.close()

        f = ad.file(self.temp.path)
        self.assertEqual(f['NX'][...], NX)
        self.assertTrue((f['val1'][:] == val1).all())
        self.assertTrue((f['val2'][:] == val2).all())
Example #10
0
    def test_writer_transform(self):
        self.temp = TempFile()

        NX = 10
        val1 = np.array(list(range(NX)), dtype=np.int32)
        val2 = np.array(list(range(5)), dtype='f8')

        fw = ad.writer(self.temp.path)
        fw.declare_group("group", method="POSIX1")

        fw['NX'] = NX
        fw['val1'] = val1
        fw['val2'] = val2
        fw['val1'].transform = 'identity'
        fw.close()

        f = ad.file(self.temp.path)
        self.assertEqual(f['NX'][...], NX)
        self.assertTrue((f['val1'][:] == val1).all())
        self.assertTrue((f['val2'][:] == val2).all())
Example #11
0
    def setUp(self):
        self.temp = TempFile()

        ad.init_noxml()

        ad.allocate_buffer(ad.BUFFER_ALLOC_WHEN.NOW, 10)
        g = ad.declare_group("temperature", "", ad.FLAG.YES)
        ad.define_var(g, "NX", "", ad.DATATYPE.integer, "", "", "")
        ad.define_var(g, "size", "", ad.DATATYPE.integer, "", "", "")
        ad.define_var(g, "temperature", "", ad.DATATYPE.double, "size,NX",
                      "size,NX", "0,0")
        ad.define_var(g, "temperature", "", ad.DATATYPE.double, "size,NX",
                      "size,NX", "0,0")
        self.msg = "this is a test"
        ad.define_attribute(g, "desc", "", ad.DATATYPE.string, self.msg, "")
        ad.define_attribute(g, "temperature/unit", "", ad.DATATYPE.string, "C",
                            "")
        ad.define_attribute(g, "temperature/desc", "", ad.DATATYPE.string,
                            "description", "")
        ad.define_attribute(g, "/subgroup/subsubgroup/otherattr", "",
                            ad.DATATYPE.string, "another", "")
        ad.define_var(g, "/subgroup/subsubgroup/othervar", "",
                      ad.DATATYPE.integer, "", "", "")
        ad.select_method(g, "POSIX1", "verbose=3", "")

        fd = ad.open("temperature", self.temp.path, "w")
        self.NX = 10
        self.size = 2
        groupsize = 4 + 4 + 8 * self.size * self.NX + 4
        t = np.array(range(self.NX * self.size), dtype=np.float64)
        self.tt = t.reshape((self.size, self.NX))
        ad.set_group_size(fd, groupsize)
        ad.write_int(fd, "NX", self.NX)
        ad.write_int(fd, "size", self.size)
        ad.write(fd, "temperature", self.tt)
        ad.write_int(fd, "/subgroup/subsubgroup/othervar", 99)
        ad.close(fd)

        ad.finalize()

        self.f = ad.file(self.temp.path)
Example #12
0
    def test_writer_timeaggregation(self):
        self.temp = TempFile()

        NX = 10
        val1 = np.array(list(range(NX)), dtype=np.int32)
        val2 = np.array(list(range(5)), dtype='f8')

        fw = ad.writer(self.temp.path)
        fw.declare_group("group", method="POSIX1")
        fw.set_time_aggregation(3200)

        fw['NX'] = NX
        fw['val1'] = val1
        fw['val2'] = val2
        fw.close()
        ad.finalize()

        f = ad.file(self.temp.path)
        self.assertEqual(f['NX'][...], NX)
        self.assertTrue((f['val1'][:] == val1).all())
        self.assertTrue((f['val2'][:] == val2).all())
    def test_writer_attr(self):
        self.temp = TempFile()

        NX = 10
        val1 = np.array(range(NX), dtype=np.int32)
        val2 = np.array(range(5), dtype='f8')

        single_string = "ABCD"
        three_string = ("AA", "BBB", "CCCC")
        single_int = 10
        five_int = np.array(range(5), dtype=np.int32)
        single_double = 1.1
        five_double = np.array(range(5), dtype='double') * 1.1

        fw = ad.writer(self.temp.path)
        fw.declare_group("group", method="POSIX1")

        fw.define_attr("single_string")
        fw.define_attr("three_string")
        fw.define_attr("single_int")
        fw.define_attr("five_int")
        fw.define_attr("single_double")
        fw.define_attr("five_double")

        fw['single_string'] = single_string
        fw['three_string'] = three_string
        fw['single_int'] = single_int
        fw['five_int'] = five_int
        fw['single_double'] = single_double
        fw['five_double'] = five_double
        fw.close()

        f = ad.file(self.temp.path)
        self.assertEqual(f['single_string'].value, single_string)
        self.assertTrue((f['three_string'].value == three_string).all())
        self.assertEqual(f['single_int'].value, single_int)
        self.assertTrue((f['five_int'].value == five_int).all())
        self.assertEqual(f['single_double'].value, single_double)
        self.assertTrue((f['five_double'].value == five_double).all())
    def test_writer_var(self):
        self.temp = TempFile()

        NX = 10
        val1 = np.array(range(NX), dtype=np.int32)
        val2 = np.array(range(5), dtype='f8')

        fw = ad.writer(self.temp.path)
        fw.declare_group("group", method="POSIX1")

        fw.define_var("NX")
        fw.define_var("val1", "NX")
        fw.define_var("val2", val2.shape)

        fw['NX'] = NX
        fw['val1'] = val1
        fw['val2'] = val2
        fw.close()

        f = ad.file(self.temp.path)
        self.assertEqual(f['NX'][...], NX)
        self.assertTrue((f['val1'][:] == val1).all())
        self.assertTrue((f['val2'][:] == val2).all())
 def setUp(self):
     self.temp = TempFile()