File: test_colorbar.py

package info (click to toggle)
python-vispy 0.14.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,840 kB
  • sloc: python: 59,436; javascript: 6,800; makefile: 69; sh: 6
file content (179 lines) | stat: -rw-r--r-- 5,987 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
69
70
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
174
175
176
177
178
179
# -*- coding: utf-8 -*-

"""
Tests for ColorbarVisual
All images are of size (100,100) to keep a small file size
"""

from vispy.scene import visuals
from vispy.testing import (requires_application, TestingCanvas,
                           run_tests_if_main, raises)
from vispy.testing.image_tester import assert_image_approved


def create_colorbar(pos, size, orientation, label='label string here'):
    colorbar = visuals.ColorBar(pos=pos,
                                size=size,
                                orientation=orientation,
                                label=label,
                                cmap='autumn',
                                border_color='white',
                                border_width=2)

    colorbar.label.color = 'white'
    colorbar.label.font_size = 5

    colorbar.ticks[0].color = 'white'
    colorbar.ticks[0].font_size = 5

    colorbar.ticks[1].color = 'white'
    colorbar.ticks[1].font_size = 5

    return colorbar


@requires_application()
def test_colorbar_draw():
    """Test drawing Colorbar without transform using ColorbarVisual"""
    with TestingCanvas() as c:
        colorbar_top = create_colorbar(pos=(50, 50),
                                       size=(60, 4),
                                       orientation='top')

        c.draw_visual(colorbar_top)
        assert_image_approved(c.render(), 'visuals/colorbar/top.png')
        colorbar_top.parent = None

        colorbar_bottom = create_colorbar(pos=(50, 50),
                                          size=(60, 4),
                                          orientation='bottom')

        c.draw_visual(colorbar_bottom)
        assert_image_approved(c.render(), 'visuals/colorbar/bottom.png')
        colorbar_bottom.parent = None

        colorbar_left = create_colorbar(pos=(50, 50),
                                        size=(60, 4),
                                        orientation='left')

        c.draw_visual(colorbar_left)
        assert_image_approved(c.render(), 'visuals/colorbar/left.png')
        colorbar_left.parent = None

        colorbar_right = create_colorbar(pos=(50, 50),
                                         size=(60, 4),
                                         orientation='right')

        c.draw_visual(colorbar_right)
        assert_image_approved(c.render(), 'visuals/colorbar/right.png')


@requires_application()
def test_reactive_draw():
    """Test reactive RectPolygon attributes"""
    with TestingCanvas() as c:
        colorbar = create_colorbar(pos=(50, 50),
                                   size=(60, 4),
                                   orientation='top')
        c.draw_visual(colorbar)

        colorbar.cmap = "ice"
        assert_image_approved(c.render(),
                              'visuals/colorbar/reactive_cmap.png')

        colorbar.clim = (-20, 20)
        assert_image_approved(c.render(),
                              'visuals/colorbar/reactive_clim.png')

        colorbar.label.text = "new label"
        assert_image_approved(c.render(),
                              'visuals/colorbar/reactive_label.png')

        colorbar.ticks[0].color = "red"
        colorbar.ticks[1].color = "blue"
        assert_image_approved(c.render(),
                              'visuals/colorbar/reactive_ticks.png')

        colorbar.border_width = 0
        assert_image_approved(c.render(),
                              'visuals/colorbar/reactive_border_width.png')

        colorbar.border_width = 5
        colorbar.border_color = "red"
        assert_image_approved(c.render(),
                              'visuals/colorbar/reactive_border_color.png')


@requires_application()
def test_attributes():
    """Test if attribute checks are in place"""
    with TestingCanvas():

        # initialize with major axis < minor axis
        with raises(ValueError):
            create_colorbar(pos=(50, 50),
                            size=(1, 30),
                            orientation='top')

        # set major axis to 0
        with raises(ValueError):
            create_colorbar(pos=(50, 50),
                            size=(0, 1),
                            orientation='right')

        # set negative major axis
        with raises(ValueError):
            create_colorbar(pos=(50, 50),
                            size=(-10, 1),
                            orientation='right')

        # set negative minor axis
        with raises(ValueError):
            create_colorbar(pos=(50, 50),
                            size=(1, -10),
                            orientation='right')

        # set minor axis to 0
        with raises(ValueError):
            create_colorbar(pos=(50, 50),
                            size=(1, 0),
                            orientation='right')

        # set invalid orientation
        with raises(ValueError):
            create_colorbar(pos=(50, 50),
                            size=(60, 4),
                            orientation='top-invalid')


@requires_application()
def test_colorbar_label_change():
    with TestingCanvas() as c:
        colorbar = create_colorbar(pos=(50, 50),
                                   size=(60, 4),
                                   orientation='top')
        colorbar.cmap = "ice"
        orig_text_vis = colorbar.label
        colorbar.label = "New Label"
        assert colorbar.label.text == "New Label"
        assert colorbar.label is orig_text_vis

        c.draw_visual(colorbar)


@requires_application()
def test_colorbar_label_as_textvisual():
    with TestingCanvas() as c:
        label = visuals.Text("my label")
        colorbar = create_colorbar(pos=(50, 50),
                                   size=(60, 4),
                                   orientation='top',
                                   label=label)
        colorbar.cmap = "ice"
        assert colorbar.label.text == "my label"
        assert colorbar.label is label

        c.draw_visual(colorbar)


run_tests_if_main()