Source code for qharv.sieve.scalar_h5
import os
import h5py
import numpy as np
[docs]def twist_average_h5(fh5, weights=None, **suffix_kwargs):
""" twist average data in an HDF5 archive
see extract_twists for h5 file format
Args:
fh5 (str): h5 file location
Return:
(dict, np.array, np.array): (meta data, mean, error)
"""
from qharv.sieve.mean_df import taw
meta, yma, yea = extract_twists(fh5, **suffix_kwargs)
ntwist = len(yma)
# twist average with weights
if weights is None:
weights = np.ones(ntwist)
else:
if len(weights) != ntwist:
raise RuntimeError('%d weights for %d twists' % (len(weights), ntwist))
ym, ye = taw(yma, yea, weights)
return meta, ym, ye
[docs]def get_ymean_yerror(fp, twist0, msuffix='_mean', esuffix='_error'):
ymean = None
yerror = None
for name in fp[twist0].keys():
if name.endswith(msuffix):
ymean = name
if name.endswith(esuffix):
yerror = name
if ymean is None:
raise RuntimeError('no entry with suffix %s' % msuffix)
if yerror is None:
raise RuntimeError('no entry with suffix %s' % esuffix)
ynamem = '_'.join(ymean.split('_')[:-1])
ynamee = '_'.join(yerror.split('_')[:-1])
if ynamem != ynamee:
raise RuntimeError('yname mismatch')
return ymean, yerror
[docs]def twist_concat_h5(fh5, name, twists=None):
fp = h5py.File(fh5, 'r')
if twists is None:
twists = fp.keys()
data = []
for twist in twists:
path = os.path.join(twist, name)
val = fp[path][()]
data.append(val)
fp.close()
return np.concatenate(data, axis=0)