-
Notifications
You must be signed in to change notification settings - Fork 0
/
matdiff
executable file
·63 lines (52 loc) · 1.82 KB
/
matdiff
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
#!/Users/andrewyale/.virtualenvs/default/bin/python
"""compares two matrices files"""
import sys
import os
import numpy as np
if len(sys.argv) != 3:
print("Usage: matdiff <filename> <filename2>")
sys.exit()
match = True
if sys.argv[1].endswith(".mat"):
import scipy.io as sio
data1 = sio.loadmat(os.path.join(os.getcwd(), sys.argv[1]))
data2 = sio.loadmat(os.path.join(os.getcwd(), sys.argv[2]))
keys1 = sorted(data1.keys())
keys2 = sorted(data2.keys())
if len(set(keys1).difference(keys2)):
print("Variables are different")
print(sys.argv[1] + " keys: " + " ".join(keys1))
print(sys.argv[2] + " keys: " + " ".join(keys2))
sys.exit()
for k in keys1:
if k[:2] != "__":
if not np.allclose(data1[k], data2[k]):
print("Variable " + k + " does not match")
match = False
elif sys.argv[1].endswith(".csv"):
import pandas as pd
data1 = pd.read_csv(os.path.join(os.getcwd(), sys.argv[1]))
data2 = pd.read_csv(os.path.join(os.getcwd(), sys.argv[2]))
if data1.shape != data2.shape:
print("Shapes are different")
sys.exit()
if all(data1 == data2):
print("All variables match")
sys.exit()
try:
if not np.allclose(data1, data2):
print("Doesn't match")
match = False
except TypeError as te:
if not np.allclose(
data1.select_dtypes(exclude='object'),
data2.select_dtypes(exclude='object')):
print("Numbers don't match")
match = False
if not (data1.select_dtypes(
include='object') == data2.select_dtypes(
include='object')).values.all():
print("Objects don't match")
match = False
if match:
print("All variables match")