Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add as_float, change repr(x) -> str(x) #214

Merged
merged 15 commits into from
Jul 12, 2024
Merged

Conversation

harmsm
Copy link
Contributor

@harmsm harmsm commented Jul 3, 2024

Fixes a bug that does not allow graphs to render when using numpy 2.0. This is because numpy changed the string representation of scalar values. (repr(some_numpy_float) now returns "np.float32(float_value)" instead of "float_value"). See Issue 213 for details.

  • Adds two functions (toyplot.require.as_float and toyplot.require.as_int)
  • Replaces most float(value) and int(value) calls in the package with these new functions.
  • Replaces the repr(value) calls in toyplot.html with str(value).

I tested the new code in Python 2.11 with both numpy 1.26.4 and numpy 2.0. It successfully fixed the initial bug.

I ran the regression tests on both the main branch and my new commits. Not all tests were successful on the main branch; no new tests failed with my changes. (regression.py output gives: 2511 steps passed, 18 failed, 34 skipped, 0 undefined).

@coveralls
Copy link

Coverage Status

coverage: 94.81% (-0.2%) from 94.969%
when pulling 3a0c513 on harmsm:np2_repr
into 45e9ceb on sandialabs:main.

@tshead2
Copy link
Member

tshead2 commented Jul 12, 2024

Closing-and-reopening this PR, in hopes that it will re-run the tests.

@tshead2 tshead2 closed this Jul 12, 2024
@tshead2 tshead2 reopened this Jul 12, 2024
@coveralls
Copy link

coveralls commented Jul 12, 2024

Coverage Status

coverage: 94.497% (-0.1%) from 94.629%
when pulling cce091b on harmsm:np2_repr
into 93ab5d6 on sandialabs:main.

@tshead2
Copy link
Member

tshead2 commented Jul 12, 2024

@harmsm - I've cleaned-up the existing regression tests and added explicit tests for both major versions of NumPy, so you should see that while this PR doesn't break anything for NumPy 1, it hasn't fixed everything yet for NumPy 2 - could you take a look?

Thanks,
Tim

@harmsm
Copy link
Contributor Author

harmsm commented Jul 12, 2024

Yes, I'll check into it.

@harmsm
Copy link
Contributor Author

harmsm commented Jul 12, 2024

Most of the failures traced back to "%r" % value format calls. I simply replaced "%r" with "%s". All tests pass for numpy 1.2x and all but two tests pass for numpy 2.0.

The two remaining failures are both in features/image-visualization: "numpy 8 bit L image with colormap" and "pillow 8 bit L image with colormap". I'm struggling to understand the nature of the failure. The output and reference svg files have identical tags and look visually identical if I load them in Inkscape, but have different "data:image/png;base64" output strings. Presumably the images need to be binary identical for the test to pass?

I'm going to keep working on it, but pushed changes now in case this is a problem with the test framework rather than a true problem with the library.

@harmsm
Copy link
Contributor Author

harmsm commented Jul 12, 2024

Okay, odd. I have two tests failing on my local machine, but they're passing in the github workflow. 🤷

@tshead2
Copy link
Member

tshead2 commented Jul 12, 2024

Yep, those images are supposed to be identical, but maybe there are some platform differences we should be aware of - what's your dev environment look like? In the meantime, passing tests are passing tests ... I'll merge the request.

@tshead2 tshead2 merged commit f32537e into sandialabs:main Jul 12, 2024
9 of 10 checks passed
@harmsm
Copy link
Contributor Author

harmsm commented Jul 12, 2024

I'm testing in a fresh conda environment (Python 3.12, NumPy 2.0, and whatever was installed with pip install . run in toyplot/), MacOS (M1, 14.4.1). Full list below for reference.

Glad it's passing more generally. Thanks for the great library and merging the fix.

