Skip to content

Commit 68bd41e

Browse files
committed
[no ci] docs: formatting, mostly in transforms
1 parent 2e8dba8 commit 68bd41e

24 files changed

+246
-179
lines changed

bayesflow/adapters/transforms/as_set.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55

66
class AsSet(ElementwiseTransform):
7-
"""
8-
The `.as_set(["x", "y"])` transform indicates that both `x` and `y` are treated as sets.
7+
"""The `.as_set(["x", "y"])` transform indicates that both `x` and `y` are treated as sets.
8+
99
That is, their values will be treated as *exchangable* such that they will imply
1010
the same inference regardless of the values' order.
1111
This is useful, for example, in a linear regression context where we can index
@@ -17,12 +17,9 @@ class AsSet(ElementwiseTransform):
1717
In the future, the transform will have more advanced behavior
1818
to better ensure the correct treatment of sets.
1919
20-
Useage:
21-
22-
adapter = (
23-
bf.Adapter()
24-
.as_set(["x", "y"])
25-
)
20+
Examples
21+
--------
22+
>>> adapter = bf.Adapter().as_set(["x", "y"])
2623
"""
2724

2825
def forward(self, data: np.ndarray, **kwargs) -> np.ndarray:

bayesflow/adapters/transforms/as_time_series.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,18 @@
44

55

66
class AsTimeSeries(ElementwiseTransform):
7-
"""
8-
The `.as_time_series` transform can be used to indicate that
9-
variables shall be treated as time series.
7+
"""The `.as_time_series` transform can be used to indicate that variables shall be treated as time series.
108
119
Currently, all this transformation does is to ensure that the variable
1210
arrays are at least 3D. The 2rd dimension is treated as the
1311
time series dimension and the 3rd dimension as the data dimension.
1412
In the future, the transform will have more advanced behavior
1513
to better ensure the correct treatment of time series data.
1614
17-
Useage:
15+
Examples
16+
--------
1817
19-
adapter = (
20-
bf.Adapter()
21-
.as_time_series(["x", "y"])
22-
)
18+
>>> adapter = bf.Adapter().as_time_series(["x", "y"])
2319
"""
2420

2521
def forward(self, data: np.ndarray, **kwargs) -> np.ndarray:

bayesflow/adapters/transforms/broadcast.py

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,50 @@ class Broadcast(Transform):
1515
"""
1616
Broadcasts arrays or scalars to the shape of a given other array.
1717
18-
Parameters:
19-
20-
expand: Where should new dimensions be added to match the number of dimensions in `to`?
21-
Can be "left", "right", or an integer or tuple containing the indices of the new dimensions.
22-
The latter is needed if we want to include a dimension in the middle, which will be required
23-
for more advanced cases. By default we expand left.
24-
25-
exclude: Which dimensions (of the dimensions after expansion) should retain their size,
26-
rather than being broadcasted to the corresponding dimension size of `to`?
27-
By default we exclude the last dimension (usually the data dimension) from broadcasting the size.
28-
29-
Examples:
30-
shape (1, ) array:
31-
>>> a = np.array((1,))
32-
shape (2, 3) array:
33-
>>> b = np.array([[1, 2, 3], [4, 5, 6]])
34-
shape (2, 2, 3) array:
35-
>>> c = np.array([[[1, 2, 3], [4, 5, 6]], [[4, 5, 6], [1, 2, 3]]])
36-
>>> dat = dict(a=a, b=b, c=c)
37-
38-
>>> bc = bf.adapters.transforms.Broadcast("a", to="b")
39-
>>> new_dat = bc.forward(dat)
40-
>>> new_dat["a"].shape
41-
(2, 1)
42-
43-
>>> bc = bf.adapters.transforms.Broadcast("a", to="b", exclude=None)
44-
>>> new_dat = bc.forward(dat)
45-
>>> new_dat["a"].shape
46-
(2, 3)
47-
48-
>>> bc = bf.adapters.transforms.Broadcast("b", to="c", expand=1)
49-
>>> new_dat = bc.forward(dat)
50-
>>> new_dat["b"].shape
51-
(2, 2, 3)
18+
Parameters
19+
----------
20+
keys: sequence of str,
21+
Input a list of strings, where the strings are the names of data variables.
22+
expand: str or int or tuple, optional
23+
Where should new dimensions be added to match the number of dimensions in `to`?
24+
Can be "left", "right", or an integer or tuple containing the indices of the new dimensions.
25+
The latter is needed if we want to include a dimension in the middle, which will be required
26+
for more advanced cases. By default we expand left.
27+
28+
exclude: int or tuple, optional
29+
Which dimensions (of the dimensions after expansion) should retain their size,
30+
rather than being broadcasted to the corresponding dimension size of `to`?
31+
By default we exclude the last dimension (usually the data dimension) from broadcasting the size.
32+
33+
Examples
34+
--------
35+
shape (1, ) array:
36+
37+
>>> a = np.array((1,))
38+
39+
shape (2, 3) array:
40+
41+
>>> b = np.array([[1, 2, 3], [4, 5, 6]])
42+
43+
shape (2, 2, 3) array:
44+
45+
>>> c = np.array([[[1, 2, 3], [4, 5, 6]], [[4, 5, 6], [1, 2, 3]]])
46+
47+
>>> dat = dict(a=a, b=b, c=c)
48+
>>> bc = bf.adapters.transforms.Broadcast("a", to="b")
49+
>>> new_dat = bc.forward(dat)
50+
>>> new_dat["a"].shape
51+
(2, 1)
52+
53+
>>> bc = bf.adapters.transforms.Broadcast("a", to="b", exclude=None)
54+
>>> new_dat = bc.forward(dat)
55+
>>> new_dat["a"].shape
56+
(2, 3)
57+
58+
>>> bc = bf.adapters.transforms.Broadcast("b", to="c", expand=1)
59+
>>> new_dat = bc.forward(dat)
60+
>>> new_dat["b"].shape
61+
(2, 2, 3)
5262
5363
It is recommended to precede this transform with a :class:`bayesflow.adapters.transforms.ToArray` transform.
5464
"""

