File: generic_test.py

package info (click to toggle)
normaliz 3.9.4%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 27,624 kB
  • sloc: cpp: 39,173; makefile: 2,008; python: 715; sh: 6
file content (277 lines) | stat: -rw-r--r-- 9,385 bytes parent folder | download | duplicates (3)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#/usr/bin/env python
# Test file for PyNormaliz module
#
# Just run
#
#    $ python generic_test.py
#
# or alternatively from a Python console
#
#    >>> import generic_test
#    >>> generic_test.Test1(verbose=True).run(repeat=50)

from __future__ import print_function

import unittest
from sys import stdout

from PyNormaliz import NmzCone, NmzResult, normaliz_error

from itertools import product
from random import shuffle

methods = [
         "AffineDim",
         "Automorphisms",
         "Congruences",
         "Dehomogenization",
         "Equations",
         "EmbeddingDim",
# NOTE: floating point value
#         "EuclideanVolume",
         "EhrhartQuasiPolynomial",
         "ExcludedFaces",
         "ExternalIndex",
         "ExtremeRays",
         "GeneratorOfInterior",
         "Grading",
         "GradingDenom",
         "HilbertQuasiPolynomial",
         "InclusionExclusionData",
         "IsIntegrallyClosed",
         "IsInhomogeneous",
         "IsPointed",
         "IsTriangulationNested",
         "IsTriangulationPartial",
         "MaximalSubspace",
         "ModuleRank",
         "ModuleGenerators",
         "ReesPrimaryMultiplicity",
         "SupportHyperplanes",
         "Triangulation",
         "TriangulationSize",
         "UnitGroupIndex",
         "VerticesOfPolyhedron",
         "Volume",
         "Rank",
         "RecessionRank",
# NOTE: depends on CoCoALib
#         "WeightedEhrhartQuasiPolynomial",
         "WitnessNotIntegrallyClosed",
         ]

postprocess = {
    "VerticesOfPolyhedron": sorted,
}

class GenericPyNormalizTest:
    def __init__(self, verbosity=1):
        self.verbosity = verbosity

    def expected_answers(self):
        ans = {}
        for meth in methods:
            try:
                obj = getattr(self, meth)
            except AttributeError:
                continue
            else:
                ans[meth] = obj
        return ans

    def run(self, repeat=1):
        tested = failed = 0
        if self.verbosity >= 1:
            print("Unittest {} ({} repetitions)".format(self, repeat))

        ans = self.expected_answers()
        def_methods = sorted(ans.keys())

        if self.verbosity >= 2:
            undef_methods = sorted(set(methods).difference(def_methods))
            print("Testing: {}".format(", ".join(meth for meth in def_methods)))
            print("Skipping: {}".format(", ".join(meth for meth in undef_methods)))

        for i in range(repeat):
            if self.verbosity >= 3:
                print("*"*80)
                print("{} run {}".format(self, i))
                stdout.flush()
            polytope = NmzCone(**self.init_data)

            shuffle(def_methods)
            for meth in def_methods:
                tested += 1

                obj = ans[meth] 
                if self.verbosity >= 3:
                    print("Testing {}... ".format(meth), end='')
                    stdout.flush()

                # if we expect an exception we just check that calling
                # the method indeed raises exception
                if type(obj) is type and issubclass(obj, Exception):
                    try:
                        NmzResult(polytope, meth)
                    except obj:
                        if self.verbosity >= 3:
                            print("pass")
                            stdout.flush()
                        continue
                    except Exception as obj2:
                        print("*"*50)
                        print("Unittest {}, failure {}".format(self, meth))
                        print("{}: got {} instead of {}".format(meth, obj2, obj))
                        print("*"*50)
                        stdout.flush()
                        failed += 1

                # otherwise we just check equality of results
                # (possibly after some postprocessing)
                else:
                    obj2 = NmzResult(polytope, meth)
                    post = postprocess.get(meth)
                    if post is not None:
                        obj = post(obj)
                        obj2 = post(obj2)

                    if obj != obj2:
                        print("*"*50)
                        print("Unittest {}, failure {}".format(self, meth))
                        print("{}: got {} instead of {}".format(meth, obj2, obj))
                        print("*"*50)
                        stdout.flush()
                        failed += 1
                    elif self.verbosity >= 3:
                        print("pass")
                        stdout.flush()

        return failed

class Test1(GenericPyNormalizTest):
    init_data = {"vertices": [(-3,-2,-1,1), (-1,1,-1,2), (1,1,-1,1), (1,1,1,1)]}

    # expected results
    AffineDim = 3
    Congruences = []
    Dehomogenization = [0,0,0,1]
    EhrhartQuasiPolynomial = [[6,7,6,5], 6]
    Equations = []
    EmbeddingDim = 4
    ExcludedFaces = normaliz_error
    ExternalIndex = normaliz_error
    ExtremeRays = []
    GeneratorOfInterior = normaliz_error
    Grading = normaliz_error
    GradingDenom = normaliz_error
    HilbertQuasiPolynomial = normaliz_error
    InclusionExclusionData = normaliz_error
    IsIntegrallyClosed = normaliz_error
    IsInhomogeneous = True
    IsPointed = True
    IsTriangulationPartial = Exception # what is the actual error we got here?
    IsTriangulationNested = Exception # idem
    MaximalSubspace = []
    ModuleRank = 4
    ModuleGenerators = [[-3,-2,-1,1], [1,1,-1,1], [1,1,0,1], [1,1,1,1]]
    Rank = 4
    RecessionRank = 0
    ReesPrimaryMultiplicity = normaliz_error
    SupportHyperplanes = [[-3,4,0,-1], [1,-3,0,2], [3,-4,5,6], [7,-6,-5,4]]
    Triangulation = [[[[0, 1, 2, 3], 10, []]], [[-3, -2, -1, 1], [-1, 1, -1, 2], [1, 1, -1, 1], [1, 1, 1, 1]]]
    TriangulationSize = 1
    UnitGroupIndex = normaliz_error
    VerticesOfPolyhedron = [[-3,-2,-1,1], [-1,1,-1,2], [1,1,-1,1], [1,1,1,1]]
    Volume = [5,1]
    WitnessNotIntegrallyClosed = normaliz_error

