visiannot.tools.ToolsPyqtgraph

Module with sub-classes and functions for scientific graphics with pyqtgraph

See https://pyqtgraph.readthedocs.io/en/latest

Summary

Classes

visiannot.tools.ToolsPyqtgraph.PlotItemCustom([…])

Create a new PlotItem.

visiannot.tools.ToolsPyqtgraph.ProgressWidget(nframes)

Subclass of pyqtgraph.PlotWidget that defines the widget used as a progression bar for video/signal navigation in ViSiAnnoT window

visiannot.tools.ToolsPyqtgraph.SignalWidget([…])

Subclass of pyqtgraph.PlotWidget so that he effect of “auto-range” button is applied only on Y axis

Functions

visiannot.tools.ToolsPyqtgraph.addLegendTo2DWidget(…)

Adds a legend to a 2D widget

visiannot.tools.ToolsPyqtgraph.addMeanStdPlotTo2DWidget(…)

Adds a plot item to a 2D widget, a vertical line is associated to each point of the plot item, particularly useful to display a mean/median curve with the standard deviation

visiannot.tools.ToolsPyqtgraph.addPlotTo2DWidget(…)

Adds a plot item to a 2D widget

visiannot.tools.ToolsPyqtgraph.addTextItemTo2DWidget(…)

Adds a text item to a 2D widget

visiannot.tools.ToolsPyqtgraph.basic2DPlot(data)

Creates a window with a 2D plot

visiannot.tools.ToolsPyqtgraph.basicImagePlot(im, …)

Creates a window with an image

visiannot.tools.ToolsPyqtgraph.create2DWidget(…)

Creates a 2D widget and adds it to a grid layout

visiannot.tools.ToolsPyqtgraph.createColorMap(…)

Creates a color map

visiannot.tools.ToolsPyqtgraph.createWidgetColorBar(…)

Creates a widget with a color bar (useful for the legend of a heat map)

visiannot.tools.ToolsPyqtgraph.createWidgetImage(…)

Creates a widget containing an image and adds it to a grid layout

visiannot.tools.ToolsPyqtgraph.createWidgetLogo(…)

Creates a widget with no mouse interaction containing an image and adds it to a grid layout

visiannot.tools.ToolsPyqtgraph.createWidgetSignal(…)

