Skip to content

Commit

Permalink
ruff fixes hopefully passes the linter, but weird stuff going on in t…
Browse files Browse the repository at this point in the history
…he broadcast documentation
  • Loading branch information
eodole committed Dec 4, 2024
1 parent ef345e7 commit 62b1e1f
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 55 deletions.
11 changes: 6 additions & 5 deletions bayesflow/adapters/transforms/as_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@

class AsSet(ElementwiseTransform):
"""
The `.as_set(["x", "y"])` transform indicates that both `x` and `y` are treated as sets.
That is, their values will be treated as *exchangable* such that they will imply the same inference regardless of the values' order.
This would be useful in a linear regression context where we can index the observations in arbitrary order and always get the same regression line.
The `.as_set(["x", "y"])` transform indicates that both `x` and `y` are treated as sets.
That is, their values will be treated as *exchangable* such that they will imply the same inference regardless of
the values' order. This would be useful in a linear regression context where we can index the observations in
arbitrary order and always get the same regression line.
Useage:
Useage:
adapter = (
bf.Adapter()
.as_set(["x", "y"])
Expand Down
14 changes: 7 additions & 7 deletions bayesflow/adapters/transforms/concatenate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@

@serializable(package="bayesflow.adapters")
class Concatenate(Transform):
"""Concatenate multiple arrays into a new key. Used to specify how data variables should be treated by the network.
"""Concatenate multiple arrays into a new key. Used to specify how data variables should be treated by the network.
Parameters:
Parameters:
keys: Input a list of strings, where the strings are the names of data variables.
into: A string telling network how to use the variables named in keys.
into: A string telling network how to use the variables named in keys.
-options: "inference_variables", "summary_variables", "inference_conditions"
axis: integer specifing along which axis to concatonate the keys. The last axis is used by default.
axis: integer specifing along which axis to concatonate the keys. The last axis is used by default.
Example:
Suppose you have a simulator that generates variables beta, sigma from priors and then observation variables "x" and "y".
We can then use concatonate in the following way
Example:
Suppose you have a simulator that generates variables beta, sigma from priors and then observation
variables "x" and "y". We can then use concatonate in the following way
adapter = (
bf.Adapter()
Expand Down
25 changes: 13 additions & 12 deletions bayesflow/adapters/transforms/constrain.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,34 @@
@serializable(package="bayesflow.adapters")
class Constrain(ElementwiseTransform):
"""
Constrains neural network predictions of a data variable to specificied bounds.
Parameters:
String containing the name of the data variable to be transformed e.g. "sigma". See examples below.
Constrains neural network predictions of a data variable to specificied bounds.
Named Parameters:
Parameters:
String containing the name of the data variable to be transformed e.g. "sigma". See examples below.
Named Parameters:
lower: Lower bound for named data variable.
upper: Upper bound for named data variable.
method: Method by which to shrink the network predictions space to specified bounds. Choose from
method: Method by which to shrink the network predictions space to specified bounds. Choose from
- Double bounded methods: sigmoid, expit, (default = sigmoid)
- Lower bound only methods: softplus, exp, (default = softplus)
- Upper bound only methods: softplus, exp, (default = softplus)
Examples:
Let sigma be the standard deviation of a normal distribution, then sigma should always be greater than zero.
Useage:
Examples:
Let sigma be the standard deviation of a normal distribution, then sigma should always be greater than zero.
Useage:
adapter = (
bf.Adapter()
.constrain("sigma", lower=0)
)
Suppose p is the parameter for a binomial distribution where p must be in [0,1] then we would constrain the neural network to estimate p in the following way
Suppose p is the parameter for a binomial distribution where p must be in [0,1] then we would constrain the
neural network to estimate p in the following way
Usage:
Usage:
adapter = (
bf.Adapter()
.constrain("p", lower=0, upper=1, method = "sigmoid")
Expand Down
3 changes: 2 additions & 1 deletion bayesflow/adapters/transforms/convert_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
@serializable(package="bayesflow.adapters")
class ConvertDType(ElementwiseTransform):
"""
Default transform used to convert all floats from float64 to float32 to be in line with keras framework.
Default transform used to convert all floats from float64 to float32 to be in line with keras framework.
"""

def __init__(self, from_dtype: str, to_dtype: str):
super().__init__()

Expand Down
22 changes: 11 additions & 11 deletions bayesflow/adapters/transforms/drop.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@

@serializable(package="bayesflow.adapters")
class Drop(Transform):
"""
Transform to drop variables from further calculation.
"""
Transform to drop variables from further calculation.
Parameters:
keys: list of strings, containing names of data variables that should be dropped
Parameters:
keys: list of strings, containing names of data variables that should be dropped
Example:
Example:
>>> import bayesflow as bf
>>> a = [1,2,3,4]
>>> b = [[1,2],[3,4]]
>>> c = [[5,6,7,8]]
>>> dat = dict(a=a,b=b,c=c)
>>> a = [1, 2, 3, 4]
>>> b = [[1, 2], [3, 4]]
>>> c = [[5, 6, 7, 8]]
>>> dat = dict(a=a, b=b, c=c)
>>> dat
{'a': [1, 2, 3, 4], 'b': [[1, 2], [3, 4]], 'c': [[5, 6, 7, 8]]}
>>> drop = bf.adapters.transforms.Drop(("b","c"))
>>> drop = bf.adapters.transforms.Drop(("b", "c"))
>>> drop.forward(dat)
{'a': [1, 2, 3, 4]}
"""
Expand Down
1 change: 0 additions & 1 deletion bayesflow/adapters/transforms/elementwise_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

@serializable(package="bayesflow.adapters")
class ElementwiseTransform:

"""Base class on which other transforms are based"""

def __call__(self, data: np.ndarray, inverse: bool = False, **kwargs) -> np.ndarray:
Expand Down
27 changes: 14 additions & 13 deletions bayesflow/adapters/transforms/keep.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,36 @@
@serializable(package="bayesflow.adapters")
class Keep(Transform):
"""
Name the data parameters that should be kept for futher calculation.
Name the data parameters that should be kept for futher calculation.
Parameters:
Parameters:
cls: tuple containing the names of kept data variables as strings.
cls: tuple containing the names of kept data variables as strings.
Useage:
Useage:
Two moons simulator generates data for priors alpha, r and theta as well as observation data x.
We are interested only in theta and x, to keep only theta and x we should use the following;
We are interested only in theta and x, to keep only theta and x we should use the following;
adapter = (
bf.adapters.Adapter()
# drop data from unneeded priors alpha, and r
# drop data from unneeded priors alpha, and r
.keep(("theta", "x"))
)
Example:
>>> a = [1,2,3,4]
>>> b = [[1,2],[3,4]]
>>> c = [[5,6,7,8]]
>>> dat = dict(a=a,b=b,c =c)
# Here we want to only keep elements b and c
>>> keeper = bf.adapters.transforms.Keep(("b","c"))
Example:
>>> a = [1, 2, 3, 4]
>>> b = [[1, 2], [3, 4]]
>>> c = [[5, 6, 7, 8]]
>>> dat = dict(a=a, b=b, c=c)
# Here we want to only keep elements b and c
>>> keeper = bf.adapters.transforms.Keep(("b", "c"))
>>> keeper.forward(dat)
{'b': [[1, 2], [3, 4]], 'c': [[5, 6, 7, 8]]}
"""

def __init__(self, keys: Sequence[str]):
self.keys = keys

Expand Down
9 changes: 4 additions & 5 deletions bayesflow/adapters/transforms/to_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@
class ToArray(ElementwiseTransform):
"""
Checks provided data for any non-arrays and converts them to numpy arrays.
This ensures all data is in a format suitable for training.
This ensures all data is in a format suitable for training.
Example:
Example:
>>> ta = bf.adapters.transforms.ToArray()
>>> a = [1,2,3,4]
>>> a = [1, 2, 3, 4]
>>> ta.forward(a)
array([1, 2, 3, 4])
>>> b = [[1,2],[3,4]]
>>> b = [[1, 2], [3, 4]]
>>> ta.forward(b)
array([[1, 2],
[3, 4]])
"""


def __init__(self):
super().__init__()
self.original_type = None
Expand Down

0 comments on commit 62b1e1f

Please sign in to comment.