如何过滤HDF5-fi中的特定对象

2024-06-30 08:13:04 发布

您现在位置:Python中文网/ 问答频道 /正文

学习ILNumerics HDF5 API。我非常喜欢使用C对象初始化器在一个表达式中设置复杂的HDF5文件。我创建了以下文件:

using (var f = new H5File("myFile.h5")) {

    f.Add(new H5Group("myTopNode") {
        new H5dataset("dsNo1", ILMath.vec<float>(1,200)),  // no attributes 
        new H5Group("myGroup") {
            new H5Dataset("dsYes", ILMath.rand(100,200)) { // matching dataset
                Attributes = { 
                    { "att1", 1 },
                    { "att2", 2 } 
                }
            }, 
            new H5Dataset("dsNo2") {  // attributes but wrong name
                Attributes = { 
                    { "wrong1", -100 },
                    { "wrong2", -200 } 
                }
            }
        }
    });
}

现在我正在寻找一种聪明的方法来遍历文件并过滤具有特定属性的数据集。我希望找到名称中至少有一个属性为“att”的所有数据集,收集并返回其内容。这是我目前所做的:

^{pr2}$

但它不能递归地工作。我可以采用它,但ILNumerics声称很方便,所以一定有更好的方法吗?类似于python中的h5py?在


Tags: 文件数据对象方法apinew属性表达式
1条回答
网友
1楼 · 发布于 2024-06-30 08:13:04

H5Group提供了Find<T>方法,该方法正好满足您的需要。它迭代整个子树,考虑任意谓词:

var matches = f.Find<H5Dataset>(
                 predicate: ds => ds.Attributes.Any(a => a.Name.Contains("att")));

为什么不让函数返回“ILCell”而不是“List”?这将更好地集成到ILNumerics内存管理中(不会有存储空间在周围等待垃圾回收器的到来):

^{pr2}$

一些链接:

http://ilnumerics.net/hdf5-interface.html

http://ilnumerics.net/Cells.html

http://ilnumerics.net/GeneralRules.html

相关问题 更多 >