Added check for valid function expression.

This commit is contained in:
jupfi 2023-07-29 16:34:03 +02:00
parent 18e25155a3
commit a1bf3df72c
2 changed files with 30 additions and 5 deletions

View file

@ -21,7 +21,7 @@ class Function:
def __init__(self, expr) -> None:
self.parameters = []
self.expr = expr
self.resolution = 16/30.72e6
self.resolution = 1/30.72e6
self.start_x = -1
self.end_x = 1
@ -38,10 +38,6 @@ class Function:
t = np.linspace(self.start_x, self.end_x, n)
x = sympy.symbols("x")
# If the expression is a string, convert it to a sympy expression
if isinstance(self.expr, str):
self.expr = sympy.sympify(self.expr)
found_variables = dict()
# Create a dictionary of the parameters and their values
for parameter in self.parameters:
@ -110,6 +106,22 @@ class Function:
obj.add_parameter(Function.Parameter.from_json(parameter))
return obj
@property
def expr(self):
return self._expr
@expr.setter
def expr(self, expr):
if isinstance(expr, str):
try:
self._expr = sympy.sympify(expr)
except:
logger.error("Could not convert %s to a sympy expression", expr)
raise SyntaxError("Could not convert %s to a sympy expression" % expr)
elif isinstance(expr, sympy.Expr):
self._expr = expr
class Parameter:
def __init__(self, name: str, symbol: str, value: float) -> None:

View file

@ -102,3 +102,16 @@ class PulseSequence:
obj.events.append(cls.Event.load_event(event_data, pulse_parameter_options))
return obj
class Variable:
""" A variable is a parameter that can be used within a pulsesequence as a placeholder.
For example the event duration a Variable with name a can be set. This variable can then be set to a list of different values.
On execution of the pulse sequence the event duration will be set to the first value in the list.
Then the pulse sequence will be executed with the second value of the list. This is repeated until the pulse sequence has
been executed with all values in the list."""
pass
class VariableGroup:
""" Variables can be grouped together.
If we have groups a and b the pulse sequence will be executed for all combinations of variables in a and b."""
pass