Skip to content

Commit

Permalink
Make arguments attributes, integrate with AutoContext
Browse files Browse the repository at this point in the history
This makes them accessible to future "data analysis" methods.
sbourdeauducq committed Jan 10, 2015
1 parent 2ad063c commit 06914bb
Showing 5 changed files with 59 additions and 29 deletions.
46 changes: 35 additions & 11 deletions artiq/language/context.py
Original file line number Diff line number Diff line change
@@ -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 @@ def __init__(self, default=NoDefault, write_db=False):
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 @@ def __init__(self, mvs=None, **kwargs):
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()
4 changes: 2 additions & 2 deletions artiq/management/worker_impl.py
Original file line number Diff line number Diff line change
@@ -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():
13 changes: 8 additions & 5 deletions examples/photon_histogram.py
Original file line number Diff line number Diff line change
@@ -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 @@ def cool_detect(self):
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)
21 changes: 12 additions & 9 deletions examples/transport.py
Original file line number Diff line number Diff line change
@@ -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 @@ def one(self):
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)
4 changes: 2 additions & 2 deletions frontend/artiq_run.py
Original file line number Diff line number Diff line change
@@ -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:")

0 comments on commit 06914bb

Please sign in to comment.