[英]How to save a QTable to a file (.txt, .csv, etc.)
I am trying to save a QTable object which originates from a Isophote list of an elliptical isophote fit, to really any filetype (.txt, .csv, etc.) that allows me to simply just load it back into the script without having to clean it or any of that sorts.我正在尝试将 QTable object 保存为任何文件类型(.txt、.csv 等),它源自椭圆等光度拟合的等光度列表,让我只需将其加载回脚本而无需清理它或任何一种。
example_isolist = ellipse_example.fit_image(sclip=3., nclip=3) #performing elliptical isophote fit and creating the Isophote list
example_isolist_tab = example_isolist.to_table() #converting to QTable
import json
with open("example_isolist_tab.txt", 'w') as f:
json.dump(example_isolist_tab, f, indent=2)
Using json here does not work.在这里使用 json 不起作用。 The following error occurrs:出现以下错误:
TypeError: Object of type QTable is not JSON serializable
Anybody here got experience with photutils data handling or saving an isophote fit to a file?这里的任何人都有过 photutils 数据处理或将等光度适合保存到文件的经验吗? It should be a very simple thing really, just a way so I don't have to rerun my entire script each time I want to work with the results from the isophote fit.这应该是一件非常简单的事情,只是一种方式,这样我就不必每次想要使用等光度拟合的结果时都重新运行整个脚本。 My whole data set contains 26 images which would mean roughly 2.5h computation time without saving in between.我的整个数据集包含 26 张图像,这意味着大约需要 2.5 小时的计算时间,而不会在两者之间进行保存。
Thanks in advance!提前致谢!
Astropy already has numerous built-in formats for saving Tables with Table.write . Astropy 已经有许多用于保存表格的内置格式Table.write 。
For QTable
in particular, if you want a round-trippable text-based format the ECSV format is highly recommended, as it also outputs metadata concerning each column's types and units.特别是对于QTable
,如果您想要一个可往返的基于文本的格式,强烈建议使用ECSV格式,因为它还输出有关每列类型和单位的元数据。 If you name your file with a .ecsv
extension, this format is used automatically.如果您使用.ecsv
扩展名命名文件,则会自动使用此格式。 For example:例如:
>>> from astropy.table import QTable
>>> import astropy.units as u
>>> import numpy as np
>>> a = np.array([1, 4, 5], dtype=np.int32)
>>> b = [2.0, 5.0, 8.5]
>>> c = ['x', 'y', 'z']
>>> d = [10, 20, 30] * u.m / u.s
>>> t = QTable([a, b, c, d],
... names=('a', 'b', 'c', 'd'),
... meta={'name': 'first table'})
>>> t.write('table.ecsv')
>>> print(open('table.ecsv').read())
# %ECSV 0.9
# ---
# datatype:
# - {name: a, datatype: int32}
# - {name: b, datatype: float64}
# - {name: c, datatype: string}
# - {name: d, unit: m / s, datatype: float64}
# meta:
# __serialized_columns__:
# d:
# __class__: astropy.units.quantity.Quantity
# unit: !astropy.units.Unit {unit: m / s}
# value: !astropy.table.SerializedColumn {name: d}
# name: first table
# schema: astropy-2.0
a b c d
1 2.0 x 10.0
4 5.0 y 20.0
5 8.5 z 30.0
>>> QTable.read('table.ecsv')
<QTable length=3>
a b c d
m / s
int32 float64 str1 float64
----- ------- ---- -------
1 2.0 x 10.0
4 5.0 y 20.0
5 8.5 z 30.0
Use the astropy ECSV writer to write a QTable
losslessly:使用 astropy ECSV编写器无损地编写QTable
:
>>> example_isolist_tab.write('example_isolist_tab.ecsv')
>>> t = QTable.read('example_isolist_tab.ecsv') # returns the same table
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.