-
Notifications
You must be signed in to change notification settings - Fork 4
/
dataset_census_ft.py
240 lines (199 loc) · 8.76 KB
/
dataset_census_ft.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
# This script is for generating .pk file for mixed data types dataset
import pickle
import yaml
import os
import math
import re
import numpy as np
import pandas as pd
import category_encoders as ce
from torch.utils.data import DataLoader, Dataset
def process_func(path: str, cat_list, missing_ratio=0.1, encode=True):
data = pd.read_csv(path, header=None)
# Swap columns
temp_list = [i for i in range(data.shape[1]) if i not in cat_list]
temp_list.extend(cat_list)
new_cols_order = temp_list
data = data.reindex(columns=data.columns[new_cols_order])
data.columns = [i for i in range(data.shape[1])]
# create two lists to store position
cont_list = [i for i in range(0, data.shape[1] - len(cat_list))]
cat_list = [i for i in range(len(cont_list), data.shape[1])]
observed_values = data.values
observed_masks = ~pd.isnull(data)
observed_masks = observed_masks.values
masks = observed_masks.copy()
# In this section, obtain gt_masks
# for each column, mask `missing_ratio` % of observed values.
for col in range(masks.shape[1]):
obs_indices = np.where(masks[:, col])[0]
miss_indices = np.random.choice(
obs_indices, (int)(len(obs_indices) * missing_ratio), replace=False
)
masks[miss_indices, col] = False
# gt_mask: 0 for missing elements and manully maksed elements
gt_masks = masks.reshape(observed_masks.shape)
num_cate_list = []
if encode == True:
# set encoder here
encoder = ce.ordinal.OrdinalEncoder(cols=data.columns[cat_list])
encoder.fit(data)
new_df = encoder.transform(data)
# we now need to transform these masks to the new one, suitable for mixed data types.
cum_num_bits = 0
new_observed_masks = observed_masks.copy()
new_gt_masks = gt_masks.copy()
for index, col in enumerate(cat_list):
num_cate_list.append(new_df.iloc[:, col].nunique())
corresponding_cols = len(
[
s
for s in new_df.columns
if isinstance(s, str) and s.startswith(str(col) + "_")
]
)
add_col_num = corresponding_cols
insert_col_obs = observed_masks[:, col]
insert_col_gt = gt_masks[:, col]
for i in range(add_col_num - 1):
new_observed_masks = np.insert(
new_observed_masks, cum_num_bits + col, insert_col_obs, axis=1
)
new_gt_masks = np.insert(
new_gt_masks, cum_num_bits + col, insert_col_gt, axis=1
)
cum_num_bits += add_col_num - 1
new_observed_values = new_df.values
new_observed_values = np.nan_to_num(new_observed_values)
new_observed_values = new_observed_values.astype(np.float)
with open("./data_census_ft/transformed_columns.pk", "wb") as f:
pickle.dump([cont_list, num_cate_list], f)
with open("./data_census_ft/encoder.pk", "wb") as f:
pickle.dump(encoder, f)
if encode == True:
return new_observed_values, new_observed_masks, new_gt_masks, cont_list
else:
cont_cols = [i for i in data.columns if i not in cat_list]
return observed_values, observed_masks, gt_masks, cont_list
class tabular_Dataset(Dataset):
# eval_length should be equal to attributes number.
def __init__(self, eval_length=15, use_index_list=None, missing_ratio=0.1, seed=0):
self.eval_length = eval_length
np.random.seed(seed)
dataset_path = "./data_census_ft/adult_trim.data"
processed_data_path = (
f"./data_census_ft/missing_ratio-{missing_ratio}_seed-{seed}.pk"
)
processed_data_path_norm = f"./data_census_ft/missing_ratio-{missing_ratio}_seed-{seed}_max-min_norm.pk"
# self.cont_cols is only saved in .pk file before normalization.
cat_list = [1, 3, 5, 6, 7, 8, 9, 13, 14]
if not os.path.isfile(processed_data_path):
(
self.observed_values,
self.observed_masks,
self.gt_masks,
self.cont_cols,
) = process_func(
dataset_path,
cat_list=cat_list,
missing_ratio=missing_ratio,
encode=True,
)
with open(processed_data_path, "wb") as f:
pickle.dump(
[
self.observed_values,
self.observed_masks,
self.gt_masks,
self.cont_cols,
],
f,
)
print("--------Dataset created--------")
elif os.path.isfile(processed_data_path_norm): # load datasetfile
with open(processed_data_path_norm, "rb") as f:
self.observed_values, self.observed_masks, self.gt_masks = pickle.load(
f
)
print("--------Normalized dataset loaded--------")
if use_index_list is None:
self.use_index_list = np.arange(len(self.observed_values))
else:
self.use_index_list = use_index_list
def __getitem__(self, org_index):
index = self.use_index_list[org_index]
s = {
"observed_data": self.observed_values[index],
"observed_mask": self.observed_masks[index],
"gt_mask": self.gt_masks[index],
"timepoints": np.arange(self.eval_length),
}
return s
def __len__(self):
return len(self.use_index_list)
def get_dataloader(seed=1, nfold=5, batch_size=16, missing_ratio=0.1):
dataset = tabular_Dataset(missing_ratio=missing_ratio, seed=seed)
print(f"Dataset size:{len(dataset)} entries")
indlist = np.arange(len(dataset))
np.random.seed(seed + 1)
np.random.shuffle(indlist)
tmp_ratio = 1 / nfold
start = (int)((nfold - 1) * len(dataset) * tmp_ratio)
end = (int)(nfold * len(dataset) * tmp_ratio)
test_index = indlist[start:end]
remain_index = np.delete(indlist, np.arange(start, end))
np.random.shuffle(remain_index)
num_train = (int)(len(remain_index) * 1)
train_index = remain_index[:num_train]
valid_index = remain_index[num_train:]
# Here we perform max-min normalization.
processed_data_path_norm = (
f"./data_census_ft/missing_ratio-{missing_ratio}_seed-{seed}_max-min_norm.pk"
)
if not os.path.isfile(processed_data_path_norm):
print(
"--------------Dataset has not been normalized yet. Perform data normalization and store the mean value of each column.--------------"
)
# data transformation after train-test split.
col_num = len(dataset.cont_cols)
max_arr = np.zeros(col_num)
min_arr = np.zeros(col_num)
mean_arr = np.zeros(col_num)
for index, k in enumerate(dataset.cont_cols):
# Using observed_mask to avoid counting missing values (now represented as 0)
obs_ind = dataset.observed_masks[train_index, k].astype(bool)
temp = dataset.observed_values[train_index, k]
max_arr[index] = max(temp[obs_ind])
min_arr[index] = min(temp[obs_ind])
print(
f"--------------Max-value for cont-variable column {max_arr}--------------"
)
print(
f"--------------Min-value for cont-variable column {min_arr}--------------"
)
for index, k in enumerate(dataset.cont_cols):
dataset.observed_values[:, k] = (
(dataset.observed_values[:, k] - (min_arr[index] - 1))
/ (max_arr[index] - min_arr[index] + 1)
) * dataset.observed_masks[:, k]
with open(processed_data_path_norm, "wb") as f:
pickle.dump(
[dataset.observed_values, dataset.observed_masks, dataset.gt_masks], f
)
# Now the path exists, so the dataset object initialization performs data loading.
train_dataset = tabular_Dataset(
use_index_list=train_index, missing_ratio=missing_ratio, seed=seed
)
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=1)
valid_dataset = tabular_Dataset(
use_index_list=valid_index, missing_ratio=missing_ratio, seed=seed
)
valid_loader = DataLoader(valid_dataset, batch_size=batch_size, shuffle=0)
test_dataset = tabular_Dataset(
use_index_list=test_index, missing_ratio=missing_ratio, seed=seed
)
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=0)
print(f"Training dataset size: {len(train_dataset)}")
print(f"Validation dataset size: {len(valid_dataset)}")
print(f"Testing dataset size: {len(test_dataset)}")
return train_loader, valid_loader, test_loader