Repository navigation
Improving error message for width and position type mismatch in violinplot #30545
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
be9f788
Improving error message for width and position type mismatch in violi…
hasanrashid 806b7be
Integrate tests in test_axes.py
hasanrashid c22a7a3
Bump mpl-sphinx-theme version
dstansby 7935cce
Improve cursor icons with RectangleSelector
dstansby 43fd154
Use an enum for RectangleSelector state
dstansby 59f54c0
Improve signature of set_span_cursor
dstansby 30adbc8
Improve variable naming
dstansby ce577fc
Add --debug flag to python -mmatplotlib.dviread CLI.
anntzer 2245b1d
Include step info in str(scroll_event).
anntzer dd65c51
Update lib/matplotlib/axes/_axes.py
hasanrashid 90806de
Merge branch 'matplotlib:main' into enh-30417
hasanrashid 1e2bcae
Improving error message for width and position type mismatch in violi…
hasanrashid c5e3e0d
Integrate tests in test_axes.py
hasanrashid 666fd6d
Update lib/matplotlib/axes/_axes.py
hasanrashid 3119002
Merge branch 'enh-30417' of github.com:hasanrashid/matplotlib into en…
hasanrashid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Improving error message for width and position type mismatch in violi…
…nplot
- Loading branch information
commit 1e2bcae3e50a0b63950b017d4aea45593eeb1e7d
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| """ | ||
| Unit tests for proactive validation of datetime | ||
| positions and timedelta widths in violinplot. | ||
| """ | ||
|
|
||
| import datetime | ||
| import pytest | ||
|
|
||
| import matplotlib.pyplot as plt | ||
|
|
||
|
|
||
| def make_vpstats(): | ||
| """Create minimal valid stats for a violin plot.""" | ||
|
|
||
|
|
||
|
hasanrashid marked this conversation as resolved.
Outdated
|
||
| def violin_plot_stats(): | ||
| # Stats for violin plot | ||
| datetimes = [ | ||
| datetime.datetime(2023, 2, 10), | ||
| datetime.datetime(2023, 5, 18), | ||
| datetime.datetime(2023, 6, 6) | ||
| ] | ||
| return [{ | ||
| 'coords': datetimes, | ||
| 'vals': [0.1, 0.5, 0.2], | ||
| 'mean': datetimes[1], | ||
| 'median': datetimes[1], | ||
| 'min': datetimes[0], | ||
| 'max': datetimes[-1], | ||
| 'quantiles': datetimes | ||
| }, { | ||
| 'coords': datetimes, | ||
| 'vals': [0.2, 0.3, 0.4], | ||
| 'mean': datetimes[2], | ||
| 'median': datetimes[2], | ||
| 'min': datetimes[0], | ||
| 'max': datetimes[-1], | ||
| 'quantiles': datetimes | ||
| }] | ||
|
|
||
|
|
||
| def test_datetime_positions_with_float_widths_raises(): | ||
| """Test that datetime positions with float widths raise TypeError.""" | ||
| fig, ax = plt.subplots() | ||
| try: | ||
| vpstats = violin_plot_stats() | ||
| positions = [datetime.datetime(2020, 1, 1), datetime.datetime(2021, 1, 1)] | ||
| widths = [0.5, 1.0] | ||
| with pytest.raises(TypeError, | ||
| match="positions are datetime/date.*widths as datetime\\.timedelta"): | ||
| ax.violin(vpstats, positions=positions, widths=widths) | ||
| finally: | ||
| plt.close(fig) | ||
|
timhoffm marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| def test_datetime_positions_with_scalar_float_width_raises(): | ||
| """Test that datetime positions with scalar float width raise TypeError.""" | ||
| fig, ax = plt.subplots() | ||
| try: | ||
| vpstats = violin_plot_stats() | ||
| positions = [datetime.datetime(2020, 1, 1), datetime.datetime(2021, 1, 1)] | ||
| widths = 0.75 | ||
| with pytest.raises(TypeError, | ||
| match="positions are datetime/date.*widths as datetime\\.timedelta"): | ||
| ax.violin(vpstats, positions=positions, widths=widths) | ||
| finally: | ||
| plt.close(fig) | ||
|
|
||
|
|
||
| def test_numeric_positions_with_float_widths_ok(): | ||
| """Test that numeric positions with float widths work.""" | ||
| fig, ax = plt.subplots() | ||
| try: | ||
| vpstats = violin_plot_stats() | ||
| positions = [1.0, 2.0] | ||
| widths = [0.5, 1.0] | ||
| ax.violin(vpstats, positions=positions, widths=widths) | ||
| finally: | ||
| plt.close(fig) | ||
|
|
||
|
|
||
| def test_mixed_positions_datetime_and_numeric_behaves(): | ||
| """Test that mixed datetime and numeric positions | ||
| with float widths raise TypeError. | ||
| """ | ||
| fig, ax = plt.subplots() | ||
| try: | ||
| vpstats = violin_plot_stats() | ||
| positions = [datetime.datetime(2020, 1, 1), 2.0] | ||
| widths = [0.5, 1.0] | ||
| with pytest.raises(TypeError, | ||
| match="positions are datetime/date.*widths as datetime\\.timedelta"): | ||
| ax.violin(vpstats, positions=positions, widths=widths) | ||
| finally: | ||
| plt.close(fig) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it enough to check the first position? I think mixed positions are not allowed anyway. Also, do we need to handle np.datetime64 arrays?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hasanrashid You've marked this as resolved. Please comment how my above questions are addressed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timhoffm Yes, it is enough to check the first position. The position argument can't mix datetime with float. width can't mix timedelta and float either.
I was approaching this by converting entries into numeric equivalent until it was suggested that I provide clearer message instead.
Also, yes, datetime64 isn't needed either. I was thinking of all test cases, but I realize datetime64 isn't a supported type in violinplot.
Making the adjustments
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is datetime64 not supported? Should we rather fix this? (Can be later, but already add the check here?)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timhoffm
If I try a testcase like this:
This produces the following error:
numpy._core._exceptions._UFuncBinaryResolutionError: ufunc 'add' cannot use operands with types dtype('float64') and dtype('<M8[D]')I took this to mean that datetime64 was not an expected type of datetime for biolinplots- thus unsupported.
If we need to investigate adding datetime64 support in violineplot, should it be another issue? We are updating the error message enforcing timedelta with datetimes; datetime64 support- to me- seems like it should be investigated in a separate issue (which I am happy to open and work on)