forked from Jamesyang2333/SAM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
350 lines (281 loc) · 11.7 KB
/
common.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
"""Data abstractions."""
import copy
import time
import numpy as np
import pandas as pd
import torch
from torch.utils import data
# Na/NaN/NaT Semantics
#
# Some input columns may naturally contain missing values. These are handled
# by the corresponding numpy/pandas semantics.
#
# Specifically, for any value (e.g., float, int, or np.nan) v:
#
# np.nan <op> v == False.
#
# This means that in progressive sampling, if a column's domain contains np.nan
# (at the first position in the domain), it will never be a valid sample
# target.
#
# The above evaluation is consistent with SQL semantics.
class Column(object):
"""A column. Data is write-once, immutable-after.
Typical usage:
col = Column('Attr1').Fill(data, infer_dist=True)
The passed-in 'data' is copied by reference.
"""
def __init__(self, name, distribution_size=None, pg_name=None):
self.name = name
# Data related fields.
self.data = None
self.all_distinct_values = None
self.all_distinct_values_with_counts = None
self.distribution_size = distribution_size
# pg_name is the name of the corresponding column in Postgres. This is
# put here since, e.g., PG disallows whitespaces in names.
self.pg_name = pg_name if pg_name else name
def Name(self):
"""Name of this column."""
return self.name
def DistributionSize(self):
"""This column will take on discrete values in [0, N).
Used to dictionary-encode values to this discretized range.
"""
return self.distribution_size
def ValToBin(self, val):
if isinstance(self.all_distinct_values, list):
return self.all_distinct_values.index(val)
if type(val) == float or type(val) == int:
if np.isnan(val):
if type(self.all_distinct_values[0]) == float or type(self.all_distinct_values[0]) == int:
if np.isnan(self.all_distinct_values[0]):
return np.nan
inds = np.where(self.all_distinct_values == val)
assert len(inds[0]) > 0, (val, type(val))
return inds[0][0]
def SetDistribution(self, distinct_values, distinct_values_with_counts):
"""This is all the values this column will ever see."""
assert self.all_distinct_values is None
# pd.isnull returns true for both np.nan and np.datetime64('NaT').
is_nan = pd.isnull(distinct_values)
contains_nan = np.any(is_nan)
dv_no_nan = distinct_values[~is_nan]
# NOTE: np.sort puts NaT values at beginning, and NaN values at end.
# For our purposes we always add any null value to the beginning.
vs = np.sort(np.unique(dv_no_nan))
if contains_nan and np.issubdtype(distinct_values.dtype, np.datetime64):
vs = np.insert(vs, 0, np.datetime64('NaT'))
elif contains_nan:
vs = np.insert(vs, 0, np.nan)
if self.distribution_size is not None:
assert len(vs) == self.distribution_size
self.all_distinct_values = vs
self.distribution_size = len(vs)
self.all_distinct_values_with_counts = distinct_values_with_counts.sort_index()
return self
def Fill(self, data_instance, infer_dist=False):
assert self.data is None
self.data = data_instance
# If no distribution is currently specified, then infer distinct values
# from data.
if infer_dist:
self.SetDistribution(self.data, None)
return self
def __repr__(self):
return 'Column({}, distribution_size={})'.format(
self.name, self.distribution_size)
class Table(object):
"""A collection of Columns."""
def __init__(self, name, columns, pg_name=None):
"""Creates a Table.
Args:
name: Name of this table object.
columns: List of Column instances to populate this table.
pg_name: name of the corresponding table in Postgres.
"""
self.name = name
self.cardinality = self._validate_cardinality(columns)
self.columns = columns
self.val_to_bin_funcs = [c.ValToBin for c in columns]
self.name_to_index = {c.Name(): i for i, c in enumerate(self.columns)}
if pg_name:
self.pg_name = pg_name
else:
self.pg_name = name
def __repr__(self):
return '{}({})'.format(self.name, self.columns)
def _validate_cardinality(self, columns):
"""Checks that all the columns have same the number of rows."""
cards = [len(c.data) for c in columns]
c = np.unique(cards)
assert len(c) == 1, c
return c[0]
def Name(self):
"""Name of this table."""
return self.name
def Columns(self):
"""Return the list of Columns under this table."""
return self.columns
def ColumnIndex(self, name):
"""Returns index of column with the specified name."""
assert name in self.name_to_index
return self.name_to_index[name]
class CsvTable(Table):
"""Wraps a CSV file or pd.DataFrame as a Table."""
def __init__(self,
name,
filename_or_df,
cols,
type_casts={},
pg_name=None,
pg_cols=None,
**kwargs):
"""Accepts the same arguments as pd.read_csv().
Args:
filename_or_df: pass in str to reload; otherwise accepts a loaded
pd.Dataframe.
cols: list of column names to load; can be a subset of all columns.
type_casts: optional, dict mapping column name to the desired numpy
datatype.
pg_name: optional str, a convenient field for specifying what name
this table holds in a Postgres database.
pg_cols: optional list of str, a convenient field for specifying
what names this table's columns hold in a Postgres database.
**kwargs: keyword arguments that will be pass to pd.read_csv().
"""
self.name = name
self.pg_name = pg_name
if isinstance(filename_or_df, str):
self.data = self._load(filename_or_df, cols, **kwargs)
else:
assert (isinstance(filename_or_df, pd.DataFrame))
self.data = filename_or_df
self.columns = self._build_columns(self.data, cols, type_casts, pg_cols)
self.data_cardinality_list = []
self._buil_cardinality_list(self.data)
super(CsvTable, self).__init__(name, self.columns, pg_name)
def _load(self, filename, cols, **kwargs):
print('Loading csv...', end=' ')
s = time.time()
if self.name == "Census":
df = pd.read_csv(filename, usecols=cols, **kwargs)
if self.name == "DMV":
df = pd.read_csv(filename, header=0, names=cols, **kwargs)
if cols is not None:
df = df[cols]
print('done, took {:.1f}s'.format(time.time() - s))
return df
def _build_columns(self, data, cols, type_casts, pg_cols):
"""Example args:
cols = ['Model Year', 'Reg Valid Date', 'Reg Expiration Date']
type_casts = {'Model Year': int}
Returns: a list of Columns.
"""
print('Parsing...', end=' ')
s = time.time()
for col, typ in type_casts.items():
if col not in data:
continue
if typ != np.datetime64:
data[col] = data[col].astype(typ, copy=False)
else:
# Both infer_datetime_format and cache are critical for perf.
data[col] = pd.to_datetime(data[col],
infer_datetime_format=True,
cache=True)
# Discretize & create Columns.
if cols is None:
cols = data.columns
columns = []
if pg_cols is None:
pg_cols = [None] * len(cols)
for c, p in zip(cols, pg_cols):
col = Column(c, pg_name=p)
col.Fill(data[c])
# dropna=False so that if NA/NaN is present in data,
# all_distinct_values will capture it.
#
# For numeric: np.nan
# For datetime: np.datetime64('NaT')
col.SetDistribution(data[c].value_counts(dropna=False).index.values, data[c].value_counts(dropna=False))
columns.append(col)
print('done, took {:.1f}s'.format(time.time() - s))
return columns
def _buil_cardinality_list(self, data):
data_cardinality_list = data.groupby(data.columns.tolist())[data.columns.tolist()[0]].transform('size')
self.data_cardinality_list = (np.array(data_cardinality_list)/len(self.columns[0].data))
class TableDataset(data.Dataset):
"""Wraps a Table and yields each row as a PyTorch Dataset element."""
def __init__(self, table):
super(TableDataset, self).__init__()
self.table = copy.deepcopy(table)
print('Discretizing table...', end=' ')
s = time.time()
# [cardianlity, num cols].
self.tuples_np = np.stack(
[self.Discretize(c) for c in self.table.Columns()], axis=1)
self.tuples = torch.as_tensor(
self.tuples_np.astype(np.float32, copy=False))
print('done, took {:.1f}s'.format(time.time() - s))
def Discretize(self, col):
"""Discretize values into its Column's bins.
Args:
col: the Column.
Returns:
col_data: discretized version; an np.ndarray of type np.int32.
"""
return Discretize(col)
def size(self):
return len(self.tuples)
def __len__(self):
return len(self.tuples)
def __getitem__(self, idx):
return (idx, self.tuples[idx])
def Discretize(col, data=None):
"""Transforms data values into integers using a Column's vocab.
Args:
col: the Column.
data: list-like data to be discretized. If None, defaults to col.data.
Returns:
col_data: discretized version; an np.ndarray of type np.int32.
"""
# pd.Categorical() does not allow categories be passed in an array
# containing np.nan. It makes it a special case to return code -1
# for NaN values.
if data is None:
data = col.data
# pd.isnull returns true for both np.nan and np.datetime64('NaT').
isnan = pd.isnull(col.all_distinct_values)
if isnan.any():
# We always add nan or nat to the beginning.
assert isnan.sum() == 1, isnan
assert isnan[0], isnan
dvs = col.all_distinct_values[1:]
bin_ids = pd.Categorical(data, categories=dvs).codes
assert len(bin_ids) == len(data)
# Since nan/nat bin_id is supposed to be 0 but pandas returns -1, just
# add 1 to everybody
bin_ids = bin_ids + 1
else:
# This column has no nan or nat values.
dvs = col.all_distinct_values
bin_ids = pd.Categorical(data, categories=dvs).codes
assert len(bin_ids) == len(data), (len(bin_ids), len(data))
bin_ids = bin_ids.astype(np.int32, copy=False)
assert (bin_ids >= 0).all(), (col, data, bin_ids)
return bin_ids
class QueryDataset(data.Dataset):
"""Wraps a Table and yields each row as a PyTorch Dataset element."""
def __init__(self, valid_i_list):
super(QueryDataset, self).__init__()
self.valid_i_list = copy.deepcopy(valid_i_list)
print('Handling queries...', end=' ')
s = time.time()
print('done, took {:.1f}s'.format(time.time() - s))
def size(self):
return len(self.valid_i_list)
def __len__(self):
return len(self.valid_i_list)
def __getitem__(self, idx):
return (idx, self.valid_i_list[idx])