/
test_dataset.py
240 lines (200 loc) · 7.67 KB
/
test_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
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
import io
import os
import shutil
import unittest
from test import test_dir_path
import pandas as pd
from nose.tools import *
import torch
from deepmatcher.data.dataset import *
from deepmatcher.data.field import FastText, MatchingField
from deepmatcher.data.process import _make_fields, process
from torchtext.utils import unicode_csv_reader
try:
from urllib.parse import urljoin
from urllib.request import pathname2url
except ImportError:
from urlparse import urljoin
from urllib import path2pathname2url
# import nltk
# nltk.download('perluniprops')
# nltk.download('nonbreaking_prefixes')
class ClassMatchingDatasetTestCases(unittest.TestCase):
def test_init_1(self):
fields = [('left_a', MatchingField()), ('right_a', MatchingField())]
col_naming = {'id': 'id', 'label': 'label', 'left': 'left', 'right': 'right'}
path = os.path.join(test_dir_path, 'test_datasets', 'sample_table_small.csv')
md = MatchingDataset(fields, col_naming, path=path)
self.assertEqual(md.id_field, 'id')
self.assertEqual(md.label_field, 'label')
self.assertEqual(md.all_left_fields, ['left_a'])
self.assertEqual(md.all_right_fields, ['right_a'])
self.assertEqual(md.all_text_fields, ['left_a', 'right_a'])
self.assertEqual(md.canonical_text_fields, ['_a'])
class MatchingDatasetSplitsTestCases(unittest.TestCase):
def setUp(self):
self.data_dir = os.path.join(test_dir_path, 'test_datasets')
self.train = 'test_train.csv'
self.validation = 'test_valid.csv'
self.test = 'test_test.csv'
self.cache_name = 'test_cacheddata.pth'
with io.open(
os.path.expanduser(os.path.join(self.data_dir, self.train)),
encoding="utf8") as f:
header = next(unicode_csv_reader(f))
id_attr = 'id'
label_attr = 'label'
ignore_columns = ['left_id', 'right_id']
self.fields = _make_fields(header, id_attr, label_attr, ignore_columns, True,
'nltk', False)
self.column_naming = {
'id': id_attr,
'left': 'left_',
'right': 'right_',
'label': label_attr
}
def tearDown(self):
cache_name = os.path.join(self.data_dir, self.cache_name)
if os.path.exists(cache_name):
os.remove(cache_name)
def test_splits_1(self):
datasets = MatchingDataset.splits(
self.data_dir,
self.train,
self.validation,
self.test,
self.fields,
None,
None,
self.column_naming,
self.cache_name,
train_pca=False)
@raises(MatchingDataset.CacheStaleException)
def test_splits_2(self):
datasets = MatchingDataset.splits(
self.data_dir,
self.train,
self.validation,
self.test,
self.fields,
None,
None,
self.column_naming,
self.cache_name,
train_pca=False)
datasets_2 = MatchingDataset.splits(
self.data_dir,
'sample_table_small.csv',
self.validation,
self.test,
self.fields,
None,
None,
self.column_naming,
self.cache_name,
True,
False,
train_pca=False)
def test_splits_3(self):
datasets = MatchingDataset.splits(
self.data_dir,
self.train,
self.validation,
self.test,
self.fields,
None,
None,
self.column_naming,
self.cache_name,
train_pca=False)
datasets_2 = MatchingDataset.splits(
self.data_dir,
self.train,
self.validation,
self.test,
self.fields,
None,
None,
self.column_naming,
self.cache_name,
False,
False,
train_pca=False)
class DataframeSplitTestCases(unittest.TestCase):
def test_split_1(self):
labeled_path = os.path.join(test_dir_path, 'test_datasets',
'sample_table_large.csv')
labeled_table = pd.read_csv(labeled_path)
ori_cols = list(labeled_table.columns)
out_path = os.path.join(test_dir_path, 'test_datasets')
train_prefix = 'train.csv'
valid_prefix = 'valid.csv'
test_prefix = 'test.csv'
split(labeled_table, out_path, train_prefix, valid_prefix, test_prefix)
train_path = os.path.join(out_path, train_prefix)
valid_path = os.path.join(out_path, valid_prefix)
test_path = os.path.join(out_path, test_prefix)
train = pd.read_csv(train_path)
valid = pd.read_csv(valid_path)
test = pd.read_csv(test_path)
self.assertEqual(list(train.columns), ori_cols)
self.assertEqual(list(valid.columns), ori_cols)
self.assertEqual(list(test.columns), ori_cols)
if os.path.exists(train_path):
os.remove(train_path)
if os.path.exists(valid_path):
os.remove(valid_path)
if os.path.exists(test_path):
os.remove(test_path)
def test_split_2(self):
labeled_path = os.path.join(test_dir_path, 'test_datasets',
'sample_table_large.csv')
labeled_table = pd.read_csv(labeled_path)
ori_cols = list(labeled_table.columns)
out_path = os.path.join(test_dir_path, 'test_datasets')
train_prefix = 'train.csv'
valid_prefix = 'valid.csv'
test_prefix = 'test.csv'
split(labeled_path, out_path, train_prefix, valid_prefix, test_prefix)
train_path = os.path.join(out_path, train_prefix)
valid_path = os.path.join(out_path, valid_prefix)
test_path = os.path.join(out_path, test_prefix)
train = pd.read_csv(train_path)
valid = pd.read_csv(valid_path)
test = pd.read_csv(test_path)
self.assertEqual(list(train.columns), ori_cols)
self.assertEqual(list(valid.columns), ori_cols)
self.assertEqual(list(test.columns), ori_cols)
if os.path.exists(train_path):
os.remove(train_path)
if os.path.exists(valid_path):
os.remove(valid_path)
if os.path.exists(test_path):
os.remove(test_path)
class GetRawTableTestCases(unittest.TestCase):
def test_get_raw_table(self):
vectors_cache_dir = '.cache'
if os.path.exists(vectors_cache_dir):
shutil.rmtree(vectors_cache_dir)
data_cache_path = os.path.join(test_dir_path, 'test_datasets', 'cacheddata.pth')
if os.path.exists(data_cache_path):
os.remove(data_cache_path)
vec_dir = os.path.abspath(os.path.join(test_dir_path, 'test_datasets'))
filename = 'fasttext_sample.vec.zip'
url_base = urljoin('file:', pathname2url(vec_dir)) + os.path.sep
ft = FastText(filename, url_base=url_base, cache=vectors_cache_dir)
train = process(
path=os.path.join(test_dir_path, 'test_datasets'),
train='sample_table_small.csv',
id_attr='id',
embeddings=ft,
embeddings_cache_path='',
pca=False)
train_raw = train.get_raw_table()
ori_train = pd.read_csv(
os.path.join(test_dir_path, 'test_datasets', 'sample_table_small.csv'))
self.assertEqual(set(train_raw.columns), set(ori_train.columns))
if os.path.exists(data_cache_path):
os.remove(data_cache_path)
if os.path.exists(vectors_cache_dir):
shutil.rmtree(vectors_cache_dir)