forked from M-Labs/artiq
1
0
Fork 0

language/scan: add argument processor

This commit is contained in:
Sebastien Bourdeauducq 2015-07-19 16:38:33 +02:00
parent 937ca853aa
commit bb05ed268e
2 changed files with 78 additions and 1 deletions

View File

@ -1,10 +1,12 @@
from artiq.language import core, environment, units
from artiq.language import core, environment, units, scan
from artiq.language.core import *
from artiq.language.environment import *
from artiq.language.units import *
from artiq.language.scan import *
__all__ = []
__all__.extend(core.__all__)
__all__.extend(environment.__all__)
__all__.extend(units.__all__)
__all__.extend(scan.__all__)

View File

@ -1,6 +1,27 @@
from random import Random, shuffle
import inspect
from artiq.language.core import *
from artiq.language.environment import NoDefault, DefaultMissing
__all__ = ["NoScan", "LinearScan", "RandomScan", "ExplicitScan", "Scannable"]
class NoScan:
def __init__(self, value):
self.value = value
@portable
def _gen(self):
yield self.value
@portable
def __iter__(self):
return self._gen()
def describe(self):
return {"ty": "NoScan", "value": self.value}
class LinearScan:
@ -20,6 +41,10 @@ class LinearScan:
def __iter__(self):
return self._gen()
def describe(self):
return {"ty": "LinearScan",
"min": self.min, "max": self.max, "npoints": self.npoints}
class RandomScan:
def __init__(self, min, max, npoints, seed=0):
@ -30,6 +55,11 @@ class RandomScan:
def __iter__(self):
return iter(self.sequence)
def describe(self):
return {"ty": "RandomScan",
"min": self.min, "max": self.max, "npoints": self.npoints}
class ExplicitScan:
def __init__(self, sequence):
@ -38,3 +68,48 @@ class ExplicitScan:
@portable
def __iter__(self):
return iter(self.sequence)
def describe(self):
return {"ty": "ExplicitScan", "sequence": self.sequence}
_ty_to_scan = {
"NoScan": NoScan,
"LinearScan": LinearScan,
"RandomScan": RandomScan,
"ExplicitScan": ExplicitScan
}
class Scannable:
def __init__(self, global_min=None, global_max=None, global_step=None,
unit="", default=NoDefault):
self.global_min = global_min
self.global_max = global_max
self.global_step = global_step
self.unit = unit
if default is not NoDefault:
self.default_value = default
def default(self):
if not hasattr(self, "default_value"):
raise DefaultMissing
return self.default_value
def process(self, x):
cls = _ty_to_scan[x["ty"]]
args = dict()
for arg in inspect.getargspec(cls).args[1:]:
if arg in x:
args[arg] = x[arg]
return cls(**args)
def describe(self):
d = {"ty": "Scannable"}
d["global_min"] = self.global_min
d["global_max"] = self.global_max
d["global_step"] = self.global_step
d["unit"]= self.unit
if hasattr(self, "default_value"):
d["default"] = self.default_value.describe()
return d