Creates a widget for plotting signals (see ToolsPyqtgraph.SignalWidget) and adds it to a grid layout (used in :class:.ViSiAnnoT`)

visiannot.tools.ToolsPyqtgraph.deleteNaNForPlot(data)

Deletes NaNs from an array so that they are ignored for plotting

visiannot.tools.ToolsPyqtgraph.initializeDisplayAndBgColor([color])

Creates a Qt application for display and sets background color of the pyqtgraph widgets

visiannot.tools.ToolsPyqtgraph.setBackgroundColor([color])

Sets background color of pyqtgraph widgets (independent of PyQt5 widgets)

visiannot.tools.ToolsPyqtgraph.setTicksTextStyle(…)

Sets ticks text style of an axis item

API

Class PlotItemCustom

Methods:

autoBtnClicked()

Re-implemented

class visiannot.tools.ToolsPyqtgraph.PlotItemCustom(parent=None, name=None, labels=None, title=None, viewBox=None, axisItems=None, enableMenu=True, **kargs)[source]

Bases: pyqtgraph.graphicsItems.PlotItem.PlotItem.PlotItem

Create a new PlotItem. All arguments are optional. Any extra keyword arguments are passed to PlotItem.plot().

Arguments:

title

Title to display at the top of the item. Html is allowed.

labels

A dictionary specifying the axis labels to display:

{'left': (args), 'bottom': (args), ...}

The name of each axis and the corresponding arguments are passed to PlotItem.setLabel() Optionally, PlotItem my also be initialized with the keyword arguments left, right, top, or bottom to achieve the same effect.

name

Registers a name for this view so that others may link to it

viewBox

If specified, the PlotItem will be constructed with this as its ViewBox.

axisItems

Optional dictionary instructing the PlotItem to use pre-constructed items for its axes. The dict keys must be axis names (‘left’, ‘bottom’, ‘right’, ‘top’) and the values must be instances of AxisItem (or at least compatible with AxisItem).

autoBtnClicked()[source]

Re-implemented

Class ProgressWidget

Methods:

getDragged()

Get method for attribute dragged

getFirstLine()

Get method for attribute first_line

getLastLine()

Get method for attribute first_line

getMouseXPosition(ev)

Computes the position of the mouse on the X axis in the progress plot coordinates

getNFrames()

Get method for attribute nframes

getProgressCurve()

Get method for attribute progress_curve

getProgressPlot()

Get method for attribute progress_plot

mouseMoveEvent(ev)

Re-implemented in order to set the new position of the sliding progression point while dragging

mousePressEvent(ev)

Re-implemented in order to set the new position of the sliding progression point and launch the mouse dragging

mouseReleaseEvent(ev)

Re-implemented in order to set the new position of the sliding progression point and terminate the mouse dragging

updateNFrames(nframes)

Sets a new value for the number of frames in the progress bar

class visiannot.tools.ToolsPyqtgraph.ProgressWidget(nframes, parent=None, progress_style={'brush': '#F00', 'size': 7, 'symbol': 'o'}, bg_progress_style={'pen': {'color': 'b', 'width': 2}}, line_style={'color': (0, 0, 0), 'width': 2}, title=None, title_style={'color': '#000', 'size': '9pt'}, ticks_color='#000', ticks_size=9, ticks_offset=0)[source]

Bases: pyqtgraph.widgets.PlotWidget.PlotWidget

Subclass of pyqtgraph.PlotWidget that defines the widget used as a progression bar for video/signal navigation in ViSiAnnoT window

See https://pyqtgraph.readthedocs.io/en/latest/widgets/plotwidget.html for details about parent class.

The constructor is re-implemented. It calls the constructor of PlotWidget and adds new attributes.

NB: attributes have the prefix _, so one should use the get methods to access them.

Parameters
  • nframes (int) – number of frames in the progress bar

  • parent – see https://pyqtgraph.readthedocs.io/en/latest/widgets/plotwidget.html

  • progress_style (dict) – plot style of the sliding progression point

  • bg_progress_style (dict) – plot style of the background progression bar

  • line_style (dict) – plot style of the current temporal range bounds

  • title (str) – progress bar title

  • title_style (dict) – title style

  • ticks_color (str or tuple) – color of the ticks text in HEX string or RGB format

  • ticks_size (float or int) – font size of the ticks text in pt

  • ticks_offset (int) – ticks text offset

getDragged()[source]

Get method for attribute dragged

Returns

specify if the sliding progress point is dragged

Return type

bool

getFirstLine()[source]

Get method for attribute first_line

Returns

start boundary of the current temporal range in the associated ViSiAnnoT window

Return type

pyqtgraph.InfiniteLine

getLastLine()[source]

Get method for attribute first_line

Returns

end boundary of the current temporal range in the associated ViSiAnnoT window

Return type

pyqtgraph.InfiniteLine

getMouseXPosition(ev)[source]

Computes the position of the mouse on the X axis in the progress plot coordinates

Parameters

ev (QtGui.QMouseEvent) – emitted when the mouse is clicked/moved

Returns

position of the mouse

Return type

int

getNFrames()[source]

Get method for attribute nframes

Returns

number of frames

Return type

int

getProgressCurve()[source]

Get method for attribute progress_curve

Returns

background progression bar

Return type

pyqtgraph.PlotCurveItem

getProgressPlot()[source]

Get method for attribute progress_plot

Returns

sliding progress point

Return type

pyqtgraph.ScatterPlotItem

mouseMoveEvent(ev)[source]

Re-implemented in order to set the new position of the sliding progression point while dragging

Parameters

ev (QtGui.QMouseEvent) – emitted when the mouse is clicked/moved

mousePressEvent(ev)[source]

Re-implemented in order to set the new position of the sliding progression point and launch the mouse dragging

Parameters

ev (QtGui.QMouseEvent) – emitted when the mouse is clicked/moved

mouseReleaseEvent(ev)[source]

Re-implemented in order to set the new position of the sliding progression point and terminate the mouse dragging

Parameters

ev (QtGui.QMouseEvent) – emitted when the mouse is clicked/moved

updateNFrames(nframes)[source]

Sets a new value for the number of frames in the progress bar

Parameters

nframes (int) – new number of frames in the progress bar

Class SignalWidget

class visiannot.tools.ToolsPyqtgraph.SignalWidget(parent=None, background='default', **kargs)[source]

Bases: pyqtgraph.widgets.PlotWidget.PlotWidget

Subclass of pyqtgraph.PlotWidget so that he effect of “auto-range” button is applied only on Y axis

The constructor is re-implemented so that a PlotItemCustom instance is used as the central item of the widget.

See https://pyqtgraph.readthedocs.io/en/latest/widgets/plotwidget.html for details about parent class.

Functions

visiannot.tools.ToolsPyqtgraph.addLegendTo2DWidget(widget, item_dict, size=None, offset=(0, 0), position='inside', legend_wid_size=(0, 0))[source]

Adds a legend to a 2D widget

If specified, this function creates a widget dedicated to the legend, instead of having a legend item inside the 2D widget.

Parameters
  • widget (pyqtgraph.PlotWidget) – widget containing the plot items to legend

  • item_dict (dict) – plot items to legend, key is the legend label and value is the associated plot item

  • size (tuple) – size of the legend item, length 2 (width, height)

  • offset (tuple) – offset position in pixels of the legend item in the widget

  • position (str) –

    legend mode

    • "inside": legend item inside the widget

    • "right": legend item is in a new widget created at the right side of the widget

    • "bottom": legend is in a new widget created at the bottom side of the 2D widget

  • legend_wid_size (tuple) – size of the created legend widget, length 2 (width, height) in case position is "right" or "bottom"

Returns

  • legend (-pyqtgraph.LegendItem) – legend item

  • legend_widget (pyqtgraph.PlotWidget) – widget containing the legend item if it has been created (otherwise None)

visiannot.tools.ToolsPyqtgraph.addMeanStdPlotTo2DWidget(wid, data_mean, data_std, data_X=None, pen_std_style={'color': 'k'}, n_population_list=[], **kwargs)[source]

Adds a plot item to a 2D widget, a vertical line is associated to each point of the plot item, particularly useful to display a mean/median curve with the standard deviation

The method raises an exception if data_mean and data_std or data_mean and data_X do not have the same length.

Parameters
  • wid (pyqtgraph.PlotWidget) – widget where to add the plot items

  • data_mean (numpy array) – mean/median values (1D array)

  • data_std (numpy array) – standard deviation values (1D array), same length as data_mean

  • data_X (numpy array) – coordinates of the mean/std values on the X axis (1D array), must have the same length as data_mean

  • pen_std_style (dict) – style of the standard deviation plot item

  • n_population_list (list) – number of samples at each point (same length than data_mean), a text item is then added near each point

  • kwargs – keyword arguments of the function ToolsPyqtgraph.addPlotTo2DWidget(), used for the plot item of the mean/median values

Returns

  • mean_plot (pyqtgraph.PlotDataItem) – plot item of the mean/median values

  • std_plot (pyqtgraph.PlotDataItem) – plot item of the standard deviation values

  • text_item_list (list) – text items (pyqtgraph.TextItem) of the number of samples at each point, empty list if n_population_list is empty

visiannot.tools.ToolsPyqtgraph.addPlotTo2DWidget(widget, data, flag_clear=False, flag_nan_void=True, plot_style={'pen': {'color': 'b', 'width': 1}})[source]

Adds a plot item to a 2D widget

For details about plot style, see https://pyqtgraph.readthedocs.io/en/latest/graphicsItems/plotdataitem.html

For details about color, see https://pyqtgraph.readthedocs.io/en/latest/functions.html#color-pen-and-brush-functions

Parameters
  • widget (pyqtgraph.PlotWidget) – widget where the plot must be displayed

  • data (numpy array) –

    data to plot, two possibilites:

    • shape (n, 2), first column contains X values, second column contains Y values, if only one point to plot make sure to have shape (1, 2)

    • shape (n,), only Y values, then X values are the array indexes

  • flag_clear (bool) – specify if content currently displayed must be cleared before plotting the data

  • flag_nan_void (bool) –

    specify how to handle NaN values

    • False: default behaviour of pyqtgraph

    • True: there is no plotting where NaN

    NB: when calling the method plot.setData(data_array), the behaviour is back to default. If NaNs must be ignored, the function ToolsPyqtgraph.deleteNaNForPlot() must be called before.

  • plot_style (dict) – plot style, keys are keyword arguments of the constructor of pyqtgraph.PlotDataItem, see link above

Returns

plot item (or a list of pyqtgraph.PlotDataItem in case nan_void is true)

Return type

pyqtgraph.PlotDataItem

visiannot.tools.ToolsPyqtgraph.addTextItemTo2DWidget(widget, pos, text='', color='#000000', html=None, anchor=(0.5, 0.5), border=None, fill=None, angle=0, flag_arrow=False, arrow_angle=0, arrow_style={'brush': '#000000', 'headLen': 15, 'pen': '#000000', 'tipAngle': 20})[source]

Adds a text item to a 2D widget

For details about color, see https://pyqtgraph.readthedocs.io/en/latest/functions.html#color-pen-and-brush-functions

Parameters
  • widget (pyqtgraph.PlotWidget) – widget where the text item must be displayed

  • pos (tuple) – position of the text item in the widget, length 2 (x, y)

  • text (str) – text to display

  • color (str or tuple) – text color, may be a tuple with RGB or a HEX string

  • html (str) – html text to display, it overwrites text and color

  • anchor (tuple) – coordinate of the text item that is anchored to its position pos ((0, 0) => top left, (1, 0) => top right, (1, 0) => bottom left, (1, 1) => bottom right

  • border (str or tuple) – color of the border, may be a tuple with RGB or a HEX string

  • fill (str or tuple) – color used when filling within the border, may be a tuple with RGB or a HEX

  • angle (float) – angle in degrees to rotate the text item

  • flag_arrow (bool) – specify if an arrow must be added

  • arrow_angle (float) – angle in degrees to rotate the arrow (arrow pointing to the left)

  • arrow_style (dict) – see https://pyqtgraph.readthedocs.io/en/latest/graphicsItems/arrowitem.html#pyqtgraph.ArrowItem.setStyle

Returns

  • text_item (pyqtgraph.TextItem)

  • arrow_item (pyqtgraph.ArrowItem) – may be None if arrow not specified

visiannot.tools.ToolsPyqtgraph.basic2DPlot(data, opts_win_dict={}, opts_wid_dict={}, plot_style={'pen': {'color': 'b', 'width': 1}})[source]

Creates a window with a 2D plot

For details about plot style, see https://pyqtgraph.readthedocs.io/en/latest/graphicsItems/plotdataitem.html

For details about color, see https://pyqtgraph.readthedocs.io/en/latest/functions.html#color-pen-and-brush-functions

Parameters
  • data (numpy array) –

    data to plot, two possibilites:

    • shape (n, 2), first column contains X values, second column contains Y values, if only one point to plot make sure to have shape (1, 2)

    • shape (n,), only Y values, then X values are the array indexes

  • opts_win_dict (dict) – keyword arguments of the function ToolsPyQt.createWindow()

  • opts_wid_dict (dict) – keyword arguments of the function ToolsPyqtgraph.create2DWidget()

  • plot_style (dict) – plot style, keys are keyword arguments of the constructor of pyqtgraph.PlotDataItem, see link above

Returns

  • win (QWidgets.QWidget) – window container

  • lay (QWidgets.QGridLayout) – layout

  • widget (pyqtgraph.PlotWidget) – 2D widget filling the whole layout

  • plot (pyqtgraph.PlotDataItem) – plot item

visiannot.tools.ToolsPyqtgraph.basicImagePlot(im, **kwargs)[source]

Creates a window with an image

Parameters
  • im (numpy array) – RGB image array of shape (width, height, 3)

  • kwargs – keyword arguments of the function ToolsPyQt.createWindow(), if background color of the window is specified, then it is also applied to the image widget

Returns

  • win (QWidgets.QWidget) – window container

  • lay (QWidgets.QGridLayout) – layout

  • widget (pyqtgraph.PlotWidget) – 2D widget filling the whole layout

  • img (pyqtgraph.ImageItem) – image item

visiannot.tools.ToolsPyqtgraph.create2DWidget(lay, widget_position, widget_size=(0, 0), widget_title=None, title_style={'color': '#0000', 'size': '9pt'}, x_range=[], y_range=[], axes_label_dict={'bottom': None, 'left': None}, flag_invert_x=False, flag_invert_y=False, flag_aspect_locked=False, flag_plot_menu=True)[source]

Creates a 2D widget and adds it to a grid layout

The widget content is empty.

Parameters
  • lay (QtWidgets.QGridLayout) – parent layout where the widget is added

  • widget_position (tuple) – position of the widget in the parent layout, length 2 (row, col) or 4 (row, col, rowspan, colspan)

  • widget_size (tuple) – maximum size of the widget, length 2 (width, height), set a value to 0 in order to have an automatic sizing in the corresponding axis

  • widget_title (str) – widget title, basic HTML allowed

  • title_style (dict) – title style

  • x_range (tuple) – range values on the X axis, length 2 (x_min, x_max)

  • y_range (tuple) – range values on the Y axis, length 2 (y_min, y_max)

  • axes_label_dict (dict) –

    axes to show and their respective label

    • Key is the axis identifier ("left", "right", "bottom" or "top")

    • Value is the associated label as a list of length 2:

      • axis label (str)

      • label style (dict), e.g. {'color': '#000', 'font-size': '10pt'}

    • In case no label is associated to the axis, set the corresponding value to None

  • flag_invert_x (bool) – specify if the X axis must be inverted

  • flag_invert_y (bool) – specify if the Y axis must be inverted

  • flag_aspect_locked (bool) – specify if the aspect ratio must be locked

  • flag_plot_menu (bool) – specify if the plot menu is enabled

Returns

widget

Return type

pyqtgraph.PlotWidget

visiannot.tools.ToolsPyqtgraph.createColorMap(values, colors, lut_dim=256)[source]

Creates a color map

See https://pyqtgraph.readthedocs.io/en/latest/colormap.html for details.

Parameters
  • values (list or tuple) – values spanning the color map, at least two values are required, it may be a 1D numpy array

  • colors (list or numpy array) – colors corresponding to the values, must be the same length as values, each element is a (RGB) color

  • lut_dim (int) – dimension of the look up table

Returns

  • color_map (pyqtgraph.ColorMap)

  • lut (numpy array) – color of the look up table, shape (lut_dim, 4)

visiannot.tools.ToolsPyqtgraph.createWidgetColorBar(lay, widget_position, color_map, lut, ticks_values, widget_width=80, ticks_val_formatting='%s', img_width=5)[source]

Creates a widget with a color bar (useful for the legend of a heat map)

Parameters
  • lay (QtWidgets.QGridLayout) – parent layout where the widget is added

  • widget_position (tuple) – position of the widget in the parent layout, length 2 (row, col) or 4 (row, col, rowspan, colspan)

  • color_map (pyqtgraph.ColorMap) – color map to define the color bar

  • lut (numpy array) – look up table to define the color bar

  • ticks_values (list or tuple) – values of the ticks to display

  • widget_width (int) – width of the widget in pixels

  • ticks_val_formatting (str) – formatting pattern of the ticks text

  • img_width (int) – width of the color bar in pixels

Returns

  • wid_bar (ToolsPyqtgraph.PlotWidget) – widget containing the color bar

  • bar_img_item (pyqtgraph.ImageItem) – image item of the color bar

visiannot.tools.ToolsPyqtgraph.createWidgetImage(lay, widget_position, im=None, title=None, title_style={'color': '#000', 'size': '9pt'})[source]

Creates a widget containing an image and adds it to a grid layout

Parameters
  • lay (QtWidgets.QGridLayout) – parent layout where the widget is added

  • widget_position (tuple) – position of the widget in the parent layout, length 2 (row, col) or 4 (row, col, rowspan, colspan)

  • im (numpy array) – RGB image array of shape (width, height, 3)

  • title (str) – widget title

  • title_style (dict) – widget title style

Returns

  • widget (ToolsPyqtgraph.PlotWidget) – image container

  • img (pyqtgraph.ImageItem) – image item

Creates a widget with no mouse interaction containing an image and adds it to a grid layout

Parameters
  • lay (QtWidgets.QGridLayout instance of QtWidgets.QGridLayout where the widget is added) – parent layout where the widget is added

  • widget_position (tuple) – position of the widget in the parent layout, length 2 (row, col) or 4 (row, col, rowspan, colspan)

  • im (numpy array) – RGB image array of shape (width, height, 3)

  • box_size (int or tuple) – size of the image, either an integer if same value for width and height, or a tuple of length 2 (width, height)

Returns

widget containing the image

Return type

ToolsPyqtgraph.PlotWidget

visiannot.tools.ToolsPyqtgraph.createWidgetSignal(lay, widget_position, y_range=[], left_label='', left_label_style={'color': '#000', 'font-size': '10pt'}, ticks_color='#000', ticks_size=9, ticks_offset=0)[source]

Creates a widget for plotting signals (see ToolsPyqtgraph.SignalWidget) and adds it to a grid layout (used in :class:.ViSiAnnoT`)

