forked from ron-rivest/audit-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
292 lines (224 loc) · 8.83 KB
/
utils.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# utils.py
# Ronald L. Rivest
# July 27, 2017
# python3
"""
Code to work with multi.py on post-election audits.
Various utilities.
"""
import datetime
import numpy as np
import os
import sys
##############################################################################
# datetime
##############################################################################
def datetime_string():
""" Return current datetime as string e.g. '2017-06-26-21-18-30'
Year-Month-Day-Hours-Minutes-Seconds
May be used as a version label in an output filename.
"""
# https://docs.python.org/3.6/library/datetime.html
t = datetime.datetime.now()
return t.strftime("%Y-%m-%d-%H-%M-%S")
def date_string():
""" Return current date as string e.g. '2017-06-26'
Year-Month-Day
May be used as a version label in an output filename.
"""
# https://docs.python.org/3.6/library/datetime.html
t = datetime.datetime.now()
return t.strftime("%Y-%m-%d")
# Global variable for other modules to reference
start_datetime_string = datetime_string()
##############################################################################
# myprint (like logging, maybe, but maybe simpler)
##############################################################################
myprint_files = {"stdout": sys.stdout}
def myprint(*args, **kwargs):
""" variant print statement; prints to all files in myprint_files. """
for output_file_name in myprint_files:
kwargs["file"] = myprint_files[output_file_name]
print(*args, **kwargs)
def close_myprint_files():
""" Close myprint files other than stdout and stderr. """
for output_file_name in myprint_files:
if output_file_name not in ["stdout", "stderr"]:
myprint_files[output_file_name].close()
del myprint_files[output_file_name]
# error and warning messages
def myerror(msg):
""" Print error message and halt immediately """
print("FATAL ERROR:", msg)
quit()
warnings_given = 0
def mywarning(msg):
""" Print error message, but keep going.
Keep track of how many warnings have been given.
"""
global warnings_given
warnings_given += 1
print("WARNING:", msg)
##############################################################################
# Input/output at the file-handling level
##############################################################################
def greatest_name(dirpath,
startswith,
endswith,
max_label=None,
dir_wanted=False):
"""
Return greatest filename (or dirname) meeting given specs.
Return the filename (or, optionally, directory name) in the given directory
that begins and ends with strings startswith and endswith, respectively.
If there ts more than one such file, return the greatest (lexicographically)
such filename. Raise an error if there are no such files.
The portion between the prefix startswith and the suffix endswith is called
the version label in the documentation.
If max_label is given, only files or directories with a version label
at most the given max_label will be considered.
If switch "dir_wanted" is True, then return greatest directory name, not filename.
Example: greatest_name(".", "foo", ".csv", max_label="-11-10")
will return "foo-11-08.csv" from a directory containing files:
"foo-11-13.csv"
"foo-11-08.csv"
"foo-11-07.csv" , and
"zeb-12-12.csv" .
"""
if max_label == None:
max_filename = None
else:
max_filename = os.path.join(dirpath, startswith, max_label, endswith)
selected_filename = ""
for filename in os.listdir(dirpath):
full_filename = os.path.join(dirpath,filename)
if (dir_wanted == False and os.path.isfile(full_filename) or \
dir_wanted == True and not os.path.isfile(full_filename)) and \
filename.startswith(startswith) and \
filename.endswith(endswith) and \
filename > selected_filename and \
(max_filename == None or filename <= max_filename):
selected_filename = filename
if selected_filename == "":
if dir_wanted == False:
myerror(("No files in `{}` have a name starting with `{}`"
"and ending with `{}`.")
.format(dirpath, startswith, endswith))
else:
myerror (("No directories in `{}` have a name starting with `{}`"
"and ending with `{}`.")
.format(dirpath, startswith, endswith))
return selected_filename
##############################################################################
## Using an id as a counter (for ballot manifest expansion)
##############################################################################
def count_on(start, num):
"""
Return a list of values, starting with "start", of total length num.
Here start may be an integer, in which case we just return a list of
integers.
Otherwise start may be a string, ending in a decimal field; we increment
within that decimal field. If there is no decimal field suffix to start,
then one is added, with an initial value of 1, but only if num>1.
Size of decimal suffix field is preserved, unless we need to expand it
for larger integers.
"""
assert num >= 0
if num <= 0:
return []
if num ==1:
return [start]
if isinstance(start, int):
return list(range(start, start+num))
assert isinstance(start, str)
prefix = list(start)
digits = []
while len(prefix)>0 and prefix[-1].isdigit():
digits.append(prefix.pop())
digits.reverse()
if digits==[]:
digits=["1"]
counter = int("".join(digits))
prefix = "".join(prefix)
template = "{{:0{}d}}".format(len(digits))
ans = [prefix + template.format(counter+i) \
for i in range(num)]
return ans
def test_count_on():
for start, num in [(1,3), ("x", 3), ("A-98", 3), ("y", 1)]:
print(start, num, end=" ==> ")
print(count_on(start, num))
"""
1 3 ==> [1, 2, 3]
x 3 ==> ['x1', 'x2', 'x3']
A-98 3 ==> ['A-98', 'A-99', 'A-100']
y 1 ==> ['y']
"""
# test_count_on
##############################################################################
## Convert to array of 32-bit values
##
def convert_int_to_32_bit_numpy_array(v):
"""
Convert value v, which should be an arbitrarily large python integer
(or convertible to one) to a numpy array of 32-bit values,
since this format is needed to initialize a numpy.random.RandomState
object. More precisely, the result is a numpy array of type int64,
but each value is between 0 and 2**32-1, inclusive.
Example: input 2**64 + 5 yields np.array([5, 0, 1], dtype=int)
"""
try:
v = int(v)
if v<0:
raise ValueError
except ValueError:
myerror(("convert_int_to_32_bit_numpy_array: "
"{} is not a nonnegative integer, "
"or convertible to one.").format(v))
v_parts = []
radix = 2**32
while v>0:
v_parts.append(v % radix)
v = v // radix
# note: v_parts will be empty list if v==0, that is OK
return np.array(v_parts, dtype=int)
def RandomState(seed):
"""
Return a np.random.RandomState object, initialized
from an arbitrarily-large non-negative integer seed.
The numpy.random.RandomState object does not take long integers
(more than 2**32-1) as input, although it will take as input a
numpy.array of int64's to initialize the RandomState, as long as
each element of that array is between 0 and 2**32-1, inclusive.
This can be problematic for our audit, as the audit seed may well
be a 20-digit integer.
Thus, utils.py now has the routine
utils.RandomState(seed)
which takes as input an arbitrarily large nonnegative integer seed,
and returns a numpy.random.RandomState object initialized via
an a numpy.array object
initialized from seed.
"""
if seed != None:
seed_as_array = convert_int_to_32_bit_numpy_array(seed)
return np.random.RandomState(seed_as_array)
else:
print("utils.RandomState: seed is None!")
return np.random.RandomState(seed)
##############################################################################
## nested_set -- convenient utility to assign into a tree of nested dicts
def nested_set(dic, keys, value):
"""
Here
dic = existing dict
keys = nonempty list of keys
value = an arbitrary value
Function by example:
If keys = ["A", "B", "C"], then set dic["A"]["B"]["C"] = value,
ensuring all intermediate dicts exit
"""
for key in keys[:-1]:
dic = dic.setdefault(key, {})
dic[keys[-1]] = value
if __name__=="__main__":
pass