pyclickimage.qt_linear_interpolator#

class pyclickimage.qt_linear_interpolator.QtLinearInterpolator(xmin: float, xmax: float, ymin: float, ymax: float, parent: QWidget | None = None)[source]#

Bases: QWidget

Interactive piecewise-linear interpolation widget.

The widget displays a piecewise-linear curve defined by nodes whose coordinates are constrained to the [xmin, xmax] × [ymin, ymax] domain.

The widget provides:

  • interactive node creation, translation and removal;

  • major and minor grid lines;

  • X and Y axes;

  • configurable tick labels;

  • configurable X and Y axis titles;

  • configurable value formatters for both axes;

  • configurable colors, fonts and line widths;

  • configurable margins around the plotting area.

End nodes are always present and cannot be removed. Interior nodes can be created with a left-button double click, moved by dragging and removed with a right-button click.

Parameters:
  • xmin (float) – Minimum x-coordinate of the interpolation domain.

  • xmax (float) – Maximum x-coordinate of the interpolation domain.

  • ymin (float) – Minimum y-coordinate of the interpolation range.

  • ymax (float) – Maximum y-coordinate of the interpolation range.

  • parent (QWidget, optional) – Parent Qt widget.

Notes

Nodes are indexed by their position in the internal node sequence, starting at 0. The first and last nodes are always the mandatory endpoint nodes and therefore cannot be removed.

Interior nodes may be added, removed or moved. Node indices may change after an insertion or removal, so indices should only be considered stable until the next modification of the node configuration.

The first and last nodes always have fixed X-coordinates equal to xmin and xmax respectively. To fix X-coordinate (respectively Y-coordinate), disable allow_drag_x. The Y-coordinates remain constrained to [ymin, ymax].

Node-related signals report the index at the time of the corresponding operation.

Signals#

nodes_changedpyqtSignal()

Emitted when the node configuration changes.

The signal is emitted immediately after a node is created or removed, and when a node translation is finished.

node_addedpyqtSignal(int, float, float)

Emitted when an interior node is added.

Parameters are the node index, x-coordinate and y-coordinate.

node_removedpyqtSignal(int, float, float)

Emitted when an interior node is removed.

Parameters are the node index, x-coordinate and y-coordinate of the removed node.

node_movedpyqtSignal(int, float, float, float, float)

Emitted when a node has finished being moved.

Parameters are the node index, old x-coordinate, old y-coordinate, new x-coordinate and new y-coordinate.

static _bool_property(name: str)[source]#

Create a boolean Qt property.

static _color_property(name: str)[source]#

Create a QColor property backed by a private attribute.

_draw_axes(painter: QPainter) → None[source]#

Draw axes, ticks, tick labels and axis titles.

Parameters:

painter (QPainter) – Painter used to render the axes.

_draw_grid(painter: QPainter) → None[source]#

Draw major and minor grid lines.

Parameters:

painter (QPainter) – Painter used to render the grid.

_draw_mouse_coordinates(painter: QPainter) → None[source]#

Draw the current mouse coordinates inside the plot.

_get_ticks(axis: str) → ndarray[source]#

Return tick positions for one axis.

Parameters:

axis ({"x", "y"}) – Axis for which tick positions are requested.

Returns:

Tick positions in data coordinates. Explicitly configured ticks are returned when available; otherwise, evenly spaced ticks are generated from the corresponding interpolation bounds.

Return type:

numpy.ndarray

_insert_node(position: QPointF) → None[source]#

Insert an interpolation node at a widget position.

The x-coordinate is determined by the mouse position and the y-coordinate is clipped to the interpolation range.

Parameters:

position (QPointF) – Mouse position in widget coordinates.

static _int_property(name: str)[source]#

Create a non-negative integer Qt property.

_move_node(index: int, position: QPointF) → None[source]#

Move an interpolation node vertically.

The x-coordinate remains fixed while the y-coordinate is obtained from the mouse position and clipped to [ymin, ymax].

