-
Notifications
You must be signed in to change notification settings - Fork 15
/
check.py
115 lines (77 loc) · 2.56 KB
/
check.py
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
"""
Basic checks for data dictionaries.
"""
import sys
import msg
def get_size(data):
"""Check if #entries is the same for all keys and return it
Keyword arguments:
data -- data dictionary
"""
sizes = [d.shape[0] for d in data.values()] # shape[0] = #entries
if max(sizes) != min(sizes):
msg.error("Each dataset within a file must have the "
"same number of entries!")
sys.exit(1)
return sizes[0]
def same_sizes(data1, data2):
"""Check if files have the same #entries per dataset.
Keyword arguments:
data1 -- first file
data2 -- second file
"""
if get_size(data1) != get_size(data2):
msg.error("Files must have the same number of entries to be combined.")
sys.exit(1)
def check_keys(data1, data2):
"""Check it both files have the same datasets.
Keyword arguments:
data1 -- current data dictionary
data2 -- data dictionary to be added
"""
if data1.keys() != data2.keys():
msg.error("Files have different datasets.")
sys.exit(1)
def check_shapes(data1, data2):
"""Check if shapes of datasets are the same.
Keyword arguments:
data1 -- current data dictionary
data2 -- data dictionary to be added
"""
for key in data1.keys():
if data1[key].shape[1:] != data2[key].shape[1:]:
msg.error("Different shapes for dataset: %s. " % key)
sys.exit(1)
def key_exists(key, data, filename):
"""Check if given dataset is included in the file.
Keyword arguments:
key -- key to look for
data -- data dictionary to check
"""
if key not in data.keys():
msg.error("'%(key)s' key is missing in %(file)s."
% {"key": key, "file": filename})
sys.exit(1)
def different_keys(data1, data2, skip):
"""Check if given files have different (except skip) datasets.
Keyword arguments:
data1 -- data dictionary
data2 -- data dictionary
skip -- common key
"""
for key in data1.keys():
if key == skip:
continue
if key in data2.keys():
msg.error("Duplicated dataset: %s in input files." % key)
sys.exit(1)
def check_duplicates(keys1, keys2):
"""Check if given files have different (except skip) datasets.
Keyword arguments:
keys1 -- the list of keys to be copied from file1
keys2 -- the list of keys to be copied from file2
"""
for key in keys1:
if key in keys2:
msg.error("Duplicated dataset: %s in input files." % key)
sys.exit(1)