From 06914bbaa33310e8361430c3c1a8e1f43aa34058 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sat, 10 Jan 2015 15:41:35 +0800 Subject: [PATCH] Make arguments attributes, integrate with AutoContext This makes them accessible to future "data analysis" methods. --- artiq/language/context.py | 46 +++++++++++++++++++++++++-------- artiq/management/worker_impl.py | 4 +-- examples/photon_histogram.py | 13 ++++++---- examples/transport.py | 21 ++++++++------- frontend/artiq_run.py | 4 +-- 5 files changed, 59 insertions(+), 29 deletions(-) diff --git a/artiq/language/context.py b/artiq/language/context.py index 2fcd9d0aa..6bde60699 100644 --- a/artiq/language/context.py +++ b/artiq/language/context.py @@ -26,7 +26,8 @@ class NoDefault: class Parameter(_AttributeKind): - """Represents a parameter for ``AutoContext`` to process. + """Represents a parameter (from the database) for ``AutoContext`` + to process. :param default: Default value of the parameter to be used if not found in the database. @@ -39,6 +40,18 @@ class Parameter(_AttributeKind): self.write_db = write_db +class Argument(_AttributeKind): + """Represents an argument (specifiable at instance creation) for + ``AutoContext`` to process. + + :param default: Default value of the argument to be used if not specified + at instance creation. + + """ + def __init__(self, default=NoDefault, write_db=False): + self.default = default + + class AutoContext: """Base class to automate device and parameter discovery. @@ -112,22 +125,33 @@ class AutoContext: p = getattr(self, k) if isinstance(p, Parameter) and p.write_db: self.mvs.register_parameter_wb(self, k) + if (not hasattr(self, k) + or not isinstance(getattr(self, k), _AttributeKind)): + raise ValueError( + "Got unexpected keyword argument: '{}'".format(k)) setattr(self, k, v) for k in dir(self): v = getattr(self, k) if isinstance(v, _AttributeKind): - if self.mvs is None: - if (isinstance(v, Parameter) - and v.default is not NoDefault): - value = v.default - else: - raise AttributeError("Attribute '{}' not specified" - " and no MVS present".format(k)) + if isinstance(v, Argument): + # never goes through MVS + if v.default is NoDefault: + raise AttributeError( + "No value specified for argument '{}'".format(k)) + value = v.default else: - value = self.mvs.get_missing_value(k, v, self) - if isinstance(v, Parameter) and v.write_db: - self.mvs.register_parameter_wb(self, k) + if self.mvs is None: + if (isinstance(v, Parameter) + and v.default is not NoDefault): + value = v.default + else: + raise AttributeError("Attribute '{}' not specified" + " and no MVS present".format(k)) + else: + value = self.mvs.get_missing_value(k, v, self) + if isinstance(v, Parameter) and v.write_db: + self.mvs.register_parameter_wb(self, k) setattr(self, k, value) self.build() diff --git a/artiq/management/worker_impl.py b/artiq/management/worker_impl.py index fd9feaef8..00227c698 100644 --- a/artiq/management/worker_impl.py +++ b/artiq/management/worker_impl.py @@ -21,8 +21,8 @@ def run(dps, file, unit, arguments): unit = units[0] else: unit = getattr(module, unit) - unit_inst = unit(dps) - unit_inst.run(**arguments) + unit_inst = unit(dps, **arguments) + unit_inst.run() def get_object(): diff --git a/examples/photon_histogram.py b/examples/photon_histogram.py index a77349095..95b59769e 100644 --- a/examples/photon_histogram.py +++ b/examples/photon_histogram.py @@ -6,6 +6,9 @@ class PhotonHistogram(AutoContext): bdd = Device("dds") pmt = Device("ttl_in") + nbins = Argument(100) + repeats = Argument(100) + @kernel def cool_detect(self): with parallel: @@ -20,13 +23,13 @@ class PhotonHistogram(AutoContext): return self.pmt.count() @kernel - def run(self, nbins=100, repeats=100): - hist = [0 for _ in range (nbins)] + def run(self): + hist = [0 for _ in range(self.nbins)] - for i in range(repeats): + for i in range(self.repeats): n = self.cool_detect() - if n >= nbins: - n = nbins - 1 + if n >= self.nbins: + n = self.nbins - 1 hist[n] += 1 print(hist) diff --git a/examples/transport.py b/examples/transport.py index 3f4b3fe0d..5b19de851 100644 --- a/examples/transport.py +++ b/examples/transport.py @@ -19,6 +19,9 @@ class Transport(AutoContext): wait_at_stop = Parameter(100*us) speed = Parameter(1.5) + repeats = Argument(100) + nbins = Argument(100) + def prepare(self, stop): t = transport_data["t"][:stop]*self.speed u = transport_data["u"][:stop] @@ -91,27 +94,27 @@ class Transport(AutoContext): return self.detect() @kernel - def repeat(self, repeats, nbins): - self.histogram = [0 for _ in range(nbins)] + def repeat(self): + self.histogram = [0 for _ in range(self.nbins)] - for i in range(repeats): + for i in range(self.repeats): n = self.one() - if n >= nbins: - n = nbins - 1 + if n >= self.nbins: + n = self.nbins - 1 self.histogram[n] += 1 - def scan(self, repeats, nbins, stops): + def scan(self, stops): for s in stops: self.histogram = [] # non-kernel, calculate waveforms, build frames # could also be rpc'ed from repeat() self.prepare(s) # kernel part - self.repeat(repeats, nbins) + self.repeat() # live update 2d plot with current self.histogram # broadcast(s, self.histogram) - def run(self, repeats=100, nbins=100): + def run(self): # scan transport endpoint stops = range(10, len(transport_data["t"]), 10) - self.scan(repeats, nbins, stops) + self.scan(stops) diff --git a/frontend/artiq_run.py b/frontend/artiq_run.py index c6f825705..1d6222467 100755 --- a/frontend/artiq_run.py +++ b/frontend/artiq_run.py @@ -92,8 +92,8 @@ def main(): print("Failed to parse run arguments") sys.exit(1) - unit_inst = unit(dps) - unit_inst.run(**arguments) + unit_inst = unit(dps, **arguments) + unit_inst.run() if dps.parameter_wb: print("Modified parameters:")