Skip to content

Visualization

Sequence logos, spatial model plots, and regression diagnostic figures.

Dependencies

All functions require matplotlib. The plot_pssm_logo function uses logomaker if available, falling back to a bar-chart representation.

pyprego.visualization

Visualization functions for motif logos and regression diagnostics.

Mirrors plot-logo.R and plot-regression.R from the R prego package. Uses matplotlib as the base; logomaker is optional for sequence logos. All matplotlib imports are lazy so pyprego works without matplotlib installed.

plot_pssm_logo(pssm: DataFrame, *, ax: Axes | None = None, title: str | None = None, method: str = 'bits') -> matplotlib.axes.Axes

Plot a sequence logo for a PSSM.

Tries to use logomaker for high-quality logos. If logomaker is not installed, falls back to a stacked bar chart rendered with plain matplotlib.

PARAMETER DESCRIPTION
pssm

PSSM DataFrame with columns pos, A, C, G, T.

TYPE: DataFrame

ax

Axes to plot on. If None, a new figure is created.

TYPE: Axes | None DEFAULT: None

title

Optional title for the plot.

TYPE: str | None DEFAULT: None

method

"bits" (default) scales letters by information content; "probability" scales by raw probability.

TYPE: str DEFAULT: 'bits'

RETURNS DESCRIPTION
Axes

The axes with the logo drawn.

plot_spat_model

plot_spat_model(spat: DataFrame, *, ax: Axes | None = None, title: str | None = 'Spatial model') -> matplotlib.axes.Axes

Plot a spatial model as a line-and-point chart.

Mirrors plot_spat_model() from R which uses geom_line + geom_point.

PARAMETER DESCRIPTION
spat

Spatial model DataFrame with columns bin and spat_factor.

TYPE: DataFrame

ax

Axes to plot on. If None, a new figure is created.

TYPE: Axes | None DEFAULT: None

title

Optional title.

TYPE: str | None DEFAULT: 'Spatial model'

RETURNS DESCRIPTION
Axes

The axes with the plot.

plot_regression_prediction

plot_regression_prediction(pred: ndarray, response: ndarray, *, ax: Axes | None = None, point_size: float = 0.5, alpha: float = 1.0, title: str | None = 'Regression prediction') -> matplotlib.axes.Axes

Scatter plot of predicted vs observed response.

Mirrors the R plot_regression_prediction which shows response on x-axis and prediction on y-axis, annotated with R-squared and r.

PARAMETER DESCRIPTION
pred

Predicted scores.

TYPE: ndarray

response

Observed response values.

TYPE: ndarray

ax

Axes to plot on.

TYPE: Axes | None DEFAULT: None

point_size

Marker size (matplotlib s parameter).

TYPE: float DEFAULT: 0.5

alpha

Marker transparency.

TYPE: float DEFAULT: 1.0

title

Optional title.

TYPE: str | None DEFAULT: 'Regression prediction'

RETURNS DESCRIPTION
Axes

plot_regression_prediction_binary

plot_regression_prediction_binary(pred: ndarray, response: ndarray, *, ax: Axes | None = None, title: str | None = 'Regression prediction') -> matplotlib.axes.Axes

Plot 1-ECDF of predictions stratified by binary response class.

Mirrors the R plot_regression_prediction_binary which shows the inverted empirical CDF (1 - ECDF) for class 0 (blue) and class 1 (red), plus a KS D statistic annotation.

PARAMETER DESCRIPTION
pred

Predicted scores.

TYPE: ndarray

response

Binary response (0/1).

TYPE: ndarray

ax

Axes to plot on.

TYPE: Axes | None DEFAULT: None

title

Optional title.

TYPE: str | None DEFAULT: 'Regression prediction'

RETURNS DESCRIPTION
Axes

plot_regression_qc

plot_regression_qc(result: RegressionResult, response: ndarray | None = None, *, title: str | None = None, point_size: float = 0.5, alpha: float = 0.5) -> matplotlib.figure.Figure

Multi-panel QC figure for a single-motif regression result.

Creates a figure with three panels:

  1. PSSM sequence logo
  2. Spatial model
  3. Prediction vs response scatter (continuous) or 1-ECDF (binary)

Mirrors plot_regression_qc from R.

PARAMETER DESCRIPTION
result

Output of :func:pyprego.regress_pwm.

TYPE: RegressionResult

response

Response variable. Required -- used for the prediction panel.

TYPE: ndarray | None DEFAULT: None

title

Overall figure title. Defaults to showing the consensus.

TYPE: str | None DEFAULT: None

point_size

Point size for scatter plot.

TYPE: float DEFAULT: 0.5

alpha

Transparency for scatter plot.

TYPE: float DEFAULT: 0.5

RETURNS DESCRIPTION
Figure

The figure containing all panels.

RAISES DESCRIPTION
ValueError

If response is None.

plot_regression_qc_multi

plot_regression_qc_multi(result, response: ndarray | None = None, *, title: str | None = None, point_size: float = 0.01, alpha: float = 0.5) -> matplotlib.figure.Figure

Multi-panel QC figure for a multi-motif regression result.

For each motif, shows: PSSM logo, spatial model, and prediction panel. Also shows a score summary panel at the bottom.

Mirrors plot_regression_qc_multi from R.

PARAMETER DESCRIPTION
result

Output of :func:pyprego.regress_multiple_motifs.

TYPE: MultiRegressionResult

response

Response variable. Required -- used for prediction panels.

TYPE: ndarray | None DEFAULT: None

title

Overall figure title.

TYPE: str | None DEFAULT: None

point_size

Point size for scatter plots.

TYPE: float DEFAULT: 0.01

alpha

Transparency for scatter plots.

TYPE: float DEFAULT: 0.5

RETURNS DESCRIPTION
Figure

The figure containing all panels.

RAISES DESCRIPTION
ValueError

If response is None or result has no models attribute.