繁体   English   中英

如何将 H5 表查询结果转换为 Astropy 表?

[英]How can I turn an H5 table query result into a Astropy Table?

通常我用SQL查询一个在线数据库,但是数据库宕机了。 我有一个 H5 文件,其中包含我需要查询的表。 我使用Table.read_where('condition')查询了该表,并且对于符合我的标准的每一行都有一个numpy.void元素列表。 有什么方法可以将该行列表放入 Astropy 表中? 这就是我以前使用的所有代码,我宁愿不必更改它。 这是我一直用来尝试将其转换为 Astropy 表的代码:

import tables
from astropy.table import Table
import numpy as np

Data = tables.open_file('file_path','r') #opens our .h5 file
DataTable = Data.root.TableName #Points to the table

#Queries the table for rows that meet my 'Condition', and outputs a list of numpy.void's 
#containing integers and floats. Each numpy.void represents a row in my table. 
result = [row for row in DataTable.read_where('Condition')]

#I try to turn the list of rows into a Astropy table to use in the rest of my code.
resultTable = Table(rows=result,names=('Column1','Column2','Column3'))

我得到的错误是:

Traceback (most recent call last):

  File "<ipython-input-2-aa9501cdbf2a>", line 1, in <module>
    runfile('FilePath', wdir='FilePath')

  File "Spyder File", line 827, in runfile
    execfile(filename, namespace)

  File "Spyder File", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "File Path", line 27, in <module>
    resultTable = Table(rows=result,names=('Column1','Column2','Column3'))

  File "FilePath/python3.7/site-packages/astropy/table/table.py", line 420, in __init__
    rec_data = recarray_fromrecords(rows)

  File "FilePath/lib/python3.7/site-packages/astropy/table/np_utils.py", line 196, in recarray_fromrecords
    return np.rec.fromarrays(array_list, formats=formats)

  File "FilePath/lib/python3.7/site-packages/numpy/core/records.py", line 645, in fromarrays
    _array[_names[i]] = arrayList[i]

ValueError: Can't cast from structure to non-structure, except if the structure only has a single field.

我尝试通过np.rec.fromrecords传递result以检查这是否有效,因为 [Astropy 文档] ( https://docs.astropy.org/en/stable/table/construct_table.html#construct-table ) 说必须能够通过那个 function。 它可以正常工作,没有任何错误。 我不确定 go 从这里到哪里。

我的替代计划是创建一个由result中的行组成的 PyTables 表,并从中提取列为 numpy arrays 。 我宁愿坚持只使用 Astropy,因为我使用的代码是围绕 Astropy 构建的,坚持使用它而不是通过并将其更改为 PyTables 会更容易。

根据AstroPy Table 的文档,第一个参数可以是 NumPy 结构化数组或一维同构数组(相同类型)。

PyTables(表)function DataTable.read_where('Condition')返回一个 NumPy 记录数组,该数组匹配该表的描述(在 Z3B7F949B2343F9E5390E29F6 术语中称为 dtype)。 因此,您想使用返回的数组来创建您的 AstroPy 表。 在 Pytables 调用中,您不需要row for row in 只需使用result = DataTable.read_where('Condition')

注意:默认情况下, astropy.table将使用 NumPy 数组中的字段名称。 您可以使用 ( print (result.dtype)来查看它们。添加names=参数会使用您提供的名称覆盖默认名称。

更新的代码显示了以下更改:

Data = tables.open_file('file_path','r') #opens our .h5 file
DataTable = Data.root.TableName #Points to the PyTable table

#Queries the table for rows that meet my 'Condition', and 
# returns a NumPy record array with dtype matching the table description
result = DataTable.read_where('Condition')

#reference the Numpy Array to create a AstroPy table for use in the rest of my code.
resultTable = Table(rows=result,names=('Column1','Column2','Column3'))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM