Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions src/render/series.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Draw a small table as an openms-insight ``SeriesPlot`` (lines, bars or boxes).

An openms-insight without bars, boxes or views (0.7.5 and older) gets the same
plot drawn with Plotly, so the pages work on either.

The plots of a comparison's quality control, of its comparison steps and of
the GO enrichment are computed in the session from tables already in the
cache, so they are cached here by their content and settings: a rerun reuses
Expand Down Expand Up @@ -37,15 +40,104 @@ def _fingerprint(frame: pl.DataFrame, settings: dict) -> str:
return digest.hexdigest()[:16]


def supports(plot_class, plot_kwargs: dict) -> bool:
"""Whether the installed ``SeriesPlot`` takes every argument and the mode.

Bars, boxes, horizontal orientation and views came after openms-insight
0.7.5; an install without them draws the plot with Plotly instead.
"""
import inspect

params = inspect.signature(plot_class.__init__).parameters
if plot_kwargs.get('mode', 'lines+markers') not in LINE_MODES and 'orientation' not in params:
return False
return all(name in params for name in plot_kwargs)


LINE_MODES = ('lines', 'markers', 'lines+markers')


def show_series(data, name: str, cache_path, key: str, height=None, state_manager=None, **plot_kwargs):
"""Draw ``data`` (polars or pandas) as a ``SeriesPlot``; ``plot_kwargs`` go to it."""
from openms_insight import SeriesPlot

frame = pl.from_pandas(data) if isinstance(data, pd.DataFrame) else data
if not supports(SeriesPlot, plot_kwargs):
return _show_plotly(frame, key, height, state_manager, **plot_kwargs)
cache_id = f'{name}-{_fingerprint(frame, plot_kwargs)}'
plot = SeriesPlot(
cache_id=cache_id, cache_path=str(Path(cache_path, 'insight', 'series')), data=frame.lazy(),
**plot_kwargs,
)
plot(key=key, state_manager=state_manager, height=height or 400)
return plot


def figure(frame: pl.DataFrame, x_column='x', y_column='y', series_column=None, series_names=None,
series_colors=None, mode='lines+markers', orientation='v', hover_column=None, title=None,
x_label=None, y_label=None, show_legend=True, y_range_mode='nonnegative', **_):
"""The Plotly figure of a ``SeriesPlot`` table (Streamlit-free)."""
import plotly.graph_objects as go

horizontal = orientation == 'h'
fig = go.Figure()
groups = frame.partition_by(series_column, maintain_order=True, as_dict=False) if series_column else [frame]
for part in groups:
label = str(part[series_column][0]) if series_column else None
shown = (series_names or {}).get(label, label)
color = (series_colors or {}).get(label)
xs, ys = part[x_column].to_list(), part[y_column].to_list()
axes = dict(x=ys, y=xs) if horizontal else dict(x=xs, y=ys)
common = dict(name=shown, showlegend=bool(show_legend and series_column))
if hover_column and hover_column in part.columns:
common.update(hovertext=part[hover_column].to_list())
if mode == 'bars':
fig.add_trace(go.Bar(**axes, **common, orientation=orientation, marker_color=color))
elif mode == 'box':
fig.add_trace(go.Box(**axes, **common, orientation=orientation, marker_color=color))
else:
fig.add_trace(go.Scatter(**axes, **common, mode=mode, line_color=color))
names = (y_label or y_column, x_label or x_column) if horizontal else (x_label or x_column, y_label or y_column)
fig.update_layout(title=title, barmode='group', boxmode='group', showlegend=bool(show_legend and series_column),
xaxis_title=names[0], yaxis_title=names[1], margin=dict(t=50 if title else 20))
value_axis = fig.update_xaxes if horizontal else fig.update_yaxes
value_axis(rangemode=y_range_mode)
if horizontal:
fig.update_yaxes(autorange='reversed', type='category')
return fig


