Repository navigation
Expand file tree
/
Copy pathutil.py
More file actions
68 lines (47 loc) · 1.79 KB
/
Copy pathutil.py
File metadata and controls
68 lines (47 loc) · 1.79 KB
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
import json
def write_json_file(path, data, indent=4):
with open(path, "w") as file:
json.dump(data, file, indent=indent)
def load_json_file(path):
with open(path, "r") as file:
return json.load(file)
def load_fake_json_file(path, size=None):
with open(path, "r") as file:
data = []
current_line = 0
for line in file:
if size is not None and current_line > size - 1:
break
data.append(json.loads(line))
current_line += 1
return data
def write_psl_data_file(path, data):
with open(path, 'w') as file:
for row in data:
file.write('\t'.join([str(item) for item in row]) + "\n")
def load_psl_data_file(path, dtype=str):
data = []
with open(path, 'r') as file:
for line in file:
line = line.strip()
if line == '':
continue
data.append(list(map(dtype, line.split("\t"))))
return data
def one_hot_encoding(label, num_labels):
encoding = [0] * num_labels
encoding[label] = 1
return encoding
def enumerate_hyperparameters(hyperparameters_dict, current_hyperparameters={}):
for key in sorted(hyperparameters_dict):
hyperparameters = []
for value in hyperparameters_dict[key]:
next_hyperparameters = current_hyperparameters.copy()
next_hyperparameters[key] = value
remaining_hyperparameters = hyperparameters_dict.copy()
remaining_hyperparameters.pop(key)
if remaining_hyperparameters:
hyperparameters = hyperparameters + enumerate_hyperparameters(remaining_hyperparameters, next_hyperparameters)
else:
hyperparameters.append(next_hyperparameters)
return hyperparameters