Parameters:
  • index (int) – Index of the node being moved.

  • position (QPointF) – Current mouse position in widget coordinates.

_node_at(position: QPointF) → int | None[source]#

Find the node closest to a widget position.

Parameters:

position (QPointF) – Mouse position in widget coordinates.

Returns:

Index of the first node inside the configured node tolerance, or None if no node is close enough.

Return type:

int or None

_plot_rect() → tuple[float, float, float, float][source]#

Return the plotting rectangle in widget coordinates.

Returns:

Tuple (left, top, width, height) defining the rectangle reserved for the interpolation curve.

Return type:

tuple of float

_remove_node(index: int) → None[source]#

Remove an interior interpolation node.

Parameters:

index (int) – Index of the node to remove.

Notes

The first and last nodes are mandatory and therefore cannot be removed.

_to_data(point: QPointF) → tuple[float, float][source]#

Convert widget coordinates to data coordinates.

Parameters:

point (QPointF) – Position in widget coordinates.

Returns:

Corresponding (x, y) data coordinates.

Return type:

tuple of float

_to_widget(x: float, y: float) → QPointF[source]#

Convert data coordinates to widget coordinates.

Parameters:
  • x (float) – Data-space x-coordinate.

  • y (float) – Data-space y-coordinate.

Returns:

Corresponding position in widget coordinates.

Return type:

QPointF

_validate_bounds() → None[source]#

Validate the interpolation domain limits.

Raises:

ValueError – If the minimum bound is greater than or equal to the maximum bound on either axis.

property allow_drag_x#
property allow_drag_y#
property axis_color#
property axis_width#
property background_color#
property border_color#
property border_width#
property coordinate_background_color#
property curve_color#
property curve_width#
property font: QFont#

Font used for axis tick labels and titles.

Returns:

Copy of the font currently used for axis text.

Return type:

QFont

property grid_color#
property grid_width#
property grid_x: int#

Number of major grid divisions along the X axis.

Returns:

Number of equal divisions between xmin and xmax. This value also determines the number of major X-axis intervals and X-axis tick intervals.

Return type:

int

property grid_y: int#

Number of major grid divisions along the Y axis.

Returns:

Number of equal divisions between ymin and ymax. This value also determines the number of major Y-axis intervals and Y-axis tick intervals.

Return type:

int

interpolate(values: ndarray) → ndarray[source]#

Apply the piecewise-linear interpolation to an ND-array.

Parameters:

values (numpy.ndarray) – Floating-point NumPy array containing x-coordinates. The input array may have any number of dimensions.

Returns:

Floating-point array with the same shape as values. Values inside [xmin, xmax] are linearly interpolated. Values outside the interpolation domain are replaced by NaN.

Return type:

numpy.ndarray

Raises:

TypeError – If values is not a floating-point NumPy array.

leaveEvent(event) → None[source]#

Hide mouse coordinates when leaving the widget.

property margin_bottom: int#

Bottom margin around the plotting area.

Returns:

Margin in pixels between the bottom edge of the plotting area and the bottom edge of the widget. This margin also provides space for X-axis labels and the X-axis title.

Return type:

int

property margin_left: int#

Left margin around the plotting area.

Returns:

Margin in pixels between the left edge of the widget and the plotting area. This margin also provides space for Y-axis labels and the Y-axis title.

Return type:

int

property margin_right: int#

Right margin around the plotting area.

Returns:

Margin in pixels between the right edge of the plotting area and the right edge of the widget.

Return type:

int

property margin_top: int#

Top margin around the plotting area.

Returns:

Margin in pixels between the top edge of the widget and the plotting area.

Return type:

int

property minor_grid: int#

Number of minor subdivisions between major grid lines.

Returns:

Number of minor intervals inserted into each major interval. A value of 0 disables minor grid lines.

Return type:

int

