Skip to content
Snippets Groups Projects
Commit 73cc9a5e authored by Jameson Rollins's avatar Jameson Rollins
Browse files

ifo hash and Noise.run() opportunistically run update()

Add a Struct.hash() method to return a unique hash over attributes.

Modify Noise.run() method to check for changes to the ifo and only run
the update method if the ifo has changed or other kwargs are supplied.
parent dc1d71b1
No related branches found
No related tags found
1 merge request!95precomp decorator support
......@@ -220,14 +220,36 @@ class Noise(BudgetItem):
return self._make_trace(psd=total)
def run(self, **kwargs):
"""Convenience method to load, update, and return calc traces.
"""Calculate budget noise and return BudgetTrace object.
Equivalent of load(), update(), calc_traces() run in sequence.
Keyword arguments are passed to update().
Roughly the equivalent running load(), update(), and
calc_trace() in sequence. Keyword arguments are passed to the
update() method.
NOTE: The update() method is only run if keyword arguments
(`kwargs`) are supplied, or if the `ifo` attribute has
changed.
"""
self.load()
self.update(**kwargs)
ifo = kwargs.get('ifo', getattr(self, 'ifo'))
if ifo:
if not hasattr(ifo, '_orig_keys'):
logger.debug("new ifo detected")
ifo._orig_keys = tuple(k for k, v in ifo.walk())
ifo_hash = ifo.hash()
kwargs['ifo'] = ifo
else:
ifo_hash = ifo.hash(ifo._orig_keys)
if ifo_hash != getattr(self, '_ifo_hash', 0):
logger.debug("ifo hash change")
kwargs['ifo'] = self.ifo
self._ifo_hash = ifo_hash
if kwargs:
self.update(**kwargs)
return self.calc_trace()
......
......@@ -222,6 +222,8 @@ class Struct(object):
"""
for k,v in self.__dict__.items():
if k[0] == '_':
continue
if isinstance(v, type(self)):
for sk,sv in v.walk():
yield k+'.'+sk, sv
......@@ -233,6 +235,25 @@ class Struct(object):
except (AttributeError, TypeError):
yield k, v
def hash(self, keys=None):
"""Hash of Struct.walk() data (recursive)
"""
def filter_keys(kv):
k, v = kv
if keys:
return k in keys
else:
return True
def map_tuple(kv):
k, v = kv
if isinstance(v, list):
return k, tuple(v)
else:
return k, v
return hash(tuple(sorted(
map(map_tuple, filter(filter_keys, self.walk()))
)))
def diff(self, other):
"""Return tuple of differences between target IFO.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment