Skip to content

confmaps

sleap.gui.overlays.confmaps

GUI overlay for confidence maps (not currently used).

Previously, a DataOverlay class retrieved data from a model (i.e., it ran inference on the current frame) and then used a ConfMapsPlot object to show the resulting confidence maps.

Example: ::

>>> cm = ConfMapsPlot(conf_data.get_frame(0))
>>> window.view.scene.addItem(cm)

Classes:

Name Description
ConfMapPlot

QGraphicsPixmapItem object for drawing single channel of confidence map.

ConfMapsPlot

QGraphicsObject to display multiple confidence maps in a QGraphicsView.

Functions:

Name Description
demo_confmaps

Demo function.

show_confmaps_from_h5

Demo function.

ConfMapPlot

Bases: QGraphicsPixmapItem

QGraphicsPixmapItem object for drawing single channel of confidence map.

Not currently used.

Parameters:

Name Type Description Default
confmap array

(h, w) array of one confidence map channel.

None
color list

optional (r, g, b) array for channel color.

[255, 255, 255]

Returns:

Type Description

None.

Note

In most cases this should only be called by ConfMapsPlot.

Methods:

Name Description
get_conf_image

Converts array data stored in object to QImage.

Source code in sleap/gui/overlays/confmaps.py
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
class ConfMapPlot(QtWidgets.QGraphicsPixmapItem):
    """QGraphicsPixmapItem object for drawing single channel of confidence map.

    Not currently used.

    Args:
        confmap (numpy.array): (h, w) array of one confidence map channel.
        color (list): optional (r, g, b) array for channel color.

    Returns:
        None.

    Note:
        In most cases this should only be called by ConfMapsPlot.
    """

    def __init__(
        self, confmap: np.array = None, color=[255, 255, 255], *args, **kwargs
    ):
        super(ConfMapPlot, self).__init__(*args, **kwargs)

        self.color_map = color

        if confmap is not None:
            self.confmap = confmap
            image = self.get_conf_image()
            self.setPixmap(QtGui.QPixmap(image))

    def get_conf_image(self) -> QtGui.QImage:
        """Converts array data stored in object to QImage.

        Returns:
            QImage.
        """
        if self.confmap is None:
            return

        # Get image data
        frame = self.confmap

        # Colorize single-channel overlap
        if np.ptp(frame) <= 1.0:
            frame_a = (frame * 255).astype(np.uint8)
            frame_r = (frame * self.color_map[0]).astype(np.uint8)
            frame_g = (frame * self.color_map[1]).astype(np.uint8)
            frame_b = (frame * self.color_map[2]).astype(np.uint8)
        else:
            frame_a = (frame).astype(np.uint8)
            frame_r = (frame * (self.color_map[0] / 255.0)).astype(np.uint8)
            frame_g = (frame * (self.color_map[1] / 255.0)).astype(np.uint8)
            frame_b = (frame * (self.color_map[2] / 255.0)).astype(np.uint8)

        frame_composite = np.dstack((frame_r, frame_g, frame_b, frame_a))

        # Convert ndarray to QImage
        image = ndarray_to_qimage(frame_composite)

        return image

get_conf_image()

Converts array data stored in object to QImage.

Returns:

Type Description
QImage

QImage.

Source code in sleap/gui/overlays/confmaps.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def get_conf_image(self) -> QtGui.QImage:
    """Converts array data stored in object to QImage.

    Returns:
        QImage.
    """
    if self.confmap is None:
        return

    # Get image data
    frame = self.confmap

    # Colorize single-channel overlap
    if np.ptp(frame) <= 1.0:
        frame_a = (frame * 255).astype(np.uint8)
        frame_r = (frame * self.color_map[0]).astype(np.uint8)
        frame_g = (frame * self.color_map[1]).astype(np.uint8)
        frame_b = (frame * self.color_map[2]).astype(np.uint8)
    else:
        frame_a = (frame).astype(np.uint8)
        frame_r = (frame * (self.color_map[0] / 255.0)).astype(np.uint8)
        frame_g = (frame * (self.color_map[1] / 255.0)).astype(np.uint8)
        frame_b = (frame * (self.color_map[2] / 255.0)).astype(np.uint8)

    frame_composite = np.dstack((frame_r, frame_g, frame_b, frame_a))

    # Convert ndarray to QImage
    image = ndarray_to_qimage(frame_composite)

    return image

