-
Notifications
You must be signed in to change notification settings - Fork 335
Expand file tree
/
Copy pathtest_ctgan.py
More file actions
197 lines (140 loc) · 5.23 KB
/
Copy pathtest_ctgan.py
File metadata and controls
197 lines (140 loc) · 5.23 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Integration tests for ctgan.
These tests only ensure that the software does not crash and that
the API works as expected in terms of input and output data formats,
but correctness of the data values and the internal behavior of the
model are not checked.
"""
import tempfile as tf
import numpy as np
import pandas as pd
import pytest
from ctgan.synthesizers.ctgan import CTGANSynthesizer
def test_ctgan_no_categoricals():
data = pd.DataFrame({
'continuous': np.random.random(1000)
})
ctgan = CTGANSynthesizer(epochs=1)
ctgan.fit(data, [])
sampled = ctgan.sample(100)
assert sampled.shape == (100, 1)
assert isinstance(sampled, pd.DataFrame)
assert set(sampled.columns) == {'continuous'}
def test_ctgan_dataframe():
data = pd.DataFrame({
'continuous': np.random.random(100),
'discrete': np.random.choice(['a', 'b', 'c'], 100)
})
discrete_columns = ['discrete']
ctgan = CTGANSynthesizer(epochs=1)
ctgan.fit(data, discrete_columns)
sampled = ctgan.sample(100)
assert sampled.shape == (100, 2)
assert isinstance(sampled, pd.DataFrame)
assert set(sampled.columns) == {'continuous', 'discrete'}
assert set(sampled['discrete'].unique()) == {'a', 'b', 'c'}
def test_ctgan_numpy():
data = pd.DataFrame({
'continuous': np.random.random(100),
'discrete': np.random.choice(['a', 'b', 'c'], 100)
})
discrete_columns = [1]
ctgan = CTGANSynthesizer(epochs=1)
ctgan.fit(data.values, discrete_columns)
sampled = ctgan.sample(100)
assert sampled.shape == (100, 2)
assert isinstance(sampled, np.ndarray)
assert set(np.unique(sampled[:, 1])) == {'a', 'b', 'c'}
def test_log_frequency():
data = pd.DataFrame({
'continuous': np.random.random(1000),
'discrete': np.repeat(['a', 'b', 'c'], [950, 25, 25])
})
discrete_columns = ['discrete']
ctgan = CTGANSynthesizer(epochs=100)
ctgan.fit(data, discrete_columns)
sampled = ctgan.sample(10000)
counts = sampled['discrete'].value_counts()
assert counts['a'] < 6500
ctgan = CTGANSynthesizer(log_frequency=False, epochs=100)
ctgan.fit(data, discrete_columns)
sampled = ctgan.sample(10000)
counts = sampled['discrete'].value_counts()
assert counts['a'] > 9000
def test_categorical_nan():
data = pd.DataFrame({
'continuous': np.random.random(30),
# This must be a list (not a np.array) or NaN will be cast to a string.
'discrete': [np.nan, 'b', 'c'] * 10
})
discrete_columns = ['discrete']
ctgan = CTGANSynthesizer(epochs=1)
ctgan.fit(data, discrete_columns)
sampled = ctgan.sample(100)
assert sampled.shape == (100, 2)
assert isinstance(sampled, pd.DataFrame)
assert set(sampled.columns) == {'continuous', 'discrete'}
# since np.nan != np.nan, we need to be careful here
values = set(sampled['discrete'].unique())
assert len(values) == 3
assert any(pd.isnull(x) for x in values)
assert {"b", "c"}.issubset(values)
def test_synthesizer_sample():
data = pd.DataFrame({
'discrete': np.random.choice(['a', 'b', 'c'], 100)
})
discrete_columns = ['discrete']
ctgan = CTGANSynthesizer(epochs=1)
ctgan.fit(data, discrete_columns)
samples = ctgan.sample(1000, 'discrete', 'a')
assert isinstance(samples, pd.DataFrame)
def test_save_load():
data = pd.DataFrame({
'continuous': np.random.random(100),
'discrete': np.random.choice(['a', 'b', 'c'], 100)
})
discrete_columns = ['discrete']
ctgan = CTGANSynthesizer(epochs=1)
ctgan.fit(data, discrete_columns)
with tf.TemporaryDirectory() as temporary_directory:
ctgan.save(temporary_directory + "test_tvae.pkl")
ctgan = CTGANSynthesizer.load(temporary_directory + "test_tvae.pkl")
sampled = ctgan.sample(1000)
assert set(sampled.columns) == {'continuous', 'discrete'}
assert set(sampled['discrete'].unique()) == {'a', 'b', 'c'}
def test_wrong_discrete_columns_dataframe():
data = pd.DataFrame({
'discrete': ['a', 'b']
})
discrete_columns = ['b', 'c']
ctgan = CTGANSynthesizer(epochs=1)
with pytest.raises(ValueError):
ctgan.fit(data, discrete_columns)
def test_wrong_discrete_columns_numpy():
data = pd.DataFrame({
'discrete': ['a', 'b']
})
discrete_columns = [0, 1]
ctgan = CTGANSynthesizer(epochs=1)
with pytest.raises(ValueError):
ctgan.fit(data.to_numpy(), discrete_columns)
def test_wrong_sampling_conditions():
data = pd.DataFrame({
'continuous': np.random.random(100),
'discrete': np.random.choice(['a', 'b', 'c'], 100)
})
discrete_columns = ['discrete']
ctgan = CTGANSynthesizer(epochs=1)
ctgan.fit(data, discrete_columns)
with pytest.raises(ValueError):
ctgan.sample(1, 'cardinal', "doesn't matter")
with pytest.raises(ValueError):
ctgan.sample(1, 'discrete', "d")
def test_ctgan_data_transformer_params():
data = pd.DataFrame({
'continuous': np.random.random(1000)
})
ctgan = CTGANSynthesizer(epochs=1)
ctgan.fit(data, [], data_transformer_params={'max_gm_samples': 100})
assert ctgan._transformer._max_gm_samples == 100