def test_add_catplot(): pytest.importorskip('seaborn') X = generate_counts(n_samples=100) upset = UpSet(X) # smoke test upset.add_catplot('violin') fig = matplotlib.figure.Figure() upset.plot(fig) # can't provide value with Series with pytest.raises(ValueError): upset.add_catplot('violin', value='foo') # check the above add_catplot did not break the state upset.plot(fig) X = generate_counts(n_samples=100) X.name = 'foo' X = X.to_frame() upset = UpSet(X, subset_size='count') # must provide value with DataFrame with pytest.raises(ValueError): upset.add_catplot('violin') upset.add_catplot('violin', value='foo') with pytest.raises(ValueError): # not a known column upset.add_catplot('violin', value='bar') upset.plot(fig) # invalid plot kind raises error when plotting upset.add_catplot('foobar', value='foo') with pytest.raises(AttributeError): upset.plot(fig)
def test_plot_smoke_test(kw): fig = matplotlib.figure.Figure() X = generate_counts(n_samples=100) plot(X, fig, **kw) fig.savefig(io.BytesIO(), format='png') # Also check fig is optional n_nums = len(plt.get_fignums()) plot(X, **kw) assert len(plt.get_fignums()) - n_nums == 1 assert plt.gcf().axes
def test_vertical(): X = generate_counts(n_samples=100) fig = matplotlib.figure.Figure() UpSet(X, orientation='horizontal').make_grid(fig) horz_height = fig.get_figheight() horz_width = fig.get_figwidth() assert horz_height < horz_width fig = matplotlib.figure.Figure() UpSet(X, orientation='vertical').make_grid(fig) vert_height = fig.get_figheight() vert_width = fig.get_figwidth() assert horz_width / horz_height > vert_width / vert_height # TODO: test axes positions, plot order, bar orientation pass
def test_show_counts(orientation): fig = matplotlib.figure.Figure() X = generate_counts(n_samples=100) plot(X, fig) n_artists_no_sizes = _count_descendants(fig) fig = matplotlib.figure.Figure() plot(X, fig, show_counts=True) n_artists_yes_sizes = _count_descendants(fig) assert n_artists_yes_sizes - n_artists_no_sizes > 6 fig = matplotlib.figure.Figure() plot(X, fig, show_counts='%0.2g') assert n_artists_yes_sizes == _count_descendants(fig) with pytest.raises(ValueError): fig = matplotlib.figure.Figure() plot(X, fig, show_counts='%0.2h')
def test_not_unique(sort_by, sort_categories_by): kw = { 'sort_by': sort_by, 'sort_categories_by': sort_categories_by, 'subset_size': 'sum', 'sum_over': None } Xagg = generate_counts() df1, intersections1, totals1 = _process_data(Xagg, **kw) Xunagg = generate_samples()['value'] Xunagg.loc[:] = 1 df2, intersections2, totals2 = _process_data(Xunagg, **kw) assert_series_equal(intersections1, intersections2, check_dtype=False) assert_series_equal(totals1, totals2, check_dtype=False) assert set(df1.columns) == {'_value', '_bin'} assert set(df2.columns) == {'_value', '_bin'} assert len(df2) == len(Xunagg) assert df2['_bin'].nunique() == len(intersections2)
def test_show_counts(orientation): fig = matplotlib.figure.Figure() X = generate_counts(n_samples=10000) plot(X, fig, orientation=orientation) n_artists_no_sizes = _count_descendants(fig) fig = matplotlib.figure.Figure() plot(X, fig, orientation=orientation, show_counts=True) n_artists_yes_sizes = _count_descendants(fig) assert n_artists_yes_sizes - n_artists_no_sizes > 6 assert '9547' in get_all_texts(fig) # set size assert '283' in get_all_texts(fig) # intersection size fig = matplotlib.figure.Figure() plot(X, fig, orientation=orientation, show_counts='%0.2g') assert n_artists_yes_sizes == _count_descendants(fig) assert '9.5e+03' in get_all_texts(fig) assert '2.8e+02' in get_all_texts(fig) fig = matplotlib.figure.Figure() plot(X, fig, orientation=orientation, show_percentages=True) assert n_artists_yes_sizes == _count_descendants(fig) assert '95.5%' in get_all_texts(fig) assert '2.8%' in get_all_texts(fig) fig = matplotlib.figure.Figure() plot(X, fig, orientation=orientation, show_counts=True, show_percentages=True) assert n_artists_yes_sizes == _count_descendants(fig) if orientation == 'vertical': assert '9547\n(95.5%)' in get_all_texts(fig) assert '283 (2.8%)' in get_all_texts(fig) else: assert '9547 (95.5%)' in get_all_texts(fig) assert '283\n(2.8%)' in get_all_texts(fig) with pytest.raises(ValueError): fig = matplotlib.figure.Figure() plot(X, fig, orientation=orientation, show_counts='%0.2h')
def test_element_size(): X = generate_counts(n_samples=100) figsizes = [] for element_size in range(10, 50, 5): fig = matplotlib.figure.Figure() UpSet(X, element_size=element_size).make_grid(fig) figsizes.append((fig.get_figwidth(), fig.get_figheight())) figwidths, figheights = zip(*figsizes) # Absolute width increases assert np.all(np.diff(figwidths) > 0) aspect = np.divide(figwidths, figheights) # Font size stays constant, so aspect ratio decreases assert np.all(np.diff(aspect) < 0) # But doesn't decrease by much assert np.all(aspect[:-1] / aspect[1:] < 1.1) fig = matplotlib.figure.Figure() figsize_before = fig.get_figwidth(), fig.get_figheight() UpSet(X, element_size=None).make_grid(fig) figsize_after = fig.get_figwidth(), fig.get_figheight() assert figsize_before == figsize_after
""" ==================== Vertical orientation ==================== This illustrates the effect of orientation='vertical'. """ from matplotlib import pyplot as plt from upsetplot import generate_counts, plot example = generate_counts() plot(example, orientation='vertical') plt.suptitle('A vertical plot') plt.show() plot(example, orientation='vertical', show_counts='%d') plt.suptitle('A vertical plot with counts shown') plt.show()
def test_param_validation(kw): X = generate_counts(n_samples=100) with pytest.raises(ValueError): UpSet(X, **kw)
from upsetplot import plot from upsetplot import UpSet from upsetplot import generate_counts, generate_samples from upsetplot.plotting import _process_data # TODO: warnings should raise errors def is_ascending(seq): # return np.all(np.diff(seq) >= 0) return sorted(seq) == list(seq) @pytest.mark.parametrize('x', [ generate_counts(), generate_counts().iloc[1:-2], ]) @pytest.mark.parametrize('sort_by', ['cardinality', 'degree']) @pytest.mark.parametrize('sort_categories_by', [None, 'cardinality']) def test_process_data_series(x, sort_by, sort_categories_by): assert x.name == 'value' for subset_size in ['auto', 'legacy', 'sum', 'count']: for sum_over in ['abc', False]: with pytest.raises(ValueError, match='sum_over is not applicable'): _process_data(x, sort_by=sort_by, sort_categories_by=sort_categories_by, subset_size=subset_size, sum_over=sum_over)
# TODO: warnings should raise errors def is_ascending(seq): # return np.all(np.diff(seq) >= 0) return sorted(seq) == list(seq) def get_all_texts(mpl_artist): out = [text.get_text() for text in mpl_artist.findobj(Text)] return [text for text in out if text] @pytest.mark.parametrize('x', [ generate_counts(), generate_counts().iloc[1:-2], ]) @pytest.mark.parametrize('sort_by', ['cardinality', 'degree']) @pytest.mark.parametrize('sort_categories_by', [None, 'cardinality']) def test_process_data_series(x, sort_by, sort_categories_by): assert x.name == 'value' for subset_size in ['auto', 'sum', 'count']: for sum_over in ['abc', False]: with pytest.raises(ValueError, match='sum_over is not applicable'): _process_data(x, sort_by=sort_by, sort_categories_by=sort_categories_by, subset_size=subset_size, sum_over=sum_over)