Code Monkey home page Code Monkey logo

hdf5-csharp's Introduction

HDF5-CSharp: C# wrapper for HDF5 library

Join the chat at https://gitter.im/HDF5-CSharp/community CodeQL Build Status .NET Core Desktop Dependabot Status Issues GitHub closed issues License Coverity Scan Build Status

Nuget Latest Release Commits Since Latest Release Nuget

Hdf5 Wrapper: Set of tools that help in reading and writing hdf5 files for .net environments

Usage

write an object to an HDF5 file

In the example below an object is created with some arrays and other variables The object is written to a file and than read back in a new object.

     private class TestClassWithArray
        {
            public double[] TestDoubles { get; set; }
            public string[] TestStrings { get; set; }
            public int TestInteger { get; set; }
            public double TestDouble { get; set; }
            public bool TestBoolean { get; set; }
            public string TestString { get; set; }
        }
     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" }
        };
    int fileId = Hdf5.CreateFile("testFile.H5");

    Hdf5.WriteObject(fileId, testClass, "testObject");

    TestClassWithArray readObject = new TestClassWithArray();

    readObject = Hdf5.ReadObject(fileId, readObject, "testObject");

    Hdf5.CloseFile(fileId);

Write a dataset and append new data to it

    /// <summary>
    /// create a matrix and fill it with numbers
    /// </summary>
    /// <param name="offset"></param>
    /// <returns>the matrix </returns>
    private static double[,]createDataset(int offset = 0)
    {
      var dset = new double[10, 5];
      for (var i = 0; i < 10; i++)
        for (var j = 0; j < 5; j++)
        {
          double x = i + j * 5 + offset;
          dset[i, j] = (j == 0) ? x : x / 10;
        }
      return dset;
    }

    // create a list of matrices
    dsets = new List<double[,]> {
                createDataset(),
                createDataset(10),
                createDataset(20) };

    string filename = Path.Combine(folder, "testChunks.H5");
    int fileId = Hdf5.CreateFile(filename);    

    // create a dataset and append two more datasets to it
    using (var chunkedDset = new ChunkedDataset<double>("/test", fileId, dsets.First()))
    {
      foreach (var ds in dsets.Skip(1))
        chunkedDset.AppendDataset(ds);
    }

    // read rows 9 to 22 of the dataset
    ulong begIndex = 8;
    ulong endIndex = 21;
    var dset = Hdf5.ReadDataset<double>(fileId, "/test", begIndex, endIndex);
    Hdf5.CloseFile(fileId);

for more example see unit test project

Reading H5 File:

you can use the following two method to read the structure of an existing file:

string fileName = @"FileStructure.h5";
var tree =Hdf5.ReadTreeFileStructure(fileName);
var flat = Hdf5.ReadFlatFileStructure(fileName);

in tree-like format you can drill inside the hierarchy of the file wile the flat option shows all the name is the groups and datasets.

Flat tree

Additional settings

  • Hdf5EntryNameAttribute: control the name of the field/property in the h5 file:
    [AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
   public sealed class Hdf5EntryNameAttribute : Attribute
   {
       public string Name { get; }
       public Hdf5EntryNameAttribute(string name)
       {
           Name = name;
       }
   }

example:

private class TestClass : IEquatable<TestClass>
        {
            public int TestInteger { get; set; }
            public double TestDouble { get; set; }
            public bool TestBoolean { get; set; }
            public string TestString { get; set; }
            [Hdf5EntryNameAttribute("Test_time")]
            public DateTime TestTime { get; set; }
        }
  • Time and fields names in H5 file:
    public class Settings
   {
       public DateTimeType DateTimeType { get; set; }
       public bool LowerCaseNaming { get; set; }
   }

   public enum DateTimeType
   {
       Ticks,
       UnixTimeSeconds,
       UnixTimeMilliseconds
   }
   

usage:

       [ClassInitialize()]
       public static void ClassInitialize(TestContext context)
       {
           Hdf5.Hdf5Settings.LowerCaseNaming = true;
           Hdf5.Hdf5Settings.DateTimeType = DateTimeType.UnixTimeMilliseconds;
       }
           
  • Logging: use can set logging callback via: Hdf5Utils.LogError, Hdf5Utils.LogInfo, etc
public static class Hdf5Utils
   {
       public static Action<string> LogError;
       public static Action<string> LogInfo;
       public static Action<string> LogDebug;
       public static Action<string> LogCritical;
   }

in order to log errors use this code snippet:

            Hdf5.Hdf5Settings.EnableErrorReporting(true);
            Hdf5Utils.LogWarning = (string s) => {...}
            Hdf5Utils.LogCritical = (string s) => {...}
            Hdf5Utils.LogError = (string s) => {...}

hdf5-csharp's People

Contributors

liorbanai avatar reyntjesr avatar dependabot-preview[bot] avatar dataangel avatar johncallaghansigray avatar liorbanaikama avatar oceania2018 avatar gitter-badger avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.