Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hfile pycbc version information check #4768

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions bin/all_sky_search/pycbc_add_statmap
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
with more than one ifo combination available. Cluster to keep foreground
coincs with the highest stat value.
"""
import h5py, numpy as np, argparse, logging, pycbc, pycbc.events, pycbc.io
import numpy as np, argparse, logging, pycbc, pycbc.events, pycbc.io
import pycbc.version
import pycbc.conversions as conv
from pycbc.events import coinc, significance
from pycbc.io import HFile
from ligo import segments
import sys, copy

Expand Down Expand Up @@ -91,9 +92,9 @@ else :

pycbc.init_logging(args.verbose)

files = [h5py.File(n, 'r') for n in args.statmap_files]
files = [HFile(n, 'r') for n in args.statmap_files]

f = h5py.File(args.output_file, "w")
f = HFile(args.output_file, "w")

# Work out the combinations of detectors used by each input file
all_ifo_combos = []
Expand Down Expand Up @@ -308,7 +309,7 @@ if injection_style:
# if background files are provided, this is being used for injections
# use provided background files to calculate the FARs
for bg_fname in args.background_files:
bg_f = h5py.File(bg_fname, 'r')
bg_f = HFile(bg_fname, 'r')
ifo_combo_key = bg_f.attrs['ifos'].replace(' ','')
_, far[ifo_combo_key] = significance.get_far(
bg_f['background/stat'][:],
Expand Down
36 changes: 36 additions & 0 deletions pycbc/io/hdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,47 @@
from pycbc import events, conversions, pnutils
from pycbc.events import ranking, veto
from pycbc.events import mean_if_greater_than_zero
from pycbc.version import version as pycbc_version


class HFile(h5py.File):
""" Low level extensions to the capabilities of reading an hdf5 File
"""
def __init__(
self,
filename,
permission='r',
check_pycbc_version=True,
**kwargs
):
h5py.File.__init__(self, filename, permission, **kwargs)
if permission in ['r','r+', 'a'] and check_pycbc_version:
# Check the pycbc version in the file matches the current one
if 'pycbc_version' in self.attrs:
if not self.attrs['pycbc_version'] == pycbc_version:
logging.info(
"PyCBC version of the file (%s) does not match the "
"one currently being used (%s). Results may not be "
"as expected.",
self.attrs['pycbc_version'],
pycbc_version
)
if permission in ['w', 'x', 'w-', 'r+', 'a'] and check_pycbc_version:
if permission in ['r+','a']:
original_version = self.attrs['pycbc_version'] \
if 'pycbc_version' in self.attrs else 'None'
if original_version != 'None' and not \
original_version == pycbc_version:
# Read/write on the file - update the pycbc version,
# but warn
logging.warning(
"File opened with read and write permissions, "
"updating pycbc_version from %s to %s.",
original_version,
pycbc_version
)
self.attrs['pycbc_version'] = pycbc_version

def select(self, fcn, *args, chunksize=10**6, derived=None, group='',
return_data=True, premask=None):
""" Return arrays from an hdf5 file that satisfy the given function
Expand Down
Loading