Skip to content

Commit b6d8685

Browse files
m-messerclaude
andauthored
Updated to Python 3.12 (#247)
* Updated to Python 3.12 * Handle infinity Unicode symbol and fix infinite value comparison Added ∞ as an alias for oo so students can use the Unicode infinity symbol as input. Fixed comparison of infinite expressions by using direct sympy comparison instead of subtraction, since oo - oo yields nan rather than 0. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * Handled exceptions for is_infinite checks during symbolic expression comparison * Updated sympy to 1.13 * Updated sympy to 1.14 and mpath to 1.4.1 * Update CI to use Python 3.12 * Changed mpmath version * Switched Dockerfile base image to apt package manager for Git installation. * Switched to use dnf * Added find to installed requirements * Set PYTHONPATH in Dockerfile and updated CMD entrypoint format --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 883f0dd commit b6d8685

7 files changed

Lines changed: 40 additions & 14 deletions

File tree

.github/workflows/staging-deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
strategy:
1919
fail-fast: false
2020
matrix:
21-
python-version: ["3.8"]
21+
python-version: ["3.12"]
2222
steps:
2323
- name: Checkout
2424
uses: actions/checkout@v4

.github/workflows/test-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
strategy:
1616
fail-fast: false
1717
matrix:
18-
python-version: ["3.8"]
18+
python-version: ["3.12"]
1919
steps:
2020
- name: Checkout
2121
uses: actions/checkout@v4

app/Dockerfile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Base image that bundles AWS Lambda Python 3.8 image with some middleware functions
22
# FROM base-eval-tmp
33
# FROM rabidsheep55/python-base-eval-layer
4-
FROM ghcr.io/lambda-feedback/baseevalutionfunctionlayer:main-3.8
4+
FROM ghcr.io/lambda-feedback/baseevalutionfunctionlayer:main-3.12
55

6-
RUN yum install -y git
6+
RUN dnf install -y git findutils && dnf clean all
77

88
WORKDIR /app
99

10+
ENV PYTHONPATH=/app
11+
1012
# Copy and install any packages/modules needed for your evaluation script.
1113
COPY requirements.txt .
1214
RUN pip3 install -r requirements.txt
@@ -57,4 +59,4 @@ RUN chmod 644 $(find . -type f)
5759
RUN chmod 755 $(find . -type d)
5860

5961
# The entrypoint for AWS is to invoke the handler function within the app package
60-
CMD [ "/app/app.handler" ]
62+
CMD [ "app.handler" ]

app/context/symbolic.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from copy import deepcopy
2-
from sympy import Add, Pow, Mul, Equality, pi, im, I, N
2+
from sympy import Add, Pow, Mul, Equality, pi, im, I, N, oo
33
from sympy import re as real_part
44

55
from ..utility.expression_utilities import (
@@ -83,6 +83,22 @@ def create_expressions_for_comparison(criterion, parameters_dict, local_substitu
8383
return lhs_expr, rhs_expr
8484

8585

86+
def do_comparison_infinite(comparison_symbol, lhs_expr, rhs_expr):
87+
# When either side is infinite, subtracting (e.g. oo - oo) yields nan, making
88+
# the subtraction-based do_comparison unreliable. Compare the sides directly instead.
89+
direct_comparisons = {
90+
"=": lhs_expr == rhs_expr,
91+
">": lhs_expr > rhs_expr,
92+
">=": lhs_expr >= rhs_expr,
93+
"<": lhs_expr < rhs_expr,
94+
"<=": lhs_expr <= rhs_expr,
95+
}
96+
try:
97+
return bool(direct_comparisons[comparison_symbol.strip()])
98+
except Exception:
99+
return None
100+
101+
86102
def do_comparison(comparison_symbol, expression):
87103
comparisons = {
88104
"=": lambda expr: bool(expression.cancel().simplify().simplify() == 0),
@@ -106,7 +122,14 @@ def check_equality(criterion, parameters_dict, local_substitutions=[]):
106122
elif not isinstance(lhs_expr, Equality) and isinstance(rhs_expr, Equality):
107123
result = False
108124
else:
109-
result = do_comparison(criterion.content, lhs_expr-rhs_expr)
125+
try:
126+
either_is_infinite = lhs_expr.is_infinite or rhs_expr.is_infinite
127+
except (KeyError, TypeError):
128+
either_is_infinite = False
129+
if either_is_infinite:
130+
result = do_comparison_infinite(criterion.content, lhs_expr, rhs_expr)
131+
else:
132+
result = do_comparison(criterion.content, lhs_expr-rhs_expr)
110133
# There are some types of expression, e.g. those containing hyperbolic trigonometric functions, that can behave
111134
# unpredictably when simplification is applied. For that reason we check several different combinations of
112135
# simplifications here in order to reduce the likelihood of false negatives.

app/requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pydot
22
typing_extensions
3-
mpmath==1.2.1
4-
sympy==1.12
3+
mpmath==1.3.0
4+
sympy==1.14
55
antlr4-python3-runtime==4.7.2
66
git+https://github.com/lambda-feedback/latex2sympy.git@master#egg=latex2sympy2

app/tests/symbolic_evaluation_test.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -784,10 +784,6 @@ def test_warning_inappropriate_symbol(self):
784784
'(0,002*6800*v)/1,2',
785785
'(0.002*6800*v)/1.2'
786786
),
787-
(
788-
'-∞',
789-
'-inf'
790-
),
791787
(
792788
'x.y',
793789
'x*y'
@@ -1949,6 +1945,11 @@ def test_unexpected_equalities_in_response_that_generates_set(self):
19491945
result = evaluation_function(response, answer, params)
19501946
assert result["is_correct"] is False
19511947

1948+
def test_infinity_unicode_symbol(self):
1949+
params = {'strict_syntax': True, 'elementary_functions': True}
1950+
result = evaluation_function('-∞', '-inf', params)
1951+
assert result["is_correct"] is True
1952+
19521953
def test_infinity_alias(self):
19531954
response = "2.694"
19541955
answer = "infinity"

app/utility/expression_utilities.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def _print_log(self, expr, exp=None):
6262
('acsch', ['arccsch', 'arccosech']), ('asech', ['arcsech']),
6363
('exp', ['Exp']), ('E', ['e']), ('log', ['ln']),
6464
('sqrt', []), ('sign', []), ('Abs', ['abs']), ('Max', ['max']), ('Min', ['min']), ('arg', []), ('ceiling', ['ceil']), ('floor', []),
65-
('oo',['Infinity', 'inf', 'infinity']),
65+
('oo',['Infinity', 'inf', 'infinity', '∞']),
6666
# Special symbols to make sure plus_minus and minus_plus are not destroyed during preprocessing
6767
('plus_minus', []), ('minus_plus', []),
6868
# Below this line should probably not be collected with elementary functions. Some like 'common operations' would be a better name

0 commit comments

Comments
 (0)