|
5 | 5 |
|
6 | 6 | from bayesflow.utils.serialization import deserialize, serialize
|
7 | 7 |
|
| 8 | +import bayesflow as bf |
| 9 | + |
8 | 10 |
|
9 | 11 | def test_cycle_consistency(adapter, random_data):
|
10 | 12 | processed = adapter(random_data)
|
@@ -190,3 +192,41 @@ def test_split_transform(adapter, random_data):
|
190 | 192 |
|
191 | 193 | assert "split_2" in processed
|
192 | 194 | assert processed["split_2"].shape == target_shape
|
| 195 | + |
| 196 | + |
| 197 | +def test_to_dict_transform(): |
| 198 | + import pandas as pd |
| 199 | + |
| 200 | + data = { |
| 201 | + "int32": [1, 2, 3, 4, 5], |
| 202 | + "int64": [1, 2, 3, 4, 5], |
| 203 | + "float32": [1.0, 2.0, 3.0, 4.0, 5.0], |
| 204 | + "float64": [1.0, 2.0, 3.0, 4.0, 5.0], |
| 205 | + "object": ["a", "b", "c", "d", "e"], |
| 206 | + "category": ["one", "two", "three", "four", "five"], |
| 207 | + } |
| 208 | + |
| 209 | + df = pd.DataFrame(data) |
| 210 | + df["int32"] = df["int32"].astype("int32") |
| 211 | + df["int64"] = df["int64"].astype("int64") |
| 212 | + df["float32"] = df["float32"].astype("float32") |
| 213 | + df["float64"] = df["float64"].astype("float64") |
| 214 | + df["object"] = df["object"].astype("object") |
| 215 | + df["category"] = df["category"].astype("category") |
| 216 | + |
| 217 | + ad = bf.Adapter().to_dict() |
| 218 | + |
| 219 | + # drop one element to simulate non-complete data |
| 220 | + batch = df.iloc[:-1] |
| 221 | + |
| 222 | + processed = ad(batch) |
| 223 | + |
| 224 | + assert isinstance(processed, dict) |
| 225 | + assert list(processed.keys()) == ["int32", "int64", "float32", "float64", "object", "category"] |
| 226 | + |
| 227 | + for key, value in processed.items(): |
| 228 | + assert isinstance(value, np.ndarray) |
| 229 | + assert value.dtype == "float32" |
| 230 | + |
| 231 | + # category should have 5 one-hot categories, even though it was only passed 4 |
| 232 | + assert processed["category"].shape[-1] == 5 |
0 commit comments