bayesflow/adapters/transforms/concatenate.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,25 @@
1414
class Concatenate(Transform):
1515
"""Concatenate multiple arrays into a new key. Used to specify how data variables should be treated by the network.
1616
17-
Parameters:
18-
keys: Input a list of strings, where the strings are the names of data variables.
19-
into: A string telling the network how to use the variables named in keys.
20-
axis: integer specifing along which axis to concatonate the keys. The last axis is used by default.
21-
22-
Example:
17+
Parameters
18+
----------
19+
keys: sequence of str,
20+
Input a list of strings, where the strings are the names of data variables.
21+
into: str
22+
A string telling the network how to use the variables named in keys.
23+
axis: int, optional
24+
integer specifing along which axis to concatonate the keys. The last axis is used by default.
25+
26+
Examples
27+
--------
2328
Suppose you have a simulator that generates variables "beta" and "sigma" from priors and then observation
2429
variables "x" and "y". We can then use concatonate in the following way
2530
26-
adapter = (
31+
>>> adapter = (
2732
bf.Adapter()
2833
.concatenate(["beta", "sigma"], into="inference_variables")
2934
.concatenate(["x", "y"], into="summary_variables")
30-
)
35+
)
3136
"""
3237

3338
def __init__(self, keys: Sequence[str], *, into: str, axis: int = -1, _indices: list | None = None):

bayesflow/adapters/transforms/constrain.py

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,48 @@ class Constrain(ElementwiseTransform):
1818
"""
1919
Constrains neural network predictions of a data variable to specified bounds.
2020
21-
Parameters:
21+
Parameters
22+
----------
23+
* : str
2224
String containing the name of the data variable to be transformed e.g. "sigma". See examples below.
2325
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-
inclusive: Indicates which bounds are inclusive (or exclusive).
32-
- "both" (default): Both lower and upper bounds are inclusive.
33-
- "lower": Lower bound is inclusive, upper bound is exclusive.
34-
- "upper": Lower bound is exclusive, upper bound is inclusive.
35-
- "none": Both lower and upper bounds are exclusive.
36-
epsilon: Small value to ensure inclusive bounds are not violated.
37-
Current default is 1e-15 as this ensures finite outcomes
38-
with the default transformations applied to data exactly at the boundaries.
39-
40-
41-
Examples:
42-
1) Let sigma be the standard deviation of a normal distribution,
43-
then sigma should always be greater than zero.
44-
45-
Usage:
46-
adapter = (
47-
bf.Adapter()
48-
.constrain("sigma", lower=0)
49-
)
50-
51-
2 ) Suppose p is the parameter for a binomial distribution where p must be in
52-
[0,1] then we would constrain the neural network to estimate p in the following way.
53-
54-
Usage:
55-
>>> import bayesflow as bf
56-
>>> adapter = bf.Adapter()
57-
>>> adapter.constrain("p", lower=0, upper=1, method="sigmoid", inclusive="both")
26+
lower: int or float or np.darray, optional
27+
Lower bound for named data variable.
28+
upper: int or float or np.darray, optional
29+
Upper bound for named data variable.
30+
method: str, optional
31+
Method by which to shrink the network predictions space to specified bounds. Choose from
32+
- Double bounded methods: sigmoid, expit, (default = sigmoid)
33+
- Lower bound only methods: softplus, exp, (default = softplus)
34+
- Upper bound only methods: softplus, exp, (default = softplus)
35+
inclusive: {'both', 'lower', 'upper', 'none'}, optional
36+
Indicates which bounds are inclusive (or exclusive).
37+
- "both" (default): Both lower and upper bounds are inclusive.
38+
- "lower": Lower bound is inclusive, upper bound is exclusive.
39+
- "upper": Lower bound is exclusive, upper bound is inclusive.
40+
- "none": Both lower and upper bounds are exclusive.
41+
epsilon: float, optional
42+
Small value to ensure inclusive bounds are not violated.
43+
Current default is 1e-15 as this ensures finite outcomes
44+
with the default transformations applied to data exactly at the boundaries.
45+
46+
Examples
47+
--------
48+
49+
1) Let sigma be the standard deviation of a normal distribution,
50+
then sigma should always be greater than zero.
51+
52+
>>> adapter = (
53+
bf.Adapter()
54+
.constrain("sigma", lower=0)
55+
)
56+
57+
2) Suppose p is the parameter for a binomial distribution where p must be in
58+
[0,1] then we would constrain the neural network to estimate p in the following way.
59+
60+
>>> import bayesflow as bf
61+
>>> adapter = bf.Adapter()
62+
>>> adapter.constrain("p", lower=0, upper=1, method="sigmoid", inclusive="both")
5863
"""
5964