def _show_plotly(frame, key, height, state_manager, view_column=None, view_names=None, view_identifier=None,
view_default=None, top_choices=None, top_default=None, bar_height=None, title=None, **plot_kwargs):
"""Draw a ``SeriesPlot`` table with Plotly, for an openms-insight without
the plot's mode or views; a view and the number of rows are picked above it."""
import streamlit as st

from src.common.common import follow_app_theme

if view_column and view_column in frame.columns:
values = list(dict.fromkeys(frame[view_column].to_list()))
order = [v for v in (view_names or {}) if v in values] + [v for v in values if v not in (view_names or {})]
current = state_manager.get_selection(view_identifier) if state_manager and view_identifier else None
start = current if current in order else (view_default if view_default in order else order[0])
left, right = st.columns([3, 1]) if top_choices else (st.container(), None)
view = left.segmented_control('View', order, default=start, key=f'{key}_view', label_visibility='collapsed',
format_func=lambda v: (view_names or {}).get(v, v)) or start
if state_manager and view_identifier and view != current:
state_manager.set_selection(view_identifier, view)
frame = frame.filter(pl.col(view_column) == view)
title = (title or '').replace('{view}', (view_names or {}).get(view, str(view))) or None
else:
right = st.container() if top_choices else None
if top_choices:
default = top_default if top_default in top_choices else top_choices[len(top_choices) // 2]
top = right.selectbox('Top', top_choices, index=top_choices.index(default), key=f'{key}_top',
format_func=lambda n: f'Top {n}', label_visibility='collapsed')
keep = list(dict.fromkeys(frame[plot_kwargs.get('x_column', 'x')].to_list()))[:top]
frame = frame.filter(pl.col(plot_kwargs.get('x_column', 'x')).is_in(keep))
fig = figure(frame, title=title, **plot_kwargs)
rows = frame[plot_kwargs.get('x_column', 'x')].n_unique() if frame.height else 0
height = max(height or 400, (bar_height or 0) * rows + 120) if bar_height else (height or 400)
fig.update_layout(height=height)
st.plotly_chart(follow_app_theme(fig), key=f'{key}_plotly', use_container_width=True)
return fig
31 changes: 31 additions & 0 deletions tests/test_series_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,34 @@ def test_row_height_lines_up_the_views_of_a_row():
assert quant_cells.row_height(['quant_pvalues']) is None
# The normalization view grows with the samples, and its partner with it
assert quant_cells.row_height(['quant_totals', 'quant_normalization'], samples=20) == 44 * 20 + 140


def test_supports_falls_back_for_a_series_plot_without_bars():
from src.render.series import supports

class Old:
def __init__(self, cache_id, data=None, x_column='x', y_column='y', mode='lines+markers', title=None):
pass

class New(Old):
def __init__(self, cache_id, data=None, x_column='x', y_column='y', mode='lines+markers', title=None,
orientation='v', view_column=None, **kwargs):
pass

assert supports(Old, {'mode': 'lines', 'title': 't'})
assert not supports(Old, {'mode': 'bars'})
assert not supports(Old, {'mode': 'lines', 'view_column': 'c'})
assert supports(New, {'mode': 'bars', 'orientation': 'h', 'view_column': 'c'})


def test_figure_draws_grouped_and_horizontal_bars():
from src.render.series import figure

frame = pl.DataFrame({'x': ['a', 'b', 'a', 'b'], 'y': [1, 2, 3, 4], 's': ['p', 'p', 'q', 'q']})
fig = figure(frame, series_column='s', series_colors={'p': '#ff0000'}, mode='bars')
assert [t.type for t in fig.data] == ['bar', 'bar'] and fig.layout.barmode == 'group'
assert fig.data[0].marker.color == '#ff0000'
fig = figure(frame.filter(pl.col('s') == 'p'), mode='bars', orientation='h', x_label='GO term')
assert fig.data[0].orientation == 'h' and list(fig.data[0].y) == ['a', 'b']
assert fig.layout.yaxis.title.text == 'GO term'
assert figure(frame, series_column='s', mode='box').data[0].type == 'box'
Loading