Пример #1
0
 def gen_upset_plot(self, className=None):
     # total_peps = len([pep for s in self.results.samples for pep in s.peptides])
     total_peps = np.sum([len(s.peptides) for s in self.results.samples])
     data = from_contents({s.sample_name: set(s.peptides)
                           for s in self.results.samples})
     for intersection in data.index.unique():
         if len(data.loc[intersection, :])/total_peps < 0.005:
             data.drop(index=intersection, inplace=True)
     data['peptide_length'] = np.vectorize(len)(data['id'])
     n_sets = len(data.index.unique())
     if n_sets <= 100:  # Plot horizontal
         upset = UpSet(data,
                       sort_by='cardinality',
                       #sort_categories_by=None,
                       show_counts=True,)
                       #totals_plot_elements=4,
                       #intersection_plot_elements=10)
         upset.add_catplot(value='peptide_length', kind='boxen', color='gray')
         plot = upset.plot()
         plot['totals'].grid(False)
         ylim = plot['intersections'].get_ylim()[1]
         plot['intersections'].set_ylim((0, ylim * 1.1))
         for c in plot['intersections'].get_children():
             if isinstance(c, plotText):
                 text = c.get_text()
                 text = text.replace('\n', ' ')
                 c.set_text(text)
                 c.set_rotation('vertical')
                 pos = c.get_position()
                 pos = (pos[0], pos[1] + 0.02 * ylim)
                 c.set_position(pos)
     else:  # plot vertical
         upset = UpSet(data, subset_size='count',
                       orientation='vertical',
                       sort_by='cardinality',
                       sort_categories_by=None,
                       show_counts=True)
         upset.add_catplot(value='peptide_length', kind='boxen', color='gray')
         plot = upset.plot()
         lim = plot['intersections'].get_xlim()
         plot['intersections'].set_xlim([0, lim[1] * 1.6])
         plot['totals'].grid(False)
         ylim = plot['totals'].get_ylim()[1]
         for c in plot['totals'].get_children():
             if isinstance(c, plotText):
                 text = c.get_text()
                 text = text.replace('\n', ' ')
                 c.set_text(text)
                 c.set_rotation('vertical')
                 pos = c.get_position()
                 pos = (pos[0], pos[1] + 0.1 * ylim)
                 c.set_position(pos)
         plt.draw()
     upset_fig = f'{self.fig_dir / "upsetplot.svg"}'
     plt.savefig(upset_fig, bbox_inches="tight")
     encoded_upset_fig = base64.b64encode(open(upset_fig, 'rb').read()).decode()
     card = div(className='card', style="height: 100%")
     card.add(div([b('UpSet Plot'), p('Only intersections > 0.5% are displayed')], className='card-header'))
     plot_body = div(img(src=f'data:image/svg+xml;base64,{encoded_upset_fig}',
                         className='img-fluid',
                         style=f'width: 100%; height: auto'),
                     className='card-body')
     card.add(plot_body)
     return div(card, className=className)
Пример #2
0
def test_add_catplot():
    pytest.importorskip('seaborn')
    X = generate_data(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_data(n_samples=100)
    X.name = 'foo'
    X = X.to_frame()
    upset = UpSet(X, sum_over=False)
    # 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)
Пример #3
0
# Get a binary indicator of whether each top feature is above average
boston_above_avg = boston_df > boston_df.median(axis=0)
boston_above_avg = boston_above_avg[top_features]
boston_above_avg = boston_above_avg.rename(columns=lambda x: x + '>')

# Make this indicator mask an index of boston_df
boston_df = pd.concat([boston_df, boston_above_avg], axis=1)
boston_df = boston_df.set_index(list(boston_above_avg.columns))

# Also give us access to the target (median house value)
boston_df = boston_df.assign(median_value=boston.target)

# UpSet plot it!
upset = UpSet(boston_df, subset_size='count', intersection_plot_elements=3)
upset.add_catplot(value='median_value', kind='strip', color='blue')
upset.add_catplot(value='AGE', kind='strip', color='black')
upset.plot()
plt.title("UpSet with catplots, for orientation='horizontal'")
plt.show()

# And again in vertical orientation

upset = UpSet(boston_df,
              subset_size='count',
              intersection_plot_elements=3,
              orientation='vertical')
upset.add_catplot(value='median_value', kind='strip', color='blue')
upset.add_catplot(value='AGE', kind='strip', color='black')
upset.plot()
plt.title("UpSet with catplots, for orientation='vertical'")