Fix jacobian and hessian evaluation for list expressions#729
Fix jacobian and hessian evaluation for list expressions#729MrzvskK wants to merge 8 commits intoexperimental-design:mainfrom
Conversation
Add files via upload
…_Reaction_Optimization.qmd Co-authored-by: Johannes P. Dürholt <johannespeter.duerholt@evonik.com>
|
Hi all, just to clarify: pandas has two different eval engines. If From speaking to @MrzvskK, her changes in this PR mean that the functions are now backend agnostic. The tests now pass independently of whether numexpr is installed. |
|
Thanks for the explanations, I also digged a bit, so what I found is described in this two links:
So if one does not set an engine explicitly via What do you think and what are your preferences? |
|
I agree that we should decide on a single backend. Since the pandas code isn't performance critical (all of the heavy lifting is done by Is there a way to globally set the |
|
I do agree with the comments above. However, shouldn't we aim to have as much codebase as possible to be agnostic to these types of backend differences? I believe this strategy for Jacobian and Hessian computation wouldn't in fact have conflicts regardless of the version - also talking for future improvements. |
|
I would opt for now for the simple version, and just fix it everywhere to the |
Motivation
Hi Johannes, when working on nonlinear constraints I have noticed that two tests were failing, my guess it is due to
pandas.eval()andnumexprcompatibility issues with list expressions in Python 3.13.The tests in question:
test_nonlinear_constraints_jacobian_expressiontest_nonlinear_constraints_hessian_expressionCause analysis:
The
jacobian()andhessian()methods were attempting to evaluate string expressions containing list literals such as"[2*x1, 2*x2, -1]"- directly withpandas.eval().When doing so you get error message:
ValueError: setting an array element with a sequencedue to inhomogeneous array shapes.At first I created an issue, but then I made a few modifications to the nonlinear.py and now tests pass - I hope these fixes are good enough.
Both for Jacobian and Hessian we first detect list/nested list expressions, then these components are evaluated separately and we handle all the scalars by pushing them to all rows. Not sure if using dataFrame was a good call.
Changes
jacobian(): Added parsing for list expressions like"[2*x1, 2*x2, -1]"hessian(): Added parsing for nested list expressions like"[[6*x1, 0, 0], [0, 2, 0], [0, 0, 0]]"All other tests in the constraints also passed, and we maintain compatibility with Callable expressions and backward one for non-list expressions
Have you read the Contributing Guidelines on pull requests?
yes, but I am a new contributor
Have you updated
CHANGELOG.md?no
Test Plan
I would run: pytest tests/bofire/data_models/constraints/test_nonlinear.py -v
Or if you want to test all tests run: pytest tests/bofire/data_models/constraints/ -v