For details about color, see https://pyqtgraph.readthedocs.io/en/latest/functions.html#color-pen-and-brush-functions

Parameters
  • lay (QtWidgets.QGridLayout instance of QtWidgets.QGridLayout where the widget is added) – parent layout where the widget is added

  • widget_position (tuple) – position of the widget in the parent layout, length 2 (row, col) or 4 (row, col, rowspan, colspan)

  • y_range (tuple) – visible Y range, length 2 (y_min, y_max), set to [] for auto range

  • left_label (str) – label for Y axis

  • left_label_style (dict) – axis label title style

  • ticks_color (str or tuple) – color of the ticks text in HEX string or RGB format

  • ticks_size (float or int) – font size of the ticks text in pt

  • ticks_offset (int) – ticks text offset

Returns

widget containing the signals plots

Return type

ToolsPyQt.SignalWidget

visiannot.tools.ToolsPyqtgraph.deleteNaNForPlot(data)[source]

Deletes NaNs from an array so that they are ignored for plotting

Parameters

data (numpy array) – array where to delete NaNs, shape (n,) or (n, 2) (where first column contains timestamp, second column contains data value), list and tuple are also supported

Returns

array without NaNs, shape (m, 2), with m \leq n ; if the input array is 1D (shape (n,)), then the first column of the output array contains the index of each point in the input array

