File: votca_compare

package info (click to toggle)
votca 2025.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 132,424 kB
  • sloc: xml: 345,964; cpp: 80,067; python: 15,957; sh: 4,580; perl: 2,169; javascript: 202; makefile: 34
file content (115 lines) | stat: -rwxr-xr-x 3,895 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
#! /usr/bin/env python3
#
# Copyright 2009-2024 The VOTCA Development Team (http://www.votca.org)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import sys
import os
import argparse

PROGTITLE = 'THE VOTCA::TOOLS FILE COMPARISON'
PROGDESCR = 'COMPARES TWO FILES TO EACH OTHER'
VOTCAHEADER = '''
==================================================
========   VOTCA (http://www.votca.org)   ========
==================================================

{progtitle}

please read and cite: @PROJECT_CITATION@
and submit bugs to @PROJECT_CONTACT@
votca_compare

'''.format(progtitle=PROGTITLE)

def xxquit(what=''):
    if what != '':
        print("ERROR: {what}".format(what=what))
    sys.exit(1)

def RepresentsFloat(s):
    try:
        float(s)
        return True
    except ValueError:
        return False
    
def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):
    return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)

# =============================================================================
# PROGRAM OPTIONS
# =============================================================================

class ToolsHelpFormatter(argparse.HelpFormatter):
    def _format_usage(self, usage, action, group, prefix):
        return VOTCAHEADER

progargs = argparse.ArgumentParser(prog='votca_compare',
                                   formatter_class=lambda prog:
                                   ToolsHelpFormatter(prog,
                                                      max_help_position=70),
                                   description=PROGDESCR)

progargs.add_argument('-f1', '--file1',
                      dest='file1',
                      action='store',
                      required=True,
                      type=argparse.FileType('r'),
                      help='File1 to compare.')

progargs.add_argument('-f2', '--file2',
                      dest='file2',
                      action='store',
                      required=True,
                      type=argparse.FileType('r'),
                      help='File2 to compare')

progargs.add_argument('--etol',
                      dest='etol',
                      action='store',
                      type=float,
                      default=1e-5,
                      help='Tolerance for floats to be regarded equal,default=1e-5')

OPTIONS = progargs.parse_args()

# =============================================================================
# Compare Execution
# =============================================================================

print("Comparing {} and {} to a tolerance of {}".format(OPTIONS.file1.name,
                                                        OPTIONS.file2.name, OPTIONS.etol))
logs = []
for i, (line1, line2) in enumerate(zip(OPTIONS.file1, OPTIONS.file2)):
    if line1.startswith('#') or line2.startswith('#'):
        continue
    for j, (entry1, entry2) in enumerate(zip(line1.split(), line2.split())):
        if RepresentsFloat(entry1) and RepresentsFloat(entry2):
            if not isclose(float(entry1),float(entry2),OPTIONS.etol,OPTIONS.etol):
                logs.append([i, j, entry1, entry2])
        elif entry1 != entry2:
            logs.append([i, j, entry1, entry2])

if not logs:
    sys.exit(0)
else:
    print("Diff:")
    for diff in logs:
        print("row {} col {}:\t{} vs {}".format(diff[0], diff[1], diff[2], diff[3]))
    sys.exit(1)