-
Notifications
You must be signed in to change notification settings - Fork 4
/
dataset.py
173 lines (136 loc) · 5.1 KB
/
dataset.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
import os
from argparse import Namespace
import numpy as np
import pandas as pd
import torch
from sklearn.model_selection import train_test_split
from torch.utils.data import Dataset
from tqdm.auto import tqdm
from lang import Lang
from language_model.lm_prob import LMProb
class StandardDataset(Dataset):
def __init__(self, config: Namespace, shuffle_at_init=False, seed=None):
super(StandardDataset, self).__init__()
self.config = config
self.anno_lang = Lang("anno")
self.code_lang = Lang("code")
self.__preprocess(shuffle_at_init, seed)
def __str__(self):
return f"Dataset<{os.path.basename(self.config.root_dir)}>"
def __repr__(self):
return str(self)
def __preprocess(self, shuffle, seed) -> None:
anno = np.array(
[
l.strip()
for l in open(
os.path.join(self.config.root_dir, "all.anno")
).readlines()
]
)
code = np.array(
[
l.strip()
for l in open(
os.path.join(self.config.root_dir, "all.code")
).readlines()
]
)
assert anno.shape == code.shape
if shuffle:
np.random.seed(seed)
ridx = np.random.permutation(len(anno))
anno = anno[ridx]
code = code[ridx]
self.df = pd.DataFrame({"anno": anno, "code": code})
# construct anno language
for s in anno:
self.anno_lang.add_sentence(s, tokenize_mode="anno")
self.anno_lang.build_emb_matrix(emb_file=self.config.emb_file)
# construct code language
for s in code:
self.code_lang.add_sentence(s, tokenize_mode="code")
# build examples
self.anno, self.code = [], []
for s in anno:
nums = self.anno_lang.to_numeric(
s,
tokenize_mode="anno",
min_freq=self.config.anno_min_freq,
pad_mode="post",
max_len=self.config.anno_seq_maxlen,
)
self.anno += [torch.tensor(nums)]
for s in code:
nums = self.code_lang.to_numeric(
s,
tokenize_mode="code",
min_freq=self.config.code_min_freq,
pad_mode="post",
max_len=self.config.code_seq_maxlen,
)
self.code += [torch.tensor(nums)]
# construct uniform tensor
self.anno = torch.stack(self.anno)
self.code = torch.stack(self.code)
def __getitem__(self, idx):
# if lm probabilites have been computed
if hasattr(self, "lm_probs"):
return (
self.anno[idx],
self.code[idx],
self.lm_probs["anno"][idx],
self.lm_probs["code"][idx],
)
else:
return self.anno[idx], self.code[idx]
def __len__(self):
assert len(self.anno) == len(self.code) == self.df.shape[0]
return len(self.anno)
def raw(self, idx):
return {k: self.df.iloc[idx][k] for k in self.df.columns}
def shuffle(self):
r = np.random.permutation(len(self))
self.anno = self.anno[r]
self.code = self.code[r]
if hasattr(self, "lm_probs"):
self.lm_probs["anno"] = self.lm_probs["anno"][r]
self.lm_probs["code"] = self.lm_probs["code"][r]
def compute_lm_probs(self, lm_paths):
"""
Compute LM probabilities for each unpadded, numericalized anno/code example.
"""
self.lm_probs = {"anno": [], "code": []}
pad_idx = {
"anno": self.anno_lang.token2index["<pad>"],
"code": self.code_lang.token2index["<pad>"],
}
for kind in self.lm_probs:
lm = LMProb(lm_paths[kind])
p = pad_idx[kind]
for vec in tqdm(getattr(self, kind), total=len(self), desc=f"P({kind})"):
self.lm_probs[kind] += [lm.get_prob(vec[vec != pad_idx[kind]])]
self.lm_probs[kind] = torch.stack(self.lm_probs[kind])
return self.lm_probs
def train_test_valid_split(self, test_p: float, valid_p: float, seed=None):
"""
Generate train/test/valid splits.
:param test_p : percentage of all data for test
:param valid_p: percentage of all data for train
"""
x, y = self.anno, self.code
sz = 1 - test_p - valid_p
x_train, x_test_valid, y_train, y_test_valid = train_test_split(
x, y, train_size=sz, random_state=seed
)
sz = test_p / (test_p + valid_p)
x_test, x_valid, y_test, y_valid = train_test_split(
x_test_valid, y_test_valid, train_size=sz, random_state=seed
)
assert sum(map(len, [x_train, x_test, x_valid])) == len(x)
assert sum(map(len, [y_train, y_test, y_valid])) == len(y)
splits = {
"anno": {"train": x_train, "test": x_test, "valid": x_valid},
"code": {"train": y_train, "test": y_test, "valid": y_valid},
}
return splits