property minor_grid_color#
property minor_grid_width#
mouseDoubleClickEvent(event) → None[source]#

Create an interior node on a left-button double click.

A node is inserted only when the double-click does not occur close enough to an existing node.

Parameters:

event (QMouseEvent) – Qt mouse event.

mouseMoveEvent(event) → None[source]#

Update mouse coordinates and handle node dragging.

mousePressEvent(event) → None[source]#

Start node dragging or remove a node.

Parameters:

event (QMouseEvent) – Qt mouse event.

Notes

A left-button press starts dragging the closest node.

A right-button press removes the closest interior node.

mouseReleaseEvent(event) → None[source]#

Finish an active node drag.

Parameters:

event (QMouseEvent) – Qt mouse event.

Notes

The nodes_changed signal is emitted when the left-button drag is finished.

node_added#

str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Type:

pyqtSignal(*types, name

property node_color#
node_moved#

str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Type:

pyqtSignal(*types, name

property node_radius#
node_removed#

str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Type:

pyqtSignal(*types, name

property node_tolerance#
property nodes: tuple[tuple[float, float], ...]#

Current interpolation nodes.

Returns:

Nodes represented as (x, y) pairs and sorted by increasing x-coordinate. The first and last nodes are always located at xmin and xmax respectively.

Return type:

tuple of tuple of float

nodes_changed#

str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Type:

pyqtSignal(*types, name

paintEvent(event) → None[source]#

Paint the interpolation curve and graphical elements.

Parameters:

event (QPaintEvent) – Qt paint event.

reset() → None[source]#

Remove all interior nodes.

The two mandatory end nodes are restored to (xmin, ymin) and (xmax, ymax).

The nodes_changed signal is emitted after the reset.

set_domain(xmin: float, xmax: float, ymin: float | None = None, ymax: float | None = None, keep_nodes: bool = False) → None[source]#

Replace the interpolation domain in a single operation.

The individual xmin, xmax, ymin and ymax setters validate each bound against the current other bound and nodes, so their call order matters. This method validates the new bounds together and has no ordering constraint.

Parameters:
  • xmin (float) – New x-domain. Must satisfy xmin < xmax.

  • xmax (float) – New x-domain. Must satisfy xmin < xmax.

  • ymin (float, optional) – New y-range. None keeps the current value. The final range must satisfy ymin < ymax.

  • ymax (float, optional) – New y-range. None keeps the current value. The final range must satisfy ymin < ymax.

  • keep_nodes (bool, default=False) –

    If False, the nodes are reset to (xmin, ymin) and (xmax, ymax).

    If True, interior nodes strictly inside the new x-domain are kept (others are dropped), end nodes are moved to the new x-bounds and every y-coordinate is clipped to the new y-range.

Raises:

ValueError – If the bounds are not finite or not strictly increasing.

Notes

The view (view_*) is reset to the new domain. nodes_changed is emitted once.

set_nodes(nodes: Iterable[tuple[float, float]]) → None[source]#

Replace the current interpolation nodes.

Parameters:

nodes (iterable of tuple of float) – Iterable containing (x, y) node coordinates. The first node must use xmin and the last node must use xmax.

Raises:

ValueError – If fewer than two nodes are supplied, if the first or last node does not correspond to the domain limits, if x-values are not strictly increasing, or if any coordinate is outside the interpolation domain.

set_view(xmin: float | None = None, xmax: float | None = None, ymin: float | None = None, ymax: float | None = None) → None[source]#

Set the visible range in a single operation.

Same purpose as set_domain() for the view_* properties: bounds are validated together, so they can be given in any order.

Parameters:
  • xmin (float, optional) – New visible x-range. None keeps the current value.

  • xmax (float, optional) – New visible x-range. None keeps the current value.

  • ymin (float, optional) – New visible y-range. None keeps the current value.

  • ymax (float, optional) – New visible y-range. None keeps the current value.

Raises:

ValueError – If the resulting bounds are not finite or not strictly increasing.

Notes

Only the display is affected; the interpolation is unchanged.

set_x_ticks(ticks: Iterable[float] | None) → None[source]#

Set explicit x-axis tick positions.

Parameters:

ticks (iterable of float or None) – Tick values in data coordinates. Values must lie within [xmin, xmax]. If None, x-axis ticks are generated automatically.

set_y_ticks(ticks: Iterable[float] | None) → None[source]#

Set explicit y-axis tick positions.

Parameters:

ticks (iterable of float or None) – Tick values in data coordinates. Values must lie within [ymin, ymax]. If None, y-axis ticks are generated automatically.

property show_axes#
property show_grid#
property show_labels#
property show_minor_grid#
property show_ticks#
property text_color#
property tick_length#
property tick_width#
property view_xmax: float#

Maximum x-coordinate currently visible in the plot.

Returns:

Maximum x-coordinate of the current view.

Return type:

float

property view_xmin: float#

Minimum x-coordinate currently visible in the plot.

Returns:

Minimum x-coordinate of the current view.

Return type:

float

property view_ymax: float#

Maximum y-coordinate currently visible in the plot.

Returns:

Maximum y-coordinate of the current view.

Return type:

float

property view_ymin: float#

Minimum y-coordinate currently visible in the plot.

Returns:

Minimum y-coordinate of the current view.

Return type:

float

property x_formatter: Callable[[float], str]#

Formatter used to convert X tick values to text.

The callable receives one floating-point x-coordinate and must return the text that should be displayed on the X axis.

Returns:

Current X-axis formatter.

Return type:

Callable[[float], str]

Examples

>>> widget.x_formatter = lambda x: f"{round(x / 1000)}"
property x_ticks: tuple[float, ...] | None#

X-axis tick positions.

Returns:

Explicit x-axis tick values. If None, ticks are generated automatically from xmin, xmax and grid_x.

Return type:

tuple of float or None

property x_title: str#

Title displayed below the X axis.

Returns:

X-axis title. An empty string disables the title.

Return type:

str

property xmax: float#

Maximum x-coordinate of the interpolation domain.

Returns:

Maximum x-coordinate. The last interpolation node always has this x-coordinate.

Return type:

float

property xmin: float#

Minimum x-coordinate of the interpolation domain.

Returns:

Minimum x-coordinate. The first interpolation node always has this x-coordinate.

Return type:

float

property y_formatter: Callable[[float], str]#

Formatter used to convert Y tick values to text.

The callable receives one floating-point y-coordinate and must return the text that should be displayed on the Y axis.

Returns:

Current Y-axis formatter.

Return type:

Callable[[float], str]

Examples

>>> widget.y_formatter = lambda y: f"{y:.1f}"
property y_ticks: tuple[float, ...] | None#

Y-axis tick positions.

Returns:

Explicit y-axis tick values. If None, ticks are generated automatically from ymin, ymax and grid_y.

Return type:

tuple of float or None

property y_title: str#

Title displayed beside the Y axis.

Returns:

Y-axis title. An empty string disables the title.

Return type:

str

property ymax: float#

Maximum y-coordinate allowed for interpolation nodes.

Returns:

Maximum allowed y-coordinate.

Return type:

float

property ymin: float#

Minimum y-coordinate allowed for interpolation nodes.

Returns:

Minimum allowed y-coordinate.

Return type:

float

pyclickimage.qt_linear_interpolator.interpolate(values: ndarray, nodes) → ndarray[source]#

Apply piecewise-linear interpolation to an ND-array.

Parameters:
  • values (numpy.ndarray) – Floating-point NumPy array containing x-coordinates.

  • nodes (sequence) – Interpolation nodes as (x, y) pairs.

Returns:

Floating-point array with the same shape as values. Values inside the interpolation domain are linearly interpolated. Values outside the interpolation domain are replaced by NaN.

Return type:

numpy.ndarray