Skip to content

Commit 21cd4fe

Browse files
committed
add to_dict tests
1 parent 49174e0 commit 21cd4fe

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

tests/test_adapters/test_adapters.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import numpy as np
66
import pytest
77

8+
import bayesflow as bf
9+
810

911
def test_cycle_consistency(adapter, random_data):
1012
processed = adapter(random_data)
@@ -192,3 +194,41 @@ def test_split_transform(adapter, random_data):
192194

193195
assert "split_2" in processed
194196
assert processed["split_2"].shape == target_shape
197+
198+
199+
def test_to_dict_transform():
200+
import pandas as pd
201+
202+
data = {
203+
"int32": [1, 2, 3, 4, 5],
204+
"int64": [1, 2, 3, 4, 5],
205+
"float32": [1.0, 2.0, 3.0, 4.0, 5.0],
206+
"float64": [1.0, 2.0, 3.0, 4.0, 5.0],
207+
"object": ["a", "b", "c", "d", "e"],
208+
"category": ["one", "two", "three", "four", "five"],
209+
}
210+
211+
df = pd.DataFrame(data)
212+
df["int32"] = df["int32"].astype("int32")
213+
df["int64"] = df["int64"].astype("int64")
214+
df["float32"] = df["float32"].astype("float32")
215+
df["float64"] = df["float64"].astype("float64")
216+
df["object"] = df["object"].astype("object")
217+
df["category"] = df["category"].astype("category")
218+
219+
ad = bf.Adapter().to_dict()
220+
221+
# drop one element to simulate non-complete data
222+
batch = df.iloc[:-1]
223+
224+
processed = ad(batch)
225+
226+
assert isinstance(processed, dict)
227+
assert list(processed.keys()) == ["int32", "int64", "float32", "float64", "object", "category"]
228+
229+
for key, value in processed.items():
230+
assert isinstance(value, np.ndarray)
231+
assert value.dtype == "float32"
232+
233+
# category should have 5 one-hot categories, even though it was only passed 4
234+
assert processed["category"].shape[-1] == 5

0 commit comments

Comments
 (0)