File: test_cube_axes_actor.py

package info (click to toggle)
python-pyvista 0.46.3-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 177,564 kB
  • sloc: python: 94,482; sh: 129; makefile: 70
file content (151 lines) | stat: -rw-r--r-- 5,036 bytes parent folder | download
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
"""Test the CubeAxesActor wrapping."""

from __future__ import annotations

import numpy as np
import pytest

import pyvista as pv

# A large number of tests here fail gc
pytestmark = pytest.mark.skip_check_gc


@pytest.fixture
def cube_axes_actor():
    pl = pv.Plotter()
    pl.add_mesh(pv.Sphere())
    return pl.show_bounds()


def test_cube_axes_actor():
    pl = pv.Plotter()
    actor = pv.CubeAxesActor(
        pl.camera,
        x_label_format=None,
        y_label_format=None,
        z_label_format=None,
    )
    assert isinstance(actor.camera, pv.Camera)

    # ensure label format is set to default
    assert actor.x_label_format == '%.1f'
    assert actor.y_label_format == '%.1f'
    assert actor.z_label_format == '%.1f'


def test_labels(cube_axes_actor):
    # test setting "format" to just a string
    cube_axes_actor.x_label_format = 'Value'
    assert len(cube_axes_actor.x_labels) == 5
    assert set(cube_axes_actor.x_labels) == {'Value'}

    # test no format, values should match exactly
    cube_axes_actor.y_label_format = ''
    assert len(cube_axes_actor.y_labels) == 5
    values = np.array(cube_axes_actor.y_labels, float)
    expected = np.linspace(cube_axes_actor.bounds.y_min, cube_axes_actor.bounds.y_max, 5)
    assert np.allclose(values, expected)

    # standard format
    cube_axes_actor.z_label_format = '%.1f'
    assert len(cube_axes_actor.z_labels) == 5
    assert all(len(label) < 5 for label in cube_axes_actor.z_labels)


def test_tick_location(cube_axes_actor):
    assert isinstance(cube_axes_actor.tick_location, str)

    for location in ['inside', 'outside', 'both']:
        cube_axes_actor.tick_location = location
        assert cube_axes_actor.tick_location == location


def test_use_2d_mode(cube_axes_actor):
    assert isinstance(cube_axes_actor.use_2d_mode, bool)
    cube_axes_actor.use_2d_mode = False
    assert cube_axes_actor.use_2d_mode is False


def test_label_visibility_setter(cube_axes_actor):
    assert isinstance(cube_axes_actor.x_label_visibility, bool)
    cube_axes_actor.x_label_visibility = False
    assert cube_axes_actor.x_label_visibility is False

    assert isinstance(cube_axes_actor.y_label_visibility, bool)
    cube_axes_actor.y_label_visibility = False
    assert cube_axes_actor.y_label_visibility is False

    assert isinstance(cube_axes_actor.z_label_visibility, bool)
    cube_axes_actor.z_label_visibility = False
    assert cube_axes_actor.z_label_visibility is False


def test_titles(cube_axes_actor):
    assert isinstance(cube_axes_actor.x_title, str)
    cube_axes_actor.x_title = 'x foo'
    assert cube_axes_actor.x_title == 'x foo'

    assert isinstance(cube_axes_actor.y_title, str)
    cube_axes_actor.y_title = 'y foo'
    assert cube_axes_actor.y_title == 'y foo'

    assert isinstance(cube_axes_actor.z_title, str)
    cube_axes_actor.z_title = 'z foo'
    assert cube_axes_actor.z_title == 'z foo'


def test_axis_minor_tick_visibility(cube_axes_actor):
    assert isinstance(cube_axes_actor.x_axis_minor_tick_visibility, bool)
    cube_axes_actor.x_axis_minor_tick_visibility = False
    assert cube_axes_actor.x_axis_minor_tick_visibility is False

    assert isinstance(cube_axes_actor.y_axis_minor_tick_visibility, bool)
    cube_axes_actor.y_axis_minor_tick_visibility = False
    assert cube_axes_actor.y_axis_minor_tick_visibility is False

    assert isinstance(cube_axes_actor.z_axis_minor_tick_visibility, bool)
    cube_axes_actor.z_axis_minor_tick_visibility = False
    assert cube_axes_actor.z_axis_minor_tick_visibility is False


@pytest.mark.needs_vtk_version(
    less_than=(9, 3, 0),
    reason='title offset is a tuple of floats from vtk >= 9.3',
)
def test_title_offset(cube_axes_actor):
    assert isinstance(cube_axes_actor.title_offset, float)
    cube_axes_actor.title_offset = 0.01
    assert cube_axes_actor.title_offset == 0.01

    with pytest.warns(
        UserWarning,
        match=r'Setting title_offset with a sequence is only supported from vtk >= 9\.3. '
        rf'Considering only the second value \(ie\. y-offset\) of {(y := 0.02)}',
    ):
        cube_axes_actor.title_offset = [0.01, y]
    assert cube_axes_actor.title_offset == y


@pytest.mark.needs_vtk_version(9, 3)
def test_title_offset_sequence(cube_axes_actor):
    assert isinstance(cube_axes_actor.title_offset, tuple)
    cube_axes_actor.title_offset = (t := (0.01, 0.02))
    assert cube_axes_actor.title_offset == t


@pytest.mark.needs_vtk_version(9, 3)
def test_title_offset_float(cube_axes_actor):
    with pytest.warns(
        UserWarning,
        match=r'Setting title_offset with a float is deprecated from vtk >= 9.3. Accepts now a '
        r'sequence of \(x,y\) offsets. Setting the x offset to 0\.0',
    ):
        cube_axes_actor.title_offset = (t := 0.01)
    assert cube_axes_actor.title_offset == (0.0, t)


def test_label_offset(cube_axes_actor):
    assert isinstance(cube_axes_actor.label_offset, float)
    cube_axes_actor.label_offset = 0.01
    assert cube_axes_actor.label_offset == 0.01