#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is part of sbi, a toolkit for simulation-based inference. sbi is licensed
# under the Affero General Public License v3, see <https://www.gnu.org/licenses/>.
#
# Note: To use the 'upload' functionality of this file, you must:
#   $ pipenv install twine --dev

import io
import os
import sys
from shutil import rmtree

from setuptools import find_packages, setup, Command

# Package meta-data.
NAME = "sbi"
DESCRIPTION = "Simulation-based inference."
KEYWORDS = "bayesian parameter inference system_identification simulator PyTorch"
URL = "https://github.com/mackelab/sbi"
EMAIL = "sbi@mackelab.org"
AUTHOR = "Álvaro Tejero-Cantero, Jakob H. Macke, Jan-Matthis Lückmann, Conor M. Durkan, Michael Deistler, Jan Bölts"
REQUIRES_PYTHON = ">=3.6.0"

REQUIRED = [
    "joblib",
    "matplotlib",
    "numpy",
    "pillow",
    "pyknos>=0.12",
    "pyro-ppl>=1.3.1",
    "scipy",
    "tensorboard",
    "torch>=1.5.1",
    "tqdm",
]

EXTRAS = {
    "dev": [
        "autoflake",
        "black",
        "deepdiff",
        "flake8",
        "isort",
        "jupyter",
        "mkdocs",
        "mkdocs-material",
        "markdown-include",
        "mkdocs-redirects",
        "mkdocstrings",
        "nbconvert",
        "pep517",
        "pytest",
        "pyyaml",
        "scikit-learn",
        "torchtestcase",
        "twine",
    ],
}

here = os.path.abspath(os.path.dirname(__file__))

# Import the README and use it as the long-description.
try:
    with io.open(os.path.join(here, "README.md"), encoding="utf-8") as f:
        long_description = "\n" + f.read()
except FileNotFoundError:
    long_description = DESCRIPTION

# Load the package's __version__.py module as a dictionary.
about = {}
project_slug = NAME.lower().replace("-", "_").replace(" ", "_")
with open(os.path.join(here, project_slug, "__version__.py")) as f:
    exec(f.read(), about)


class UploadCommand(Command):
    """Support setup.py upload."""

    description = "Build and publish the package."
    user_options = []

    @staticmethod
    def status(s):
        """Prints things in bold."""
        print("\033[1m{0}\033[0m".format(s))

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        try:
            self.status("Removing previous builds…")
            rmtree(os.path.join(here, "dist"))
        except OSError:
            pass

        self.status("Building Source and Wheel (universal) distribution…")
        os.system("{0} setup.py sdist bdist_wheel --universal".format(sys.executable))

        self.status("Uploading the package to PyPI via Twine…")
        os.system("twine upload dist/*")

        self.status("Pushing git tags…")
        os.system("git tag v{0}".format(about["__version__"]))
        os.system("git push --tags")

        sys.exit()


setup(
    name=NAME,
    version=about["__version__"],
    description=DESCRIPTION,
    keywords=KEYWORDS,
    long_description=long_description,
    long_description_content_type="text/markdown",
    author=AUTHOR,
    author_email=EMAIL,
    python_requires=REQUIRES_PYTHON,
    url=URL,
    packages=find_packages(exclude=["tests", "*.tests", "*.tests.*", "tests.*"]),
    install_requires=REQUIRED,
    extras_require=EXTRAS,
    include_package_data=True,
    license="AGPLv3",
    classifiers=[
        # Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers
        "Development Status :: 3 - Alpha",
        "License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)",
        "Intended Audience :: Developers",
        "Intended Audience :: Education",
        "Intended Audience :: Science/Research",
        "Topic :: Adaptive Technologies",
        "Topic :: Scientific/Engineering",
        "Topic :: Scientific/Engineering :: Artificial Intelligence",
        "Topic :: Scientific/Engineering :: Mathematics",
        "Programming Language :: Python",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.6",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
    ],
    # $ setup.py publish support.
    cmdclass=dict(upload=UploadCommand),
)

setup.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is part of sbi, a toolkit for simulation-based inference. sbi is licensed
# under the Affero General Public License v3, see <https://www.gnu.org/licenses/>.
#
# Note: To use the 'upload' functionality of this file, you must:
#   $ pipenv install twine --dev

import io
import os
import sys
from shutil import rmtree

from setuptools import find_packages, setup, Command

# Package meta-data.
NAME = "sbi"
DESCRIPTION = "Simulation-based inference."
KEYWORDS = "bayesian parameter inference system_identification simulator PyTorch"
URL = "https://github.com/mackelab/sbi"
EMAIL = "sbi@mackelab.org"
AUTHOR = "Álvaro Tejero-Cantero, Jakob H. Macke, Jan-Matthis Lückmann, Conor M. Durkan, Michael Deistler, Jan Bölts"
REQUIRES_PYTHON = ">=3.6.0"

