Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
6aa37b0
Defining some tests cases to raise the ZeroDivisionError exception
sstephanyy Oct 31, 2024
4298500
Return default color value instead of performing calculations for sin…
sstephanyy Nov 1, 2024
264b5cd
Updating documentation
sstephanyy Nov 2, 2024
fbd1298
fixing mypy test error
sstephanyy Nov 2, 2024
bba6be2
Update changelog.md
tpvasconcelos Nov 4, 2024
45a4d37
Merge branch 'main' into fix/single-trace-zero-division-error
tpvasconcelos Nov 4, 2024
17e2c61
Merge branch 'refactor-interpol' into fix/single-trace-zero-division-…
tpvasconcelos Nov 4, 2024
bbf17db
Refactor `test_no_zero_division_error` tests
tpvasconcelos Nov 4, 2024
f64fa6d
Merge branch 'main' into fix/single-trace-zero-division-error
tpvasconcelos Nov 4, 2024
d2a480e
Fix: Change condition to check for a single trace in context
sstephanyy Nov 4, 2024
2cb881e
Merge remote-tracking branch 'refs/remotes/fork/fix/single-trace-zero…
sstephanyy Nov 4, 2024
0440858
Refactoring return
sstephanyy Nov 4, 2024
7b7c968
Merge branch 'main' into fix/single-trace-zero-division-error
tpvasconcelos Nov 8, 2024
106fa08
Add more examples to `test_index_based_colormodes()`
tpvasconcelos Nov 12, 2024
5c50dc9
Revert `[[0.0]]` simplification
tpvasconcelos Nov 12, 2024
b9d67f1
Update test cases in `test_index_based_colormodes()`
tpvasconcelos Nov 12, 2024
aac09ec
Merge branch 'main' into fix/single-trace-zero-division-error
tpvasconcelos Nov 18, 2024
458e0b3
Merge branch 'main' into fix/single-trace-zero-division-error
tpvasconcelos Nov 24, 2024
31876a0
Fix edge cases for `_interpolate_trace_index_row_wise`
tpvasconcelos Nov 24, 2024
7c6ebdb
Simplify edge case for `_interpolate_row_index`
tpvasconcelos Nov 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/reference/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ We will make an effort to standardise and document our versioning policy. For no

- Rename `coloralpha` to `opacity` for consistently with Plotly Express and deprecate the old parameter name ({gh-pr}`245`)
- Rename `linewidth` to `line_width` for consistency with Plotly's API and deprecate the old parameter name ({gh-pr}`253`)
- Deprecated `colorscale='default'` and `list_all_colorscale_names()` in favour or Plotly Express' `px.colors.named_colorscales()` ({gh-pr}`262`)

### Bug fixes

- Fixed `ZeroDivisionError` for index-based colormodes when specifying single-trace or single-row plots ({gh-pr}`268`)

### Dependencies

Expand Down Expand Up @@ -249,7 +254,6 @@ This release contains a number of improvements to the docs, API reference, CI/CD
- The `colormode='index'` value has been deprecated in favor of `colormode='row-index'`, which provides the same functionality but is more explicit and allows to distinguish between the `'row-index'` and `'trace-index'` modes ({gh-pr}`114`)
- The `show_annotations` argument has been deprecated in favor of `show_yticklabels` ({gh-pr}`114`)
- The `get_all_colorscale_names()` function has been deprecated in favor of `list_all_colorscale_names()` ({gh-pr}`114`)
- Deprecated `colorscale='default'` and `list_all_colorscale_names()` in favour of Plotly Express' `px.colors.named_colorscales()` ({gh-pr}`262`)

### Features

Expand Down
9 changes: 8 additions & 1 deletion src/ridgeplot/_color/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,17 @@ def _mul(a: tuple[Numeric, ...], b: tuple[Numeric, ...]) -> tuple[Numeric, ...]:


