Improve user feedback for errors during plot compilation#3203
Merged
Conversation
Codecov Report
Additional details and impacted files@@ 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
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Because most
so.Plotmethods 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
PlotSpecErrorexception 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.