-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_data.py
192 lines (164 loc) · 6.83 KB
/
util_data.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
import collections
import csv
import logging
import statistics
from bidict import bidict
from util import query_name_dict, name_query_dict
from collections import defaultdict
import os
import pickle
def load_data(data_path, tasks, _type):
'''
Load queries and remove queries not in tasks
'''
if _type == 'train':
# (1) Load queries
# (1.1) queries[('e', ('r',)] : {(3006, (194,)), ...., (2378, (56,))}
queries: collections.defaultdict = pickle.load(open(data_path + "/train-queries.pkl", 'rb'))
# Query patterns as keys ,
"""
('e', ('r',)), ('e', ('r', 'r')), ('e', ('r', 'r', 'r')) ...
(('e', ('r', 'r', 'n')), ('e', ('r',)))
"""
answers_easy = pickle.load(open(data_path + "/train-answers.pkl", 'rb'))
answers_hard = defaultdict(set)
elif _type == 'valid':
queries = pickle.load(open(os.path.join(data_path, "valid-queries.pkl"), 'rb'))
answers_hard = pickle.load(open(os.path.join(data_path, "valid-hard-answers.pkl"), 'rb'))
answers_easy = pickle.load(open(os.path.join(data_path, "valid-easy-answers.pkl"), 'rb'))
elif _type == 'test':
queries = pickle.load(open(os.path.join(data_path, "test-queries.pkl"), 'rb'))
answers_hard = pickle.load(open(os.path.join(data_path, "test-hard-answers.pkl"), 'rb'))
answers_easy = pickle.load(open(os.path.join(data_path, "test-easy-answers.pkl"), 'rb'))
else:
raise KeyError(_type)
# remove query structures not in tasks
for task in list(queries.keys()):
if task not in query_name_dict or query_name_dict[task] not in tasks:
del queries[task]
for qs in tasks:
try:
logging.info(_type + ': ' + qs + ": " + str(len(queries[name_query_dict[qs]])))
except:
logging.warn(_type + ': ' + qs + ": not in pkl file")
return queries, answers_easy, answers_hard
def load_attr_exists_data_dummy(data_path, mode='valid'):
'''
Load queries to evaluate relations to the attr_exists dummy entity.
(e, r_a, 14505) ~ 14505 dummy entity AND
(14505, r_a_inv, e)
'''
queries = pickle.load(open(os.path.join(data_path, mode + "-attr-exists-queries.pkl"), 'rb'))
answers_easy = pickle.load(open(os.path.join(data_path, mode + "-attr-exists-answers.pkl"), 'rb'))
return queries, defaultdict(set), answers_easy
def load_attr_exists_data(args):
'''
Load queries to evaluate if an entity has an attribute.
'''
queries = pickle.load(open(os.path.join(args.data_path, "train-queries.pkl"), 'rb'))
result = dict()
result[name_query_dict['attr_exists']] = set()
result_answers = defaultdict(set)
for query in queries[name_query_dict['1ap']]:
q = (query[1][1],)
result[name_query_dict['attr_exists']].add(q)
result_answers[q].add(query[0])
return result, defaultdict(set), result_answers
def get_all_entity_descriptions(data_path):
queries, answers, _ = load_data(data_path, ('1dp',), 'train')
descriptions = dict()
for q in queries[name_query_dict['1dp']]:
descriptions[q[0]] = list(answers[q])
return descriptions
def load_stats(data_path):
try:
nentity = 0
nrelation = 0
nattributes = 0
with open(os.path.join(data_path, "entity2id.txt"), 'r') as f:
nentity = int(f.readline())
with open(os.path.join(data_path, "relation2id.txt"), 'r') as f:
nrelation = int(f.readline())
try:
with open(os.path.join(data_path, "attr2id.txt"), 'r') as f:
nattributes = int(f.readline())
except FileNotFoundError:
nattributes = 0
return nentity, nrelation, nattributes
except FileNotFoundError:
nentity = len(pickle.load(open(os.path.join(data_path, 'ent2id.pkl'), 'rb')))
nrelation = len(pickle.load(open(os.path.join(data_path, 'rel2id.pkl'), 'rb')))
return nentity, nrelation, 0
def load_mappings_from_file(path, name):
mapping = bidict()
with open(os.path.join(path, f"{name}2id.txt"), "r") as file:
reader = csv.DictReader(file, delimiter='\t', fieldnames=("name", "id"))
next(reader)
for row in reader:
mapping[row["name"]] = int(row["id"])
return mapping
def load_descriptions_from_file(path, name):
mapping = bidict()
with open(os.path.join(path, f"desc_{name}2id.txt"), "r") as file:
reader = csv.DictReader(file, delimiter='\t', fieldnames=("id", "desc"))
next(reader)
for row in reader:
mapping[int(row["id"])] = row["desc"]
return mapping
def get_all_attribute_values(path):
"""
Get all values for each attribute.
"""
attr_values = dict()
with open(os.path.join(path, "attr_train2id.txt"), "r") as file:
reader = csv.reader(file, delimiter='\t')
next(reader)
for row in reader:
if int(row[1]) in attr_values:
attr_values[int(row[1])].append(float(row[2]))
else:
attr_values[int(row[1])] = [float(row[2])]
with open(os.path.join(path, "attr_valid2id.txt"), "r") as file:
reader = csv.reader(file, delimiter='\t')
next(reader)
for row in reader:
if int(row[1]) in attr_values:
attr_values[int(row[1])].append(float(row[2]))
else:
attr_values[int(row[1])] = [float(row[2])]
with open(os.path.join(path, "attr_test2id.txt"), "r") as file:
reader = csv.reader(file, delimiter='\t')
next(reader)
for row in reader:
if int(row[1]) in attr_values:
attr_values[int(row[1])].append(float(row[2]))
else:
attr_values[int(row[1])] = [float(row[2])]
return attr_values
def get_mads(attr_values):
"""
Return mean average deviations for a given dict of attribute values.
"""
mads = dict()
for attr, values in dict(sorted(attr_values.items())).items():
try:
mads[attr] = sum([abs(statistics.mean(values) - v) for v in values]) / len(values)
except:
mads[attr] = 1.0e-10
if mads[attr] == 0.0:
mads[attr] = 1.0e-10
return mads
def denormalize(attribute, value, data_path):
with open(os.path.join(data_path, "attr2id_min_max.txt"), "r") as file:
reader = csv.reader(file, delimiter='\t')
next(reader)
for row in reader:
if int(row[1]) == attribute:
return value * (float(row[3]) - float(row[2])) + float(row[2])
def normalize(attribute, value, data_path):
with open(os.path.join(data_path, "attr2id_min_max.txt"), "r") as file:
reader = csv.reader(file, delimiter='\t')
next(reader)
for row in reader:
if int(row[1]) == attribute:
return (value - float(row[2])) / (float(row[3]) - float(row[2]))