Skip to content

Commit

Permalink
added test for single boxplot
Browse files Browse the repository at this point in the history
  • Loading branch information
CDonnerer committed Dec 26, 2020
1 parent 26bbd86 commit 61a69eb
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 13 deletions.
7 changes: 5 additions & 2 deletions src/shellplot/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def barh(*args, **kwargs):
Parameters
----------
x : array-like
The witdth of the horizontal bars. Should be 1d np.ndarray or pandas
The width of the horizontal bars. Should be 1d np.ndarray or pandas
series.
labels : array-like
Array that is used to label the bars. Needs to have the same dim as x.
Expand Down Expand Up @@ -286,6 +286,8 @@ def _barh(x, labels=None, **kwargs):
def _boxplot(x, labels=None, **kwargs):
"""Box plot"""

if labels is None:
labels = get_label(x)
x_axis, y_axis, canvas = _init_figure(**kwargs)

x = numpy_2d(x)
Expand All @@ -301,8 +303,9 @@ def _boxplot(x, labels=None, **kwargs):
np.array([0.2, 0.50, 0.8]) + np.arange(0, len(x), 1)[np.newaxis].T
)
y_axis.ticks = np.arange(0.5, len(x), 1)

if labels is not None:
y_axis.labels = labels
y_axis.labels = numpy_1d(labels)

for ii in range(len(x)):
quants = quantiles_scaled[ii, :]
Expand Down
10 changes: 10 additions & 0 deletions src/shellplot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ def _(x: pd.DataFrame):
return x.to_numpy().transpose()


@numpy_2d.register
def _(x: pd.Series):
return x.to_numpy()[np.newaxis]


@numpy_2d.register
def _(x: list):
return [numpy_1d(x) for x in x]
Expand Down Expand Up @@ -119,6 +124,11 @@ def _(x: list):
return np.array(x)


@numpy_1d.register
def _(x: str): # TODO: this should be any non-iterable
return np.array([x])


@singledispatch
def get_label(x):
"""Try to get names out of array-like inputs"""
Expand Down
53 changes: 42 additions & 11 deletions tests/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@

from shellplot.plots import _add_hbar, _add_vbar, barh, boxplot, hist, plot

# -----------------------------------------------------------------------------
# 'Integration' style 'tests' that only check everything runs end to end
# -----------------------------------------------------------------------------


def test_boxplot():
x = np.random.randn(1000)
plt_str = boxplot(x, return_type="str")
assert isinstance(plt_str, str)


# -----------------------------------------------------------------------------
# Test `plot` function
# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -238,6 +227,48 @@ def test_barh(x, labels, expected_barh):
assert plt_str == expected_barh


# -----------------------------------------------------------------------------
# Test `boxplot` function
# -----------------------------------------------------------------------------


@pytest.fixture
def expected_boxplot():
return "\n".join(
[
"",
" | ",
" | ",
" | -------------- ",
" || | | | |",
" box┤|-------| | |---------------|",
" || | | | |",
" | -------------- ",
" | ",
" | ",
" └┬-------┬-------┬------┬-------┬-------┬",
" 0 1 2 3 4 5",
"",
]
)


@pytest.mark.parametrize(
"x, labels",
[
(np.array([0, 1, 1, 1, 2, 2, 3, 3, 3, 5]), ["box"]),
(
pd.Series(data=np.array([0, 1, 1, 1, 2, 2, 3, 3, 3, 5]), name="box"),
None,
),
],
)
def test_boxplot(x, labels, expected_boxplot):
plt_str = boxplot(x, labels=labels, figsize=(40, 9), return_type="str")

assert plt_str == expected_boxplot


# -----------------------------------------------------------------------------
# Test canvas elements
# -----------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def test_load_dataset(name):
(np.array([[0, 1]]), np.array([[0, 1]])),
([np.array([0, 1])], [np.array([0, 1])]),
(pd.DataFrame(np.array([[0], [1]])), np.array([[0, 1]])),
(pd.Series([0, 1]), np.array([[0, 1]])),
],
)
def test_numpy_2d(x, expected_np_2d):
Expand All @@ -107,6 +108,7 @@ def test_numpy_2d(x, expected_np_2d):
(pd.Index([0, 1]), np.array([0, 1])),
(pd.DataFrame(np.array([0, 1])), np.array([0, 1])),
([0, 1], np.array([0, 1])),
("box", np.array(["box"])),
],
)
def test_numpy_1d(x, expected_np_1d):
Expand Down

0 comments on commit 61a69eb

Please sign in to comment.