REQUIRED = [
    "joblib",
    "matplotlib",
    "numpy",
    "pillow",
    "pyknos>=0.14",
    "pyro-ppl>=1.3.1",
    "scipy",
    "tensorboard",
    "torch>=1.5.1",
    "tqdm",
]

EXTRAS = {
    "dev": [
        "autoflake",
        "black",
        "deepdiff",
        "flake8",
        "isort",
        "jupyter",
        "mkdocs",
        "mkdocs-material",
        "markdown-include",
        "mkdocs-redirects",
        "mkdocstrings",
        "nbconvert",
        "pep517",
        "pytest",
        "pyyaml",
        "scikit-learn",
        "torchtestcase",
        "twine",
    ],
}

here = os.path.abspath(os.path.dirname(__file__))

# Import the README and use it as the long-description.
try:
    with io.open(os.path.join(here, "README.md"), encoding="utf-8") as f:
        long_description = "\n" + f.read()
except FileNotFoundError:
    long_description = DESCRIPTION

# Load the package's __version__.py module as a dictionary.
about = {}
project_slug = NAME.lower().replace("-", "_").replace(" ", "_")
with open(os.path.join(here, project_slug, "__version__.py")) as f:
    exec(f.read(), about)


class UploadCommand(Command):
    """Support setup.py upload."""

    description = "Build and publish the package."
    user_options = []

    @staticmethod
    def status(s):
        """Prints things in bold."""
        print("\033[1m{0}\033[0m".format(s))

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        try:
            self.status("Removing previous builds…")
            rmtree(os.path.join(here, "dist"))
        except OSError:
            pass

        self.status("Building Source and Wheel (universal) distribution…")
        os.system("{0} setup.py sdist bdist_wheel --universal".format(sys.executable))

        self.status("Uploading the package to PyPI via Twine…")
        os.system("twine upload dist/*")

        self.status("Pushing git tags…")
        os.system("git tag v{0}".format(about["__version__"]))
        os.system("git push --tags")

        sys.exit()


setup(
    name=NAME,
    version=about["__version__"],
    description=DESCRIPTION,
    keywords=KEYWORDS,
    long_description=long_description,
    long_description_content_type="text/markdown",
    author=AUTHOR,
    author_email=EMAIL,
    python_requires=REQUIRES_PYTHON,
    url=URL,
    packages=find_packages(exclude=["tests", "*.tests", "*.tests.*", "tests.*"]),
    install_requires=REQUIRED,
    extras_require=EXTRAS,
    include_package_data=True,
    license="AGPLv3",
    classifiers=[
        # Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers
        "Development Status :: 3 - Alpha",
        "License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)",
        "Intended Audience :: Developers",
        "Intended Audience :: Education",
        "Intended Audience :: Science/Research",
        "Topic :: Adaptive Technologies",
        "Topic :: Scientific/Engineering",
        "Topic :: Scientific/Engineering :: Artificial Intelligence",
        "Topic :: Scientific/Engineering :: Mathematics",
        "Programming Language :: Python",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.6",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
    ],
    # $ setup.py publish support.
    cmdclass=dict(upload=UploadCommand),
)

setup.py
diff --git a/setup.py b/setup.py
index 5b97c4ce9..4c227f59e 100644
--- a/setup.py
+++ b/setup.py
@@ -28,7 +28,7 @@
     "matplotlib",
     "numpy",
     "pillow",
-    "pyknos>=0.12",
+    "pyknos>=0.14",
     "pyro-ppl>=1.3.1",
     "scipy",
     "tensorboard",

