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

Make ShapeError more readable #24161

Merged
merged 2 commits into from Oct 23, 2022
Merged

Make ShapeError more readable #24161

merged 2 commits into from Oct 23, 2022

Conversation

sylee957
Copy link
Member

@sylee957 sylee957 commented Oct 23, 2022

References to other Issues or PRs

Fixes #24160

Brief description of what is fixed or changed

I have replaced all occurrences of ShapeError and there are only four of these that have the problem.
I had to pick whether it is a good option to print out the full variables or not,
But I recently found that users are allowed to use other frameworks to inspect local variables (https://stackoverflow.com/questions/19514288/locals-and-globals-in-stack-trace-on-exception-python)
And it is automatically done in like pdb, pytest, ..., so there wouldn't be a need to print out full Matrix object at all.

Other comments

Release Notes

  • matrices
    • Made ShapeError messages more readable for users.

@sympy-bot
Copy link

sympy-bot commented Oct 23, 2022

Hi, I am the SymPy bot (v167). I'm here to help you write a release notes entry. Please read the guide on how to write release notes.

Your release notes are in good order.

Here is what the release notes will look like:

  • matrices

This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.12.

Click here to see the pull request description that was parsed.
<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234" (see
https://tinyurl.com/auto-closing for more information). Also, please
write a comment on that issue linking back to this pull request once it is
open. -->

Fixes #24160

#### Brief description of what is fixed or changed

I have replaced all occurrences of ``ShapeError`` and there are only four of these that have the problem.
I had to pick whether it is a good option to print out the full variables or not,
But I recently found that users are allowed to use other frameworks to inspect local variables (https://stackoverflow.com/questions/19514288/locals-and-globals-in-stack-trace-on-exception-python)
And it is automatically done in like pdb, pytest, ..., so there wouldn't be a need to print out full Matrix object at all.

#### Other comments


#### Release Notes

<!-- Write the release notes for this release below between the BEGIN and END
statements. The basic format is a bulleted list with the name of the subpackage
and the release note for this PR. For example:

* solvers
  * Added a new solver for logarithmic equations.

* functions
  * Fixed a bug with log of integers.

or if no release note(s) should be included use:

NO ENTRY

See https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more
information on how to write release notes. The bot will check your release
notes automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->
- matrices
  - Made ``ShapeError`` messages more readable for users.
<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

@github-actions
Copy link

github-actions bot commented Oct 23, 2022

Benchmark results from GitHub Actions

Lower numbers are good, higher numbers are bad. A ratio less than 1
means a speed up and greater than 1 means a slowdown. Green lines
beginning with + are slowdowns (the PR is slower then master or
master is slower than the previous release). Red lines beginning
with - are speedups.

Significantly changed benchmark results (PR vs master)

Significantly changed benchmark results (master vs previous release)

       before           after         ratio
     [41d90958]       [7a4b4125]
     <sympy-1.11.1^0>                 
-         999±2μs          633±2μs     0.63  solve.TimeSparseSystem.time_linear_eq_to_matrix(10)
-     2.84±0.01ms         1.18±0ms     0.41  solve.TimeSparseSystem.time_linear_eq_to_matrix(20)
-     5.65±0.01ms         1.72±0ms     0.30  solve.TimeSparseSystem.time_linear_eq_to_matrix(30)

Full benchmark results can be found as artifacts in GitHub Actions
(click on checks at the top of the PR).

@oscarbenjamin
Copy link
Contributor

Looks good.

@oscarbenjamin oscarbenjamin merged commit 7a66072 into sympy:master Oct 23, 2022
@asmeurer
Copy link
Member

so there wouldn't be a need to print out full Matrix object at all.

In general you should avoid putting the string form of arbitrary expressions inside of error messages. The expression could be arbitrary large, meaning the exception itself might take a long time to compute, and a very large matrix in an error message isn't very readable anyway. I've thought about ways that we can include expressions inside of exception objects in ways that are easier to inspect but that don't require computing str(expr).

@oscarbenjamin
Copy link
Contributor

I've thought about ways that we can include expressions inside of exception objects in ways that are easier to inspect but that don't require computing str(expr).

Ideally all exceptions raised would be custom exception classes that subclass a something like SymPyError and then it should be usable with delayed formatting like the logging module e.g.:

raise ShapeError("Shape mismatch in multiplication %r", self.shape)

Then self.shape would only be formatted if printing the exception.

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

Successfully merging this pull request may close these issues.

Make non-square matrix error more understandable
4 participants