Make arguments attributes, integrate with AutoContext

This makes them accessible to future "data analysis" methods.
This commit is contained in:
Sebastien Bourdeauducq 2015-01-10 15:41:35 +08:00
parent 2ad063c377
commit 06914bbaa3
5 changed files with 59 additions and 29 deletions

View File

@ -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()

View File

@ -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():

View File

@ -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)

View File

@ -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)

View File

@ -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:")