ConfMapsPlot

Bases: QGraphicsObject

QGraphicsObject to display multiple confidence maps in a QGraphicsView.

Not currently used.

Parameters:

Name Type Description Default
frame array

Data for one frame of confidence map data. Shape of array should be (channels, height, width).

None
show list

List of channels to show. If None, show all channels.

None
show_box bool

Draw bounding box around confidence maps.

True

Returns:

Type Description

None.

When initialized, creates one child ConfMapPlot item for each channel.

Methods:

Name Description
boundingRect

Method required by Qt.

paint

Method required by Qt.

Source code in sleap/gui/overlays/confmaps.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class ConfMapsPlot(QtWidgets.QGraphicsObject):
    """QGraphicsObject to display multiple confidence maps in a QGraphicsView.

    Not currently used.

    Args:
        frame (numpy.array): Data for one frame of confidence map data.
            Shape of array should be (channels, height, width).
        show (list, optional): List of channels to show. If None, show all channels.
        show_box (bool, optional): Draw bounding box around confidence maps.

    Returns:
        None.

    When initialized, creates one child ConfMapPlot item for each channel.
    """

    def __init__(
        self, frame: np.array = None, show=None, show_box=True, *args, **kwargs
    ):
        super(ConfMapsPlot, self).__init__(*args, **kwargs)
        self.frame = frame
        self.show_box = show_box

        self.rect = QtCore.QRectF(0, 0, self.frame.shape[1], self.frame.shape[0])

        if self.show_box:
            QtWidgets.QGraphicsRectItem(self.rect, parent=self).setPen(
                QtGui.QPen("yellow")
            )

        for channel in range(self.frame.shape[2]):
            if show is None or channel in show:
                color_map = h5_colors[channel % len(h5_colors)]

                # Add QGraphicsPixmapItem as child object
                ConfMapPlot(
                    confmap=self.frame[..., channel], color=color_map, parent=self
                )

    def boundingRect(self) -> QtCore.QRectF:
        """Method required by Qt."""
        return self.rect

    def paint(self, painter, option, widget=None):
        """Method required by Qt."""
        pass

boundingRect()

Method required by Qt.

Source code in sleap/gui/overlays/confmaps.py
62
63
64
def boundingRect(self) -> QtCore.QRectF:
    """Method required by Qt."""
    return self.rect

paint(painter, option, widget=None)

Method required by Qt.

Source code in sleap/gui/overlays/confmaps.py
66
67
68
def paint(self, painter, option, widget=None):
    """Method required by Qt."""
    pass

demo_confmaps(confmaps, video, scale=None, standalone=False, callback=None)

Demo function.

Source code in sleap/gui/overlays/confmaps.py
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
def demo_confmaps(confmaps, video, scale=None, standalone=False, callback=None):
    """Demo function."""
    from qtpy import QtWidgets
    from sleap.gui.widgets.video import QtVideoPlayer

    if standalone:
        app = QtWidgets.QApplication([])

    win = QtVideoPlayer(video=video)
    win.setWindowTitle("confmaps")
    win.show()

    def plot_confmaps(parent, frame_idx):
        if frame_idx < confmaps.shape[0]:
            frame_conf_map = ConfMapsPlot(confmaps[frame_idx, ...], show_box=not scale)
            if scale:
                frame_conf_map.setScale(scale)
            win.view.scene.addItem(frame_conf_map)

    win.changedPlot.connect(plot_confmaps)
    if callback:
        win.changedPlot.connect(callback)
    win.plot()

    if standalone:
        app.exec_()

    return win

show_confmaps_from_h5(filename, input_format='channels_last', standalone=False)

Demo function.

Source code in sleap/gui/overlays/confmaps.py
131
132
133
134
135
136
137
138
139
140
141
142
143
def show_confmaps_from_h5(filename, input_format="channels_last", standalone=False):
    """Demo function."""
    from sleap_io.io.video_reading import HDF5Video

    video = HDF5Video(filename, "/box", input_format=input_format)
    conf_data = HDF5Video(
        filename, "/confmaps", input_format=input_format, convert_range=False
    )

    confmaps_ = [np.clip(conf_data.get_frame(i), 0, 1) for i in range(conf_data.frames)]
    confmaps = np.stack(confmaps_)

    return demo_confmaps(confmaps=confmaps, video=video, standalone=standalone)