1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
from dataclasses import dataclass
import numpy as np
import pandas as pd
import matplotlib as mpl
import pytest
from numpy.testing import assert_array_equal
from seaborn._marks.base import Mark, Mappable, resolve_color
class TestMappable:
def mark(self, **features):
@dataclass
class MockMark(Mark):
linewidth: float = Mappable(rc="lines.linewidth")
pointsize: float = Mappable(4)
color: str = Mappable("C0")
fillcolor: str = Mappable(depend="color")
alpha: float = Mappable(1)
fillalpha: float = Mappable(depend="alpha")
m = MockMark(**features)
return m
def test_repr(self):
assert str(Mappable(.5)) == "<0.5>"
assert str(Mappable("CO")) == "<'CO'>"
assert str(Mappable(rc="lines.linewidth")) == "<rc:lines.linewidth>"
assert str(Mappable(depend="color")) == "<depend:color>"
assert str(Mappable(auto=True)) == "<auto>"
def test_input_checks(self):
with pytest.raises(AssertionError):
Mappable(rc="bogus.parameter")
with pytest.raises(AssertionError):
Mappable(depend="nonexistent_feature")
def test_value(self):
val = 3
m = self.mark(linewidth=val)
assert m._resolve({}, "linewidth") == val
df = pd.DataFrame(index=pd.RangeIndex(10))
assert_array_equal(m._resolve(df, "linewidth"), np.full(len(df), val))
def test_default(self):
val = 3
m = self.mark(linewidth=Mappable(val))
assert m._resolve({}, "linewidth") == val
df = pd.DataFrame(index=pd.RangeIndex(10))
assert_array_equal(m._resolve(df, "linewidth"), np.full(len(df), val))
def test_rcparam(self):
param = "lines.linewidth"
val = mpl.rcParams[param]
m = self.mark(linewidth=Mappable(rc=param))
assert m._resolve({}, "linewidth") == val
df = pd.DataFrame(index=pd.RangeIndex(10))
assert_array_equal(m._resolve(df, "linewidth"), np.full(len(df), val))
def test_depends(self):
val = 2
df = pd.DataFrame(index=pd.RangeIndex(10))
m = self.mark(pointsize=Mappable(val), linewidth=Mappable(depend="pointsize"))
assert m._resolve({}, "linewidth") == val
assert_array_equal(m._resolve(df, "linewidth"), np.full(len(df), val))
m = self.mark(pointsize=val * 2, linewidth=Mappable(depend="pointsize"))
assert m._resolve({}, "linewidth") == val * 2
assert_array_equal(m._resolve(df, "linewidth"), np.full(len(df), val * 2))
def test_mapped(self):
values = {"a": 1, "b": 2, "c": 3}
def f(x):
return np.array([values[x_i] for x_i in x])
m = self.mark(linewidth=Mappable(2))
scales = {"linewidth": f}
assert m._resolve({"linewidth": "c"}, "linewidth", scales) == 3
df = pd.DataFrame({"linewidth": ["a", "b", "c"]})
expected = np.array([1, 2, 3], float)
assert_array_equal(m._resolve(df, "linewidth", scales), expected)
def test_color(self):
c, a = "C1", .5
m = self.mark(color=c, alpha=a)
assert resolve_color(m, {}) == mpl.colors.to_rgba(c, a)
df = pd.DataFrame(index=pd.RangeIndex(10))
cs = [c] * len(df)
assert_array_equal(resolve_color(m, df), mpl.colors.to_rgba_array(cs, a))
def test_color_mapped_alpha(self):
c = "r"
values = {"a": .2, "b": .5, "c": .8}
m = self.mark(color=c, alpha=Mappable(1))
scales = {"alpha": lambda s: np.array([values[s_i] for s_i in s])}
assert resolve_color(m, {"alpha": "b"}, "", scales) == mpl.colors.to_rgba(c, .5)
df = pd.DataFrame({"alpha": list(values.keys())})
# Do this in two steps for mpl 3.2 compat
expected = mpl.colors.to_rgba_array([c] * len(df))
expected[:, 3] = list(values.values())
assert_array_equal(resolve_color(m, df, "", scales), expected)
def test_color_scaled_as_strings(self):
colors = ["C1", "dodgerblue", "#445566"]
m = self.mark()
scales = {"color": lambda s: colors}
actual = resolve_color(m, {"color": pd.Series(["a", "b", "c"])}, "", scales)
expected = mpl.colors.to_rgba_array(colors)
assert_array_equal(actual, expected)
def test_fillcolor(self):
c, a = "green", .8
fa = .2
m = self.mark(
color=c, alpha=a,
fillcolor=Mappable(depend="color"), fillalpha=Mappable(fa),
)
assert resolve_color(m, {}) == mpl.colors.to_rgba(c, a)
assert resolve_color(m, {}, "fill") == mpl.colors.to_rgba(c, fa)
df = pd.DataFrame(index=pd.RangeIndex(10))
cs = [c] * len(df)
assert_array_equal(resolve_color(m, df), mpl.colors.to_rgba_array(cs, a))
assert_array_equal(
resolve_color(m, df, "fill"), mpl.colors.to_rgba_array(cs, fa)
)
|