Skip to content

Commit 6fb8b83

Browse files
Merge pull request #263 from eodole/dev
Add Docs to Adapter Transforms
2 parents 94239ae + b8b6875 commit 6fb8b83

File tree

6 files changed

+108
-1
lines changed

6 files changed

+108
-1
lines changed

bayesflow/adapters/transforms/as_set.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@
44

55

66
class AsSet(ElementwiseTransform):
7+
"""
8+
The `.as_set(["x", "y"])` transform indicates that both `x` and `y` are treated as sets.
9+
That is, their values will be treated as *exchangable* such that they will imply
10+
the same inference regardless of the values' order.
11+
This is useful, for example, in a linear regression context where we can index
12+
the observations in arbitrary order and always get the same regression line.
13+
14+
Useage:
15+
16+
adapter = (
17+
bf.Adapter()
18+
.as_set(["x", "y"])
19+
)
20+
"""
21+
722
def forward(self, data: np.ndarray, **kwargs) -> np.ndarray:
823
return np.atleast_3d(data)
924

bayesflow/adapters/transforms/concatenate.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212

1313
@serializable(package="bayesflow.adapters")
1414
class Concatenate(Transform):
15-
"""Concatenate multiple arrays into a new key."""
15+
"""Concatenate multiple arrays into a new key.
16+
Parameters:
17+
18+
keys:
19+
20+
into:
21+
22+
"""
1623

1724
def __init__(self, keys: Sequence[str], *, into: str, axis: int = -1):
1825
self.keys = keys

bayesflow/adapters/transforms/constrain.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,42 @@
1515

1616
@serializable(package="bayesflow.adapters")
1717
class Constrain(ElementwiseTransform):
18+
"""
19+
Constrains neural network predictions of a data variable to specificied bounds.
20+
21+
Parameters:
22+
String containing the name of the data variable to be transformed e.g. "sigma". See examples below.
23+
24+
Named Parameters:
25+
lower: Lower bound for named data variable.
26+
upper: Upper bound for named data variable.
27+
method: Method by which to shrink the network predictions space to specified bounds. Choose from
28+
- Double bounded methods: sigmoid, expit, (default = sigmoid)
29+
- Lower bound only methods: softplus, exp, (default = softplus)
30+
- Upper bound only methods: softplus, exp, (default = softplus)
31+
32+
33+
34+
Examples:
35+
Let sigma be the standard deviation of a normal distribution,
36+
then sigma should always be greater than zero.
37+
38+
Useage:
39+
adapter = (
40+
bf.Adapter()
41+
.constrain("sigma", lower=0)
42+
)
43+
44+
Suppose p is the parameter for a binomial distribution where p must be in [0,1]
45+
then we would constrain the neural network to estimate p in the following way.
46+
47+
Usage:
48+
adapter = (
49+
bf.Adapter()
50+
.constrain("p", lower=0, upper=1, method = "sigmoid")
51+
)
52+
"""
53+
1854
def __init__(
1955
self, *, lower: int | float | np.ndarray = None, upper: int | float | np.ndarray = None, method: str = "default"
2056
):

bayesflow/adapters/transforms/convert_dtype.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
@serializable(package="bayesflow.adapters")
1212
class ConvertDType(ElementwiseTransform):
13+
"""
14+
Default transform used to convert all floats from float64 to float32 to be in line with keras framework.
15+
"""
16+
1317
def __init__(self, from_dtype: str, to_dtype: str):
1418
super().__init__()
1519

bayesflow/adapters/transforms/keep.py

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

1212
@serializable(package="bayesflow.adapters")
1313
class Keep(Transform):
14+
"""
15+
Name the data parameters that should be kept for futher calculation.
16+
17+
Parameters:
18+
19+
cls: tuple containing the names of kept data variables as strings.
20+
21+
Useage:
22+
23+
Two moons simulator generates data for priors alpha, r and theta as well as observation data x.
24+
We are interested only in theta and x, to keep only theta and x we should use the following;
25+
26+
adapter = (
27+
bf.adapters.Adapter()
28+
# only keep theta and x
29+
.keep(("theta", "x"))
30+
)
31+
32+
Example:
33+
>>> a = [1, 2, 3, 4]
34+
>>> b = [[1, 2], [3, 4]]
35+
>>> c = [[5, 6, 7, 8]]
36+
>>> dat = dict(a=a, b=b, c=c)
37+
# Here we want to only keep elements b and c
38+
>>> keeper = bf.adapters.transforms.Keep(("b", "c"))
39+
>>> keeper.forward(dat)
40+
{'b': [[1, 2], [3, 4]], 'c': [[5, 6, 7, 8]]}
41+
42+
"""
43+
1444
def __init__(self, keys: Sequence[str]):
1545
self.keys = keys
1646

bayesflow/adapters/transforms/to_array.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@
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+
1429
def __init__(self):
1530
super().__init__()
1631
self.original_type = None

0 commit comments

Comments
 (0)