From d49a106b5ebb941768cf405b6126ba0a4c236d16 Mon Sep 17 00:00:00 2001 From: Lucian Smith Date: Wed, 21 Jun 2023 15:18:31 -0700 Subject: [PATCH 01/10] Update to use latest version of evalidate. cf https://github.com/yaroslaff/evalidate/issues/5 --- biosimulators_utils/sedml/math.py | 14 ++++++++++---- requirements.txt | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/biosimulators_utils/sedml/math.py b/biosimulators_utils/sedml/math.py index 6ac81ef2..e836b934 100644 --- a/biosimulators_utils/sedml/math.py +++ b/biosimulators_utils/sedml/math.py @@ -179,10 +179,16 @@ def compile_math(math): .replace('^', '**') ) - math_node = evalidate.evalidate(math, - addnodes=VALID_MATH_EXPRESSION_NODES, - funcs=MATHEMATICAL_FUNCTIONS.keys()) - compiled_math = compile(math_node, '', 'eval') + + model = evalidate.base_eval_model.clone() + model.nodes.extend(VALID_MATH_EXPRESSION_NODES) + model.allowed_functions.extend(MATHEMATICAL_FUNCTIONS.keys()) + + math_node = evalidate.Expr(math, model=model) + # math_node = evalidate.evalidate(math, + # addnodes=VALID_MATH_EXPRESSION_NODES, + # funcs=MATHEMATICAL_FUNCTIONS.keys()) + compiled_math = compile(math_node.code, '', 'eval') return compiled_math diff --git a/requirements.txt b/requirements.txt index d96612b0..d666f239 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ appdirs biopython cement -evalidate +evalidate >= 2.0.0 h5py kisao >= 2.32 lxml From 2cb847cd93788aa55fc98be39e9352a6478f965b Mon Sep 17 00:00:00 2001 From: Lucian Smith Date: Wed, 21 Jun 2023 15:24:43 -0700 Subject: [PATCH 02/10] Fix lint. --- biosimulators_utils/sedml/math.py | 1 - 1 file changed, 1 deletion(-) diff --git a/biosimulators_utils/sedml/math.py b/biosimulators_utils/sedml/math.py index e836b934..b06e2fb9 100644 --- a/biosimulators_utils/sedml/math.py +++ b/biosimulators_utils/sedml/math.py @@ -179,7 +179,6 @@ def compile_math(math): .replace('^', '**') ) - model = evalidate.base_eval_model.clone() model.nodes.extend(VALID_MATH_EXPRESSION_NODES) model.allowed_functions.extend(MATHEMATICAL_FUNCTIONS.keys()) From 58a79201db505d9d2f4bb30ec6ce6a910801e635 Mon Sep 17 00:00:00 2001 From: Lucian Smith Date: Wed, 21 Jun 2023 15:47:05 -0700 Subject: [PATCH 03/10] I don't believe the error is actually 'string not str'. --- biosimulators_utils/sedml/validation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/biosimulators_utils/sedml/validation.py b/biosimulators_utils/sedml/validation.py index 2bbc919b..9ee70f2b 100644 --- a/biosimulators_utils/sedml/validation.py +++ b/biosimulators_utils/sedml/validation.py @@ -1771,8 +1771,9 @@ def validate_calculation(calculation): try: compiled_math = compile_math(calculation.math) - except TypeError: + except TypeError as e: errors.append(['The mathematical expression must be a `string`, not a `{}`.'.format(calculation.math.__class__)]) + errors.append(str(e)) return (errors, warnings) except (SyntaxError, CompilationException) as exception: errors.append(['The syntax of the mathematical expression `{}` is invalid.'.format(calculation.math), [[str(exception)]]]) From 4365ef9c42baa56722cf0fa621697e8a34f0086c Mon Sep 17 00:00:00 2001 From: Lucian Smith Date: Wed, 21 Jun 2023 15:58:50 -0700 Subject: [PATCH 04/10] Again... --- biosimulators_utils/sedml/validation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/biosimulators_utils/sedml/validation.py b/biosimulators_utils/sedml/validation.py index 9ee70f2b..c8d2abd0 100644 --- a/biosimulators_utils/sedml/validation.py +++ b/biosimulators_utils/sedml/validation.py @@ -1773,7 +1773,7 @@ def validate_calculation(calculation): compiled_math = compile_math(calculation.math) except TypeError as e: errors.append(['The mathematical expression must be a `string`, not a `{}`.'.format(calculation.math.__class__)]) - errors.append(str(e)) + errors.append(e.what()) return (errors, warnings) except (SyntaxError, CompilationException) as exception: errors.append(['The syntax of the mathematical expression `{}` is invalid.'.format(calculation.math), [[str(exception)]]]) From 5366ee65de1ae92f0dd727f565a7240345bb9654 Mon Sep 17 00:00:00 2001 From: Lucian Smith Date: Wed, 21 Jun 2023 16:11:23 -0700 Subject: [PATCH 05/10] Yet another attempt to get the exception message. --- biosimulators_utils/sedml/validation.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/biosimulators_utils/sedml/validation.py b/biosimulators_utils/sedml/validation.py index c8d2abd0..29f4522f 100644 --- a/biosimulators_utils/sedml/validation.py +++ b/biosimulators_utils/sedml/validation.py @@ -1771,9 +1771,8 @@ def validate_calculation(calculation): try: compiled_math = compile_math(calculation.math) - except TypeError as e: - errors.append(['The mathematical expression must be a `string`, not a `{}`.'.format(calculation.math.__class__)]) - errors.append(e.what()) + except TypeError as exception: + errors.append(['The mathematical expression must be a `string`, not a `{}`:'.format(calculation.math.__class__), [[str(exception)]]]) return (errors, warnings) except (SyntaxError, CompilationException) as exception: errors.append(['The syntax of the mathematical expression `{}` is invalid.'.format(calculation.math), [[str(exception)]]]) From 3056ceb591fc9135e0a88e0a96f832561a45ec76 Mon Sep 17 00:00:00 2001 From: Lucian Smith Date: Wed, 21 Jun 2023 16:23:33 -0700 Subject: [PATCH 06/10] Try to convert to string It's already 'class str', but it wants it to be 'string'? Somehow? --- biosimulators_utils/sedml/validation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/biosimulators_utils/sedml/validation.py b/biosimulators_utils/sedml/validation.py index 29f4522f..65351f4d 100644 --- a/biosimulators_utils/sedml/validation.py +++ b/biosimulators_utils/sedml/validation.py @@ -1770,7 +1770,7 @@ def validate_calculation(calculation): workspace[calculation.range.id] = 1 try: - compiled_math = compile_math(calculation.math) + compiled_math = compile_math(str(calculation.math)) except TypeError as exception: errors.append(['The mathematical expression must be a `string`, not a `{}`:'.format(calculation.math.__class__), [[str(exception)]]]) return (errors, warnings) From c38cc1a632b423a9fcb2caeb5eb0a9369bc5eeb4 Mon Sep 17 00:00:00 2001 From: Lucian Smith Date: Wed, 21 Jun 2023 16:37:10 -0700 Subject: [PATCH 07/10] Aha, this could explain it... --- biosimulators_utils/sedml/math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/biosimulators_utils/sedml/math.py b/biosimulators_utils/sedml/math.py index b06e2fb9..cc4e7dcb 100644 --- a/biosimulators_utils/sedml/math.py +++ b/biosimulators_utils/sedml/math.py @@ -187,7 +187,7 @@ def compile_math(math): # math_node = evalidate.evalidate(math, # addnodes=VALID_MATH_EXPRESSION_NODES, # funcs=MATHEMATICAL_FUNCTIONS.keys()) - compiled_math = compile(math_node.code, '', 'eval') + compiled_math = compile(math_node.node, '', 'eval') return compiled_math From a97d2b5279b461ef3bddc0a9f8fdc9270e4bda97 Mon Sep 17 00:00:00 2001 From: Lucian Smith Date: Wed, 21 Jun 2023 16:49:52 -0700 Subject: [PATCH 08/10] New functionality doesn't require input to be a string. --- biosimulators_utils/sedml/validation.py | 3 --- tests/sedml/test_sedml_validation.py | 4 ---- 2 files changed, 7 deletions(-) diff --git a/biosimulators_utils/sedml/validation.py b/biosimulators_utils/sedml/validation.py index 65351f4d..f656cd0a 100644 --- a/biosimulators_utils/sedml/validation.py +++ b/biosimulators_utils/sedml/validation.py @@ -1771,9 +1771,6 @@ def validate_calculation(calculation): try: compiled_math = compile_math(str(calculation.math)) - except TypeError as exception: - errors.append(['The mathematical expression must be a `string`, not a `{}`:'.format(calculation.math.__class__), [[str(exception)]]]) - return (errors, warnings) except (SyntaxError, CompilationException) as exception: errors.append(['The syntax of the mathematical expression `{}` is invalid.'.format(calculation.math), [[str(exception)]]]) return (errors, warnings) diff --git a/tests/sedml/test_sedml_validation.py b/tests/sedml/test_sedml_validation.py index 2da94095..a2bcf060 100644 --- a/tests/sedml/test_sedml_validation.py +++ b/tests/sedml/test_sedml_validation.py @@ -1476,10 +1476,6 @@ def test_validate_calculation(self): calculation_2.math = None self.assertIn('must have math', flatten_nested_list_of_strings(validation.validate_calculation(calculation_2)[0])) - calculation_2 = copy.copy(calculation) - calculation_2.math = 10. - self.assertIn('must be a `string`', flatten_nested_list_of_strings(validation.validate_calculation(calculation_2)[0])) - calculation_2 = copy.copy(calculation) calculation_2.math = 'a * ' self.assertIn('The syntax', flatten_nested_list_of_strings(validation.validate_calculation(calculation_2)[0])) From 006b72a186b2c104ccef50569d6cfb15e1bad844 Mon Sep 17 00:00:00 2001 From: Lucian Smith Date: Wed, 21 Jun 2023 16:59:26 -0700 Subject: [PATCH 09/10] Update version number. --- biosimulators_utils/_version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/biosimulators_utils/_version.py b/biosimulators_utils/_version.py index b8d5baed..416dc749 100644 --- a/biosimulators_utils/_version.py +++ b/biosimulators_utils/_version.py @@ -1 +1 @@ -__version__ = '0.1.180' +__version__ = '0.1.181' From 4deb90be9402395c223d21a732c98596de6b468f Mon Sep 17 00:00:00 2001 From: Lucian Smith Date: Wed, 21 Jun 2023 17:04:33 -0700 Subject: [PATCH 10/10] Don't convert input to string. Also, revert tests in case they're actually OK. --- biosimulators_utils/sedml/math.py | 3 --- biosimulators_utils/sedml/validation.py | 5 ++++- tests/sedml/test_sedml_validation.py | 4 ++++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/biosimulators_utils/sedml/math.py b/biosimulators_utils/sedml/math.py index cc4e7dcb..615fd88f 100644 --- a/biosimulators_utils/sedml/math.py +++ b/biosimulators_utils/sedml/math.py @@ -184,9 +184,6 @@ def compile_math(math): model.allowed_functions.extend(MATHEMATICAL_FUNCTIONS.keys()) math_node = evalidate.Expr(math, model=model) - # math_node = evalidate.evalidate(math, - # addnodes=VALID_MATH_EXPRESSION_NODES, - # funcs=MATHEMATICAL_FUNCTIONS.keys()) compiled_math = compile(math_node.node, '', 'eval') return compiled_math diff --git a/biosimulators_utils/sedml/validation.py b/biosimulators_utils/sedml/validation.py index f656cd0a..29f4522f 100644 --- a/biosimulators_utils/sedml/validation.py +++ b/biosimulators_utils/sedml/validation.py @@ -1770,7 +1770,10 @@ def validate_calculation(calculation): workspace[calculation.range.id] = 1 try: - compiled_math = compile_math(str(calculation.math)) + compiled_math = compile_math(calculation.math) + except TypeError as exception: + errors.append(['The mathematical expression must be a `string`, not a `{}`:'.format(calculation.math.__class__), [[str(exception)]]]) + return (errors, warnings) except (SyntaxError, CompilationException) as exception: errors.append(['The syntax of the mathematical expression `{}` is invalid.'.format(calculation.math), [[str(exception)]]]) return (errors, warnings) diff --git a/tests/sedml/test_sedml_validation.py b/tests/sedml/test_sedml_validation.py index a2bcf060..2da94095 100644 --- a/tests/sedml/test_sedml_validation.py +++ b/tests/sedml/test_sedml_validation.py @@ -1476,6 +1476,10 @@ def test_validate_calculation(self): calculation_2.math = None self.assertIn('must have math', flatten_nested_list_of_strings(validation.validate_calculation(calculation_2)[0])) + calculation_2 = copy.copy(calculation) + calculation_2.math = 10. + self.assertIn('must be a `string`', flatten_nested_list_of_strings(validation.validate_calculation(calculation_2)[0])) + calculation_2 = copy.copy(calculation) calculation_2.math = 'a * ' self.assertIn('The syntax', flatten_nested_list_of_strings(validation.validate_calculation(calculation_2)[0]))