DMelt:IO/7 YAML File Format

From HandWiki
Revision as of 12:16, 14 February 2021 by imported>Jworkorg
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Member


YAML file format

DataMelt includes library to handle YAML data format. The YAML format is described in YAML web page. The included Java implementation is based on SnakeYAML. The Java API can be found in SnakeYAML API SnakeYAML API.

DMelt supports writing and reading YAML files using the jhplot.io.HFileYAML jhplot.io.HFileYAML class. It keeps data in the form key-values. Most DMelt arrays and histograms are supported,

The YAML files can hold many Java and Python object in a form of simple text files. This is useful for exchange of data between different programming languages. The files are typically smaller then similar XML files since no tags is used. This feature makes YAML files attractive to keep numeric data.

For example, Java ArrayList or Python list will be written to the text file exactly it should look:

[1,2,3,4,5]

You can also write lists, maps (dictionaries) to external files in arbitrary order. Then you can read such text files back, creating Java lists (Python lists) on the fly. Also, objects such as P1D, histograms (H1D) are supported.

This is an example of parsing YAML string messages:

This is an example:

from jhplot.io import *
f=HFileYAML("test.yaml","w")
data=[1,2,3,4]
f.write("t0",data)
f.close()
# reading  yaml file
f=HFileYAML("test.yaml")
print f.read("t0")
f.close()

Let us consider how to store multi-dimensional data and histograms in the YAML files.

from jhplot.io import *
from jhplot import *

print "Write YAML data"
f=HFileYAML("test.yaml","w")

p0=P0D("test1");
p0.randomUniform(100,0,100)

p1=P1D("test2")
p1.add(10,20,3);
p1.add(20,40,3);
p1.add(30,80,3);
p1.add(40,60,3);

p2=P2D("test3")
p2.add(1,2,4)
p2.add(3,3,5)

f.write("t0",p0)
f.write("t1",p1)
f.write("t2",p2)
f.close()

print "Read YAML data:"
f=HFileYAML("test.yaml")
print f.read("t0")
print f.read("t1")
print f.read("t2")
f.close()