-
Notifications
You must be signed in to change notification settings - Fork 8
/
util.py
44 lines (34 loc) · 989 Bytes
/
util.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
"""
This file contains various helper methods.
"""
import random
from pathlib import Path
import numpy as np
def flip_coin(prob):
return random.random() < prob
def open_file(file_name, header=None):
file = Path(file_name)
if file.is_file():
print(file_name, 'File exists: Enter (a)/w:')
c = input()
if c == "a" or c == '':
f = open(file_name, "a")
return f
f = open(file_name, "w+")
if header is not None:
f.write(header)
return f
def load_weights(weights_file):
"""
weights_file: file that contains weights to be stored or loaded
Returns: None if not to load weights else list of weights
"""
file = Path(weights_file)
if file.is_file():
print(weights_file, 'File exists: use weights:(y)/n:')
c = input()
if c == "y" or c == '':
weights = np.loadtxt(weights_file, delimiter=',', ndmin=2)
if len(weights) != 0:
return weights[-1]
return None