[英]Save results in YAML file with python
I'm stuck in this little problem after an hour of searching previous answer. 经过一个小时的搜索,我陷入了这个小问题。 I want to store matrices from my code in .yaml file
我想将我的代码中的矩阵存储在.yaml文件中
What I obtain from my code 我从代码中得到什么
Matrix
[[ 1.00665266e+03 0.00000000e+00 5.08285432e+02]
[ 0.00000000e+00 1.01086937e+03 3.45995536e+02]
[ 0.00000000e+00 0.00000000e+00 1.00000000e+00]]
How I tried to save this matrix ( mtx
is the shorter name in my code) 我如何尝试保存此矩阵(
mtx
是代码中的mtx
)
fname = "calibrationC300.yaml"
data = dict(
Matrix = mtx,
)
with open(fname, "w") as f:
yaml.dump(data, f, default_flow_style=False)
But what I read in my YAML file is totally wrong (just only bad conversion?) 但是我在YAML文件中读取的内容完全错误(只是转换不好?)
Matrix: !!python/object/apply:numpy.core.multiarray._reconstruct
args:
- &id001 !!python/name:numpy.ndarray ''
- !!python/tuple [0]
- b
state: !!python/tuple
- 1
- !!python/tuple [3, 3]
- !!python/object/apply:numpy.dtype
args: [f8, 0, 1]
state: !!python/tuple [3, <, null, null, null, -1, -1, 0]
- false
- !!binary |
cWM87e1YkEAAAAAAAAAAAIUEEyb5SH1AAAAAAAAAAACp/Z3yc2qQQFv0vPqb5nZAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAPA/
It is the first time I use Yaml files, what am I doing wrong? 这是我第一次使用Yaml文件,我在做什么错? Is there a method to obtain the matrix in the simple form (as I obtain it from the code) in the yaml file?
是否有一种方法可以在yaml文件中以简单形式(从代码中获取)获取矩阵? Thank you in advance
先感谢您
The only thing wrong here seems to be your expectation of how numpy
internals can and should be dumped to YAML. 唯一错误的地方似乎是您对
numpy
内部结构如何可以并且应该被转储到YAML的期望。
An easy check to see that what you have gotten as YAML is correct, is to load
what you dump
-ed: 一个简单的检查方法就是
load
您dump
-ed内容,以查看您在YAML中获得的内容是否正确:
import ruamel.yaml
import numpy
import pprint
mtx = [[1.00665266e+03, 0.00000000e+00, 5.08285432e+02],
[0.00000000e+00, 1.01086937e+03, 3.45995536e+02],
[0.00000000e+00, 0.00000000e+00, 1.00000000e+00],]
data = dict(Matrix=mtx)
yaml_str = ruamel.yaml.dump(data, default_flow_style=False)
data = ruamel.yaml.load(yaml_str)
print(data)
which gives: 这使:
{'Matrix': [[1006.65266, 0.0, 508.285432], [0.0, 1010.86937, 345.995536], [0.0, 0.0, 1.0]]}
The special types that numpy
uses are not dumped as simple (and readable) YAML, there is no guarantee that that could be reloaded. numpy
使用的特殊类型不会像简单的(和可读的)YAML一样转储,不能保证可以重新加载。 It might be possible for some constructs, although it easily leads to ambiguity, and AFAIK simplification it is not done for any of the numpy
types. 对于某些构造来说,这可能是可能的,尽管它很容易导致歧义,并且AFAIK简化并未针对任何
numpy
类型进行。
Of course you can dump that YAML without having numpy
supply its restore information, by doing: 当然,您可以执行以下操作来转储该YAML,而无需
numpy
提供其还原信息:
ruamel.yaml.round_trip_dump(data, sys.stdout)
which gives: 这使:
Matrix:
- - 1006.65266
- 0.0
- 508.285432
- - 0.0
- 1010.86937
- 345.995536
- - 0.0
- 0.0
- 1.0
much more readable, but not something that will ever become a numpy.multiarray automatically when you load()
it again from its YAML representation. 更具可读性,但当您再次从其YAML表示形式对其进行
load()
时,不会自动变为numpy.multiarray。
I'm not a expert as well, but the produced yaml seems correct. 我也不是专家,但所产生的Yaml似乎正确。 You can see dat the the primitive types are rendered well by default.
您可以看到默认情况下原始类型的渲染效果很好。 For the others you might have to take some action.
对于其他人,您可能需要采取一些措施。 Read here in the documentation .
在文档中阅读此处。
Good luck! 祝好运!
(I don't have enough rep. to comment, otherwise I would leave this post as a comment.) (我没有足够的代表对此发表评论,否则我将把这篇文章留为评论。)
The difference is between float and numpy.float64. float和numpy.float64之间是有区别的。 Yaml uses more sophisticated way to represent numpy.float64.
Yaml使用更复杂的方式表示numpy.float64。 You can change to float if you like more readble yaml.
如果您想要更多可读的Yaml,则可以更改为float。 See the following example:
请参见以下示例:
print(yaml.dump({'test': 1, 'data':float(0.2)}, default_flow_style=False))
print(yaml.dump({'test': 2, 'data':numpy.float64(0.2)}, default_flow_style=False))
Output are: 输出为:
data: 0.2
test: 1
data: !!python/object/apply:numpy.core.multiarray.scalar
- !!python/object/apply:numpy.dtype
args:
- f8
- 0
- 1
state: !!python/tuple
- 3
- <
- null
- null
- null
- -1
- -1
- 0
- !!binary |
mpmZmZmZyT8=
test: 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.