File: test_povray.py

package info (click to toggle)
python-ase 3.26.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,484 kB
  • sloc: python: 148,112; xml: 2,728; makefile: 110; javascript: 47
file content (99 lines) | stat: -rw-r--r-- 2,657 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
# fmt: off
from subprocess import DEVNULL, check_call

import numpy as np
import pytest

from ase import Atoms
from ase.build import molecule
from ase.cell import Cell
from ase.io import write
from ase.io.pov import (
    POVRAYIsosurface,
    get_bondpairs,
    set_high_bondorder_pairs,
    write_pov,
)


def test_povray_io(testdir, povray_executable):
    H2 = molecule('H2')
    write_pov('H2.pov', H2)
    check_call([povray_executable, 'H2.pov'], stderr=DEVNULL)


def test_povray_highorder(testdir, povray_executable):
    atoms = molecule('CH4')
    radii = [0.2] * len(atoms)
    bondpairs = get_bondpairs(atoms, radius=1.0)
    assert len(bondpairs) == 4

    high_bondorder_pairs = {}

    def setbond(target, order):
        high_bondorder_pairs[(0, target)] = ((0, 0, 0), order, (0.1, -0.2, 0))

    setbond(2, 2)
    setbond(3, 3)
    bondpairs = set_high_bondorder_pairs(bondpairs, high_bondorder_pairs)

    renderer = write_pov(
        'atoms.pov', atoms,
        povray_settings=dict(canvas_width=50, bondatoms=bondpairs),
        radii=radii,
    )

    # XXX Not sure how to test that the bondpairs data processing is correct.
    pngfile = renderer.render()
    assert pngfile.is_file()
    print(pngfile.absolute())


def test_deprecated(testdir):
    with pytest.warns(FutureWarning):
        write_pov('tmp.pov', molecule('H2'), run_povray=True)


@pytest.fixture()
def skimage():
    return pytest.importorskip('skimage')


@pytest.fixture()
def isosurface_things(skimage):
    rng = np.random.RandomState(42)
    cell = Cell(rng.random((3, 3)))

    values = np.zeros((3, 5, 7))
    values[1, 2, 3] = 1
    center_cell_position = cell.cartesian_positions([1 / 3, 2 / 5, 3 / 7])

    # This is the step which requires scikit-image:
    surface = POVRAYIsosurface(
        density_grid=values,
        cut_off=0.12345,
        cell=cell,
        cell_origin=(0, 0, 0))

    return cell, center_cell_position, surface


def test_compute_isosurface(isosurface_things):
    cell, center_cell_position, isosurf = isosurface_things

    vert_centroid = np.mean(isosurf.verts @ cell, axis=0)
    assert np.allclose(vert_centroid, center_cell_position)


def test_render_isosurface(testdir, isosurface_things, povray_executable):
    cell, _center_cell_position, isosurf = isosurface_things

    atoms = Atoms(
        'H3',
        scaled_positions=[[0, 0, 0], [1 / 3, 0, 0], [2 / 3, 0, 0]],
        cell=cell)

    renderer = write('tmp.pov', atoms, isosurface_data=[isosurf])
    png_path = renderer.render(povray_executable=povray_executable)
    # does the diamond appear over the second hydrogen atom?
    assert png_path.is_file()