class Test2(GenericPyNormalizTest):
    "A cube in a subspace"

    init_data = {"vertices": list((2,) + p+(-2,1,) for p in product([-1,0,1],repeat=2))}

    # expected results
    AffineDim = 2
    Automorphisms = [8, True, True, [[], []], [[[0, 2, 1, 3], [1, 0, 3, 2]], [[0, 1, 2, 3]]], [[[1, 0, 3, 2], [0, 2, 1, 3]], [[0, 1, 2, 3]]]]
    Congruences = []
    Dehomogenization = normaliz_error
    EmbeddingDim = 5
    EhrhartQuasiPolynomial = [[1,4,4],1]
    Equations = [[1,0,0,0,-2],[0,0,0,1,2]]
    ExcludedFaces = normaliz_error
    ExternalIndex = 1
    ExtremeRays = []
    GeneratorOfInterior = normaliz_error
    Grading = normaliz_error
    GradingDenom = normaliz_error
    HilbertQuasiPolynomial = normaliz_error
    InclusionExclusionData = normaliz_error
    IsInhomogeneous = True
    IsIntegrallyClosed = normaliz_error
    IsPointed = True
    IsTriangulationPartial = Exception # what is the actual error we got here?
    IsTriangulationNested = Exception # idem
    MaximalSubspace = []
    ModuleGenerators = [[2,-1,-1,-2,1], [2,-1,0,-2,1], [2,-1,1,-2,1], [2,0,-1,-2,1],
           [2,0,0,-2,1], [2,0,1,-2,1], [2,1,-1,-2,1], [2,1,0,-2,1], [2,1,1,-2,1]]
    ModuleRank = 9
    Rank = 3
    ReesPrimaryMultiplicity = normaliz_error
    RecessionRank = 0
    SupportHyperplanes = [[0,-1,0,0,1],[0,0,-1,0,1],[0,0,1,0,1],[0,1,0,0,1]]
    Triangulation = [[[[0, 1, 3], 1, []], [[1, 2, 3], 1, []], [[2, 3, 4], 1, []], [[2, 4, 5], 1, []], [[3, 4, 6], 1, []], [[4, 5, 6], 1, []], [[5, 6, 7], 1, []], [[5, 7, 8], 1, []]], [[2, -1, -1, -2, 1], [2, -1, 0, -2, 1], [2, -1, 1, -2, 1], [2, 0, -1, -2, 1], [2, 0, 0, -2, 1], [2, 0, 1, -2, 1], [2, 1, -1, -2, 1], [2, 1, 0, -2, 1], [2, 1, 1, -2, 1]]] 
    TriangulationSize = 8
    UnitGroupIndex = normaliz_error
    VerticesOfPolyhedron = [[2,-1,-1,-2,1], [2,-1,1,-2,1], [2,1,-1,-2,1], [2,1,1,-2,1]]
    Volume = [8,1]
    WitnessNotIntegrallyClosed = normaliz_error

class Test3(GenericPyNormalizTest):
    "A 2-dimensional linear subspace"

    init_data = {"vertices": [[1,2,3,5]], "cone": [[1,0,-1],[-1,0,1]]}

    # expected results
    AffineDim = 1
    Congruences = []
    Dehomogenization = normaliz_error
    EmbeddingDim = 4
    EhrhartQuasiPolynomial = [[1], [0], [0], [0], [0], 1]
    Equations = [[1,-2,1,0],[0,5,0,-2]]
#    Generators = ...
# apparently not well defined -- and therefore no longer in use
    Grading = normaliz_error
    HilbertQuasiPolynomial = normaliz_error
    IsInhomogeneous = True
    IsIntegrallyClosed = normaliz_error
    IsPointed = False
    VerticesOfPolyhedron = [[0,2,4,5]]
    ExtremeRays = []
    MaximalSubspace = [[1,0,-1,0]]
    ModuleGenerators = []
    ModuleRank = 0
#    SupportHyperplanes = [[0,0,0,1]] # [[0,-2,0,1]]
# not well defined
    RecessionRank = 1
    TriangulationSize = 1
    Volume = normaliz_error
    WitnessNotIntegrallyClosed = normaliz_error


class Test4(GenericPyNormalizTest):
    init_data = {"vertices": [[0,0,1],[1,0,1],[0,1,1]], "cone": [[1, 1]]}

    # expected results
    Equations = []
    ExtremeRays = [[1,1,0]]
    MaximalSubspace = []
    ModuleGenerators = [[0,0,1],[0,1,1],[1,0,1]]
    SupportHyperplanes = [[-1,1,1],[0,1,0],[1,-1,1],[1,0,0]]
    VerticesOfPolyhedron = [[0,0,1],[0,1,1],[1,0,1]]
    Volume = normaliz_error

tests = [Test1, Test2, Test3, Test4]

if __name__ == '__main__':
    for test in tests:
        test().run()