File: test_volume_property.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 (120 lines) | stat: -rw-r--r-- 3,484 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
from __future__ import annotations

import gc

import pytest

import pyvista as pv
from pyvista.plotting.volume_property import VolumeProperty


@pytest.fixture
def vol_prop():
    return VolumeProperty()


@pytest.mark.parametrize('lut', ['foo', None, True, object(), []])
def test_apply_lookup_table_raises(vol_prop: VolumeProperty, lut):
    with pytest.raises(TypeError, match='`lookup_table` must be a `pyvista.LookupTable`'):
        vol_prop.apply_lookup_table(lut)


@pytest.mark.skip_check_gc
def test_volume_lookup_table(vol_prop):
    assert vol_prop._lookup_table is None
    vol_prop.reapply_lookup_table()

    lut = pv.LookupTable(cmap='bwr')
    lut.apply_opacity([1.0, 0.0, 0.0, 0.3, 0.0, 0.0, 0.0, 0.3])
    orig = vol_prop.GetRGBTransferFunction()
    vol_prop.apply_lookup_table(lut)
    assert vol_prop.GetRGBTransferFunction() is not orig

    assert vol_prop._lookup_table is not None
    vol_prop.reapply_lookup_table()
    assert vol_prop._lookup_table is lut


def test_interpolation_type(vol_prop):
    assert isinstance(vol_prop.interpolation_type, str)

    for interpolation_type in ['nearest', 'linear']:
        vol_prop.interpolation_type = interpolation_type
        assert vol_prop.interpolation_type == interpolation_type

        vol_prop = VolumeProperty(interpolation_type=interpolation_type)
        assert vol_prop.interpolation_type == interpolation_type

    with pytest.raises(ValueError, match='must be either'):
        vol_prop.interpolation_type = 'not valid'


def test_volume_property_shade(vol_prop):
    assert isinstance(vol_prop.shade, bool)
    vol_prop.shade = True
    assert vol_prop.shade is True
    vol_prop.shade = False
    assert vol_prop.shade is False


def test_volume_independent_components(vol_prop):
    assert isinstance(vol_prop.independent_components, bool)
    vol_prop.independent_components = True
    assert vol_prop.independent_components is True
    vol_prop.independent_components = False
    assert vol_prop.independent_components is False


def test_volume_property_ambient(vol_prop):
    assert isinstance(vol_prop.ambient, float)
    value = 0.45
    vol_prop.ambient = value
    assert vol_prop.ambient == value

    vol_prop = VolumeProperty(ambient=value)
    assert vol_prop.ambient == value


def test_volume_property_diffuse(vol_prop):
    assert isinstance(vol_prop.diffuse, float)
    value = 0.45
    vol_prop.diffuse = value
    assert vol_prop.diffuse == value


def test_volume_property_specular(vol_prop):
    assert isinstance(vol_prop.specular, float)
    value = 0.45
    vol_prop.specular = value
    assert vol_prop.specular == value


def test_volume_property_specular_power(vol_prop):
    assert isinstance(vol_prop.specular_power, float)
    value = 0.45
    vol_prop.specular_power = value
    assert vol_prop.specular_power == value


def test_volume_property_copy(vol_prop):
    vol_prop.ambient = 1.0
    vol_prop_copy = vol_prop.copy()
    assert vol_prop_copy.ambient == vol_prop.ambient


def test_volume_property_repr(vol_prop):
    assert 'Interpolation type:' in repr(vol_prop)
    assert 'nearest' in repr(vol_prop)


def test_volume_property_del(vol_prop):
    # Create a mock lookup table
    lut = pv.LookupTable(cmap='bwr')
    vol_prop.apply_lookup_table(lut)

    # Ensure observer is set
    assert vol_prop._lookup_table_observer_id is not None

    # Delete the volume property and ensure cleanup
    del vol_prop
    gc.collect()  # Force garbage collection