Return type

numpy array

visiannot.tools.ToolsPyqtgraph.initializeDisplayAndBgColor(color=(255, 255, 255))[source]

Creates a Qt application for display and sets background color of the pyqtgraph widgets

It calls the functions ToolsPyQt.initializeDisplay() and Toolspyqtgraph.setBackgroundColor().

Parameters

color (str or tuple) – background color as a string or RGB (see https://pyqtgraph.readthedocs.io/en/latest/style.html for details)

Returns

instance of QtCore.QCoreApplication or QtWidgets.QApplication

visiannot.tools.ToolsPyqtgraph.setBackgroundColor(color=(255, 255, 255))[source]

Sets background color of pyqtgraph widgets (independent of PyQt5 widgets)

Parameters

color (str or tuple) – background color as a string or RGB (see https://pyqtgraph.readthedocs.io/en/latest/style.html for details)

visiannot.tools.ToolsPyqtgraph.setTicksTextStyle(axis_item, color='#000', size=9, offset=0)[source]

Sets ticks text style of an axis item

See https://pyqtgraph.readthedocs.io/en/latest/graphicsItems/axisitem.html for details.

Parameters
  • axis_item (pyqtgraph.AxisItem) – axis for which the style of ticks text must be set

  • color (str or tuple) – color of the text in HEX string or RGB format

  • size (float or int) – font size of the text in pt

  • offset (int) – text offset