cvtk.viz

This module provides functions for visualizing images, annotations, and analysis results. It supports drawing bounding boxes, segmentation masks, and generating various plots.

API Reference

cvtk.viz.plot(data, x=None, y=None, output=None, title=None, mode='lines', width=600, height=800, scale=1.0, rows=None, cols=None) plotly.graph_objects.Figure[source]

Plot specified columns from a tab-separated log file.

Reads a tab-separated file and creates line plots using Plotly. Supports multiple subplots where each subplot can contain one or more y columns.

Parameters:
  • data (str) – Path to tab-separated log file.

  • x (str|None) – Column name for x-axis. If None, defaults to ‘epoch’. Default is None.

  • y (str|list) –

    Column name(s) to plot on y-axis. Can be a single column name (str) or a list of column names/nested lists for grouped subplots:

    • ’loss’: plots single column

    • [‘loss’, ‘acc’]: plots loss and acc in separate subplots

    • [[‘train_loss’, ‘valid_loss’], [‘train_acc’, ‘valid_acc’]]: plots train_loss and valid_loss in subplot 1, train_acc and valid_acc in subplot 2

  • output (str|None) – File path to save the plot. If None, displays plot interactively.

  • title (str|None) – Plot title. Default is None.

  • mode (str) – Plotly trace mode (‘lines’, ‘markers’, ‘lines+markers’, etc.). Default is ‘lines’.

  • width (int) – Plot width in pixels. Default is 600.

  • height (int) – Plot height in pixels. Default is 800.

  • scale (float) – Scale factor for saved image resolution. Default is 1.0.

  • rows (int|None) – Number of rows in subplot grid. If None, auto-calculated for near-square layout.

  • cols (int|None) – Number of columns in subplot grid. If None, auto-calculated for near-square layout.

Returns:

The plotly figure object.

Return type:

plotly.graph_objects.Figure

Raises:
  • TypeError – If y items are not str or list/tuple.

  • ValueError – If specified column names are not found in the data file.

Examples

>>> from cvtk.viz import plot
>>> plot('train.log', y=['loss', 'acc'], output='plot.png')
>>> plot('train.log', x='step', y=[['train_loss', 'valid_loss'], ['train_acc', 'valid_acc']])
cvtk.viz.plot_cm(data, output=None, title='Confusion Matrix', xlab='Predicted Label', ylab='True Label', colorscale='YlOrRd', width=600, height=600, scale=1.0) plotly.graph_objects.Figure[source]

Plot a confusion matrix from classification test outputs.

Plots a confusion matrix as a heatmap using Plotly. Also saves a text file containing the confusion matrix values if output path is provided.

The input data should be a tab-separated file with columns: - Column 1: image/sample path - Column 2: true class label - Columns 3+: predicted probabilities for each class

Example input format:

image  label   leaf     flower   root
1.JPG  leaf    0.54791  0.20376  0.24833
2.JPG  root    0.06158  0.02184  0.91658
3.JPG  leaf    0.70320  0.04808  0.24872
4.JPG  flower  0.04723  0.90061  0.05216
Parameters:
  • data (str) – Path to tab-separated file containing test outputs.

  • output (str|None) – File path to save the heatmap image. Also saves a .txt file with the confusion matrix values. If None, displays plot interactively.

  • title (str) – Plot title. Default is ‘Confusion Matrix’.

  • xlab (str) – X-axis label. Default is ‘Predicted Label’.

  • ylab (str) – Y-axis label. Default is ‘True Label’.

  • colorscale (str) – Plotly colorscale name (e.g., ‘YlOrRd’, ‘Blues’, ‘Viridis’). Default is ‘YlOrRd’.

  • width (int) – Image width in pixels. Default is 600.

  • height (int) – Image height in pixels. Default is 600.

  • scale (float) – Scale factor for saved image resolution. Default is 1.0.

Returns:

The plotly figure object.

Return type:

plotly.graph_objects.Figure

Examples

>>> from cvtk.viz import plot_cm
>>> plot_cm('test_results.txt', output='confusion_matrix.png')