Пример #1
0
        public List <(IVariableV1, NDArray)> load_weights(string filepath, bool by_name = false, bool skip_mismatch = false, object options = null)
        {
            long fileId = Hdf5.OpenFile(filepath, true);

            bool msuccess = Hdf5.GroupExists(fileId, "model_weights");
            bool lsuccess = Hdf5.GroupExists(fileId, "layer_names");

            if (!lsuccess && msuccess)
            {
                fileId = H5G.open(fileId, "model_weights");
            }

            if (by_name)
            {
                //fdf5_format.load_weights_from_hdf5_group_by_name();
                throw new NotImplementedException("");
            }
            else
            {
                var weights = hdf5_format.load_weights_from_hdf5_group(fileId, Layers);
                Hdf5.CloseFile(fileId);
                // return a reference to prevent GC collect Variable.
                return(weights);
            }
        }
Пример #2
0
        public void WriteAndReadOneUnicodeString()
        {
            try
            {
                string test     = "Γαζέες καὶ μυρτιὲς δὲν θὰ βρῶ πιὰ στὸ χρυσαφὶ ξέφωτο";
                string filename = Path.Combine(folder, "testUnicodeString.H5");


                var fileId = Hdf5.CreateFile(filename);
                Hdf5.WriteUnicodeString(fileId, "/test", test);
                Assert.IsTrue(Hdf5.CloseFile(fileId) >= 0);


                fileId = Hdf5.OpenFile(filename);
                string readStr = Hdf5.ReadUnicodeString(fileId, "/test");
                //var readStr = Hdf5.ReadStrings(fileId, "/test");
                Assert.IsTrue(test == readStr);
                Assert.IsTrue(Hdf5.CloseFile(fileId) >= 0);
                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
Пример #3
0
        public void WriteAndOverrideChunkedDataset()
        {
            var    data        = CreateDataset();
            string filename    = Path.Combine(folder, "testOverrideChunks.H5");
            string groupName   = "/test";
            string datasetName = "Data";

            //create
            var fileId = Hdf5.CreateFile(filename);

            Assert.IsTrue(fileId > 0);
            var groupId = Hdf5.CreateOrOpenGroup(fileId, groupName);

            Assert.IsTrue(groupId >= 0);
            using (var chunkedDset = new ChunkedDataset <double>(datasetName, groupId))
            {
                chunkedDset.AppendOrCreateDataset(data);
            }

            Hdf5.CloseFile(fileId);

            //read
            fileId = Hdf5.OpenFile(filename);


            var dataRead = Hdf5.ReadDatasetToArray <double>(fileId, string.Concat(groupName, "/", datasetName));

            Hdf5.CloseFile(fileId);
            CompareDatasets(dataRead.result as double[, ], data);
        }
Пример #4
0
        static void ReadFile(string filePath)
        {
            var file  = Hdf5.OpenFile(filePath, true);
            var group = Hdf5.CreateOrOpenGroup(file, "group");

            var(ok, rawData) = Hdf5.ReadDataset <int>(group, "dataset");
            if (ok)
            {
                var data = (int[, ])rawData;
                for (int i = 0; i < data.GetLength(0); ++i)
                {
                    for (int j = 0; j < data.GetLength(1); ++j)
                    {
                        Write($"{data[i, j],3}");
                    }
                    WriteLine();
                }
            }

            //var dataSet = H5D.open(group, "dataset");
            //var hello = Hdf5.ReadUnicodeString(dataSet, "string");
            //H5D.close(dataSet);
            //WriteLine($"string: {hello}");

            Hdf5.CloseGroup(group);
            Hdf5.CloseFile(file);
        }
Пример #5
0
        public void save_weights(string filepath, bool overwrite = true, string save_format = null, object options = null)
        {
            long fileId = Hdf5.CreateFile(filepath);

            hdf5_format.save_weights_to_hdf5_group(fileId, Layers);
            Hdf5.CloseFile(fileId);
        }
Пример #6
0
        public void WriteAndReadOneString()
        {
            try
            {
                string[] str = new[] { "test" };

                string filename = Path.Combine(folder, "testOneStringList.H5");


                // Open file and write the strings
                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId > 0);
                Hdf5.WriteStrings(fileId, "/test", str);

                // Read the strings and close file
                Assert.IsTrue(fileId > 0);
                IEnumerable <string> strs2 = Hdf5.ReadStrings(fileId, "/test", "").result;
                Assert.IsTrue(strs2.Count() == 1);
                foreach (var s in strs2)
                {
                    Assert.IsTrue(str[0] == s);
                }
                ;

                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
Пример #7
0
        public void WriteAndReadSystemEvent()
        {
            string             filename = Path.Combine(folder, "testCompounds.H5");
            List <SystemEvent> se       = new List <SystemEvent>();
            Dictionary <string, List <string> > attributes = new Dictionary <string, List <string> >();

            try
            {
                se.Add(new SystemEvent(5, "55", "3300000000000000000000000", "555555555555555555555555555555555"));
                se.Add(new SystemEvent(1, "255", "3d3000000000007777773", "ggggggggggggdf"));

                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId > 0);
                var status = Hdf5.WriteCompounds(fileId, "/test", se, attributes);
                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }

            try
            {
                var fileId = Hdf5.OpenFile(filename);
                Assert.IsTrue(fileId > 0);
                var cmpList = Hdf5.ReadCompounds <SystemEvent>(fileId, "/test", "").ToArray();
                Hdf5.CloseFile(fileId);
                CollectionAssert.AreEqual(se, cmpList);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
Пример #8
0
        public void WriteAndReadListOfStrings()
        {
            try
            {
                List <string> strs = new List <string>();
                strs.Add("t");
                strs.Add("tst");
                strs.Add("test1");
                strs.Add("small test");

                string filename = Path.Combine(folder, "testStringList.H5");


                // Open file and write the strings
                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId > 0);
                Hdf5.WriteStrings(fileId, "/test", strs);

                // Read the strings and close file
                Assert.IsTrue(fileId > 0);
                IEnumerable <string> strs2 = Hdf5.ReadStrings(fileId, "/test");
                Assert.IsTrue(strs.Count() == strs2.Count());
                foreach (var item in strs2.Select((str, i) => new { i, str }))
                {
                    Assert.IsTrue(item.str == strs[item.i]);
                }

                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
Пример #9
0
        public void WriteAndReadJaggedArray()
        {
            string filename = Path.Combine(folder, $"{nameof(WriteAndReadJaggedArray)}.H5");
            var    obj      = new TestClassWithJaggedArray();

            try
            {
                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId > 0);
                var status = Hdf5.WriteObject(fileId, obj, "test");
                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }

            try
            {
                var fileId = Hdf5.OpenFile(filename);
                Assert.IsTrue(fileId > 0);
                var obj2 = Hdf5.ReadObject <TestClassWithJaggedArray>(fileId, "test");
                Assert.IsTrue(obj.Equals(obj2));
                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
Пример #10
0
        public void WriteAndReadStringAttribute()
        {
            string filename = Path.Combine(folder, "testAttributeString.H5");

            try
            {
                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId > 0);
                var groupId = Hdf5.CreateOrOpenGroup(fileId, "test");

                string attrStr = "this is an attribute";
                Hdf5.WriteAttribute(groupId, "time", attrStr);
                string readStr = Hdf5.ReadAttribute <string>(groupId, "time_Non_Exist");
                Assert.IsTrue(string.IsNullOrEmpty(readStr));
                readStr = Hdf5.ReadAttribute <string>(groupId, "time");
                Assert.IsTrue(readStr == attrStr);
                Assert.IsTrue(H5G.close(groupId) == 0);
                Assert.IsTrue(Hdf5.CloseFile(fileId) == 0);
                ErrorCountExpected = 2;
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
        public void WriteAndReadDatetimeDataset()
        {
            string filename = Path.Combine(folder, "testDatetime.H5");
            var    times    = new DateTime[10, 5];
            var    offset   = new DateTime(2000, 1, 1, 12, 0, 0);

            for (var i = 0; i < 10; i++)
            {
                for (var j = 0; j < 5; j++)
                {
                    times[i, j] = offset.AddDays(i + j * 5);
                }
            }

            try
            {
                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId > 0);
                Hdf5.WriteDataset(fileId, "/test", times);

                var timesRead = (DateTime[, ])Hdf5.ReadDataset <DateTime>(fileId, "/test").result;
                CompareDatasets(times, timesRead);

                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
Пример #12
0
        public void WriteReadAndEditAttribute()
        {
            string filename = Path.Combine(folder, "WriteReadAndEditAttribute.H5");

            try
            {
                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId > 0);
                var      groupId = Hdf5.CreateOrOpenGroup(fileId, "test");
                DateTime nowTime = DateTime.Now;
                Hdf5.WriteAttribute(groupId, "time", nowTime);
                DateTime readTime = Hdf5.ReadAttribute <DateTime>(groupId, "time");
                Assert.IsTrue(readTime == nowTime);
                Hdf5.CloseFile(fileId);
                fileId   = Hdf5.OpenFile(filename, false);
                readTime = Hdf5.ReadAttribute <DateTime>(groupId, "time");
                Assert.IsTrue(readTime == nowTime);

                nowTime = DateTime.Now;
                Hdf5.WriteAttribute(groupId, "time", nowTime);
                readTime = Hdf5.ReadAttribute <DateTime>(groupId, "time");
                Assert.IsTrue(readTime == nowTime);
                Hdf5.CloseFile(fileId);

                fileId   = Hdf5.OpenFile(filename, false);
                readTime = Hdf5.ReadAttribute <DateTime>(groupId, "time");
                Assert.IsTrue(readTime == nowTime);
                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
Пример #13
0
        public void WriteAndReadListOfList()
        {
            string filename          = Path.Combine(folder, $"{nameof(WriteAndReadList)}.H5");
            TestClassListOfList data = new TestClassListOfList();

            try
            {
                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId > 0);
                Hdf5.WriteObject(fileId, data, "test");
                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }

            try
            {
                var fileId = Hdf5.OpenFile(filename);
                Assert.IsTrue(fileId > 0);
                var objWithList = Hdf5.ReadObject <TestClassListOfList>(fileId, "test");
                Hdf5.CloseFile(fileId);
                Assert.IsTrue(objWithList.Data[0].SequenceEqual(data.Data[0]));
                Assert.IsTrue(objWithList.Data[1].SequenceEqual(data.Data[1]));
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
Пример #14
0
        //[TestMethod]
        public void ReadObject()
        {
            Hdf5.Settings.LowerCaseNaming = false;
            Hdf5.Settings.EnableErrorReporting(true);
            Hdf5Utils.LogWarning = (s) => Errors.Add(s);
            Hdf5Utils.LogError   = (s) => Errors.Add(s);
            string filename = @"D:\h5\d.hdf5";
            long   fileId   = -1;

            try
            {
                fileId = Hdf5.OpenFile(filename, true);
                Assert.IsTrue(fileId > 0);
                //var result = Hdf5.ReadObject<Coordinate>(fileId, "/MODEL_STAGE[1]/MODEL/NODES");
                var step    = "/MODEL_STAGE[1]/RESULTS/ON_NODES/DISPLACEMENT/DATA";
                var result2 = Hdf5.ReadObject <Steps>(fileId, step);
            }
            finally
            {
                if (fileId > 0)
                {
                    Hdf5.CloseFile(fileId);
                }
            }
        }
Пример #15
0
        public void WriteAndReadStructsWithArray()
        {
            string filename = Path.Combine(folder, "testArrayCompounds.H5");

            try
            {
                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId > 0);
                var status = Hdf5.WriteCompounds(fileId, "/test", responseList);
                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }

            try
            {
                var fileId = Hdf5.OpenFile(filename);
                Assert.IsTrue(fileId > 0);
                Responses[] cmpList = Hdf5.ReadCompounds <Responses>(fileId, "/test").ToArray();
                Hdf5.CloseFile(fileId);
                var isSame = responseList.Zip(cmpList, (r, c) =>
                {
                    return(r.MCID == c.MCID &&
                           r.PanelIdx == c.PanelIdx &&
                           r.ResponseValues.Zip(c.ResponseValues, (rr, cr) => rr == cr).All(v => v == true));
                });
                Assert.IsTrue(isSame.All(s => s == true));
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
        public void WriteAndReadDataset()
        {
            string filename = Path.Combine(folder, "testDataset.H5");
            var    dset     = dsets.First();

            try
            {
                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId > 0);
                Hdf5.WriteDataset(fileId, "/test", dset);
                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }

            try
            {
                var fileId = Hdf5.OpenFile(filename);
                Assert.IsTrue(fileId > 0);
                double[,] dset2 = (double[, ])Hdf5.ReadDataset <double>(fileId, "/test").result;
                CompareDatasets(dset, dset2);
                bool same = dset == dset2;

                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
Пример #17
0
        public void load_weights(string filepath, bool by_name = false, bool skip_mismatch = false, object options = null)
        {
            long fileId = Hdf5.OpenFile(filepath, true);

            if (fileId < 0)
            {
                tf_output_redirect.WriteLine($"Can't find weights file {filepath}");
                return;
            }
            bool msuccess = Hdf5.GroupExists(fileId, "model_weights");
            bool lsuccess = Hdf5.GroupExists(fileId, "layer_names");

            if (!lsuccess && msuccess)
            {
                fileId = H5G.open(fileId, "model_weights");
            }

            if (by_name)
            {
                //fdf5_format.load_weights_from_hdf5_group_by_name();
                throw new NotImplementedException("");
            }
            else
            {
                hdf5_format.load_weights_from_hdf5_group(fileId, Layers);
                Hdf5.CloseFile(fileId);
            }
        }
        public void WriteAndReadTimespanDataset()
        {
            string filename = Path.Combine(folder, "testTimespan.H5");
            var    times    = new TimeSpan[10, 5];
            var    offset   = new TimeSpan(1, 0, 0, 0, 0);

            for (var i = 0; i < 10; i++)
            {
                for (var j = 0; j < 5; j++)
                {
                    times[i, j] = offset.Add(new TimeSpan(i + j * 5, 0, 0));
                }
            }

            try
            {
                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId > 0);
                Hdf5.WriteDataset(fileId, "/test", times);

                TimeSpan[,] timesRead = (TimeSpan[, ])Hdf5.ReadDataset <TimeSpan>(fileId, "/test").result;
                CompareDatasets(times, timesRead);

                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
Пример #19
0
        public void WriteAndReadObjectWithStructs()
        {
            string filename = Path.Combine(folder, "testObjectWithStructArray.H5");


            try
            {
                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId > 0);
                var status = Hdf5.WriteObject(fileId, classWithStructs, "test");
                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }

            try
            {
                TestClassWithStructs objWithStructs;
                var fileId = Hdf5.OpenFile(filename);
                Assert.IsTrue(fileId > 0);
                objWithStructs = Hdf5.ReadObject <TestClassWithStructs>(fileId, "test");
                CollectionAssert.AreEqual(wDataList, objWithStructs.DataList);
                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
Пример #20
0
        public void WriteAndReadStructsWithDatetime()
        {
            string filename = Path.Combine(folder, "testCompounds.H5");

            try
            {
                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId > 0);
                var status = Hdf5.WriteCompounds(fileId, "/test", wDataList);
                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }

            try
            {
                var fileId = Hdf5.OpenFile(filename);
                Assert.IsTrue(fileId > 0);
                var cmpList = Hdf5.ReadCompounds <WData>(fileId, "/test").ToArray();
                Hdf5.CloseFile(fileId);
                CollectionAssert.AreEqual(wDataList, cmpList);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
Пример #21
0
        public void WriteAndReadAttribute()
        {
            string filename = Path.Combine(folder, "testAttribute.H5");

            try
            {
                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId > 0);
                var      groupId = Hdf5.CreateOrOpenGroup(fileId, "test");
                DateTime nowTime = DateTime.Now;
                Hdf5.WriteAttribute(groupId, "time", nowTime);
                Hdf5.WriteAttributes <DateTime>(groupId, "times", new List <DateTime> {
                    nowTime, nowTime.AddDays(1)
                }.ToArray());

                DateTime readTime = Hdf5.ReadAttribute <DateTime>(groupId, "time");
                var      allTimes = Hdf5.ReadAttributes <DateTime>(groupId, "times");

                Assert.IsTrue(readTime == nowTime);
                Assert.IsTrue(Hdf5.CloseFile(fileId) == 0);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
Пример #22
0
        public void WriteAndReadStructs()
        {
            string filename = Path.Combine(folder, "testCompoundsWithDifferentDisplayName.H5");
            Dictionary <string, List <string> > attributes = new Dictionary <string, List <string> >();

            try
            {
                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId > 0);
                var status = Hdf5.WriteCompounds(fileId, "/test", wData2List, attributes);
                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }

            try
            {
                var fileId = Hdf5.OpenFile(filename);
                Assert.IsTrue(fileId > 0);
                var cmpList = Hdf5.ReadCompounds <WData2>(fileId, "/test", "").ToArray();
                Hdf5.CloseFile(fileId);
                CollectionAssert.AreEqual(wData2List, cmpList);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
 public void Dispose()
 {
     if (!fileClosed)
     {
         Hdf5.CloseFile(fileId);
         fileClosed = true;
     }
 }
Пример #24
0
        public void WriteAndUpdateDataset()
        {
            string filename = "writeandupdatedatset.h5";
            long   tef2     = WriteDataset(filename);

            Hdf5.CloseFile(tef2);
            File.Delete(filename);
        }
Пример #25
0
        public void WriteAndReadGroupsWithDataset()
        {
            string filename = Path.Combine(folder, "testGroups.H5");

            try
            {
                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId > 0);
                var dset = dsets.First();

                var groupId = H5G.create(fileId, Hdf5Utils.NormalizedName("/A")); ///B/C/D/E/F/G/H
                Hdf5.WriteDataset(groupId, Hdf5Utils.NormalizedName("test"), dset);
                var subGroupId  = Hdf5.CreateOrOpenGroup(groupId, Hdf5Utils.NormalizedName("C"));
                var subGroupId2 = Hdf5.CreateOrOpenGroup(groupId, Hdf5Utils.NormalizedName("/D")); // will be saved at the root location
                dset = dsets.Skip(1).First();
                Hdf5.WriteDataset(subGroupId, Hdf5Utils.NormalizedName("test2"), dset);
                Hdf5.CloseGroup(subGroupId);
                Hdf5.CloseGroup(subGroupId2);
                Hdf5.CloseGroup(groupId);
                groupId = H5G.create(fileId, Hdf5Utils.NormalizedName("/A/B")); ///B/C/D/E/F/G/H
                dset    = dsets.Skip(1).First();
                Hdf5.WriteDataset(groupId, Hdf5Utils.NormalizedName("test"), dset);
                Hdf5.CloseGroup(groupId);

                groupId = Hdf5.CreateGroupRecursively(fileId, Hdf5Utils.NormalizedName("A/B/C/D/E/F/I"));
                Hdf5.CloseGroup(groupId);
                Hdf5.CloseFile(fileId);


                fileId = Hdf5.OpenFile(filename);
                Assert.IsTrue(fileId > 0);
                fileId = Hdf5.OpenFile(filename);

                groupId         = H5G.open(fileId, Hdf5Utils.NormalizedName("/A/B"));
                double[,] dset2 = (double[, ])Hdf5.ReadDataset <double>(groupId, Hdf5Utils.NormalizedName("test")).result;
                CompareDatasets(dset, dset2);
                Assert.IsTrue(Hdf5.CloseGroup(groupId) >= 0);
                groupId = H5G.open(fileId, Hdf5Utils.NormalizedName("/A/C"));
                dset2   = (double[, ])Hdf5.ReadDataset <double>(groupId, Hdf5Utils.NormalizedName("test2")).result;
                CompareDatasets(dset, dset2);
                Assert.IsTrue(Hdf5.CloseGroup(groupId) >= 0);
                bool same = dset == dset2;
                dset  = dsets.First();
                dset2 = (double[, ])Hdf5.ReadDataset <double>(fileId, Hdf5Utils.NormalizedName("/A/test")).result;
                CompareDatasets(dset, dset2);
                Assert.IsTrue(Hdf5Utils.ItemExists(fileId, Hdf5Utils.NormalizedName("A/B/C/D/E/F/I"), DataTypes.Hdf5ElementType.Dataset));

                Assert.IsTrue(Hdf5.CloseFile(fileId) == 0);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
        public void WriteAndReadPrimitives()
        {
            string filename  = Path.Combine(folder, "testPrimitives.H5");
            int    intValue  = 2;
            double dblValue  = 1.1;
            string strValue  = "test";
            bool   boolValue = true;
            var    groupStr  = "/test";

            string concatFunc(string x) => string.Concat(groupStr, "/", x);

            Dictionary <string, List <string> > attributes = new Dictionary <string, List <string> >();

            try
            {
                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId > 0);
                var groupId = Hdf5.CreateGroup(fileId, groupStr);
                Hdf5.WriteOneValue(groupId, concatFunc(nameof(intValue)), intValue, attributes);
                Hdf5.WriteOneValue(groupId, concatFunc(nameof(dblValue)), dblValue, attributes);
                Hdf5.WriteOneValue(groupId, concatFunc(nameof(strValue)), strValue, attributes);
                Hdf5.WriteOneValue(groupId, concatFunc(nameof(boolValue)), boolValue, attributes);
                Hdf5.CloseGroup(groupId);
                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }

            try
            {
                var fileId = Hdf5.OpenFile(filename);
                Assert.IsTrue(fileId > 0);
                var groupId = H5G.open(fileId, groupStr);
                int readInt = Hdf5.ReadOneValue <int>(groupId, concatFunc(nameof(intValue)));
                Assert.IsTrue(intValue == readInt);
                double readDbl = Hdf5.ReadOneValue <double>(groupId, concatFunc(nameof(dblValue)));
                Assert.IsTrue(dblValue == readDbl);
                string readStr = Hdf5.ReadOneValue <string>(groupId, concatFunc(nameof(strValue)));
                Assert.IsTrue(strValue == readStr);
                bool readBool = Hdf5.ReadOneValue <bool>(groupId, concatFunc(nameof(boolValue)));
                Assert.IsTrue(boolValue == readBool);
                H5G.close(groupId);
                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
        public void WriteAndReadChunckedDataset2()
        {
            string filename    = Path.Combine(folder, "testChunks2.H5");
            string groupName   = "/test";
            string datasetName = "Data";

            try
            {
                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId > 0);
                var groupId = Hdf5.CreateGroup(fileId, groupName);
                Assert.IsTrue(groupId >= 0);
                //var chunkSize = new ulong[] { 5, 5 };
                using (var chunkedDset = new ChunkedDataset <double>(datasetName, groupId))
                {
                    foreach (var ds in dsets)
                    {
                        chunkedDset.AppendOrCreateDataset(ds);
                    }
                    ;
                }
                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }

            try
            {
                var fileId = Hdf5.OpenFile(filename);
                //var groupId = H5G.open(fileId, groupName);
                //var dset = Hdf5.ReadDatasetToArray<double>(groupId, datasetName);
                var dset = Hdf5.ReadDatasetToArray <double>(fileId, string.Concat(groupName, "/", datasetName));

                Assert.IsTrue(dset.result.Rank == dsets.First().Rank);
                var xSum = dsets.Select(d => d.GetLength(0)).Sum();
                Assert.IsTrue(xSum == dset.result.GetLength(0));
                var testRange = Enumerable.Range(0, 30).Select(t => (double)t);

                // get every 5th element in the matrix
                var x0Range = dset.result.Cast <double>().Where((d, i) => i % 5 == 0);
                Assert.IsTrue(testRange.SequenceEqual(x0Range));

                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
        public void WriteAndReadSubsetOfDataset()
        {
            string filename = Path.Combine(folder, "testSubset.H5");

            try
            {
                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId > 0);
                var chunkSize = new ulong[] { 5, 5 };
                using (var chunkedDset = new ChunkedDataset <double>("/test", fileId, dsets.First()))
                {
                    foreach (var ds in dsets.Skip(1))
                    {
                        chunkedDset.AppendDataset(ds);
                    }
                    ;
                }

                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }

            try
            {
                var   fileId   = Hdf5.OpenFile(filename);
                ulong begIndex = 8;
                ulong endIndex = 21;
                var   dset     = Hdf5.ReadDataset <double>(fileId, "/test", begIndex, endIndex);
                Hdf5.CloseFile(fileId);


                Assert.IsTrue(dset.Rank == dsets.First().Rank);
                int count = Convert.ToInt32(endIndex - begIndex + 1);
                Assert.IsTrue(count == dset.GetLength(0));
                // Creat a range from number 8 to 21
                var testRange = Enumerable.Range((int)begIndex, count).Select(t => (double)t);

                // Get the first column from row index number 8 (the 9th row) to row index number 21 (22th row)
                var x0Range = dset.Cast <double>().Where((d, i) => i % 5 == 0);
                Assert.IsTrue(testRange.SequenceEqual(x0Range));
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
Пример #29
0
        public void WriteAndReadObjectWithPropertiesAndArrayPropertyTest()
        {
            try
            {
                var testClass = new TestClassWithArray()
                {
                    TestInteger = 2,
                    TestDouble  = 1.1,
                    TestBoolean = true,
                    TestString  = "test string",
                    TestDoubles = new double[] { 1.1, 1.2, -1.1, -1.2 },
                    TestStrings = new string[] { "one", "two", "three", "four" }
                };
                testClassWithArrays.TestInteger = 2;
                testClassWithArrays.TestDouble  = 1.1;
                testClassWithArrays.TestBoolean = true;
                testClassWithArrays.TestString  = "test string";
                testClassWithArrays.TestDoubles = new double[] { 1.1, 1.2, -1.1, -1.2 };
                testClassWithArrays.TestStrings = new string[] { "one", "two", "three", "four" };

                string filename = Path.Combine(folder, "testArrayObjects.H5");

                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId >= 0);

                Hdf5.WriteObject(fileId, testClassWithArrays, "objectWithTwoArrays");

                TestClassWithArray readObject = new TestClassWithArray
                {
                    TestStrings = new string[0],
                    TestDoubles = null,
                    TestDouble  = double.NaN
                };

                readObject = Hdf5.ReadObject(fileId, readObject, "objectWithTwoArrays");
                Assert.IsTrue(testClassWithArrays.Equals(readObject));

                readObject = Hdf5.ReadObject <TestClassWithArray>(fileId, "objectWithTwoArrays");
                Assert.IsTrue(testClassWithArrays.Equals(readObject));

                Assert.IsTrue(Hdf5.CloseFile(fileId) >= 0);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }
Пример #30
0
        public void ReadStructs()
        {
            string filename = Path.Combine(folder, "data", "testCompounds_WData2_WData3.H5");

            try
            {
                var fileId = Hdf5.OpenFile(filename);
                Assert.IsTrue(fileId > 0);
                var cmpList = Hdf5.ReadCompounds <WData3>(fileId, "/test", "").ToArray();
                Hdf5.CloseFile(fileId);
                CollectionAssert.AreEqual(wData2List, cmpList);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }