- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个 C++ 示例,我试图使用 h5py 重现它,但它没有按预期工作。我正在使用 h5py 获取空填充字符串,我希望它是空终止字符串。
这是我的 C++ 驱动程序...
main.cpp
#include <hdf5.h>
int main(void) {
auto file = H5Fcreate("test-c.h5", H5F_ACC_TRUNC,
H5P_DEFAULT, H5P_DEFAULT);
char strings[5][64] = {
"please work 0",
"please work 1",
"please work 2",
"please work 3",
"please work 4"};
auto H5T_C_S1_64 = H5Tcopy (H5T_C_S1);
H5Tset_size(H5T_C_S1_64, 64);
hsize_t dims[1] = {5};
auto dataspace = H5Screate_simple(1, dims, NULL);
auto dataset = H5Dcreate(file, "test dataset", H5T_C_S1_64, dataspace,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
H5Dwrite (dataset, H5T_C_S1_64, H5S_ALL, H5S_ALL, H5P_DEFAULT, strings);
H5Dclose(dataset);
H5Sclose(dataspace);
H5Tclose(H5T_C_S1_64);
H5Fclose(file);
return 0;
}
我使用以下 SCons 脚本构建。
SConstruct
env = Environment()
env.Append(LIBS=['hdf5'],
CPPFLAGS=['-std=c++11'])
env.Program('writeh5', 'main.cpp')
这是我的 python 脚本,我试图用它写出相同的 hdf5 文件。
main.py
import h5py
hdf5 = h5py.File('test-p.h5', 'w')
H5T_C_S1_64 = h5py.h5t.C_S1.copy()
H5T_C_S1_64.set_size(64)
print "Null Terminated String: %s" % (
H5T_C_S1_64.get_strpad() == h5py.h5t.STR_NULLTERM)
dataset = hdf5.create_dataset('test dataset', (5,),
data=['please work %s' % n for n in xrange(5)],
dtype=H5T_C_S1_64)
hdf5.close()
我使用的是 python v2.7.11,我已经用 h5py v2.5.0 和 v2.6.0 尝试过,结果相同。
>> python --version
Python 2.7.11
>> python -c "import h5py; print h5py.version.version"
2.5.0
>> tree
.
├── main.cpp
├── main.py
└── SConstruct
0 directories, 3 files
>> scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o main.o -c -std=c++11 main.cpp
g++ -o writeh5 main.o -lhdf5
scons: done building targets.
>> tree
.
├── main.cpp
├── main.o
├── main.py
├── SConstruct
└── writeh5
0 directories, 5 files
>> ./writeh5
>> tree
.
├── main.cpp
├── main.o
├── main.py
├── SConstruct
├── test-c.h5
└── writeh5
0 directories, 6 files
>> python main.py
Null Terminated String: True
>> tree
.
├── main.cpp
├── main.o
├── main.py
├── SConstruct
├── test-c.h5
├── test-p.h5
└── writeh5
0 directories, 7 files
>> h5dump test-c.h5
HDF5 "test-c.h5" {
GROUP "/" {
DATASET "test dataset" {
DATATYPE H5T_STRING {
STRSIZE 64;
STRPAD H5T_STR_NULLTERM;
CSET H5T_CSET_ASCII;
CTYPE H5T_C_S1;
}
DATASPACE SIMPLE { ( 5 ) / ( 5 ) }
DATA {
(0): "please work 0", "please work 1", "please work 2",
(3): "please work 3", "please work 4"
}
}
}
}
>> h5dump test-p.h5
HDF5 "test-p.h5" {
GROUP "/" {
DATASET "test dataset" {
DATATYPE H5T_STRING {
STRSIZE 64;
STRPAD H5T_STR_NULLPAD;
CSET H5T_CSET_ASCII;
CTYPE H5T_C_S1;
}
DATASPACE SIMPLE { ( 5 ) / ( 5 ) }
DATA {
(0): "please work 0\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000",
(1): "please work 1\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000",
(2): "please work 2\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000",
(3): "please work 3\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000",
(4): "please work 4\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
}
}
}
}
正如您从上面的输出中看到的那样,在使用 h5py 时我仍然以空填充的固定长度字符串结束,即使我指定我想要空终止的固定长度字符串也是如此。
那么我该如何修改我的 python 脚本以在数据集中以 null 终止的固定长度字符串结束?如果它是 h5py 中的错误,是否有任何解决方法?
在此先感谢您的帮助。
最佳答案
编辑:在下面找到了适用于 'vanilla' h5py 的解决方案
在 h5py 源中有以下 cython code :
cdef TypeStringID _c_string(dtype dt):
# Strings (fixed-length)
cdef hid_t tid
tid = H5Tcopy(H5T_C_S1)
H5Tset_size(tid, dt.itemsize)
H5Tset_strpad(tid, H5T_STR_NULLPAD)
return TypeStringID(tid)
我不完全确定它的作用。但是,在注释掉 H5Tset_strpad(tid, H5T_STR_NULLPAD)
行并编译库后,问题似乎解决了,而 python2 setup.py test
没有报告任何意外失败测试。它是唯一一个不在可变长度字符串上下文中引用 H5T_C_S1
的函数。看起来有点像错误。
因此,一种(hacky)方法是在脚本目录中执行以下命令。
$ https://github.com/h5py/h5py h5py-source
$ mkdir fake-root
$ sed -i '/H5Tset_strpad(tid, H5T_STR_NULLPAD)/d' h5py-source/h5py/h5t.pyx
$ (cd h5py-source; python2 setup.py install --root fake-root)
$ mv fake-root/usr/lib/python2.7/site-packages/h5py .
然后,当导入 h5py
时,您本地目录中的 h5py
将覆盖系统范围内安装的版本。在用户站点包、虚拟环境或打开问题中使用安装可能会更好。
请注意,应用此修复程序可能会以意想不到的方式破坏事物(我以前从未使用过 hdf5,也不知道这可能会产生什么影响)。真正的解决方案可能涉及从 dt
加载 strpad。
我做了更多研究:
documentation仅列出 3 种支持的字符串,零填充固定长度字符串和两种不同类型的可变长度字符串。没有提到零终止字符串。所以看起来 h5py 公共(public) api 不支持空终止字符串(即使代码中提到了空 c 字符串)。
接下来,dtype 参数应该是一个有效的 numpy dtype。没有明确提及对 H5T 的支持。但是,H5T 类型仍然以某种方式被解释为字符串。更改填充不会更改 TypeStringID
中收到的 dtype
的任何属性。
numpy dtype 到 h5t 类型的转换发生在 dataset.py:736 中:
if isinstance(dtype, Datatype):
# Named types are used as-is
tid = dtype.id
dtype = tid.dtype # Following code needs this
else:
# Validate dtype
if dtype is None and data is None:
dtype = numpy.dtype("=f4")
elif dtype is None and data is not None:
dtype = data.dtype
else:
dtype = numpy.dtype(dtype)
tid = h5t.py_create(dtype, logical=1)
其中 numpy.dtype(H5T_C_S1)
给出了带有 kind='S'
的 dtype。接下来,调用 h5t.py_create(dtype, logical=1)
将其分派(dispatch)给上面的 _c_string(dt)
。因此,修复确实会破坏事情,因为所有固定长度的字符串最终都会以 null 终止。
但是,这也显示了一种更好的解决方案。通过从 H5T tid 构建 dtype,我们可以绕过 numpy.dtype
转换。
此代码可在 vanilla h5py 安装下正常工作:
import h5py
hdf5 = h5py.File('test-p.h5', 'w')
tid = h5py.h5t.C_S1.copy()
tid.set_size(64)
H5T_C_S1_64 = h5py.Datatype(tid)
dataset = hdf5.create_dataset('test dataset', (5,),
data=['please work %s' % n for n in range(5)],
dtype=H5T_C_S1_64)
hdf5.close()
这还允许您使用任何您想要的填充方案。但是,我找不到它的文档,因此 api 将来可能会更改。
关于python - 如何使用 h5py 编写空终止固定长度字符串的数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38267076/
我已经为使用 JGroups 编写了简单的测试。有两个像这样的简单应用程序 import org.jgroups.*; import org.jgroups.conf.ConfiguratorFact
我有一个通过 ajax 检索的 json 编码数据集。我尝试检索的一些数据点将返回 null 或空。 但是,我不希望将那些 null 或空值显示给最终用户,或传递给其他函数。 我现在正在做的是检查
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Why does one often see “null != variable” instead of “
嗨在我们公司,他们遵循与空值进行比较的严格规则。当我编码 if(variable!=null) 在代码审查中,我收到了对此的评论,将其更改为 if(null!=variable)。上面的代码对性能有影
我正在尝试使用 native Cordova QR 扫描仪插件编译项目,但是我不断收到此错误。据我了解,这是代码编写方式的问题,它向构造函数发送了错误的值,或者根本就没有找到构造函数。那么我该如何解决
我在装有 Java 1.8 的 Windows 10 上使用 Apache Nutch 1.14。我已按照 https://wiki.apache.org/nutch/NutchTutorial 中提
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: what is “=null” and “ IS NULL” Is there any difference bet
Three-EyedRaven 内网渗透初期,我们都希望可以豪无遗漏的尽最大可能打开目标内网攻击面,故,设计该工具的初衷是解决某些工具内网探测速率慢、运行卡死、服务爆破误报率高以及socks流
我想在Scala中像在Java中那样做: public void recv(String from) { recv(from, null); } public void recv(String
我正在尝试从一组图像补丁中创建一个密码本。我已将图像(Caltech 101)分成20 X 20图像块。我想为每个补丁创建一个SIFT描述符。但是对于某些图像补丁,它不返回任何描述符/关键点。我尝试使
我在验证器类中自动连接的两个服务有问题。这些服务工作正常,因为在我的 Controller 中是自动连接的。我有一个 applicationContext.xml 文件和 MyApp-servlet.
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭10 年前。 问题必须表现出对要解决的问题的最低程度的了解。告诉我们您尝试过做什么,为什么不起作用,以
大家好,我正在对数据库进行正常的选择,但是 mysql_num_rowsis 为空,我不知道为什么,我有 7 行选择。 如果您发现问题,请告诉我。 真的谢谢。 代码如下: function get_b
我想以以下格式创建一个字符串:id[]=%@&stringdata[]=%@&id[]=%@&stringdata[]=%@&id[]=%@&stringdata[]=%@&等,在for循环中,我得到
我正在尝试使用以下代码将URL转换为字符串: NSURL *urlOfOpenedFile = _service.myURLRequest.URL; NSString *fileThatWasOpen
我正在尝试将NSNumber传递到正在工作的UInt32中。然后,我试图将UInt32填充到NSData对象中。但是,这在这里变得有些时髦... 当我尝试将NSData对象中的内容写成它返回的字符串(
我正在进行身份验证并收到空 cookie。我想存储这个 cookie,但服务器没有返回给我 cookie。但响应代码是 200 ok。 httpConn.setRequestProperty(
我认为 Button bTutorial1 = (Button) findViewById(R.layout.tutorial1); bTutorial1.setOnClickListener
我的 Controller 中有这样的东西: model.attribute("hiringManagerMap",hiringManagerMap); 我正在访问此 hiringManagerMap
我想知道如何以正确的方式清空列表。在 div 中有一个列表然后清空 div 或列表更好吗? 我知道这是一个蹩脚的问题,但请帮助我理解这个 empty() 函数:) 案例)如果我运行这个脚本会发生什么:
我是一名优秀的程序员,十分优秀!