From a32faf2a72d1b6a85a4e3ed51bc2217832b7e250 Mon Sep 17 00:00:00 2001 From: GarethCabournDavies Date: Fri, 31 May 2024 05:10:10 -0700 Subject: [PATCH 1/4] Add a check for pycbc version on HFile opening --- pycbc/io/hdf.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/pycbc/io/hdf.py b/pycbc/io/hdf.py index 3c8cec35660..81037adef8a 100644 --- a/pycbc/io/hdf.py +++ b/pycbc/io/hdf.py @@ -24,11 +24,48 @@ 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.warning( + "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 From 6ddebf8a04b9fedc62336b0a10a59a53bb2c0b05 Mon Sep 17 00:00:00 2001 From: GarethCabournDavies Date: Fri, 31 May 2024 06:35:22 -0700 Subject: [PATCH 2/4] Use in add_statmap --- bin/all_sky_search/pycbc_add_statmap | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bin/all_sky_search/pycbc_add_statmap b/bin/all_sky_search/pycbc_add_statmap index 1b8b7006e67..1919ab1edeb 100755 --- a/bin/all_sky_search/pycbc_add_statmap +++ b/bin/all_sky_search/pycbc_add_statmap @@ -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 @@ -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 = [] @@ -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'][:], From a6a081d721b7067dfa92c07a890df0f47e063162 Mon Sep 17 00:00:00 2001 From: GarethCabournDavies Date: Fri, 31 May 2024 06:52:52 -0700 Subject: [PATCH 3/4] Different versions change from warning to info --- pycbc/io/hdf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pycbc/io/hdf.py b/pycbc/io/hdf.py index 81037adef8a..e89cbdc61f3 100644 --- a/pycbc/io/hdf.py +++ b/pycbc/io/hdf.py @@ -42,7 +42,7 @@ def __init__( # 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.warning( + logging.info( "PyCBC version of the file (%s) does not match the " "one currently being used (%s). Results may not be " "as expected.", From bf07f97adfc6dfc60f3d819901ceb8027bbae0e4 Mon Sep 17 00:00:00 2001 From: GarethCabournDavies Date: Fri, 31 May 2024 07:36:13 -0700 Subject: [PATCH 4/4] CC --- pycbc/io/hdf.py | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/pycbc/io/hdf.py b/pycbc/io/hdf.py index e89cbdc61f3..55feb3a39ed 100644 --- a/pycbc/io/hdf.py +++ b/pycbc/io/hdf.py @@ -31,12 +31,12 @@ 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 - ): + 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 @@ -54,18 +54,17 @@ def __init__( 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 - ) + 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