From 18da094de0d580d52d179ce8cd5c9d4137accfa6 Mon Sep 17 00:00:00 2001 From: Jameson Graef Rollins <jrollins@finestructure.net> Date: Wed, 5 Feb 2020 22:26:40 -0800 Subject: [PATCH] introduce Noise/Budget .run() method to simplify basic usage Should cover/simplify the most common use case, equivalent to running: load() update(**kwargs) calc_traces --- README.md | 2 +- gwinc/__init__.py | 2 +- gwinc/__main__.py | 5 +---- gwinc/nb.py | 15 +++++++++++++-- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1b780f37..bb04aa78 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ description is loaded, the noise budget can be calculated and plotted: >>> freq = np.logspace(1, 3, 1000) >>> Budget = gwinc.load_budget('aLIGO') >>> ifo = gwinc.precompIFO(freq, Budget.ifo) ->>> traces = Budget(freq, ifo=ifo).calc_trace() +>>> traces = Budget(freq, ifo=ifo).run() >>> fig = gwinc.plot_noise(freq, traces) >>> fig.show() ``` diff --git a/gwinc/__init__.py b/gwinc/__init__.py index 18ca39b1..3347d489 100644 --- a/gwinc/__init__.py +++ b/gwinc/__init__.py @@ -113,7 +113,7 @@ def gwinc(freq, ifo, source=None, plot=False, PRfixed=True): # from just ifo description, without having to specify full budget Budget = load_budget('aLIGO') ifo = precompIFO(freq, ifo, PRfixed) - traces = Budget(freq, ifo=ifo).calc_trace() + traces = Budget(freq, ifo=ifo).run() plot_style = getattr(Budget, 'plot_style', {}) # construct matgwinc-compatible noises structure diff --git a/gwinc/__main__.py b/gwinc/__main__.py index d1c81943..d6b220f3 100644 --- a/gwinc/__main__.py +++ b/gwinc/__main__.py @@ -179,10 +179,7 @@ def main(): logging.info("precomputing ifo...") ifo = precompIFO(freq, ifo) logging.info("calculating budget...") - budget = Budget(freq=freq, ifo=ifo) - budget.load() - budget.update() - traces = budget.calc_trace() + traces = Budget(freq=freq, ifo=ifo).run() # logging.info('recycling factor: {: >0.3f}'.format(ifo.gwinc.prfactor)) # logging.info('BS power: {: >0.3f} W'.format(ifo.gwinc.pbs)) diff --git a/gwinc/nb.py b/gwinc/nb.py index 9434ef13..18c09a4d 100644 --- a/gwinc/nb.py +++ b/gwinc/nb.py @@ -32,7 +32,7 @@ class BudgetItem: return None def update(self, **kwargs): - """Overload method for updating data needed to calculate final PSD. + """Overload method for updating data. By default any keyword arguments provided are written directly as attribute variables (as with __init__). @@ -42,7 +42,7 @@ class BudgetItem: setattr(self, key, val) def calc(self): - """Overload method for calculation of final PSD. + """Overload method for final PSD calculation. Should return an array of power-referenced values evaluated at all evaluation frequencies (self.freq). @@ -152,6 +152,17 @@ class Noise(BudgetItem): data = None return data, self.style + def run(self, **kwargs): + """Convenience method to load, update, and return calc traces. + + Equivalent of load(), update(), calc_traces() run in sequence. + Keyword arguments are passed to update(). + + """ + self.load() + self.update(**kwargs) + return self.calc_trace() + class Budget(Noise): """GWINC Budget class -- GitLab