def _interpolate_row_index(ctx: InterpolationContext) -> ColorscaleInterpolants:
if ctx.n_rows == 1:
return [[0.0] * ctx.n_traces]
return [
[((ctx.n_rows - 1) - ith_row) / (ctx.n_rows - 1)] * len(row)
for ith_row, row in enumerate(ctx.densities)
]


def _interpolate_trace_index(ctx: InterpolationContext) -> ColorscaleInterpolants:
if ctx.n_traces == 1:
return [[0.0]]
ps = []
ith_trace = 0
for row in ctx.densities:
Expand All @@ -135,7 +139,10 @@ def _interpolate_trace_index(ctx: InterpolationContext) -> ColorscaleInterpolant

def _interpolate_trace_index_row_wise(ctx: InterpolationContext) -> ColorscaleInterpolants:
return [
[((len(row) - 1) - ith_row_trace) / (len(row) - 1) for ith_row_trace in range(len(row))]
[
((len(row) - 1) - ith_row_trace) / (len(row) - 1) if len(row) > 1 else 0.0
for ith_row_trace in range(len(row))
]
for row in ctx.densities
]

Expand Down
40 changes: 39 additions & 1 deletion tests/unit/color/test_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

from ridgeplot import ridgeplot
from ridgeplot._color.interpolation import (
SOLID_COLORMODE_MAPS,
ColorscaleInterpolants,
InterpolationContext,
SolidColormode,
_interpolate_color,
_interpolate_mean_means,
_interpolate_mean_minmax,
Expand All @@ -16,7 +19,7 @@
from ridgeplot._color.utils import to_rgb

if TYPE_CHECKING:
from ridgeplot._types import ColorScale
from ridgeplot._types import ColorScale, Densities


# ==============================================================
Expand Down Expand Up @@ -94,6 +97,41 @@ def test_interpolate_mean_means() -> None:
assert ps == [[0.0], [0.5], [1.0]]


_DENSITY_01 = [(0, 1), (1, 2), (2, 1)]
_DENSITY_02 = [(1, 1), (2, 2), (3, 1)]

_DENSITIES_ONE_TRACE = [[_DENSITY_01]]
_DENSITIES_ONE_ROW = [[_DENSITY_01, _DENSITY_02]]
_DENSITIES_ONE_TRACE_PER_ROW = [[_DENSITY_01, _DENSITY_02], [_DENSITY_02]]


@pytest.mark.parametrize(
("colormode", "densities", "expected"),
[
# One trace
("row-index", _DENSITIES_ONE_TRACE, [[0.0]]),
("trace-index", _DENSITIES_ONE_TRACE, [[0.0]]),
("trace-index-row-wise", _DENSITIES_ONE_TRACE, [[0.0]]),
# One row
("row-index", _DENSITIES_ONE_ROW, [[0.0, 0.0]]),
("trace-index", _DENSITIES_ONE_ROW, [[1.0, 0.0]]),
("trace-index-row-wise", _DENSITIES_ONE_ROW, [[1.0, 0.0]]),
# One trace per row
("row-index", _DENSITIES_ONE_TRACE_PER_ROW, [[1.0, 1.0], [0.0]]),
("trace-index", _DENSITIES_ONE_TRACE_PER_ROW, [[1.0, 0.5], [0.0]]),
("trace-index-row-wise", _DENSITIES_ONE_TRACE_PER_ROW, [[1.0, 0.0], [0.0]]),
],
)
def test_index_based_colormodes(
colormode: SolidColormode, densities: Densities, expected: ColorscaleInterpolants
) -> None:
"""ZeroDivisionError should never be raised, even when there is only one
trace, one row, or one trace per row."""
interpolate_func = SOLID_COLORMODE_MAPS[colormode]
interpolants = interpolate_func(ctx=InterpolationContext.from_densities(densities))
assert interpolants == expected


# ==============================================================
# --- _slice_colorscale()
# ==============================================================
Expand Down
Loading