Saving and loading observables to/from disk

[1]:
import numpy
import pyobs

%matplotlib notebook

First let’s create an obseervable

[2]:
N=5000 # number of configs
mu = 0.5 # central values
cov = (mu*0.5)**2 # error^2

# autocorrelation time
tau=4.0

rng = pyobs.random.generator('tutorial')
data = rng.markov_chain(mu,cov,tau,N)

yobs = pyobs.observable()
yobs.create('ensA',data)
yobs
Random generator initialized with seed = 649309182 [tutorial]
[2]:
0.03(26)

and let’s add a systematic error (to test both the data and cdata subclasses)

[3]:
yobs.add_syst_err('syst.error',[0.04])
yobs.error(plot=True)
[3]:
[array([0.02686853]), array([0.25824497])]

If we use the bison file format we can save any python type to disk, including numpy arrays and observables. For simplicity we store all the relavant data in a dictionary.

[4]:
pyobs.save('./test-obs.pyobs',{
    'yobs': yobs, 'metadata': 'some text describing the observable',
    'parameter1': 0.345, 'array': numpy.array([0,4,5,7],dtype=numpy.int32)
});
[Bison] : Written 0.0411482 MB at 34.1488 MB/s

We check that the file has been created

[5]:
%ls -al test-obs.pyobs
-rw-r--r--  1 mbruno  staff  43147 May 11 09:50 test-obs.pyobs

We reload the file. As expected the returned object is a dictionary. Note the type of the numpy array is also preserved.

[6]:
pyobs.load('./test-obs.pyobs')
[Bison] : Reading file ./test-obs.pyobs
[Bison] : File created by mbruno at macthxbruno.local on Wed May 11 09:50:38 2022
[Bison] : Read 0.0411482 MB at 6.86835 MB/s
[6]:
{'yobs': 0.03(26),
 'metadata': 'some text describing the observable',
 'parameter1': 0.345,
 'array': array([0, 4, 5, 7], dtype=int32)}
[7]:
%rm test-obs.pyobs