Skip to content

Commit

Permalink
Detriplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
titodalcanton committed Feb 16, 2024
1 parent fdfd459 commit 349ad9c
Showing 1 changed file with 20 additions and 36 deletions.
56 changes: 20 additions & 36 deletions pycbc/events/eventmgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,23 @@ def cluster_reduce(idx, snr, window_size):
return idx.take(ind), snr.take(ind)


class H5FileSyntSugar(object):
"""Convenience class that adds some syntactic sugar to h5py.File.
"""
def __init__(self, name, prefix=''):
self.f = h5py.File(name, 'w')
self.prefix = prefix

def __setitem__(self, name, data):
self.f.create_dataset(
self.prefix + '/' + name,
data=data,
compression='gzip',
compression_opts=9,
shuffle=True
)


class EventManager(object):
def __init__(self, opt, column, column_types, **kwds):
self.opt = opt
Expand Down Expand Up @@ -420,23 +437,11 @@ def write_events(self, outname):
raise ValueError('Unsupported event output file format')

def write_to_hdf(self, outname):
class fw(object):
def __init__(self, name, prefix):
self.f = h5py.File(name, 'w')
self.prefix = prefix

def __setitem__(self, name, data):
col = self.prefix + '/' + name
self.f.create_dataset(col, data=data,
compression='gzip',
compression_opts=9,
shuffle=True)

self.events.sort(order='template_id')
th = numpy.array([p['tmplt'].template_hash for p in
self.template_params])
tid = self.events['template_id']
f = fw(outname, self.opt.channel_name[0:2])
f = H5FileSyntSugar(outname, self.opt.channel_name[0:2])

if len(self.events):
f['snr'] = abs(self.events['snr'])
Expand Down Expand Up @@ -703,19 +708,10 @@ def add_template_events_to_network(self, columns, vectors):
self.template_events = None

def write_to_hdf(self, outname):
class fw(object):
def __init__(self, name):
self.f = h5py.File(name, 'w')

def __setitem__(self, name, data):
col = self.prefix + '/' + name
self.f.create_dataset(
col, data=data, compression='gzip', compression_opts=9,
shuffle=True)
self.events.sort(order='template_id')
th = numpy.array(
[p['tmplt'].template_hash for p in self.template_params])
f = fw(outname)
f = H5FileSyntSugar(outname)
self.write_gating_info_to_hdf(f)
# Output network stuff
f.prefix = 'network'
Expand Down Expand Up @@ -972,23 +968,11 @@ def finalize_template_events(self, perform_coincidence=True,
dtype=self.event_dtype)

def write_to_hdf(self, outname):
class fw(object):
def __init__(self, name):
self.f = h5py.File(name, 'w')
self.prefix = ''

def __setitem__(self, name, data):
col = self.prefix + '/' + name
self.f.create_dataset(col, data=data,
compression='gzip',
compression_opts=9,
shuffle=True)

self.events.sort(order='template_id')
th = numpy.array([p['tmplt'].template_hash for p in
self.template_params])
tid = self.events['template_id']
f = fw(outname)
f = H5FileSyntSugar(outname)
self.write_gating_info_to_hdf(f)
for ifo in self.ifos:
f.prefix = ifo
Expand Down

0 comments on commit 349ad9c

Please sign in to comment.