diff --git a/bin/testing/fuzzycomparedata.py b/bin/testing/fuzzycomparedata.py index ce5133fbb5aaedf3662d597eb9997c64a80d5113..21a75baed68657a4e9c53245be126990bb9daad1 100644 --- a/bin/testing/fuzzycomparedata.py +++ b/bin/testing/fuzzycomparedata.py @@ -11,6 +11,10 @@ import json import sys from fuzzycomparevtu import isFuzzyEqualText +# Note: these issues can be improved on by factoring out functions +# but we ignore it for know ("legacy code") +# pylint: disable=too-many-arguments,too-many-locals,too-many-branches,too-many-statements + def compareData( dataFile1, @@ -19,8 +23,9 @@ def compareData( absolute=1.5e-7, relative=1e-2, zeroValueThreshold=None, + ignoreFields=None, verbose=True, -): # pylint: disable=too-many-arguments +): """take two data files and compare them. Returns an exit key as returnvalue. Arguments: @@ -40,6 +45,8 @@ def compareData( A dictionary of parameter value pairs that set the threshold under which a number is treated as zero for a certain parameter. Use this parameter if you have to avoid comparisons of very small numbers for a certain parameter. + ignoreFields: list + A list of field names to be ignored in the comparison verbose : bool If the script should produce informative output. Enabled by default as the details give the tester a lot more information on why tests fail. @@ -54,6 +61,11 @@ def compareData( zeroValueThreshold = zeroValueThreshold or {} + # implement ignoring fields by setting a very high threshold + if ignoreFields is not None: + for field in ignoreFields: + zeroValueThreshold[field] = 1e100 + # construct element tree from data files with open(dataFile1, "r") as data1: data1 = list(csv.reader(data1, delimiter=delimiter)) @@ -128,6 +140,12 @@ if __name__ == "__main__": 'e.g. {"vel":1e-7,"delP":1.0}' ), ) + parser.add_argument( + "-i", + "--ignore", + nargs="+", + help=("Space separated list of fields to ignore in the comparison"), + ) args = vars(parser.parse_args()) sys.exit( @@ -139,5 +157,6 @@ if __name__ == "__main__": args["relative"], args["zeroThreshold"], args["verbose"], + args["ignore"], ) ) diff --git a/bin/testing/fuzzycomparevtu.py b/bin/testing/fuzzycomparevtu.py index 49df8ed8c5a95aa32bbc9a69e49a8dc029f8901a..99be77b0a34aa3fd2f5ed3fa717d5a402627b93c 100644 --- a/bin/testing/fuzzycomparevtu.py +++ b/bin/testing/fuzzycomparevtu.py @@ -19,7 +19,15 @@ import functools # pylint: disable=too-many-arguments,too-many-locals,too-many-branches,too-many-statements -def compareVTK(vtk1, vtk2, absolute=1.5e-7, relative=1e-2, zeroValueThreshold=None, verbose=True): +def compareVTK( + vtk1, + vtk2, + absolute=1.5e-7, + relative=1e-2, + zeroValueThreshold=None, + ignoreFields=None, + verbose=True, +): """take two vtk files and compare them. Returns an exit key as returnvalue. Arguments: @@ -41,6 +49,8 @@ def compareVTK(vtk1, vtk2, absolute=1.5e-7, relative=1e-2, zeroValueThreshold=No A dictionary of parameter value pairs that set the threshold under which a number is treated as zero for a certain parameter. Use this parameter if you have to avoid comparisons of very small numbers for a certain parameter. + ignoreFields: list + A list of field names to be ignored in the comparison verbose : bool If the script should produce informative output. Enabled by default as the details give the tester a lot more information on why tests fail. @@ -54,6 +64,11 @@ def compareVTK(vtk1, vtk2, absolute=1.5e-7, relative=1e-2, zeroValueThreshold=No zeroValueThreshold = zeroValueThreshold or {} + # implement ignoring fields by setting a very high threshold + if ignoreFields is not None: + for field in ignoreFields: + zeroValueThreshold[field] = 1e100 + # convert parallel vtu to sequential vtu if necessary convertedFromParallelVtu = False if vtk1.endswith(".pvtu"): @@ -668,6 +683,12 @@ if __name__ == "__main__": 'e.g. {"vel":1e-7,"delP":1.0}' ), ) + parser.add_argument( + "-i", + "--ignore", + nargs="+", + help=("Space separated list of fields to ignore in the comparison"), + ) parser.add_argument("-v", "--verbose", dest="verbose", action="store_true") parser.add_argument("--no-verbose", dest="verbose", action="store_false") parser.set_defaults(verbose=True) @@ -681,5 +702,6 @@ if __name__ == "__main__": args["relative"], args["zeroThreshold"], args["verbose"], + args["ignore"], ) ) diff --git a/bin/testing/runtest.py b/bin/testing/runtest.py index 9ee9b3acb54147172950120850fbaf324ba4a50e..db18bb619ac4683dd13e2f8eba625e449f6a2307 100755 --- a/bin/testing/runtest.py +++ b/bin/testing/runtest.py @@ -24,6 +24,7 @@ try: protocols.MeshFields = meshcompare.MeshFields protocols.TabularFields = tabularcompare.TabularFields + # pylint: disable=too-many-arguments def makePredicateSelector( relThreshold, absThreshold, @@ -43,7 +44,12 @@ try: return _selector def fieldcompareMeshData( - source, ref, absThreshold=0.0, relThreshold=1e-7, zeroValueThreshold=None + source, + ref, + absThreshold=0.0, + relThreshold=1e-7, + zeroValueThreshold=None, + ignoreFields=None, ): """Compares mesh data with the fieldcompare library""" @@ -67,10 +73,17 @@ try: sourceFields.domain.set_tolerances(abs_tol=ScaledTolerance(1e-6), rel_tol=1.5e-7) referenceFields.domain.set_tolerances(abs_tol=ScaledTolerance(1e-6), rel_tol=1.5e-7) - compare = MeshFieldsComparator(source=sourceFields, reference=referenceFields) + ignoreFields = ignoreFields or [] + compare = MeshFieldsComparator( + source=sourceFields, + reference=referenceFields, + field_exclusion_filter=lambda name: name in ignoreFields, + ) result = compare( predicate_selector=makePredicateSelector( - relThreshold, absThreshold, zeroValueThreshold + relThreshold=relThreshold, + absThreshold=absThreshold, + zeroValueThreshold=zeroValueThreshold, ), fieldcomp_callback=DefaultFieldComparisonCallback(verbosity=1), reordering_callback=lambda msg: print(f"-- {msg}"), @@ -82,9 +95,14 @@ try: return 1 return 0 - # pylint: disable=too-many-arguments def fieldcompareCSVData( - source, ref, delimiter, absThreshold=0.0, relThreshold=1e-7, zeroValueThreshold=None + source, + ref, + delimiter, + absThreshold=0.0, + relThreshold=1e-7, + zeroValueThreshold=None, + ignoreFields=None, ): """Compares CSV data with the fieldcompare library""" @@ -103,13 +121,18 @@ try: if not isinstance(referenceFields, protocols.TabularFields): raise IOError("Reference file could not been identified as CSV-like file!") - compare = FieldDataComparator(source=sourceFields, reference=referenceFields) + ignoreFields = ignoreFields or [] + compare = FieldDataComparator( + source=sourceFields, + reference=referenceFields, + field_exclusion_filter=lambda name: name in ignoreFields, + ) result = compare( predicate_selector=makePredicateSelector( - relThreshold, - absThreshold, - zeroValueThreshold, - lambda name: f"row {float(name.strip('field_'))}", + relThreshold=relThreshold, + absThreshold=absThreshold, + zeroValueThreshold=zeroValueThreshold, + sourceFieldNameTransform=lambda name: f"row {float(name.strip('field_'))}", ), fieldcomp_callback=DefaultFieldComparisonCallback(verbosity=1), ) @@ -186,6 +209,12 @@ def readCmdParameters(): ' a parameter as a python dict e.g. {"vel":1e-7,"delP":1.0}' ), ) + parser.add_argument( + "-i", + "--ignore", + nargs="+", + help=("Space separated list of fields to ignore in the comparison"), + ) args = vars(parser.parse_args()) # check parameters @@ -236,8 +265,14 @@ def _fuzzyMeshComparison(args): relThreshold = args["relative"] absThreshold = args["absolute"] zeroValueThreshold = args["zeroThreshold"] + ignoreFields = args["ignore"] numFailed += fieldcompareMeshData( - source, ref, absThreshold, relThreshold, zeroValueThreshold + source, + ref, + absThreshold, + relThreshold, + zeroValueThreshold, + ignoreFields, ) return int(numFailed > 0) @@ -255,8 +290,15 @@ def _fuzzyDataComparison(args): relThreshold = args["relative"] absThreshold = args["absolute"] zeroValueThreshold = args["zeroThreshold"] + ignoreFields = args["ignore"] numFailed += fieldcompareCSVData( - source, ref, delimiter, absThreshold, relThreshold, zeroValueThreshold + source, + ref, + delimiter, + absThreshold, + relThreshold, + zeroValueThreshold, + ignoreFields, ) return int(numFailed > 0)