Package                       Version
----------------------------- --------------
alabaster                     0.7.16
anyio                         4.4.0
appnope                       0.1.4
argon2-cffi                   23.1.0
argon2-cffi-bindings          21.2.0
arrow                         1.3.0
asttokens                     2.4.1
async-lru                     2.0.4
attrs                         23.2.0
Babel                         2.15.0
beautifulsoup4                4.12.3
behave                        1.2.6
bleach                        6.1.0
certifi                       2024.7.4
cffi                          1.16.0
chardet                       5.2.0
charset-normalizer            3.3.2
comm                          0.2.2
coverage                      7.6.0
custom-inherit                2.4.1
debugpy                       1.8.2
decorator                     5.1.1
defusedxml                    0.7.1
docutils                      0.21.2
executing                     2.0.1
fastjsonschema                2.20.0
fqdn                          1.5.1
h11                           0.14.0
httpcore                      1.0.5
httpx                         0.27.0
idna                          3.7
imageio                       2.34.2
imagesize                     1.4.1
ipykernel                     6.29.5
ipython                       8.26.0
isoduration                   20.11.0
jedi                          0.19.1
Jinja2                        3.1.4
json5                         0.9.25
jsonpointer                   3.0.0
jsonschema                    4.23.0
jsonschema-specifications     2023.12.1
jupyter_client                8.6.2
jupyter_core                  5.7.2
jupyter-events                0.10.0
jupyter-lsp                   2.2.5
jupyter_server                2.14.1
jupyter_server_terminals      0.5.3
jupyterlab                    4.2.3
jupyterlab_pygments           0.3.0
jupyterlab_server             2.27.2
lazy_loader                   0.4
MarkupSafe                    2.1.5
matplotlib-inline             0.1.7
mistune                       3.0.2
multipledispatch              1.0.0
nbclient                      0.10.0
nbconvert                     7.16.4
nbformat                      5.10.4
nest-asyncio                  1.6.0
networkx                      3.3
notebook_shim                 0.2.4
numpy                         2.0.0
overrides                     7.7.0
packaging                     24.1
pandas                        2.2.2
pandocfilters                 1.5.1
parse                         1.20.2
parse-type                    0.6.2
parso                         0.8.4
pexpect                       4.9.0
pillow                        10.4.0
pip                           24.0
platformdirs                  4.2.2
prometheus_client             0.20.0
prompt_toolkit                3.0.47
psutil                        6.0.0
ptyprocess                    0.7.0
pure-eval                     0.2.2
pycparser                     2.22
Pygments                      2.18.0
pypng                         0.20220715.0
python-dateutil               2.9.0.post0
python-json-logger            2.0.7
pytz                          2024.1
PyYAML                        6.0.1
pyzmq                         26.0.3
referencing                   0.35.1
reportlab                     4.2.2
requests                      2.32.3
rfc3339-validator             0.1.4
rfc3986-validator             0.1.1
rpds-py                       0.19.0
scikit-image                  0.24.0
scipy                         1.14.0
Send2Trash                    1.8.3
setuptools                    70.3.0
six                           1.16.0
sniffio                       1.3.1
snowballstemmer               2.2.0
soupsieve                     2.5
Sphinx                        7.3.7
sphinxcontrib-applehelp       1.0.8
sphinxcontrib-devhelp         1.0.6
sphinxcontrib-htmlhelp        2.0.5
sphinxcontrib-jsmath          1.0.1
sphinxcontrib-qthelp          1.0.7
sphinxcontrib-serializinghtml 1.1.10
stack-data                    0.6.3
terminado                     0.18.1
tifffile                      2024.7.2
tinycss2                      1.3.0
tornado                       6.4.1
toyplot                       2.0.0.dev0
traitlets                     5.14.3
types-python-dateutil         2.9.0.20240316
tzdata                        2024.1
uri-template                  1.3.0
urllib3                       2.2.2
wcwidth                       0.2.13
webcolors                     24.6.0
webencodings                  0.5.1
websocket-client              1.8.0
wheel                         0.43.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants