-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseline_utils.py
230 lines (202 loc) · 6.95 KB
/
baseline_utils.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
import pandas as pd
import os
import random
import numpy as np
from sdv.metadata import SingleTableMetadata
from sdv.metadata import MultiTableMetadata
from sdv.single_table import CTGANSynthesizer
from smote.sample_smote import sample_smote_baseline
def get_group_sizes(child_df, foreign_key):
group_sizes = {}
for group, group_df in child_df.groupby(foreign_key):
group_sizes[group] = len(group_df)
return group_sizes
def get_group_size_prob(group_size_dict):
freqs = {}
for _, freq in group_size_dict.items():
if freq not in freqs:
freqs[freq] = 0
freqs[freq] += 1
probs = {}
for freq, count in freqs.items():
probs[freq] = count / len(group_size_dict)
return probs
def get_multi_metadata(tables, relation_order):
metadata = MultiTableMetadata()
for table_name, val in tables.items():
df = val['original_df']
metadata.detect_table_from_dataframe(
table_name,
df
)
id_cols = [col for col in df.columns if '_id' in col]
for id_col in id_cols:
metadata.update_column(
table_name=table_name,
column_name=id_col,
sdtype='id'
)
domain = tables[table_name]['domain']
for col, dom in domain.items():
if col in df.columns:
if dom['type'] == 'discrete':
metadata.update_column(
table_name=table_name,
column_name=col,
sdtype='categorical',
)
elif dom['type'] == 'continuous':
metadata.update_column(
table_name=table_name,
column_name=col,
sdtype='numerical',
)
else:
raise ValueError(f'Unknown domain type: {dom["type"]}')
metadata.set_primary_key(
table_name=table_name,
column_name=f'{table_name}_id'
)
for parent, child in relation_order:
if parent is not None:
metadata.add_relationship(
parent_table_name=parent,
child_table_name=child,
parent_primary_key=f'{parent}_id',
child_foreign_key=f'{parent}_id'
)
return metadata
def get_merged_metadata(merged_df, parent_domain_dict, child_domain_dict):
metadata = SingleTableMetadata()
df_without_ids = merged_df.drop(columns=[col for col in merged_df.columns if '_id' in col])
metadata.detect_from_dataframe(df_without_ids)
for col in df_without_ids.columns:
domain_dict = None
if col in parent_domain_dict:
domain_dict = parent_domain_dict
elif col in child_domain_dict:
domain_dict = child_domain_dict
if domain_dict is not None:
if domain_dict[col]['type'] == 'discrete':
if domain_dict[col]['size'] < 1000:
metadata.update_column(
column_name=col,
sdtype='categorical',
)
else:
metadata.update_column(
column_name=col,
sdtype='numerical',
)
else:
metadata.update_column(
column_name=col,
sdtype='numerical',
)
metadata.remove_primary_key()
return metadata
def get_metadata(df, domain_dict=None):
metadata = SingleTableMetadata()
df_without_ids = df.drop(columns=[col for col in df.columns if '_id' in col])
metadata.detect_from_dataframe(df_without_ids)
if domain_dict is not None:
for col in df_without_ids.columns:
if domain_dict[col]['type'] == 'discrete':
if domain_dict[col]['size'] < 1000:
metadata.update_column(
column_name=col,
sdtype='categorical',
)
else:
metadata.update_column(
column_name=col,
sdtype='numerical',
)
else:
metadata.update_column(
column_name=col,
sdtype='numerical',
)
metadata.remove_primary_key()
return metadata, df_without_ids
def train_ctgan(df, domain_dict, batch_size):
metadata, df_without_ids = get_metadata(df, domain_dict)
synthesizer = CTGANSynthesizer(metadata, batch_size=batch_size, verbose=True)
synthesizer.fit(df_without_ids)
synthetic_data = synthesizer.sample(num_rows=len(df_without_ids))
return synthetic_data
def baseline_load_synthetic_data(path, tables):
syn = {}
for table, val in tables.items():
syn[table] = {}
syn[table]['df'] = pd.read_csv(os.path.join(
path,
'final',
f'{table}_synthetic.csv'
))
syn[table]['domain'] = val['domain']
return syn
def lava_load_synthetic_data(path, tables):
syn = {}
for table, val in tables.items():
syn[table] = {}
syn[table]['df'] = pd.read_csv(os.path.join(
path,
table,
'_final',
f'{table}_synthetic.csv'
))
syn[table]['domain'] = val['domain']
return syn
def sdv_load_synthetic_data(path, tables):
syn = {}
for table, val in tables.items():
syn[table] = {}
syn[table]['df'] = pd.read_csv(os.path.join(
path,
f'{table}.csv'
))
syn[table]['domain'] = val['domain']
return syn
def get_smote_res(df, domain_dict):
id_cols = [col for col in df.columns if '_id' in col]
df_no_id = df.drop(columns=id_cols)
num_cols = []
cat_cols = []
for col, val in domain_dict.items():
if val['type'] == 'discrete':
cat_cols.append(col)
else:
num_cols.append(col)
all_cols = num_cols + cat_cols
y_col = random.choice(all_cols)
if y_col in num_cols:
num_cols.remove(y_col)
is_regression = True
else:
cat_cols.remove(y_col)
is_regression = False
X_num = {}
X_num['train'] = df[num_cols].values
X_cat = {}
X_cat['train'] = df[cat_cols].values
y = {}
y['train'] = df[y_col].values
syn_x_num, syn_x_cat, res_y = sample_smote_baseline(
'smote_res',
X_num,
X_cat,
y,
eval_type = "synthetic",
k_neighbours = 5,
frac_samples = 1.0,
frac_lam_del = 0.0,
change_val = False,
save = False,
seed = 0,
is_regression=is_regression
)
res = np.concatenate((syn_x_num, syn_x_cat, res_y.reshape((-1, 1))), axis=1)
res_df = pd.DataFrame(res, columns=num_cols + cat_cols + [y_col])
res_df = res_df[df_no_id.columns]
return res_df, cat_cols, num_cols, y_col