6065
def __init__(

bayesflow/adapters/transforms/convert_dtype.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
class ConvertDType(ElementwiseTransform):
1313
"""
1414
Default transform used to convert all floats from float64 to float32 to be in line with keras framework.
15+
16+
Parameters
17+
----------
18+
from_dtype : str
19+
Original dtype
20+
to_dtype : str
21+
Target dtype
1522
"""
1623

1724
def __init__(self, from_dtype: str, to_dtype: str):

bayesflow/adapters/transforms/drop.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ class Drop(Transform):
1414
"""
1515
Transform to drop variables from further calculation.
1616
17-
Parameters:
18-
keys: list of strings, containing names of data variables that should be dropped
17+
Parameters
18+
----------
19+
keys: sequence of str
20+
Names of data variables that should be dropped
1921
20-
Example:
22+
Examples
23+
--------
2124
2225
>>> import bayesflow as bf
2326
>>> a = [1, 2, 3, 4]

bayesflow/adapters/transforms/expand_dims.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,32 @@
1212
class ExpandDims(ElementwiseTransform):
1313
"""
1414
Expand the shape of an array.
15-
Examples:
16-
shape (3,) array:
17-
>>> a = np.array([1, 2, 3])
18-
shape (2, 3) array:
19-
>>> b = np.array([[1, 2, 3], [4, 5, 6]])
20-
>>> dat = dict(a=a, b=b)
21-
22-
>>> ed = bf.adapters.transforms.ExpandDims("a", axis=0)
23-
>>> new_dat = ed.forward(dat)
24-
>>> new_dat["a"].shape
25-
(1, 3)
26-
27-
>>> ed = bf.adapters.transforms.ExpandDims("a", axis=1)
28-
>>> new_dat = ed.forward(dat)
29-
>>> new_dat["a"].shape
30-
(3, 1)
31-
32-
>>> ed = bf.adapters.transforms.ExpandDims("b", axis=1)
33-
>>> new_dat = ed.forward(dat)
34-
>>> new_dat["b"].shape
35-
(2, 1, 3)
15+
16+
Examples
17+
--------
18+
shape (3,) array:
19+
20+
>>> a = np.array([1, 2, 3])
21+
22+
shape (2, 3) array:
23+
24+
>>> b = np.array([[1, 2, 3], [4, 5, 6]])
25+
>>> dat = dict(a=a, b=b)
26+
27+
>>> ed = bf.adapters.transforms.ExpandDims("a", axis=0)
28+
>>> new_dat = ed.forward(dat)
29+
>>> new_dat["a"].shape
30+
(1, 3)
31+
32+
>>> ed = bf.adapters.transforms.ExpandDims("a", axis=1)
33+
>>> new_dat = ed.forward(dat)
34+
>>> new_dat["a"].shape
35+
(3, 1)
36+
37+
>>> ed = bf.adapters.transforms.ExpandDims("b", axis=1)
38+
>>> new_dat = ed.forward(dat)
39+
>>> new_dat["b"].shape
40+
(2, 1, 3)
3641
3742
It is recommended to precede this transform with a :class:`bayesflow.adapters.transforms.ToArray` transform.
3843
"""

bayesflow/adapters/transforms/filter_transform.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ def __call__(self, key: str, value: np.ndarray, inverse: bool) -> bool:
2222
@serializable(package="bayesflow.adapters")
2323
class FilterTransform(Transform):
2424
"""
25-
Implements a transform that applies a different transform on a subset of the data. Used by other transforms and
26-
base adapter class.
25+
Implements a transform that applies a different transform on a subset of the data.
26+
27+
Used by other transforms and base adapter class.
2728
"""
2829

2930
def __init__(

0 commit comments

Comments
 (0)