SNPE with NSF fails when sampling with MCMC
This occurs in a very particular setting: `SNPE` inference with `NSF` density estimator and `sample_with_mcmc=True` (no matter which type of MCMC. 

- it works with `sample_with_mcmc=False`, 
- and it works with `SNLE`! 

I tried to chase it down, but no success so far. You can reproduce it locally by running

```
pytest -s tests/linearGaussian_snpe_test.py::test_c2st_snpe_external_data_on_linearGaussian
```

and setting 
https://github.com/mackelab/sbi/blob/6b5ed7be1d7522546b06c39aec1f206a354cc2ef/tests/linearGaussian_snpe_test.py#L286

to `True`. 

This is the error trace:
```python

>       samples = posterior.sample((num_samples,))

tests/linearGaussian_snpe_test.py:289:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
sbi/inference/posteriors/direct_posterior.py:336: in sample
    samples = self._sample_posterior_mcmc(
sbi/inference/posteriors/base_posterior.py:333: in _sample_posterior_mcmc
    samples = self._slice_np_mcmc(
sbi/inference/posteriors/base_posterior.py:397: in _slice_np_mcmc
    posterior_sampler.gen(int(warmup_steps))
sbi/mcmc/slice_numpy.py:93: in gen
    self._tune_bracket_width(rng)
sbi/mcmc/slice_numpy.py:145: in _tune_bracket_width
    x[i], wi = self._sample_from_conditional(i, x[i], rng)
sbi/mcmc/slice_numpy.py:173: in _sample_from_conditional
    while Li(lx) >= logu and cxi - lx < self.max_width:
sbi/mcmc/slice_numpy.py:162: in <lambda>
    Li = lambda t: self.lp_f(np.concatenate([self.x[:i], [t], self.x[i + 1 :]]))
sbi/inference/posteriors/direct_posterior.py:477: in np_potential
    target_log_prob = self.posterior_nn.log_prob(
.sbi_env/lib/python3.8/site-packages/nflows/distributions/base.py:40: in log_prob
    return self._log_prob(inputs, context)
.sbi_env/lib/python3.8/site-packages/nflows/flows/base.py:39: in _log_prob
    noise, logabsdet = self._transform(inputs, context=embedded_context)
.sbi_env/lib/python3.8/site-packages/torch/nn/modules/module.py:722: in _call_impl
    result = self.forward(*input, **kwargs)
.sbi_env/lib/python3.8/site-packages/nflows/transforms/base.py:56: in forward
    return self._cascade(inputs, funcs, context)
.sbi_env/lib/python3.8/site-packages/nflows/transforms/base.py:50: in _cascade
    outputs, logabsdet = func(outputs, context)
.sbi_env/lib/python3.8/site-packages/torch/nn/modules/module.py:722: in _call_impl
    result = self.forward(*input, **kwargs)
.sbi_env/lib/python3.8/site-packages/nflows/transforms/base.py:56: in forward
    return self._cascade(inputs, funcs, context)
.sbi_env/lib/python3.8/site-packages/nflows/transforms/base.py:50: in _cascade
    outputs, logabsdet = func(outputs, context)
.sbi_env/lib/python3.8/site-packages/torch/nn/modules/module.py:722: in _call_impl
    result = self.forward(*input, **kwargs)
.sbi_env/lib/python3.8/site-packages/nflows/transforms/base.py:56: in forward
    return self._cascade(inputs, funcs, context)
.sbi_env/lib/python3.8/site-packages/nflows/transforms/base.py:50: in _cascade
    outputs, logabsdet = func(outputs, context)
.sbi_env/lib/python3.8/site-packages/torch/nn/modules/module.py:722: in _call_impl
    result = self.forward(*input, **kwargs)
.sbi_env/lib/python3.8/site-packages/nflows/transforms/coupling.py:84: in forward
    transform_split, logabsdet = self._coupling_transform_forward(
.sbi_env/lib/python3.8/site-packages/nflows/transforms/coupling.py:194: in _coupling_transform_forward
    return self._coupling_transform(inputs, transform_params, inverse=False)
.sbi_env/lib/python3.8/site-packages/nflows/transforms/coupling.py:211: in _coupling_transform
    outputs, logabsdet = self._piecewise_cdf(inputs, transform_params, inverse)
.sbi_env/lib/python3.8/site-packages/nflows/transforms/coupling.py:492: in _piecewise_cdf
    return spline_fn(
.sbi_env/lib/python3.8/site-packages/nflows/transforms/splines/rational_quadratic.py:45: in unconstrained_rational_quadratic_spline
    ) = rational_quadratic_spline(
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

inputs = tensor([]), unnormalized_widths = tensor([], size=(0, 10)), unnormalized_heights = tensor([], size=(0, 10)), unnormalized_derivatives = tensor([], size=(0, 11))
inverse = False, left = -3.0, right = 3.0, bottom = -3.0, top = 3.0, min_bin_width = 0.001, min_bin_height = 0.001, min_derivative = 0.001

    def rational_quadratic_spline(
        inputs,
        unnormalized_widths,
        unnormalized_heights,
        unnormalized_derivatives,
        inverse=False,
        left=0.0,
        right=1.0,
        bottom=0.0,
        top=1.0,
        min_bin_width=DEFAULT_MIN_BIN_WIDTH,
        min_bin_height=DEFAULT_MIN_BIN_HEIGHT,
        min_derivative=DEFAULT_MIN_DERIVATIVE,
    ):
>       if torch.min(inputs) < left or torch.max(inputs) > right:
E       RuntimeError: operation does not have an identity.

.sbi_env/lib/python3.8/site-packages/nflows/transforms/splines/rational_quadratic.py:77: RuntimeError
```
