首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >支持H5Attributes中的基本数据类型?

支持H5Attributes中的基本数据类型?
EN

Stack Overflow用户
提问于 2014-06-18 10:10:00
回答 2查看 93关注 0票数 1

我正在试用无数的测试版hdf5工具包。

目前,我看到H5Attributes只支持无数个数组。是否有计划将其扩展到基本数据类型(例如字符串),作为最终版本的一部分?

H5包装器是否提供了将任何功能扩展到特定数据类型的功能?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-27 21:40:00

当然,ILNumerics内部使用来自HDF的官方HDF5库。H5Attributes在HDF5中对应于数据集,其限制是不能部分I/O,而且H5Attributes是普通数组!通过假设存储为标量的数组提供了对基本(标量)元素类型的支持。

字符串是一个完全不同的故事:字符串通常是可变长度的数据类型。就HDF5而言,字符串是元素类型Char的数组。字符串中的字符数决定数组的长度。为了将字符串存储到dataset或属性中,您必须将其单个字符存储为数组的元素。在ILNumerics中,可以将字符串转换为ILArrray或ILArray (用于ASCII数据),并将其存储到dataset/属性中。

请参阅以下测试用例,该测试用例将字符串作为值存储到属性中,并将内容读入字符串。

免责声明:这是我们内部测试套件的一部分。您将无法直接编译该示例,因为它取决于可能不可用的几个函数的存在。但是,您将能够理解如何将字符串存储到数据集和属性中:

代码语言:javascript
运行
复制
public void StringASCIAttribute() {
    string file = "deleteA0001.h5";
    string val = "This is a long string to be stored into an attribute.\r\n";
    // transfer string into ILArray<Char>
    ILArray<Char> A = ILMath.array<Char>(' ', 1, val.Length);
    for (int i = 0; i < val.Length; i++) {
        A.SetValue(val[i], 0, i);
    }
    // store the string as attribute of a group
    using (var f = new H5File(file)) {
        f.Add(new H5Group("grp1") {
            Attributes = { 
                { "title", A }
            }
        });
    }
    // check by reading back
    // read back 
    using (var f = new H5File(file)) {
        // must exist in the file
        Assert.IsTrue(f.Get<H5Group>("grp1").Attributes.ContainsKey("title"));
        // check size 
        var attr = f.Get<H5Group>("grp1").Attributes["title"];
        Assert.IsTrue(attr.Size == ILMath.size(1, val.Length));
        // read back 
        ILArray<Char> titleChar = attr.Get<Char>();
        ILArray<byte> titleByte = attr.Get<byte>();

        // compare byte values (sum) 
        int origsum = 0;
        foreach (var c in val) origsum += (Byte)c;
        Assert.IsTrue(ILMath.sumall(ILMath.toint32(titleByte)) == origsum);

        StringBuilder title = new StringBuilder(attr.Size[1]);
        for (int i = 0; i < titleChar.Length; i++) {
            title.Append(titleChar.GetValue(i));
        }
        Assert.IsTrue(title.ToString() == val);
    }
}

这会将任意字符串存储为'Char-array‘,并将其存储到HDF5属性中,并且对H5Dataset也同样有效。

票数 1
EN

Stack Overflow用户

发布于 2016-06-10 06:57:35

作为另一种解决方案,您可以使用HDF5DotNet (http://hdf5.net/default.aspx)包装器将属性写入字符串:

代码语言:javascript
运行
复制
H5.open()
Uri destination = new Uri(@"C:\yourFileLocation\FileName.h5");
//Create an HDF5 file
H5FileId fileId = H5F.create(destination.LocalPath, H5F.CreateMode.ACC_TRUNC);
//Add a group to the file
H5GroupId groupId = H5G.create(fileId, "groupName");

string myString = "String attribute";
byte[] attrData = Encoding.ASCII.GetBytes(myString);

//Create an attribute of type STRING attached to the group
H5AttributeId attrId = H5A.create(groupId, "attributeName", H5T.create(H5T.CreateClass.STRING, attrData.Length),
               H5S.create(H5S.H5SClass.SCALAR));
//Write the string into the attribute
H5A.write(attributeId, H5T.create(H5T.CreateClass.STRING, attrData.Length), new H5Array<byte>(attrData));

H5A.close(attributeId);
H5G.close(groupId);
H5F.close(fileId);
H5.close();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24282832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档