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

Improve user feedback for errors during plot compilation #3203

Merged
merged 4 commits into from
Dec 28, 2022

Conversation

mwaskom
Copy link
Owner

@mwaskom mwaskom commented Dec 27, 2022

Because most so.Plot methods simply define a spec and defer operations until the time when the plot is compiled, errors caused by bad user inputs (or seaborn edge cases) that cannot be trivially detected may not arise until later and will not be clearly connected to the problem.

In general I'd like to try and be as helpful as possible with what we actually raise to avoid this becoming a pain point for the new interface. This PR represents the first step, defining a PlotSpecError exception class that gets raised in an exception chain to provide some additional context that may help diagnose issues. Right now the use is limited to scale setup and scaling operations and the main benefit is that the exception message can call out the plot variable that was being processed when an exception was raised.

e.g., the traceback for the operation in #3106 (which is being obviated anyway by #3190, but is a useful demonstration):

TypeError                                 Traceback (most recent call last)
File ~/code/seaborn/seaborn/_core/plot.py:1254, in Plotter._setup_scales(self, p, common, layers, variables)
   1253 try:
-> 1254     self._scales[var] = scale._setup(var_df[var], prop)
   1255 except Exception as err:

File ~/code/seaborn/seaborn/_core/scales.py:351, in ContinuousBase._setup(self, data, prop, axis)
    350 a = forward(vmin)
--> 351 b = forward(vmax) - forward(vmin)
    353 def normalize(x):

TypeError: numpy boolean subtract, the `-` operator, is not supported, use the bitwise_xor, the `^` operator, or the logical_xor function instead.

The above exception was the direct cause of the following exception:

PlotSpecError                             Traceback (most recent call last)
Cell In [2], line 1
----> 1 so.Plot(["a", "b"], [1, 2], color=[True, False]).add(so.Bar()).plot()

File ~/code/seaborn/seaborn/_core/plot.py:821, in Plot.plot(self, pyplot)
    817 """
    818 Compile the plot spec and return the Plotter object.
    819 """
    820 with theme_context(self._theme_with_defaults()):
--> 821     return self._plot(pyplot)

File ~/code/seaborn/seaborn/_core/plot.py:842, in Plot._plot(self, pyplot)
    839 plotter._compute_stats(self, layers)
    841 # Process scale spec for semantic variables and coordinates computed by stat
--> 842 plotter._setup_scales(self, common, layers)
    844 # TODO Remove these after updating other methods
    845 # ---- Maybe have debug= param that attaches these when True?
    846 plotter._data = common

File ~/code/seaborn/seaborn/_core/plot.py:1256, in Plotter._setup_scales(self, p, common, layers, variables)
   1254         self._scales[var] = scale._setup(var_df[var], prop)
   1255     except Exception as err:
-> 1256         raise PlotSpecError._during("Scale setup", var) from err
   1258 if axis is None or (var != coord and coord in p._variables):
   1259     # Everything below here applies only to coordinate variables
   1260     continue

PlotSpecError: Scale setup failed for the `color` variable. See the traceback above for more information.

@mwaskom mwaskom added this to the v0.12.2 milestone Dec 27, 2022
@codecov
Copy link

codecov bot commented Dec 27, 2022

Codecov Report

Merging #3203 (d1ffe30) into master (87e0972) will decrease coverage by 0.00%.
The diff coverage is 98.59%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3203      +/-   ##
==========================================
- Coverage   98.42%   98.41%   -0.01%     
==========================================
  Files          76       77       +1     
  Lines       24115    24175      +60     
==========================================
+ Hits        23735    23793      +58     
- Misses        380      382       +2     
Impacted Files Coverage Δ
seaborn/_core/exceptions.py 90.00% <90.00%> (ø)
seaborn/_core/plot.py 99.34% <100.00%> (+<0.01%) ⬆️
seaborn/_marks/base.py 98.47% <100.00%> (+0.07%) ⬆️
seaborn/palettes.py 99.60% <100.00%> (ø)
tests/_core/test_plot.py 98.72% <100.00%> (+0.03%) ⬆️
tests/_core/test_scales.py 100.00% <100.00%> (ø)
tests/test_distributions.py 99.80% <100.00%> (ø)
seaborn/utils.py 95.21% <0.00%> (-0.29%) ⬇️

@mwaskom mwaskom merged commit 4a9e549 into master Dec 28, 2022
@mwaskom mwaskom deleted the ux/scale_errors branch December 28, 2022 01:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant