将PyTables / HDF5文件中的所有数组从float64转换为float32

时间:2017-07-04 17:33:50

标签: arrays numpy hdf5 pytables h5py

我有一个包含大量子目录的PyTables文件。我有一种方法可以遍历表中的所有数组数据类型。它们是float64;我想将所有数据点从float64转换为float32,然后将文件转换为

According to this question,覆盖数组的一种方法是分配值。我有以下代码片段试图采取这个"计数"表中的value /数组,将其转换为float32,并将其分配回表:

import h5py
import numpy as np

# filehead is a string for a file
with h5py.File(filehead, 'r+') as f:
    # Lots of stuff here ... e.g. `head` is a string

    print("/obsnorm/Standardizer/count {}".format(f[head+'/obsnorm/Standardizer/count']))
    print("count value: {}".format(f[head+'/obsnorm/Standardizer/count'].value))
    f[head+'/obsnorm/Standardizer/count'][...] = (f[head+'/obsnorm/Standardizer/count'].value).astype('float32')
    print("/obsnorm/Standardizer/count {}".format(f[head+'/obsnorm/Standardizer/count']))
    print("count value: {}".format(f[head+'/obsnorm/Standardizer/count'].value))

不幸的是,打印的结果是:

/obsnorm/Standardizer/count <HDF5 dataset "count": shape (), type "<f8">
count value: 512364.0
/obsnorm/Standardizer/count <HDF5 dataset "count": shape (), type "<f8">
count value: 512364.0

换句话说,在赋值之前,计数的类型是f8或float64。在投射之后,类型仍然是 float64。

如何就地修改此数据,以便将数据真正理解为float32?

1 个答案:

答案 0 :(得分:1)

正如hpaulj在评论中所建议的那样,我决定简单地重新创建一个重复的HDF5文件,除了制作类型<?php $resourceURI = "http://nifi-eventhub.servicebus.windows.net/hub1"; $keyName = "hub-user"; $key = "secret"; $expiry = '1499177142'; // timestamp // The format for the string is <resourceURI> + \n + <expiry> $stringToSign = strtolower(rawurlencode($resourceURI)) . "\n" . $expiry; // Hash the URL encoded string using the shared access key $sig = hash_hmac("sha256", utf8_encode($stringToSign), utf8_encode($key), false); // Convert hexadecimal string to binary and then to base64 $sig = hex2bin($sig); $sig = base64_encode($sig); // 7kS3apSDpJFTYI1vxuo4t7syGG3FTBYI8foamMOtrEE= echo $sig . "<br>\n"; // Generate authorization token $token = "SharedAccessSignature sr=" . urlencode($resourceURI) . "&sig=" . rawurlencode($sig) . "&se=" . $expiry . "&skn=" . $keyName; echo $token . "<br>\n"; 的数据集(与float32相同)并且我能够实现我的编码目标。

伪代码如下:

string expiry = "1499177142";
string resourceUri = "http://nifi-eventhub.servicebus.windows.net/hub1";
string keyName = "hub-user";
string secretkey = "secret";

string stringToSign = HttpUtility.UrlEncode(resourceUri) + "\n" + expiry;

HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(secretkey));
byte[] hashBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign));

var signature = Convert.ToBase64String(hashBytes);

// 7kS3apSDpJFTYI1vxuo4t7syGG3FTBYI8foamMOtrEE=
Console.WriteLine(signature);

var sasToken = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), expiry, keyName);

Console.WriteLine(sasToken);