简体   繁体   English

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

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

Usually I query an online database with SQL, but the database is down.通常我用SQL查询一个在线数据库,但是数据库宕机了。 I have an H5 file containing the table I need to query.我有一个 H5 文件,其中包含我需要查询的表。 I queried the table using Table.read_where('condition') , and have a list of numpy.void elements for each row that fit my criteria.我使用Table.read_where('condition')查询了该表,并且对于符合我的标准的每一行都有一个numpy.void元素列表。 Is there any way to take that list of rows and make it into a Astropy table?有什么方法可以将该行列表放入 Astropy 表中? That's what all of my code previously used, and I'd rather not have to change it.这就是我以前使用的所有代码,我宁愿不必更改它。 Here is the code I've been using to try and convert it into a Astropy table:这是我一直用来尝试将其转换为 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'))

The error I'm getting is:我得到的错误是:

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.

I tried passing result through np.rec.fromrecords to check if that is valid, since the [Astropy documentation] ( https://docs.astropy.org/en/stable/table/construct_table.html#construct-table ) says it has to be able to pass through that function.我尝试通过np.rec.fromrecords传递result以检查这是否有效,因为 [Astropy 文档] ( https://docs.astropy.org/en/stable/table/construct_table.html#construct-table ) 说必须能够通过那个 function。 It works without any errors.它可以正常工作,没有任何错误。 I'm not sure where to go from here.我不确定 go 从这里到哪里。

My alternative plan is to create a PyTables table of the made up of the rows in result , and pull columns as numpy arrays from that.我的替代计划是创建一个由result中的行组成的 PyTables 表,并从中提取列为 numpy arrays 。 I'd rather stick with just using Astropy since the code I'm using is built around Astropy and it would be easier to stick with that instead of going through and changing it to PyTables.我宁愿坚持只使用 Astropy,因为我使用的代码是围绕 Astropy 构建的,坚持使用它而不是通过并将其更改为 PyTables 会更容易。

Based on the documentation for AstroPy Table , the first argument can be a NumPy structured array or 1-d homogeneous array (same type).根据AstroPy Table 的文档,第一个参数可以是 NumPy 结构化数组或一维同构数组(相同类型)。

The PyTables (tables) function DataTable.read_where('Condition') returns a NumPy record array matching that Table's description (aka dtype in NumPy terminology). PyTables(表)function DataTable.read_where('Condition')返回一个 NumPy 记录数组,该数组匹配该表的描述(在 Z3B7F949B2343F9E5390E29F6 术语中称为 dtype)。 So, you want to use the returned array to create your AstroPy Table.因此,您想使用返回的数组来创建您的 AstroPy 表。 You don't need row for row in in the Pytables call;在 Pytables 调用中,您不需要row for row in just use result = DataTable.read_where('Condition') .只需使用result = DataTable.read_where('Condition')

Note: By default, astropy.table will use the field names from the NumPy array.注意:默认情况下, astropy.table将使用 NumPy 数组中的字段名称。 You can view them with ( print (result.dtype) . Adding the names= parameter overrides the default names with the names you provide.您可以使用 ( print (result.dtype)来查看它们。添加names=参数会使用您提供的名称覆盖默认名称。

Updated code showing that change below:更新的代码显示了以下更改:

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