File: rtImageTest.py

package info (click to toggle)
paraview 5.13.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544,220 kB
  • sloc: cpp: 3,374,605; ansic: 1,332,409; python: 150,381; xml: 122,166; sql: 65,887; sh: 7,317; javascript: 5,262; yacc: 4,417; java: 3,977; perl: 2,363; lex: 1,929; f90: 1,397; makefile: 170; objc: 153; tcl: 59; pascal: 50; fortran: 29
file content (140 lines) | stat: -rwxr-xr-x 4,949 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
#!/usr/bin/env python
# This is the script that runs Python regression test scripts.
# The test script to be run must be the first argument.

from vtkmodules.vtkCommonCore import vtkMath
from vtkmodules.vtkTestingRendering import vtkTesting
import os
import sys
import re
import traceback

def _GetController():
    try:
        from vtkmodules.vtkParallelMPI import vtkMPIController
        controller = vtkMPIController();

        # If MPI was not initialized, we do not want to use MPI
        if not controller.GetCommunicator():
            return None
        return controller
    except:
        pass
    return None

def main(test_script):
    """Run a regression test, and compare the contents of the window against
    against a valid image.  This will use arguments from sys.argv to set the
    testing options via the vtkTesting class, run the test script, and then
    call vtkTesting.RegressionTest() to validate the image.  The return
    value will the one provided by vtkTesting.RegressionTest().
    """

    # find the first argument that's an option
    opt1 = 1
    while opt1 < len(sys.argv) and not sys.argv[opt1].startswith('-'):
        opt1 += 1

    # get the "-A" option, which isn't handled by vtkTester
    for i in range(opt1, len(sys.argv)):
        if sys.argv[i] == '-A' and i < len(sys.argv) - 1:
            sys.path.append(sys.argv[i + 1])

    # create the testing class to do the work
    rtTester = vtkTesting()

    try:
        rtTester.SetController(_GetController())
    except:
        pass

    for arg in sys.argv[opt1:]:
        rtTester.AddArgument(arg)

    # if test is not interactive, make a mock interactor with a
    # disabled Start method
    if rtTester.IsInteractiveModeSpecified() == 0:
        from vtkmodules.vtkRenderingCore import vtkRenderWindowInteractor
        import vtkmodules.vtkRenderingUI
        irenType = type(vtkRenderWindowInteractor())
        class vtkTestingInteractor(irenType):
            def Start(self):
                pass
        irenType.override(vtkTestingInteractor)

    # seed the random number generator
    vtkMath.RandomSeed(6)

    # read the test script
    with open(test_script) as test_file:
        test_code = test_file.read()

    # inject the test script's directory into sys.path
    test_script_dir = os.path.abspath(os.path.dirname(test_script))
    sys.path.insert(0, test_script_dir)

    # we provide an initial set of variables for the test script
    test_script_vars = { "__name__" : "__main__" }

    try:
        # run the test and capture all of its global variables
        exec(compile(test_code, test_script, 'exec'), test_script_vars)
    except Exception:
        traceback.print_exc()
        return vtkTesting.FAILED
    finally:
        # undo the change to the path
        sys.path.remove(test_script_dir)

    # undo the vtkRenderWindowInteractor override
    if rtTester.IsInteractiveModeSpecified() == 0:
       irenType.override(None)

    # if script has "if __name__ == '__main__':", then we assume that
    # it would have raised an exception or called exit(1) if it failed.
    if re.search('^if *__name__ *== *[\'\"]__main__[\'\"]', test_code, flags=re.MULTILINE):
        return vtkTesting.PASSED

    # if script didn't set "threshold", use default value
    try:
        threshold = test_script_vars["threshold"]
    except KeyError:
        # Used to be 0.15. Changed to 0.05 for new SSIM method.
        threshold = 0.05

    # we require a valid regression image
    if rtTester.IsValidImageSpecified():
        # look for a renderWindow ImageWindow or ImageViewer
        # first check for some common names
        if "iren" in test_script_vars:
            iren = test_script_vars["iren"]
            rtTester.SetRenderWindow(iren.GetRenderWindow())
            iren.GetRenderWindow().Render()
        elif "renWin" in test_script_vars:
            renWin = test_script_vars["renWin"]
            rtTester.SetRenderWindow(renWin)
        elif "viewer" in test_script_vars:
            viewer = test_script_vars["viewer"]
            rtTester.SetRenderWindow(viewer.GetRenderWindow())
            viewer.Render()
        elif "imgWin" in test_script_vars:
            imgWin = test_script_vars["imgWin"]
            rtTester.SetRenderWindow(imgWin)
            imgWin.Render()
        else:
            sys.stderr.write("Test driver rtImageTest.py says \"no iren, renWin, viewer, or imgWin\": %s\n" % test_script)
            return vtkTesting.FAILED

        return rtTester.RegressionTest(threshold)

    return vtkTesting.FAILED

if __name__ == '__main__':
    # We don't parse the arguments (vtkTesting does that), but we need
    # to extract the name of the test script to run.
    if len(sys.argv) < 2 or sys.argv[1].startswith('-'):
        print("Usage %s <test script> [<addition arguments>]" % argv[0])
        sys.exit(1)

    if main(sys.argv[1]) == vtkTesting.FAILED:
        sys.exit(1)