微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有没有办法在第二维中扩展 PyTables EArray?

如何解决有没有办法在第二维中扩展 PyTables EArray?

我有一个 2D 数组,它可以增长到比我能够容纳的内存更大的大小,所以我尝试使用 Pytables 将它存储在一个 h5 文件中。行数是预先知道的,但每行的长度是未知的,并且在行之间是可变的。经过一些研究,我认为沿着这些方向的一些东西会起作用,我可以将可扩展维度设置为第二个维度。

filename = os.path.join(tempfile.mkdtemp(),'example.h5')
h5_file = open_file(filename,mode="w",title="Example Extendable Array")
h5_group = h5_file.create_group("/","example_on_dim_2")
e_array = h5_file.create_earray(h5_group,"example",Int32Atom(shape=()),(100,0)) # Assume num of rows is 100

# Add some item to index 2
print(e_array[2]) # should print an empty array
e_array[2] = np.append(e_array[2],5) # add the value 5 to row 2
print(e_array[2]) # should print [5],currently printing empty array

我不确定是否可以以这种方式添加元素(我可能误解了 earrays 的工作方式),但任何帮助将不胜感激!

解决方法

你很接近...但对一些论点和行为有一点误解。当您使用 shape=(100,0) 创建 EArray 时,您没有任何数据......只是一个指定为具有 100 行可以添加列的对象。此外,您需要使用 e_array.append() 来添加数据,而不是 np.append()。此外,如果您要创建一个非常大的数组,请考虑定义 expectedrows= 参数以在 EArray 增长时提高性能。

看看这段代码。

import tables as tb
import numpy as np

filename = 'example.h5'
with tb.File(filename,mode="w",title="Example Extendable Array") as h5_file :
    h5_group = h5_file.create_group("/","example_on_dim_2")
    # Assume num of rows is 100
    #e_array = h5_file.create_earray(h5_group,"example",Int32Atom(shape=()),(100,0)) 
    e_array = h5_file.create_earray(h5_group,atom=tb.IntAtom(),shape=(100,0)) 
    print (e_array.shape)
    e_array.append(np.arange(100,dtype=int).reshape(100,1)) # append a column of values
    print (e_array.shape)
    print(e_array[2]) # prints [2]
,

以下示例展示了如何创建 VLArray(可变长度)。它类似于上面的 EArray 示例,并遵循 Pytables 文档中的示例(上面注释中的链接)。 然而,尽管 VLArray 支持可变长度的行,它没有将项目添加到现有行的机制(AFAIK)。

function get_api_data() {


$url = "https://api.mysite.com/?access_token=1234567";


$arguments = array(
    'method' => 'POST',$response = wp_remote_post( $url,$arguments );

if ( is_wp_error( $response ) ) {
    $error_message = $response->get_error_message();
    return "Something went wrong: $error_message";
} else {
    echo '<pre>';
    var_dump( wp_remote_retrieve_body( $response ) );
    echo '</pre>';
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。