From 6aa37b033cb4effe21b384f9e1b69ed14d9b8ebb Mon Sep 17 00:00:00 2001 From: Stephany Date: Thu, 31 Oct 2024 20:38:59 -0300 Subject: [PATCH 01/13] Defining some tests cases to raise the ZeroDivisionError exception --- tests/unit/color/test_interpolation.py | 43 ++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/unit/color/test_interpolation.py b/tests/unit/color/test_interpolation.py index dfe00e89..c7b39f0c 100644 --- a/tests/unit/color/test_interpolation.py +++ b/tests/unit/color/test_interpolation.py @@ -91,6 +91,49 @@ def test_interpolate_color_fails_for_p_out_of_bounds(p: float) -> None: interpolate_color(colorscale=..., p=p) # type: ignore[arg-type] +@pytest.fixture +def sample_data() -> list[list[int]]: + return [[1, 2, 2, 3]] + + +@pytest.fixture +def colormodes() -> list[str]: + return ["row-index", "trace-index", "trace-index-row-wise"] + + +def test_no_zero_division_error_single_trace_single_row( + sample_data: list[list[int]], colormodes: list[str] +) -> None: + try: + ridgeplot(sample_data, colormode=colormodes[0]) + except ZeroDivisionError: + pytest.fail( + "ZeroDivisionError raised unexpectedly for row-index colormode with a single trace." + ) + + +def test_no_zero_division_error_ingle_trace_trace_index( + sample_data: list[list[int]], colormodes: list[str] +) -> None: + try: + ridgeplot(sample_data, colormode=colormodes[1]) + except ZeroDivisionError: + pytest.fail( + "ZeroDivisionError raised unexpectedly for row-index colormode with a single trace." + ) + + +def test_no_zero_division_error_single_trace_trace_index_row_wise( + sample_data: list[list[int]], colormodes: list[str] +) -> None: + try: + ridgeplot(sample_data, colormode=colormodes[2]) + except ZeroDivisionError: + pytest.fail( + "ZeroDivisionError raised unexpectedly for row-index colormode with a single trace." + ) + + # ============================================================== # --- slice_colorscale() # ============================================================== From 42985007e8f68d17c812390b292d88fe99a787f7 Mon Sep 17 00:00:00 2001 From: Stephany Date: Thu, 31 Oct 2024 21:10:59 -0300 Subject: [PATCH 02/13] Return default color value instead of performing calculations for single-row datasets --- src/ridgeplot/_color/interpolation.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/ridgeplot/_color/interpolation.py b/src/ridgeplot/_color/interpolation.py index 9ffc6793..d3d80493 100644 --- a/src/ridgeplot/_color/interpolation.py +++ b/src/ridgeplot/_color/interpolation.py @@ -68,6 +68,8 @@ 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] * len(row) for row in ctx.densities] return [ [((ctx.n_rows - 1) - ith_row) / (ctx.n_rows - 1)] * len(row) for ith_row, row in enumerate(ctx.densities) @@ -75,6 +77,8 @@ def _interpolate_row_index(ctx: InterpolationContext) -> ColorscaleInterpolants: def _interpolate_trace_index(ctx: InterpolationContext) -> ColorscaleInterpolants: + if ctx.n_traces <= 1: + return [[0.0] * len(row) for row in ctx.densities] ps = [] ith_trace = 0 for row in ctx.densities: @@ -87,6 +91,8 @@ def _interpolate_trace_index(ctx: InterpolationContext) -> ColorscaleInterpolant def _interpolate_trace_index_row_wise(ctx: InterpolationContext) -> ColorscaleInterpolants: + if ctx.n_rows <= 1: + return [[0.0] * len(row) for row in ctx.densities] return [ [((len(row) - 1) - ith_row_trace) / (len(row) - 1) for ith_row_trace in range(len(row))] for row in ctx.densities From 264b5cd5de3760e66f18b464c989cbecf4980339 Mon Sep 17 00:00:00 2001 From: Stephany Date: Fri, 1 Nov 2024 21:03:50 -0300 Subject: [PATCH 03/13] Updating documentation --- docs/reference/changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/reference/changelog.md b/docs/reference/changelog.md index 9b7f6997..6e80be3c 100644 --- a/docs/reference/changelog.md +++ b/docs/reference/changelog.md @@ -241,6 +241,7 @@ This release contains a number of improvements to the docs, API reference, CI/CD - Remove `mdformat` from the automated CI checks. It can still be triggered manually ({gh-pr}`114`) - Improved type annotations and type checking ({gh-pr}`114`) +- Fixed a ZeroDivisionError in `index-based` colormodes for single-trace or single-row ridgeline plots by adding a check that assigns a default color when only one row is present ({gh-pr}`268`). --- From fbd129812e7f42c269290e4ef3fe65bfa605538f Mon Sep 17 00:00:00 2001 From: Stephany Date: Fri, 1 Nov 2024 21:19:28 -0300 Subject: [PATCH 04/13] fixing mypy test error --- tests/unit/color/test_interpolation.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/unit/color/test_interpolation.py b/tests/unit/color/test_interpolation.py index c7b39f0c..d1055545 100644 --- a/tests/unit/color/test_interpolation.py +++ b/tests/unit/color/test_interpolation.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Literal import pytest @@ -91,18 +91,21 @@ def test_interpolate_color_fails_for_p_out_of_bounds(p: float) -> None: interpolate_color(colorscale=..., p=p) # type: ignore[arg-type] +ColormodeType = Literal["row-index", "trace-index", "trace-index-row-wise"] + + @pytest.fixture def sample_data() -> list[list[int]]: return [[1, 2, 2, 3]] @pytest.fixture -def colormodes() -> list[str]: +def colormodes() -> list[ColormodeType]: return ["row-index", "trace-index", "trace-index-row-wise"] def test_no_zero_division_error_single_trace_single_row( - sample_data: list[list[int]], colormodes: list[str] + sample_data: list[list[int]], colormodes: list[ColormodeType] ) -> None: try: ridgeplot(sample_data, colormode=colormodes[0]) @@ -113,7 +116,7 @@ def test_no_zero_division_error_single_trace_single_row( def test_no_zero_division_error_ingle_trace_trace_index( - sample_data: list[list[int]], colormodes: list[str] + sample_data: list[list[int]], colormodes: list[ColormodeType] ) -> None: try: ridgeplot(sample_data, colormode=colormodes[1]) @@ -124,7 +127,7 @@ def test_no_zero_division_error_ingle_trace_trace_index( def test_no_zero_division_error_single_trace_trace_index_row_wise( - sample_data: list[list[int]], colormodes: list[str] + sample_data: list[list[int]], colormodes: list[ColormodeType] ) -> None: try: ridgeplot(sample_data, colormode=colormodes[2]) From bba6be22c124d47c11f95fcdb1c602a01325369d Mon Sep 17 00:00:00 2001 From: Tomas Pereira de Vasconcelos Date: Mon, 4 Nov 2024 00:22:29 +0000 Subject: [PATCH 05/13] Update changelog.md --- docs/reference/changelog.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/reference/changelog.md b/docs/reference/changelog.md index 6e80be3c..f02e197a 100644 --- a/docs/reference/changelog.md +++ b/docs/reference/changelog.md @@ -23,6 +23,11 @@ Unreleased changes - 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 @@ -224,7 +229,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 or Plotly Express' `px.colors.named_colorscales()` ({gh-pr}`262`) ### Features @@ -241,7 +245,6 @@ This release contains a number of improvements to the docs, API reference, CI/CD - Remove `mdformat` from the automated CI checks. It can still be triggered manually ({gh-pr}`114`) - Improved type annotations and type checking ({gh-pr}`114`) -- Fixed a ZeroDivisionError in `index-based` colormodes for single-trace or single-row ridgeline plots by adding a check that assigns a default color when only one row is present ({gh-pr}`268`). --- From bbf17dbd463d99593ea856f2bac12e395bcfe620 Mon Sep 17 00:00:00 2001 From: Tomas Pereira de Vasconcelos Date: Mon, 4 Nov 2024 01:03:31 +0000 Subject: [PATCH 06/13] Refactor `test_no_zero_division_error` tests --- tests/unit/color/test_interpolation.py | 50 ++++++-------------------- 1 file changed, 10 insertions(+), 40 deletions(-) diff --git a/tests/unit/color/test_interpolation.py b/tests/unit/color/test_interpolation.py index ff0710e4..60565980 100644 --- a/tests/unit/color/test_interpolation.py +++ b/tests/unit/color/test_interpolation.py @@ -1,12 +1,13 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Literal +from typing import TYPE_CHECKING import pytest from ridgeplot import ridgeplot from ridgeplot._color.interpolation import ( InterpolationContext, + SolidColormode, _interpolate_color, _interpolate_mean_means, _interpolate_mean_minmax, @@ -94,46 +95,15 @@ def test_interpolate_mean_means() -> None: assert ps == [[0.0], [0.5], [1.0]] -ColormodeType = Literal["row-index", "trace-index", "trace-index-row-wise"] - - -@pytest.fixture -def sample_data() -> list[list[int]]: - return [[1, 2, 2, 3]] - - -@pytest.fixture -def colormodes() -> list[ColormodeType]: - return ["row-index", "trace-index", "trace-index-row-wise"] - - -def test_no_zero_division_error_single_trace_single_row( - sample_data: list[list[int]], colormodes: list[ColormodeType] -) -> None: - try: - ridgeplot(sample_data, colormode=colormodes[0]) - except ZeroDivisionError: - pytest.fail( - "ZeroDivisionError raised unexpectedly for row-index colormode with a single trace." - ) - - -def test_no_zero_division_error_ingle_trace_trace_index( - sample_data: list[list[int]], colormodes: list[ColormodeType] -) -> None: - try: - ridgeplot(sample_data, colormode=colormodes[1]) - except ZeroDivisionError: - pytest.fail( - "ZeroDivisionError raised unexpectedly for row-index colormode with a single trace." - ) - - -def test_no_zero_division_error_single_trace_trace_index_row_wise( - sample_data: list[list[int]], colormodes: list[ColormodeType] -) -> None: +@pytest.mark.parametrize( + "colormode", + ["row-index", "trace-index", "trace-index-row-wise"], +) +def test_no_zero_division_error(colormode: SolidColormode) -> None: + """ZeroDivisionError should never be raised, even when there is only one + trace, one row, or one trace per row.""" try: - ridgeplot(sample_data, colormode=colormodes[2]) + ridgeplot([[1, 2, 2, 3]], colormode=colormode) except ZeroDivisionError: pytest.fail( "ZeroDivisionError raised unexpectedly for row-index colormode with a single trace." From d2a480e40625ff1050586533431f6f3add783f9e Mon Sep 17 00:00:00 2001 From: Stephany Date: Mon, 4 Nov 2024 19:38:49 -0300 Subject: [PATCH 07/13] Fix: Change condition to check for a single trace in context --- src/ridgeplot/_color/interpolation.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ridgeplot/_color/interpolation.py b/src/ridgeplot/_color/interpolation.py index d3d80493..424aa392 100644 --- a/src/ridgeplot/_color/interpolation.py +++ b/src/ridgeplot/_color/interpolation.py @@ -68,7 +68,7 @@ def _mul(a: tuple[Numeric, ...], b: tuple[Numeric, ...]) -> tuple[Numeric, ...]: def _interpolate_row_index(ctx: InterpolationContext) -> ColorscaleInterpolants: - if ctx.n_rows <= 1: + if ctx.n_rows == 1: return [[0.0] * len(row) for row in ctx.densities] return [ [((ctx.n_rows - 1) - ith_row) / (ctx.n_rows - 1)] * len(row) @@ -77,7 +77,7 @@ def _interpolate_row_index(ctx: InterpolationContext) -> ColorscaleInterpolants: def _interpolate_trace_index(ctx: InterpolationContext) -> ColorscaleInterpolants: - if ctx.n_traces <= 1: + if ctx.n_traces == 1: return [[0.0] * len(row) for row in ctx.densities] ps = [] ith_trace = 0 @@ -91,7 +91,7 @@ def _interpolate_trace_index(ctx: InterpolationContext) -> ColorscaleInterpolant def _interpolate_trace_index_row_wise(ctx: InterpolationContext) -> ColorscaleInterpolants: - if ctx.n_rows <= 1: + if ctx.n_rows == 1: return [[0.0] * len(row) for row in ctx.densities] return [ [((len(row) - 1) - ith_row_trace) / (len(row) - 1) for ith_row_trace in range(len(row))] From 04408583d06c9a618bea909cc30b72dfe7b41081 Mon Sep 17 00:00:00 2001 From: Stephany Date: Mon, 4 Nov 2024 19:52:10 -0300 Subject: [PATCH 08/13] Refactoring return --- src/ridgeplot/_color/interpolation.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ridgeplot/_color/interpolation.py b/src/ridgeplot/_color/interpolation.py index 5a18d288..a05705a5 100644 --- a/src/ridgeplot/_color/interpolation.py +++ b/src/ridgeplot/_color/interpolation.py @@ -116,7 +116,7 @@ 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] * len(row) for row in ctx.densities] + return [[0.0]] return [ [((ctx.n_rows - 1) - ith_row) / (ctx.n_rows - 1)] * len(row) for ith_row, row in enumerate(ctx.densities) @@ -125,7 +125,7 @@ def _interpolate_row_index(ctx: InterpolationContext) -> ColorscaleInterpolants: def _interpolate_trace_index(ctx: InterpolationContext) -> ColorscaleInterpolants: if ctx.n_traces == 1: - return [[0.0] * len(row) for row in ctx.densities] + return [[0.0]] ps = [] ith_trace = 0 for row in ctx.densities: @@ -139,7 +139,7 @@ def _interpolate_trace_index(ctx: InterpolationContext) -> ColorscaleInterpolant def _interpolate_trace_index_row_wise(ctx: InterpolationContext) -> ColorscaleInterpolants: if ctx.n_rows == 1: - return [[0.0] * len(row) for row in ctx.densities] + return [[0.0]] return [ [((len(row) - 1) - ith_row_trace) / (len(row) - 1) for ith_row_trace in range(len(row))] for row in ctx.densities From 106fa0894c49dc8996f45e2d7d7f8cc3a4cd21e2 Mon Sep 17 00:00:00 2001 From: Tomas Pereira de Vasconcelos Date: Tue, 12 Nov 2024 17:26:26 +0100 Subject: [PATCH 09/13] Add more examples to `test_index_based_colormodes()` --- tests/unit/color/test_interpolation.py | 74 ++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 10 deletions(-) diff --git a/tests/unit/color/test_interpolation.py b/tests/unit/color/test_interpolation.py index 60565980..095a618e 100644 --- a/tests/unit/color/test_interpolation.py +++ b/tests/unit/color/test_interpolation.py @@ -6,6 +6,8 @@ from ridgeplot import ridgeplot from ridgeplot._color.interpolation import ( + SOLID_COLORMODE_MAPS, + ColorscaleInterpolants, InterpolationContext, SolidColormode, _interpolate_color, @@ -17,7 +19,7 @@ from ridgeplot._color.utils import to_rgb if TYPE_CHECKING: - from ridgeplot._types import ColorScale + from ridgeplot._types import ColorScale, Densities # ============================================================== @@ -95,19 +97,71 @@ def test_interpolate_mean_means() -> None: assert ps == [[0.0], [0.5], [1.0]] +_EXAMPLE_DENSITY_01 = [(0, 1), (1, 2), (2, 1)] +_EXAMPLE_DENSITY_02 = [(1, 1), (2, 2), (3, 1)] + + @pytest.mark.parametrize( - "colormode", - ["row-index", "trace-index", "trace-index-row-wise"], + ("colormode", "densities", "expected"), + [ + # One trace + ( + "row-index", + [[_EXAMPLE_DENSITY_01]], + [[0]], + ), + ( + "trace-index", + [[_EXAMPLE_DENSITY_01]], + [[0]], + ), + ( + "trace-index-row-wise", + [[_EXAMPLE_DENSITY_01]], + [[0]], + ), + # One row + ( + "row-index", + [[_EXAMPLE_DENSITY_01, _EXAMPLE_DENSITY_02]], + [[0, 0]], + ), + ( + "trace-index", + [[_EXAMPLE_DENSITY_01, _EXAMPLE_DENSITY_02]], + [[0, 1]], + ), + ( + "trace-index-row-wise", + [[_EXAMPLE_DENSITY_01, _EXAMPLE_DENSITY_02]], + [[0, 1]], + ), + # One trace per row + ( + "row-index", + [[_EXAMPLE_DENSITY_01], [_EXAMPLE_DENSITY_02]], + [[0, 1]], + ), + ( + "trace-index", + [[_EXAMPLE_DENSITY_01], [_EXAMPLE_DENSITY_02]], + [[0, 1]], + ), + ( + "trace-index-row-wise", + [[_EXAMPLE_DENSITY_01], [_EXAMPLE_DENSITY_02]], + [[0, 1]], + ), + ], ) -def test_no_zero_division_error(colormode: SolidColormode) -> None: +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.""" - try: - ridgeplot([[1, 2, 2, 3]], colormode=colormode) - except ZeroDivisionError: - pytest.fail( - "ZeroDivisionError raised unexpectedly for row-index colormode with a single trace." - ) + interpolate_func = SOLID_COLORMODE_MAPS[colormode] + interpolants = interpolate_func(ctx=InterpolationContext.from_densities(densities)) + assert interpolants == expected # ============================================================== From 5c50dc9a36cd4fea930a1bc1498be2e10473cafb Mon Sep 17 00:00:00 2001 From: Tomas Pereira de Vasconcelos Date: Tue, 12 Nov 2024 17:29:59 +0100 Subject: [PATCH 10/13] Revert `[[0.0]]` simplification --- src/ridgeplot/_color/interpolation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ridgeplot/_color/interpolation.py b/src/ridgeplot/_color/interpolation.py index a05705a5..11e91caa 100644 --- a/src/ridgeplot/_color/interpolation.py +++ b/src/ridgeplot/_color/interpolation.py @@ -116,7 +116,7 @@ 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]] + return [[0.0] * len(row) for row in ctx.densities] return [ [((ctx.n_rows - 1) - ith_row) / (ctx.n_rows - 1)] * len(row) for ith_row, row in enumerate(ctx.densities) @@ -139,7 +139,7 @@ def _interpolate_trace_index(ctx: InterpolationContext) -> ColorscaleInterpolant def _interpolate_trace_index_row_wise(ctx: InterpolationContext) -> ColorscaleInterpolants: if ctx.n_rows == 1: - return [[0.0]] + return [[0.0] * len(row) for row in ctx.densities] return [ [((len(row) - 1) - ith_row_trace) / (len(row) - 1) for ith_row_trace in range(len(row))] for row in ctx.densities From b9d67f17fa42bf48ff64e13c588b10799d9d9ba0 Mon Sep 17 00:00:00 2001 From: Tomas Pereira de Vasconcelos Date: Tue, 12 Nov 2024 17:44:01 +0100 Subject: [PATCH 11/13] Update test cases in `test_index_based_colormodes()` --- tests/unit/color/test_interpolation.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/unit/color/test_interpolation.py b/tests/unit/color/test_interpolation.py index 095a618e..901fe0cc 100644 --- a/tests/unit/color/test_interpolation.py +++ b/tests/unit/color/test_interpolation.py @@ -108,49 +108,49 @@ def test_interpolate_mean_means() -> None: ( "row-index", [[_EXAMPLE_DENSITY_01]], - [[0]], + [[0.0]], ), ( "trace-index", [[_EXAMPLE_DENSITY_01]], - [[0]], + [[0.0]], ), ( "trace-index-row-wise", [[_EXAMPLE_DENSITY_01]], - [[0]], + [[0.0]], ), # One row ( "row-index", [[_EXAMPLE_DENSITY_01, _EXAMPLE_DENSITY_02]], - [[0, 0]], + [[0.0, 0.0]], ), ( "trace-index", [[_EXAMPLE_DENSITY_01, _EXAMPLE_DENSITY_02]], - [[0, 1]], + [[1.0, 0.0]], ), ( "trace-index-row-wise", [[_EXAMPLE_DENSITY_01, _EXAMPLE_DENSITY_02]], - [[0, 1]], + [[1.0, 0.0]], ), # One trace per row ( "row-index", [[_EXAMPLE_DENSITY_01], [_EXAMPLE_DENSITY_02]], - [[0, 1]], + [[1.0], [0.0]], ), ( "trace-index", [[_EXAMPLE_DENSITY_01], [_EXAMPLE_DENSITY_02]], - [[0, 1]], + [[1.0], [0.0]], ), ( "trace-index-row-wise", [[_EXAMPLE_DENSITY_01], [_EXAMPLE_DENSITY_02]], - [[0, 1]], + [[1.0], [0.0]], ), ], ) From 31876a05a75d58cc68009e6403d9f20d15403d52 Mon Sep 17 00:00:00 2001 From: Tomas Pereira de Vasconcelos Date: Sun, 24 Nov 2024 22:30:45 +0000 Subject: [PATCH 12/13] Fix edge cases for `_interpolate_trace_index_row_wise` --- src/ridgeplot/_color/interpolation.py | 7 +-- tests/unit/color/test_interpolation.py | 62 +++++++------------------- 2 files changed, 19 insertions(+), 50 deletions(-) diff --git a/src/ridgeplot/_color/interpolation.py b/src/ridgeplot/_color/interpolation.py index 11e91caa..f70b086d 100644 --- a/src/ridgeplot/_color/interpolation.py +++ b/src/ridgeplot/_color/interpolation.py @@ -138,10 +138,11 @@ def _interpolate_trace_index(ctx: InterpolationContext) -> ColorscaleInterpolant def _interpolate_trace_index_row_wise(ctx: InterpolationContext) -> ColorscaleInterpolants: - if ctx.n_rows == 1: - return [[0.0] * len(row) for row in ctx.densities] 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 ] diff --git a/tests/unit/color/test_interpolation.py b/tests/unit/color/test_interpolation.py index 901fe0cc..5a7ae793 100644 --- a/tests/unit/color/test_interpolation.py +++ b/tests/unit/color/test_interpolation.py @@ -97,61 +97,29 @@ def test_interpolate_mean_means() -> None: assert ps == [[0.0], [0.5], [1.0]] -_EXAMPLE_DENSITY_01 = [(0, 1), (1, 2), (2, 1)] -_EXAMPLE_DENSITY_02 = [(1, 1), (2, 2), (3, 1)] +_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", - [[_EXAMPLE_DENSITY_01]], - [[0.0]], - ), - ( - "trace-index", - [[_EXAMPLE_DENSITY_01]], - [[0.0]], - ), - ( - "trace-index-row-wise", - [[_EXAMPLE_DENSITY_01]], - [[0.0]], - ), + ("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", - [[_EXAMPLE_DENSITY_01, _EXAMPLE_DENSITY_02]], - [[0.0, 0.0]], - ), - ( - "trace-index", - [[_EXAMPLE_DENSITY_01, _EXAMPLE_DENSITY_02]], - [[1.0, 0.0]], - ), - ( - "trace-index-row-wise", - [[_EXAMPLE_DENSITY_01, _EXAMPLE_DENSITY_02]], - [[1.0, 0.0]], - ), + ("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", - [[_EXAMPLE_DENSITY_01], [_EXAMPLE_DENSITY_02]], - [[1.0], [0.0]], - ), - ( - "trace-index", - [[_EXAMPLE_DENSITY_01], [_EXAMPLE_DENSITY_02]], - [[1.0], [0.0]], - ), - ( - "trace-index-row-wise", - [[_EXAMPLE_DENSITY_01], [_EXAMPLE_DENSITY_02]], - [[1.0], [0.0]], - ), + ("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( From 7c6ebdb14d71569daf635d6ab10f2e57e26ec870 Mon Sep 17 00:00:00 2001 From: Tomas Pereira de Vasconcelos Date: Sun, 24 Nov 2024 22:31:01 +0000 Subject: [PATCH 13/13] Simplify edge case for `_interpolate_row_index` --- src/ridgeplot/_color/interpolation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ridgeplot/_color/interpolation.py b/src/ridgeplot/_color/interpolation.py index f70b086d..1980ec92 100644 --- a/src/ridgeplot/_color/interpolation.py +++ b/src/ridgeplot/_color/interpolation.py @@ -116,7 +116,7 @@ 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] * len(row) for row in ctx.densities] + 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)