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);
            }
        }
        public KamaAcquisitionFile(string filename, AcquisitionInterface acquisitionInterface, ILogger logger,
                                   int eitDefaultChunkSize = 24000, int ecgDefaultChunkSize = 10)
        {
            FileName            = filename;
            Logger              = logger;
            EITDefaultChunkSize = eitDefaultChunkSize;
            ECGDefaultChunkSize = ecgDefaultChunkSize;

            RecordNumber = 1;
            H5E.set_auto(H5E.DEFAULT, null, IntPtr.Zero);
            fileId               = Hdf5.CreateFile(filename);
            groupRoot            = fileId;
            groupEIT             = Hdf5.CreateOrOpenGroup(groupRoot, "eit");
            ProcedureInformation = new ProcedureInformation(fileId, groupRoot, logger)
            {
                ProcedureDirectory = Path.GetDirectoryName(filename),
                StartDateTime      = DateTime.Now,
                EndDateTime        = DateTime.Now
            };

            SystemInformation            = new SystemInformation(fileId, groupRoot, logger);
            SystemInformation.SystemType = acquisitionInterface.ToString();
            //  InjectionGroup = new InjectionGroup(fileId, groupRoot);
            CalibrationGroup = new CalibrationGroup(fileId, groupRoot, logger);
            SystemEvents     = new SystemEventGroup(fileId, groupRoot, logger);
            RPosition        = new RPositionGroup(fileId, groupRoot, logger);
            Tags             = new TagsGroup(fileId, groupRoot, logger);
            UserEventsGroup  = new UserEventsGroup(fileId, groupRoot, logger);
        }
        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);
            }
        }
        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);
            }
        }
Esempio n. 5
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);
        }
Esempio n. 6
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);
        }
Esempio n. 7
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 static void save_weights_to_hdf5_group(long f, List <ILayer> layers)
        {
            List <string> layerName = new List <string>();

            foreach (var layer in layers)
            {
                layerName.Add(layer.Name);
            }
            save_attributes_to_hdf5_group(f, "layer_names", layerName.ToArray());
            Hdf5.WriteAttribute(f, "backend", "tensorflow");
            Hdf5.WriteAttribute(f, "keras_version", "2.5.0");

            long g = 0, crDataGroup = 0;
            List <IVariableV1> weights = new List <IVariableV1>();
            //List<IVariableV1> weight_values = new List<IVariableV1>();
            List <string> weight_names = new List <string>();

            foreach (var layer in layers)
            {
                weight_names = new List <string>();
                g            = Hdf5.CreateOrOpenGroup(f, Hdf5Utils.NormalizedName(layer.Name));
                weights      = _legacy_weights(layer);
                //weight_values= keras.backend.batch_get_value(weights);
                foreach (var weight in weights)
                {
                    weight_names.Add(weight.Name);
                }
                save_attributes_to_hdf5_group(g, "weight_names", weight_names.ToArray());
                Tensor tensor = null;
                foreach (var(name, val) in zip(weight_names, weights))
                {
                    tensor = val.AsTensor();
                    if (name.IndexOf("/") > 1)
                    {
                        crDataGroup = Hdf5.CreateOrOpenGroup(g, Hdf5Utils.NormalizedName(name.Split('/')[0]));
                        WriteDataset(crDataGroup, name.Split('/')[1], tensor);
                        Hdf5.CloseGroup(crDataGroup);
                    }
                    else
                    {
                        WriteDataset(crDataGroup, name, tensor);
                    }

                    tensor = null;
                }
                Hdf5.CloseGroup(g);
                weight_names = null;
            }
            weights = null;
            // weight_values = null;
        }
Esempio n. 9
0
        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.CreateOrOpenGroup(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);
            }
        }
Esempio n. 10
0
        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.CreateOrOpenGroup(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);
            }
        }
Esempio n. 11
0
        static void WriteFile(string filePath)
        {
            var file = Hdf5.CreateFile(filePath);

            var group = Hdf5.CreateOrOpenGroup(file, "group");

            Hdf5.WriteDataset(group, "dataset",
                              new int[, ]
            {
                { 1, 2, 3, 4 },
                { 5, 6, 7, 8 },
                { 9, 10, 11, 12 }
            });

            var hello = "早上好!";

            Hdf5.WriteStringAttribute(group, "string", hello, "dataset");

            Hdf5.CloseGroup(group);
            Hdf5.CloseFile(file);
        }
Esempio n. 12
0
        //[TestMethod]
        public void ReadTable()
        {
            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);
                var groupName = "/MODEL_STAGE[1]/RESULTS/ON_NODES/DISPLACEMENT/DATA";
                var groupId   = Hdf5.CreateOrOpenGroup(fileId, groupName);
                TabularData <double> tableOfData = Hdf5.Read2DTable <double>(groupId, "STEP_0");
            }
            finally
            {
                if (fileId > 0)
                {
                    Hdf5.CloseFile(fileId);
                }
            }
        }
Esempio n. 13
0
        public void WriteAndReadAttributes()
        {
            string filename = Path.Combine(folder, "testAttributes.H5");

            int[]  intValues = new[] { 1, 2 };
            double dblValue  = 1.1;
            string strValue  = "test";

            string[] strValues = new string[2] {
                "test", "another test"
            };
            bool     boolValue = true;
            DateTime dateValue = new DateTime(1969, 1, 12);
            var      groupStr  = "/test";

            //string concatFunc(string x) => string.Concat(groupStr, "/", x);
            string intName  = nameof(intValues);
            string dblName  = nameof(dblValue);
            string strName  = nameof(strValue);
            string strNames = nameof(strValues);
            string boolName = nameof(boolValue);
            string dateName = nameof(dateValue);

            try
            {
                var fileId = Hdf5.CreateFile(filename);
                Assert.IsTrue(fileId > 0);
                var groupId = Hdf5.CreateOrOpenGroup(fileId, groupStr);
                Hdf5.WriteAttributes <int>(groupId, intName, intValues);
                Hdf5.WriteAttribute(groupId, dblName, dblValue);
                Hdf5.WriteAttribute(groupId, strName, strValue);
                Hdf5.WriteAttributes <string>(groupId, strNames, strValues);
                Hdf5.WriteAttribute(groupId, boolName, boolValue);
                Hdf5.WriteAttribute(groupId, dateName, dateValue);
                H5G.close(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);
                IEnumerable <int> readInts = (int[])Hdf5.ReadAttributes <int>(groupId, intName).result;
                Assert.IsTrue(intValues.SequenceEqual(readInts));
                double readDbl = Hdf5.ReadAttribute <double>(groupId, dblName);
                Assert.IsTrue(dblValue == readDbl);
                string readStr = Hdf5.ReadAttribute <string>(groupId, strName);
                Assert.IsTrue(strValue == readStr);
                IEnumerable <string> readStrs = (string[])Hdf5.ReadAttributes <string>(groupId, strNames).result;
                Assert.IsTrue(strValues.SequenceEqual(readStrs));
                bool readBool = Hdf5.ReadAttribute <bool>(groupId, boolName);
                Assert.IsTrue(boolValue == readBool);
                DateTime readDate = Hdf5.ReadAttribute <DateTime>(groupId, dateName);
                Assert.IsTrue(dateValue == readDate);
                H5G.close(groupId);
                Hdf5.CloseFile(fileId);
            }
            catch (Exception ex)
            {
                CreateExceptionAssert(ex);
            }
        }