Skip to content

v2.0.2 #447

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 34 commits into from
Apr 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
688f22c
[no ci] notebook tests: increase timeout, fix platform/backend depend…
vpratz Apr 23, 2025
de30009
Enable use of summary networks with functional API again (#434)
vpratz Apr 23, 2025
0eefa69
[no ci] docs: add GitHub and Discourse links, reorder navbar
vpratz Apr 25, 2025
a97b5a2
[no ci] docs: acknowledge scikit-learn website
vpratz Apr 25, 2025
8ac8aa3
[no ci] docs: capitalize navigation headings
vpratz Apr 25, 2025
7ea287f
More tests (#437)
LarsKue Apr 25, 2025
42fa035
Automatically run slow tests when main is involved. (#438)
vpratz Apr 25, 2025
206d706
Update dispatch
stefanradev93 Apr 25, 2025
25f5c64
Update dispatching distributions
stefanradev93 Apr 25, 2025
f6a70b5
Improve workflow tests with multiple summary nets / approximators
stefanradev93 Apr 25, 2025
28652ac
Merge branch 'dev' of https://github.com/stefanradev93/BayesFlow into…
stefanradev93 Apr 25, 2025
7ce37cf
Fix zombie find_distribution import
stefanradev93 Apr 25, 2025
ea5a78d
Add readme entry [no ci]
stefanradev93 Apr 25, 2025
dc3cf81
Update README: NumFOCUS affiliation, awesome-abi list (#445)
marvinschmitt Apr 25, 2025
af38458
Merge branch 'dev' of https://github.com/stefanradev93/BayesFlow into…
stefanradev93 Apr 25, 2025
3b1c053
fix is_symbolic_tensor
LarsKue Apr 25, 2025
c638124
remove multiple batch sizes, remove multiple python version tests, re…
LarsKue Apr 25, 2025
de8e1cb
implement compile_from_config and get_compile_config (#442)
LarsKue Apr 25, 2025
16491be
Fix Optimal Transport for Compiled Contexts (#446)
LarsKue Apr 25, 2025
ec0ee2f
update dispatch tests for more coverage
LarsKue Apr 25, 2025
acf1c72
Update issue templates (#448)
LarsKue Apr 26, 2025
d24f5a3
Robustify kwargs passing inference networks, add class variables
stefanradev93 Apr 26, 2025
a43fd07
Merge branch 'dev' of https://github.com/stefanradev93/BayesFlow into…
stefanradev93 Apr 26, 2025
f67e443
Merge branch 'main' into dev
LarsKue Apr 26, 2025
463c0c7
fix convergence method to debug for non-log sinkhorn
LarsKue Apr 26, 2025
8f3739c
Bump optimal transport default to False
stefanradev93 Apr 26, 2025
40eccd4
use logging.info for backend selection instead of logging.debug
LarsKue Apr 26, 2025
8903089
fix model comparison approximator
LarsKue Apr 26, 2025
cbc86b8
improve docs and type hints
LarsKue Apr 26, 2025
77ddc5a
improve One-Sample T-Test Notebook:
LarsKue Apr 26, 2025
ad01171
remove backend print
LarsKue Apr 26, 2025
a742d9c
[skip ci] turn all single-quoted strings into double-quoted strings
LarsKue Apr 26, 2025
b450961
turn all single-quoted strings into double-quoted strings
LarsKue Apr 26, 2025
898a869
Merge remote-tracking branch 'origin/dev' into dev
LarsKue Apr 26, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
name: Bug report
about: Create a bug report to help us improve BayesFlow
title: "[BUG]"
labels: ''
assignees: ''

---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Minimal steps to reproduce the behavior:
1. Import '...'
2. Create network '....'
3. Call '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.

**Traceback**
If you encounter an error, please provide a complete traceback to help explain your problem.

**Environment**
- OS: [e.g. Ubuntu]
- Python Version: [e.g. 3.11]
- Backend: [e.g. jax, tensorflow, pytorch]
- BayesFlow Version: [e.g. 2.0.2]

**Additional context**
Add any other context about the problem here.

**Minimality**
- [ ] I verify that my example is minimal, does not rely on third-party packages, and is most likely an issue in BayesFlow.
20 changes: 20 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
name: Feature request
about: Suggest a new feature to be implemented in BayesFlow
title: "[FEATURE]"
labels: feature
assignees: ''

---

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.
2 changes: 0 additions & 2 deletions .github/workflows/style.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ on:
branches:
- main
- dev
- update-workflows
push:
branches:
- main
- dev
- update-workflows

jobs:
check-code-style:
Expand Down
9 changes: 6 additions & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
python-version: ["3.10", "3.11"]
python-version: ["3.10"] # we usually only need to test the oldest python version
backend: ["jax", "tensorflow", "torch"]

runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -73,8 +73,11 @@ jobs:
pytest -x -m "not slow"
- name: Run Slow Tests
# run all slow tests only on manual trigger
if: github.event_name == 'workflow_dispatch'
# Run slow tests on manual trigger and pushes/PRs to main.
# Limit to one OS and Python version to save compute.
# Multiline if statements are weird, https://github.com/orgs/community/discussions/25641,
# but feel free to convert it.
if: ${{ ((github.event_name == 'workflow_dispatch') || (github.event_name == 'push' && github.ref_name == 'main') || (github.event_name == 'pull_request' && github.base_ref == 'main')) && ((matrix.os == 'windows-latest') && (matrix.python-version == '3.10')) }}
run: |
pytest -m "slow"
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
![Codecov](https://img.shields.io/codecov/c/github/bayesflow-org/bayesflow?style=for-the-badge&link=https%3A%2F%2Fapp.codecov.io%2Fgh%2Fbayesflow-org%2Fbayesflow%2Ftree%2Fmain)
[![DOI](https://img.shields.io/badge/DOI-10.21105%2Fjoss.05702-blue?style=for-the-badge)](https://doi.org/10.21105/joss.05702)
![PyPI - License](https://img.shields.io/pypi/l/bayesflow?style=for-the-badge)
![NumFOCUS Affiliated Project](https://img.shields.io/badge/NumFOCUS-Affiliated%20Project-orange?style=for-the-badge)

BayesFlow is a Python library for simulation-based **Amortized Bayesian Inference** with neural networks.
It provides users and researchers with:
Expand Down Expand Up @@ -225,8 +226,10 @@ You can find and install the old Bayesflow version via the `stable-legacy` branc

## Awesome Amortized Inference

If you are interested in a curated list of resources, including reviews, software, papers, and other resources related to amortized inference, feel free to explore our [community-driven list](https://github.com/bayesflow-org/awesome-amortized-inference).
If you are interested in a curated list of resources, including reviews, software, papers, and other resources related to amortized inference, feel free to explore our [community-driven list](https://github.com/bayesflow-org/awesome-amortized-inference). If you'd like a paper (by yourself or someone else) featured, please add it to the list with a pull request, an issue, or a message to the maintainers.

## Acknowledgments

This project is currently managed by researchers from Rensselaer Polytechnic Institute, TU Dortmund University, and Heidelberg University. It is partially funded by the Deutsche Forschungsgemeinschaft (DFG, German Research Foundation) Projects 528702768 and 508399956. The project is further supported by Germany's Excellence Strategy -- EXC-2075 - 390740016 (Stuttgart Cluster of Excellence SimTech) and EXC-2181 - 390900948 (Heidelberg Cluster of Excellence STRUCTURES), the collaborative research cluster TRR 391 – 520388526, as well as the Informatics for Life initiative funded by the Klaus Tschira Foundation.

BayesFlow is a [NumFOCUS Affiliated Project](https://numfocus.org/sponsored-projects/affiliated-projects).
2 changes: 1 addition & 1 deletion bayesflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def setup():

from bayesflow.utils import logging

logging.debug(f"Using backend {keras.backend.backend()!r}")
logging.info(f"Using backend {keras.backend.backend()!r}")

if keras.backend.backend() == "torch":
import torch
Expand Down
2 changes: 1 addition & 1 deletion bayesflow/approximators/approximator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def build_adapter(cls, **kwargs) -> Adapter:
raise NotImplementedError

def build_from_data(self, data: Mapping[str, any]) -> None:
self.compute_metrics(**data, stage="training")
self.compute_metrics(**filter_kwargs(data, self.compute_metrics), stage="training")
self.built = True

@classmethod
Expand Down
36 changes: 33 additions & 3 deletions bayesflow/approximators/continuous_approximator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
Additional arguments passed to the :py:class:`bayesflow.approximators.Approximator` class.
"""

SAMPLE_KEYS = ["summary_variables", "inference_conditions"]

def __init__(
self,
*,
Expand All @@ -51,6 +53,7 @@
inference_variables: Sequence[str],
inference_conditions: Sequence[str] = None,
summary_variables: Sequence[str] = None,
standardize: bool = True,
sample_weight: str = None,
) -> Adapter:
"""Create an :py:class:`~bayesflow.adapters.Adapter` suited for the approximator.
Expand All @@ -63,9 +66,12 @@
Names of the inference conditions in the data
summary_variables : Sequence of str, optional
Names of the summary variables in the data
standardize : bool, optional
Decide whether to standardize all variables, default is True
sample_weight : str, optional
Name of the sample weights
"""

adapter = Adapter()
adapter.to_array()
adapter.convert_dtype("float64", "float32")
Expand All @@ -82,7 +88,9 @@
adapter = adapter.rename(sample_weight, "sample_weight")

adapter.keep(["inference_variables", "inference_conditions", "summary_variables", "sample_weight"])
adapter.standardize(exclude="sample_weight")

if standardize:
adapter.standardize(exclude="sample_weight")

return adapter

Expand All @@ -104,6 +112,12 @@

return super().compile(*args, **kwargs)

def compile_from_config(self, config):
self.compile(**deserialize(config))
if hasattr(self, "optimizer") and self.built:
# Create optimizer variables.
self.optimizer.build(self.trainable_variables)

Check warning on line 119 in bayesflow/approximators/continuous_approximator.py

View check run for this annotation

Codecov / codecov/patch

bayesflow/approximators/continuous_approximator.py#L119

Added line #L119 was not covered by tests

def compute_metrics(
self,
inference_variables: Tensor,
Expand Down Expand Up @@ -213,6 +227,16 @@

return base_config | serialize(config)

def get_compile_config(self):
base_config = super().get_compile_config() or {}

config = {
"inference_metrics": self.inference_network._metrics,
"summary_metrics": self.summary_network._metrics if self.summary_network is not None else None,
}

return base_config | serialize(config)

def estimate(
self,
conditions: Mapping[str, np.ndarray],
Expand Down Expand Up @@ -318,12 +342,18 @@
dict[str, np.ndarray]
Dictionary containing generated samples with the same keys as `conditions`.
"""

# Apply adapter transforms to raw simulated / real quantities
conditions = self.adapter(conditions, strict=False, stage="inference", **kwargs)
# at inference time, inference_variables are estimated by the networks and thus ignored in conditions
conditions.pop("inference_variables", None)

# Ensure only keys relevant for sampling are present in the conditions dictionary
conditions = {k: v for k, v in conditions.items() if k in ContinuousApproximator.SAMPLE_KEYS}

conditions = keras.tree.map_structure(keras.ops.convert_to_tensor, conditions)
conditions = {"inference_variables": self._sample(num_samples=num_samples, **conditions, **kwargs)}
conditions = keras.tree.map_structure(keras.ops.convert_to_numpy, conditions)

# Back-transform quantities and samples
conditions = self.adapter(conditions, inverse=True, strict=False, **kwargs)

if split:
Expand Down
28 changes: 25 additions & 3 deletions bayesflow/approximators/model_comparison_approximator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
The network backbone (e.g, an MLP) that is used for model classification.
The input of the classifier network is created by concatenating `classifier_variables`
and (optional) output of the summary_network.
summary_network: bg.networks.SummaryNetwork, optional
summary_network: bf.networks.SummaryNetwork, optional
The summary network used for data summarization (default is None).
The input of the summary network is `summary_variables`.
"""

SAMPLE_KEYS = ["summary_variables", "classifier_conditions"]

def __init__(
self,
*,
Expand Down Expand Up @@ -118,6 +120,12 @@

return super().compile(*args, **kwargs)

def compile_from_config(self, config):
self.compile(**deserialize(config))
if hasattr(self, "optimizer") and self.built:

Check warning on line 125 in bayesflow/approximators/model_comparison_approximator.py

View check run for this annotation

Codecov / codecov/patch

bayesflow/approximators/model_comparison_approximator.py#L124-L125

Added lines #L124 - L125 were not covered by tests
# Create optimizer variables.
self.optimizer.build(self.trainable_variables)

Check warning on line 127 in bayesflow/approximators/model_comparison_approximator.py

View check run for this annotation

Codecov / codecov/patch

bayesflow/approximators/model_comparison_approximator.py#L127

Added line #L127 was not covered by tests

def compute_metrics(
self,
*,
Expand Down Expand Up @@ -262,6 +270,16 @@

return base_config | serialize(config)

def get_compile_config(self):
base_config = super().get_compile_config() or {}

Check warning on line 274 in bayesflow/approximators/model_comparison_approximator.py

View check run for this annotation

Codecov / codecov/patch

bayesflow/approximators/model_comparison_approximator.py#L274

Added line #L274 was not covered by tests

config = {

Check warning on line 276 in bayesflow/approximators/model_comparison_approximator.py

View check run for this annotation

Codecov / codecov/patch

bayesflow/approximators/model_comparison_approximator.py#L276

Added line #L276 was not covered by tests
"classifier_metrics": self.classifier_network._metrics,
"summary_metrics": self.summary_network._metrics if self.summary_network is not None else None,
}

return base_config | serialize(config)

Check warning on line 281 in bayesflow/approximators/model_comparison_approximator.py

View check run for this annotation

Codecov / codecov/patch

bayesflow/approximators/model_comparison_approximator.py#L281

Added line #L281 was not covered by tests

def predict(
self,
*,
Expand All @@ -288,9 +306,13 @@
np.ndarray
Predicted posterior model probabilities given `conditions`.
"""

# Apply adapter transforms to raw simulated / real quantities
conditions = self.adapter(conditions, strict=False, stage="inference", **kwargs)
# at inference time, model_indices are predicted by the networks and thus ignored in conditions
conditions.pop("model_indices", None)

# Ensure only keys relevant for sampling are present in the conditions dictionary
conditions = {k: v for k, v in conditions.items() if k in ModelComparisonApproximator.SAMPLE_KEYS}

conditions = keras.tree.map_structure(keras.ops.convert_to_tensor, conditions)

output = self._predict(**conditions, **kwargs)
Expand Down
4 changes: 3 additions & 1 deletion bayesflow/approximators/point_approximator.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@ def log_prob(

def _prepare_conditions(self, conditions: Mapping[str, np.ndarray], **kwargs) -> dict[str, Tensor]:
"""Adapts and converts the conditions to tensors."""

conditions = self.adapter(conditions, strict=False, stage="inference", **kwargs)
conditions.pop("inference_variables", None)
conditions = {k: v for k, v in conditions.items() if k in ContinuousApproximator.SAMPLE_KEYS}

return keras.tree.map_structure(keras.ops.convert_to_tensor, conditions)

def _apply_inverse_adapter_to_estimates(
Expand Down
2 changes: 0 additions & 2 deletions bayesflow/distributions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from .diagonal_student_t import DiagonalStudentT
from .mixture import Mixture

from .find_distribution import find_distribution

from ..utils._docs import _add_imports_to_all

_add_imports_to_all(include_modules=[])
Loading