tpvasconcelos / ridgeplot

Beautiful ridgeline plots in Python
https://ridgeplot.readthedocs.io/
MIT License
65 stars 5 forks source link

[BUG] Index-based colormodes raise `ZeroDivisionError` for single-trace plots #252

Open tpvasconcelos opened 1 week ago

tpvasconcelos commented 1 week ago

All index-based colormodes raise a ZeroDivisionError when plotting a ridgeline plot with either a single trace or a single trace per row.

The index-based colormodes are:

colormodes = [
    "row-index",
    "trace-index",
    "trace-index-row-wise",
]

MVE:

>>> from ridgeplot import ridgeplot
>>> ridgeplot(samples=[[1, 2, 2, 3]], colormode=colormodes[0])
Traceback (most recent call last):
...
  File "[...]/ridgeplot/_color/interpolation.py", line 68, in _interpolate_row_index
    [((ctx.n_rows - 1) - ith_row) / (ctx.n_rows - 1)] * len(row)
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
ZeroDivisionError: division by zero
sstephanyy commented 1 week ago

@tpvasconcelos oops, I skipped the math classes 🥺 :rofl: . Please, give me some tips (without giving me the final answer) how could I debug to solve this bug.

tpvasconcelos commented 6 days ago

Hey @sstephanyy I updated the description to give a bit more context, but it's completely okay if you still think that the context is not enough to go out and try to debug the issue.

In short, the error is being raised in the _interpolate_row_index, _interpolate_trace_index (etc) functions because we did not account for the edge cases where either the number of traces is only 1 or the number of traces per row is only 1. Resulting in things like ((ctx.n_rows - 1) - ith_row) / (ctx.n_rows - 1) raising the ZeroDivisionError exception

tpvasconcelos commented 6 days ago

If you are interested in giving it at try, you could consider taking a look at TDD as a methodology to help arrive at the solution faster. i.e.,

  1. Define some tests cases that should work. These will currently fail and raise the ZeroDivisionError exception
  2. Debug the issue and make the necessary changes until all tests pass (including all existing tests)
  3. Once all tests are green, refactor the code until you're happy with the final result (making sure that the tests never stop passing)
  4. Done!
sstephanyy commented 6 days ago

If you are interested in giving it at try, you could consider taking a look at TDD as a methodology to help arrive at the solution faster. i.e.,

1. Define some tests cases that should work. These will currently fail and raise the `ZeroDivisionError` exception

2. Debug the issue and make the necessary changes until all tests pass (including all existing tests)

3. Once all tests are green, refactor the code until you're happy with the final result (making sure that the tests never stop passing)

4. Done!

Thanks so much @tpvasconcelos . I'll give it a try :smiley: . It may take a few days for me to get into a solution, because I am still slow developing and debugging code :fearful: