Skip to content

Commit

Permalink
Expression.evaluate: avoid exec by using locals
Browse files Browse the repository at this point in the history
  • Loading branch information
mathause committed Oct 1, 2024
1 parent 660c45c commit 538aa38
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions mesmer/mesmer_x/train_utils_mesmerx.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,19 +353,14 @@ def evaluate(self, coefficients_values, inputs_values, forced_shape=None):
if len(shapes) > 1:
raise ValueError("Different shapes of inputs detected.")

# Evaluation 1: coefficients
for c in coefficients_values:
exec(c + " = coefficients_values[c]")

# Evaluation 2: inputs
for i in inputs_values:
exec(i + " = inputs_values[i]")
# gather coefficients and covariates (can't use d1 | d2, does not work for dataset)
locals = {**coefficients_values, **inputs_values}

# Evaluation 3: parameters
self.parameters_values = {}
for param in self.parameters_list:
for param, expr in self.parameters_expressions.items():
# may need to silence warnings here, to avoid spamming
self.parameters_values[param] = eval(self.parameters_expressions[param])
self.parameters_values[param] = eval(expr, None, locals)

# Correcting shapes 1: scalar parameters must have the shape of the inputs
if len(self.inputs_list) > 0:
Expand Down

0 comments on commit 538aa38

Please sign in to comment.