Skip to content

Commit 638065d

Browse files
committed
examples for keep and to_array in style of broadcast as well as usage
1 parent 7f90933 commit 638065d

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

bayesflow/adapters/transforms/keep.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111

1212
@serializable(package="bayesflow.adapters")
1313
class Keep(Transform):
14-
'''
14+
"""
1515
Name the data parameters that should be kept for futher calculation.
1616
1717
Parameters:
1818
1919
cls: tuple containing the names of kept data variables as strings.
2020
21-
Example:
21+
Example 1:
22+
2223
Two moons simulator generates data for priors alpha, r and theta as well as observation data x.
2324
We are interested only in theta and x, to keep only theta and x we should use the following;
2425
@@ -30,8 +31,20 @@ class Keep(Transform):
3031
3132
)
3233
34+
Example 2:
35+
36+
>>> a = [1,2,3,4]
37+
>>> b = [[1,2],[3,4]]
38+
>>> c = [[5,6,7,8]]
39+
>>> dat = dict(a=a,b=b,c =c)
40+
# Here we want to only keep elements b and c
41+
>>> keeper = bf.adapters.transforms.Keep(("b","c"))
42+
>>> keeper.forward(dat)
43+
{'b': [[1, 2], [3, 4]], 'c': [[5, 6, 7, 8]]}
44+
45+
3346
34-
'''
47+
"""
3548
def __init__(self, keys: Sequence[str]):
3649
self.keys = keys
3750

bayesflow/adapters/transforms/to_array.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@
1111

1212
@serializable(package="bayesflow.adapters")
1313
class ToArray(ElementwiseTransform):
14+
"""
15+
Checks provided data for any non-arrays and converts them to numpy arrays.
16+
This ensures all data is in a format suitable for training.
17+
18+
Example:
19+
>>> ta = bf.adapters.transforms.ToArray()
20+
>>> a = [1,2,3,4]
21+
>>> ta.forward(a)
22+
array([1, 2, 3, 4])
23+
>>> b = [[1,2],[3,4]]
24+
>>> ta.forward(b)
25+
array([[1, 2],
26+
[3, 4]])
27+
"""
28+
29+
1430
def __init__(self):
1531
super().__init__()
1632
self.original_type = None

0 commit comments

Comments
 (0)