From e8d74483d2fdb46301d3ef1c7c949dacf18f9d76 Mon Sep 17 00:00:00 2001 From: Leona Odole Date: Fri, 29 Nov 2024 13:11:24 +0100 Subject: [PATCH 1/8] nothing significant to concatonate --- bayesflow/adapters/transforms/concatenate.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/bayesflow/adapters/transforms/concatenate.py b/bayesflow/adapters/transforms/concatenate.py index a09e9b699..5f9e54c27 100644 --- a/bayesflow/adapters/transforms/concatenate.py +++ b/bayesflow/adapters/transforms/concatenate.py @@ -12,7 +12,14 @@ @serializable(package="bayesflow.adapters") class Concatenate(Transform): - """Concatenate multiple arrays into a new key.""" + """Concatenate multiple arrays into a new key. + Parameters: + + keys: + + into: + + """ def __init__(self, keys: Sequence[str], *, into: str, axis: int = -1): self.keys = keys From 7f90933c73d84b1c75fa7518fed9fd05716a8aee Mon Sep 17 00:00:00 2001 From: Leona Odole Date: Fri, 29 Nov 2024 13:20:15 +0100 Subject: [PATCH 2/8] added explaination for keep transform --- bayesflow/adapters/transforms/keep.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/bayesflow/adapters/transforms/keep.py b/bayesflow/adapters/transforms/keep.py index 164616b62..c95ff0b9f 100644 --- a/bayesflow/adapters/transforms/keep.py +++ b/bayesflow/adapters/transforms/keep.py @@ -11,6 +11,27 @@ @serializable(package="bayesflow.adapters") class Keep(Transform): + ''' + Name the data parameters that should be kept for futher calculation. + + Parameters: + + cls: tuple containing the names of kept data variables as strings. + + Example: + 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; + + adapter = ( + bf.adapters.Adapter() + + # drop data from unneeded priors alpha, and r + .keep(("theta", "x")) + + ) + + + ''' def __init__(self, keys: Sequence[str]): self.keys = keys From 638065d015589e5e351e410399892de1cde4c5ca Mon Sep 17 00:00:00 2001 From: Leona Odole Date: Sat, 30 Nov 2024 11:59:19 +0100 Subject: [PATCH 3/8] examples for keep and to_array in style of broadcast as well as usage --- bayesflow/adapters/transforms/keep.py | 19 ++++++++++++++++--- bayesflow/adapters/transforms/to_array.py | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/bayesflow/adapters/transforms/keep.py b/bayesflow/adapters/transforms/keep.py index c95ff0b9f..a4053cd79 100644 --- a/bayesflow/adapters/transforms/keep.py +++ b/bayesflow/adapters/transforms/keep.py @@ -11,14 +11,15 @@ @serializable(package="bayesflow.adapters") class Keep(Transform): - ''' + """ Name the data parameters that should be kept for futher calculation. Parameters: cls: tuple containing the names of kept data variables as strings. - Example: + Example 1: + 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; @@ -30,8 +31,20 @@ class Keep(Transform): ) + Example 2: + + >>> 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 diff --git a/bayesflow/adapters/transforms/to_array.py b/bayesflow/adapters/transforms/to_array.py index 7c62af9a0..75b27d4c1 100644 --- a/bayesflow/adapters/transforms/to_array.py +++ b/bayesflow/adapters/transforms/to_array.py @@ -11,6 +11,22 @@ @serializable(package="bayesflow.adapters") 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. + + Example: + >>> ta = bf.adapters.transforms.ToArray() + >>> a = [1,2,3,4] + >>> ta.forward(a) + array([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 From fe048f7120eef044ac98036e02f1386bc0f4c51f Mon Sep 17 00:00:00 2001 From: Leona Odole Date: Sat, 30 Nov 2024 11:59:38 +0100 Subject: [PATCH 4/8] small change to keep --- bayesflow/adapters/transforms/keep.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/bayesflow/adapters/transforms/keep.py b/bayesflow/adapters/transforms/keep.py index a4053cd79..3e1501258 100644 --- a/bayesflow/adapters/transforms/keep.py +++ b/bayesflow/adapters/transforms/keep.py @@ -18,7 +18,7 @@ class Keep(Transform): cls: tuple containing the names of kept data variables as strings. - Example 1: + 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; @@ -28,11 +28,9 @@ class Keep(Transform): # drop data from unneeded priors alpha, and r .keep(("theta", "x")) - ) - Example 2: - + Example: >>> a = [1,2,3,4] >>> b = [[1,2],[3,4]] >>> c = [[5,6,7,8]] @@ -42,8 +40,6 @@ class Keep(Transform): >>> keeper.forward(dat) {'b': [[1, 2], [3, 4]], 'c': [[5, 6, 7, 8]]} - - """ def __init__(self, keys: Sequence[str]): self.keys = keys From 9deda9d391cc0727d908749d60adbbea5173f598 Mon Sep 17 00:00:00 2001 From: Leona Odole Date: Sat, 30 Nov 2024 12:10:21 +0100 Subject: [PATCH 5/8] explaination of purpose of ad_set with useage --- bayesflow/adapters/transforms/as_set.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bayesflow/adapters/transforms/as_set.py b/bayesflow/adapters/transforms/as_set.py index cf6306316..485a6d02e 100644 --- a/bayesflow/adapters/transforms/as_set.py +++ b/bayesflow/adapters/transforms/as_set.py @@ -4,6 +4,12 @@ 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. + """ + def forward(self, data: np.ndarray, **kwargs) -> np.ndarray: return np.atleast_3d(data) From d29ff5a96878aa2e1d1ea03466f992388fc121b3 Mon Sep 17 00:00:00 2001 From: Leona Odole Date: Sat, 30 Nov 2024 12:38:42 +0100 Subject: [PATCH 6/8] added documentation of constrain transform --- bayesflow/adapters/transforms/as_set.py | 7 +++++ bayesflow/adapters/transforms/constrain.py | 34 ++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/bayesflow/adapters/transforms/as_set.py b/bayesflow/adapters/transforms/as_set.py index 485a6d02e..a901d43f5 100644 --- a/bayesflow/adapters/transforms/as_set.py +++ b/bayesflow/adapters/transforms/as_set.py @@ -8,6 +8,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. + + Useage: + + adapter = ( + bf.Adapter() + .as_set(["x", "y"]) + ) """ def forward(self, data: np.ndarray, **kwargs) -> np.ndarray: diff --git a/bayesflow/adapters/transforms/constrain.py b/bayesflow/adapters/transforms/constrain.py index c0ac36692..2ce9552f8 100644 --- a/bayesflow/adapters/transforms/constrain.py +++ b/bayesflow/adapters/transforms/constrain.py @@ -15,6 +15,40 @@ @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. + + 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 + - 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: + 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 + + Usage: + adapter = ( + bf.Adapter() + .constrain("p", lower=0, upper=1, method = "sigmoid") + ) + """ + def __init__( self, *, lower: int | float | np.ndarray = None, upper: int | float | np.ndarray = None, method: str = "default" ): From 2931ff9a60ff1d102029caaf534f33d069d6cd30 Mon Sep 17 00:00:00 2001 From: Leona Odole Date: Sat, 30 Nov 2024 12:47:32 +0100 Subject: [PATCH 7/8] convert dtype documentation, could maybe add 'modfidy with caution' --- bayesflow/adapters/transforms/convert_dtype.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bayesflow/adapters/transforms/convert_dtype.py b/bayesflow/adapters/transforms/convert_dtype.py index be3059a67..f601abec7 100644 --- a/bayesflow/adapters/transforms/convert_dtype.py +++ b/bayesflow/adapters/transforms/convert_dtype.py @@ -10,6 +10,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. + """ def __init__(self, from_dtype: str, to_dtype: str): super().__init__() From b8b68757b0ae1a5f34bf656a837abbeb77e2ec62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul-Christian=20B=C3=BCrkner?= Date: Tue, 3 Dec 2024 13:53:25 +0100 Subject: [PATCH 8/8] run linter --- bayesflow/adapters/transforms/as_set.py | 12 ++++---- bayesflow/adapters/transforms/concatenate.py | 6 ++-- bayesflow/adapters/transforms/constrain.py | 26 +++++++++-------- .../adapters/transforms/convert_dtype.py | 3 +- bayesflow/adapters/transforms/keep.py | 28 +++++++++---------- bayesflow/adapters/transforms/to_array.py | 9 +++--- 6 files changed, 44 insertions(+), 40 deletions(-) diff --git a/bayesflow/adapters/transforms/as_set.py b/bayesflow/adapters/transforms/as_set.py index a901d43f5..6ac73c315 100644 --- a/bayesflow/adapters/transforms/as_set.py +++ b/bayesflow/adapters/transforms/as_set.py @@ -5,12 +5,14 @@ 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 is useful, for example, 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"]) diff --git a/bayesflow/adapters/transforms/concatenate.py b/bayesflow/adapters/transforms/concatenate.py index 5f9e54c27..19d4cedac 100644 --- a/bayesflow/adapters/transforms/concatenate.py +++ b/bayesflow/adapters/transforms/concatenate.py @@ -13,11 +13,11 @@ @serializable(package="bayesflow.adapters") class Concatenate(Transform): """Concatenate multiple arrays into a new key. - Parameters: + Parameters: - keys: + keys: - into: + into: """ diff --git a/bayesflow/adapters/transforms/constrain.py b/bayesflow/adapters/transforms/constrain.py index 2ce9552f8..904492765 100644 --- a/bayesflow/adapters/transforms/constrain.py +++ b/bayesflow/adapters/transforms/constrain.py @@ -16,33 +16,35 @@ @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") diff --git a/bayesflow/adapters/transforms/convert_dtype.py b/bayesflow/adapters/transforms/convert_dtype.py index f601abec7..e392cf17f 100644 --- a/bayesflow/adapters/transforms/convert_dtype.py +++ b/bayesflow/adapters/transforms/convert_dtype.py @@ -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__() diff --git a/bayesflow/adapters/transforms/keep.py b/bayesflow/adapters/transforms/keep.py index 3e1501258..165a0f887 100644 --- a/bayesflow/adapters/transforms/keep.py +++ b/bayesflow/adapters/transforms/keep.py @@ -12,35 +12,35 @@ @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 + # only keep theta and x .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 diff --git a/bayesflow/adapters/transforms/to_array.py b/bayesflow/adapters/transforms/to_array.py index 75b27d4c1..14abb9b3c 100644 --- a/bayesflow/adapters/transforms/to_array.py +++ b/bayesflow/adapters/transforms/to_array.py @@ -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