python - 如何使用 `slice` 对象读取 pytables.CArray?

标签 python numpy slice pytables

如何使用 slice 对象访问 tables.CArray?我目前拥有的是

In:  coord_slice
Out: [slice(0, 31, None), slice(0, 5760, None), slice(0, 2880, None)]

In:  _ds
Out:  /data/mydata (CArray(31, 5760, 2880), shuffle, blosc(5)) ''
      atom := Float32Atom(shape=(), dflt=0.0)
      maindim := 0
      flavor  := 'numpy'
      byteorder := 'little'
      chunkshape := (1, 45, 2880)

In: _ds[coord_slice]
Out: *** TypeError: long() argument must be a string or a number, not 'slice'

最佳答案

下面是来自 tables.CArray 文档的修改示例。如果 coord_slice 是一个 tuple 而不是 list,您的代码应该可以工作。 Here's a closed issue on Github with some clues as to why lists and tuples can't be used interchangeably .

import tables
import numpy

fileName = 'carray1.h5'
shape = (200, 300)
atom = tables.UInt8Atom()
filters = tables.Filters(complevel=5, complib='zlib')

h5f = tables.openFile(fileName, 'w')
ca = h5f.createCArray(h5f.root, 'carray', atom, shape, filters=filters)

coord_slice = [slice(10,60,None),slice(20,70,None)]

# attempt to do multi-dimensional indexing with coord_slice as a list
try:
    ca[coord_slice] = numpy.ones((50, 50))
except TypeError:
    print 'TypeError was thrown with list'

# convert coord_slice to a tuple and try again.  This should work.
coord_slice = tuple(coord_slice)
try:
    ca[coord_slice] = numpy.ones((50, 50))
except TypeError:
    print 'TypeError was thrown with tuple'


print ca[coord_slice]
h5f.close()

关于python - 如何使用 `slice` 对象读取 pytables.CArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14460483/

相关文章:

python - 如果 'W' 在 'X' 中,则将 'Y' 附加到 'Z'

python - 如何在 Python/Numpy 中以最快的方式获取 "n"矩阵的最大值?

arrays - python : append to numpy array

string - 切片包含 Unicode 字符的字符串

Javascript:如何将数组值复制到变量中

arrays - Go接口(interface) slice 和指针

python - fastapi - 从 main.py 导入配置

python - 如何系统地将列表中的一个数字与列表的其余部分进行比较 [Python]

python - 功能缩进不按预期工作

python - 使用另一个数组中的值构建 NumPy 数组