|
| 1 | +import logging |
1 | 2 | import math |
2 | 3 |
|
3 | 4 | import dask.dataframe |
| 5 | +import datashader as ds |
4 | 6 | import matplotlib |
5 | 7 | import matplotlib.pyplot as plt |
6 | 8 | import numpy as np |
|
23 | 25 |
|
24 | 26 | import spatialdata_plot # noqa: F401 |
25 | 27 | from spatialdata_plot._logging import logger, logger_warns |
| 28 | +from spatialdata_plot.pl._datashader import ( |
| 29 | + _build_datashader_color_key, |
| 30 | + _ds_aggregate, |
| 31 | + _ds_shade_categorical, |
| 32 | +) |
| 33 | +from spatialdata_plot.pl.render import _warn_groups_ignored_continuous |
26 | 34 | from tests.conftest import DPI, PlotTester, PlotTesterMeta, _viridis_with_under_over, get_standard_RNG |
27 | 35 |
|
28 | 36 | sc.pl.set_rcParams_defaults() |
@@ -741,3 +749,165 @@ def test_datashader_alpha_not_applied_twice(sdata_blobs: SpatialData): |
741 | 749 | "on top of the alpha already in the RGBA channels — causing double transparency." |
742 | 750 | ) |
743 | 751 | plt.close(fig) |
| 752 | + |
| 753 | + |
| 754 | +# --------------------------------------------------------------------------- |
| 755 | +# Tests for datashader pipeline fixes (parameter forwarding, warnings) |
| 756 | +# --------------------------------------------------------------------------- |
| 757 | + |
| 758 | + |
| 759 | +def _make_ds_canvas_and_df(n=500, seed=42): |
| 760 | + """Small datashader Canvas + DataFrame with x, y, cat, val columns.""" |
| 761 | + rng = np.random.default_rng(seed) |
| 762 | + df = pd.DataFrame( |
| 763 | + { |
| 764 | + "x": rng.uniform(-10, 10, n), |
| 765 | + "y": rng.uniform(-10, 10, n), |
| 766 | + "cat": pd.Categorical(rng.choice(["A", "B", "C"], n)), |
| 767 | + "val": rng.normal(0, 1, n), |
| 768 | + } |
| 769 | + ) |
| 770 | + cvs = ds.Canvas(plot_width=50, plot_height=50, x_range=(-10, 10), y_range=(-10, 10)) |
| 771 | + return cvs, df |
| 772 | + |
| 773 | + |
| 774 | +def test_ds_aggregate_default_reduction_is_forwarded(): |
| 775 | + """default_reduction must affect the actual aggregation, not just the log message.""" |
| 776 | + cvs, df = _make_ds_canvas_and_df() |
| 777 | + agg_sum, _, _ = _ds_aggregate(cvs, df.copy(), "val", False, None, "sum", "points") |
| 778 | + agg_max, _, _ = _ds_aggregate(cvs, df.copy(), "val", False, None, "max", "points") |
| 779 | + assert not np.allclose( |
| 780 | + np.nan_to_num(agg_sum.values, nan=0), |
| 781 | + np.nan_to_num(agg_max.values, nan=0), |
| 782 | + ) |
| 783 | + |
| 784 | + |
| 785 | +def test_ds_aggregate_default_reduction_equals_explicit(): |
| 786 | + """default_reduction='max' with ds_reduction=None must equal explicit ds_reduction='max'.""" |
| 787 | + cvs, df = _make_ds_canvas_and_df() |
| 788 | + agg_default, _, _ = _ds_aggregate(cvs, df.copy(), "val", False, None, "max", "points") |
| 789 | + agg_explicit, _, _ = _ds_aggregate(cvs, df.copy(), "val", False, "max", "max", "points") |
| 790 | + np.testing.assert_array_equal( |
| 791 | + np.nan_to_num(agg_default.values, nan=0), |
| 792 | + np.nan_to_num(agg_explicit.values, nan=0), |
| 793 | + ) |
| 794 | + |
| 795 | + |
| 796 | +def test_ds_aggregate_explicit_overrides_default(): |
| 797 | + """Explicit ds_reduction takes precedence over default_reduction.""" |
| 798 | + cvs, df = _make_ds_canvas_and_df() |
| 799 | + agg, _, _ = _ds_aggregate(cvs, df.copy(), "val", False, "max", "sum", "points") |
| 800 | + agg_max, _, _ = _ds_aggregate(cvs, df.copy(), "val", False, "max", "max", "points") |
| 801 | + np.testing.assert_array_equal( |
| 802 | + np.nan_to_num(agg.values, nan=0), |
| 803 | + np.nan_to_num(agg_max.values, nan=0), |
| 804 | + ) |
| 805 | + |
| 806 | + |
| 807 | +def test_ds_reduction_ignored_for_categorical(caplog): |
| 808 | + """Categorical aggregation always uses ds.count(); a warning is emitted when ds_reduction is set.""" |
| 809 | + cvs, df = _make_ds_canvas_and_df() |
| 810 | + with logger_warns(caplog, logger, match="ignored.*categorical"): |
| 811 | + _ds_aggregate(cvs, df.copy(), "cat", True, "mean", "mean", "points") |
| 812 | + |
| 813 | + |
| 814 | +def test_ds_reduction_no_warning_when_none(caplog): |
| 815 | + """No spurious warning when ds_reduction is None (the default).""" |
| 816 | + cvs, df = _make_ds_canvas_and_df() |
| 817 | + with caplog.at_level(logging.WARNING, logger=logger.name): |
| 818 | + logger.addHandler(caplog.handler) |
| 819 | + try: |
| 820 | + _ds_aggregate(cvs, df.copy(), "cat", True, None, "sum", "points") |
| 821 | + finally: |
| 822 | + logger.removeHandler(caplog.handler) |
| 823 | + assert not any("ignored" in r.message.lower() for r in caplog.records) |
| 824 | + |
| 825 | + |
| 826 | +@pytest.mark.parametrize("reduction", ["mean", "max", "min", "count", "std", "var"]) |
| 827 | +def test_ds_reduction_categorical_always_uses_count(reduction): |
| 828 | + """Categorical aggregation always uses ds.count(), regardless of ds_reduction (by design).""" |
| 829 | + cvs, df = _make_ds_canvas_and_df() |
| 830 | + base, _, _ = _ds_aggregate(cvs, df.copy(), "cat", True, "sum", "sum", "points") |
| 831 | + agg, _, _ = _ds_aggregate(cvs, df.copy(), "cat", True, reduction, reduction, "points") |
| 832 | + np.testing.assert_array_equal(agg.values, base.values) |
| 833 | + |
| 834 | + |
| 835 | +def test_groups_warns_when_continuous_points(sdata_blobs: SpatialData, caplog): |
| 836 | + """Using groups with a continuous color column should warn.""" |
| 837 | + n = len(sdata_blobs["blobs_points"]) |
| 838 | + sdata_blobs["blobs_points"]["cont_val"] = pd.Series(list(range(n)), dtype=float) |
| 839 | + with logger_warns(caplog, logger, match="groups.*ignored.*continuous"): |
| 840 | + sdata_blobs.pl.render_points("blobs_points", color="cont_val", groups=["nonexistent"]).pl.show() |
| 841 | + |
| 842 | + |
| 843 | +def test_warn_groups_ignored_continuous_emits(caplog): |
| 844 | + """_warn_groups_ignored_continuous emits when groups is set but data is continuous.""" |
| 845 | + with logger_warns(caplog, logger, match="ignored.*continuous"): |
| 846 | + _warn_groups_ignored_continuous(["A"], None, "my_col") |
| 847 | + |
| 848 | + |
| 849 | +def test_warn_groups_ignored_continuous_silent_for_categorical(caplog): |
| 850 | + """No warning when color_source_vector is present (categorical).""" |
| 851 | + with caplog.at_level(logging.WARNING, logger=logger.name): |
| 852 | + logger.addHandler(caplog.handler) |
| 853 | + try: |
| 854 | + _warn_groups_ignored_continuous(["A"], pd.Categorical(["A", "B"]), "cat_col") |
| 855 | + finally: |
| 856 | + logger.removeHandler(caplog.handler) |
| 857 | + assert not any("ignored" in r.message for r in caplog.records) |
| 858 | + |
| 859 | + |
| 860 | +def test_color_key_warns_on_short_color_vector(caplog): |
| 861 | + """Warning when color_vector is shorter than categorical series.""" |
| 862 | + cat = pd.Categorical(["A", "B", "C", "A", "B", "C", "A"]) |
| 863 | + with logger_warns(caplog, logger, match="color_vector length"): |
| 864 | + result = _build_datashader_color_key(cat, ["#ff0000", "#00ff00", "#0000ff", "#ff0000", "#00ff00"], "#cccccc") |
| 865 | + assert "A" in result and "B" in result and "C" in result |
| 866 | + |
| 867 | + |
| 868 | +def test_color_key_warns_on_long_color_vector(caplog): |
| 869 | + """Warning when color_vector is longer than categorical series.""" |
| 870 | + cat = pd.Categorical(["A", "B"]) |
| 871 | + with logger_warns(caplog, logger, match="color_vector length"): |
| 872 | + _build_datashader_color_key(cat, ["#ff0000", "#00ff00", "#0000ff", "#ffff00"], "#cccccc") |
| 873 | + |
| 874 | + |
| 875 | +def test_color_key_no_warning_when_lengths_match(caplog): |
| 876 | + """No warning when lengths match.""" |
| 877 | + cat = pd.Categorical(["A", "B", "C"]) |
| 878 | + with caplog.at_level(logging.WARNING, logger=logger.name): |
| 879 | + logger.addHandler(caplog.handler) |
| 880 | + try: |
| 881 | + _build_datashader_color_key(cat, ["#ff0000", "#00ff00", "#0000ff"], "#cccccc") |
| 882 | + finally: |
| 883 | + logger.removeHandler(caplog.handler) |
| 884 | + assert not any("color_vector length" in r.message for r in caplog.records) |
| 885 | + |
| 886 | + |
| 887 | +def test_color_key_unseen_category_gets_na_color(caplog): |
| 888 | + """Categories only appearing after the truncation point get na_color.""" |
| 889 | + cat = pd.Categorical(["A", "B", "A", "B", "A", "D"]) |
| 890 | + with logger_warns(caplog, logger, match="color_vector length"): |
| 891 | + result = _build_datashader_color_key(cat, ["#ff0000", "#00ff00", "#ff0000", "#00ff00"], "#cccccc") |
| 892 | + assert result["D"] == "#cccccc" |
| 893 | + |
| 894 | + |
| 895 | +def test_shade_categorical_color_key_overrides_cmap(): |
| 896 | + """When color_key is provided, different color_vector[0] values must produce identical output.""" |
| 897 | + cvs, df = _make_ds_canvas_and_df(n=100) |
| 898 | + agg = cvs.points(df, "x", "y", agg=ds.by("cat", ds.count())) |
| 899 | + color_key = {"A": "#ff0000", "B": "#00ff00", "C": "#0000ff"} |
| 900 | + |
| 901 | + shaded1 = _ds_shade_categorical(agg, color_key, np.array(["#ff0000"] * 100), alpha=1.0) |
| 902 | + shaded2 = _ds_shade_categorical(agg, color_key, np.array(["#0000ff"] * 100), alpha=1.0) |
| 903 | + np.testing.assert_array_equal(np.asarray(shaded1), np.asarray(shaded2)) |
| 904 | + |
| 905 | + |
| 906 | +def test_shade_categorical_cmap_used_when_no_color_key(): |
| 907 | + """When color_key is None (no color column), cmap from color_vector[0] affects output.""" |
| 908 | + cvs, df = _make_ds_canvas_and_df(n=100) |
| 909 | + agg = cvs.points(df, "x", "y", agg=ds.count()) |
| 910 | + shaded_red = _ds_shade_categorical(agg, None, np.array(["#ff0000"] * 100), alpha=1.0) |
| 911 | + shaded_blue = _ds_shade_categorical(agg, None, np.array(["#0000ff"] * 100), alpha=1.0) |
| 912 | + # Different color_vector[0] values should produce different shaded output |
| 913 | + assert not np.array_equal(np.asarray(shaded_red), np.asarray(shaded_blue)) |
0 commit comments