Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

RuntimeError in kde plot when data includes NaNs #96

Closed
lukashergt opened this issue Jun 8, 2020 · 0 comments · Fixed by #102
Closed

RuntimeError in kde plot when data includes NaNs #96

lukashergt opened this issue Jun 8, 2020 · 0 comments · Fixed by #102
Labels
bug Something isn't working

Comments

@lukashergt
Copy link
Collaborator

When the data contains NaNs kde_contour_plot_2d raises a RuntimeError. This should not happen for cases where the corresponding logL is -inf and weight is 0. In such cases these entries should be simply ignored.

NaNs can appear for derived parameters which never got calculated when the sampling parameters fell into an excluded region (logL=-inf and weight=0) of parameter space.

MWE

data: unilog_r.log

data = pandas.read_csv("unilog_r.log", sep='\t')
ns = NestedSamples(data=data[['n_s', 'r', 'logr']], logL=data.logL, logL_birth=data.logL_birth, w=data.weight)
ns.n_s[:10] = np.nan
assert np.all(ns.weight[:10] == 0)
fig = plt.figure(figsize=(5, 5))
fig, axes = make_2d_axes(['n_s', 'r', 'logr'], fig=fig)
ns.plot_2d(axes)
axes.iloc[-1, 0].legend(bbox_to_anchor=(len(axes), len(axes)), loc='upper left');

The error message:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-160-14d6415b9f86> in <module>
      5 fig = plt.figure(figsize=(5, 5))
      6 fig, axes = make_2d_axes(['n_s', 'r', 'logr'], fig=fig)
----> 7 ns.plot_2d(axes)
      8 axes.iloc[-1, 0].legend(bbox_to_anchor=(len(axes), len(axes)), loc='upper left');

~/.virtualenvs/py38env/lib/python3.8/site-packages/anesthetic/samples.py in plot_2d(self, axes, *args, **kwargs)
    352                     plot_type = types.get(pos, None)
    353                     lkwargs = local_kwargs.get(pos, {})
--> 354                     self.plot(ax_, x, y, plot_type=plot_type, *args, **lkwargs)
    355 
    356         return fig, axes

~/.virtualenvs/py38env/lib/python3.8/site-packages/anesthetic/samples.py in plot(self, ax, paramname_x, paramname_y, *args, **kwargs)
    192                     x = self[paramname_x]
    193                     y = self[paramname_y]
--> 194                     return kde_contour_plot_2d(ax, x, y, weights=self.weight,
    195                                                xmin=xmin, xmax=xmax,
    196                                                ymin=ymin, ymax=ymax,

~/.virtualenvs/py38env/lib/python3.8/site-packages/anesthetic/plot.py in kde_contour_plot_2d(ax, data_x, data_y, *args, **kwargs)
    528 
    529     cov = np.cov(data_x, data_y, aweights=weights)
--> 530     tri, w = triangular_sample_compression_2d(data_x, data_y, cov,
    531                                               weights, ncompress)
    532     kde = gaussian_kde([tri.x, tri.y], weights=w)

~/.virtualenvs/py38env/lib/python3.8/site-packages/anesthetic/utils.py in triangular_sample_compression_2d(x, y, cov, w, n)
    300 
    301     # Generate triangulation
--> 302     tri = scaled_triangulation(x[i], y[i], cov)
    303 
    304     # For each point find corresponding triangles

~/.virtualenvs/py38env/lib/python3.8/site-packages/anesthetic/utils.py in scaled_triangulation(x, y, cov)
    257     Linv = np.linalg.inv(L)
    258     x_, y_ = Linv.dot([x, y])
--> 259     tri = Triangulation(x_, y_)
    260     return Triangulation(x, y, tri.triangles)
    261 

~/.virtualenvs/py38env/lib/python3.8/site-packages/matplotlib/tri/triangulation.py in __init__(self, x, y, triangles, mask)
     52             # No triangulation specified, so use matplotlib._qhull to obtain
     53             # Delaunay triangulation.
---> 54             self.triangles, self._neighbors = _qhull.delaunay(x, y)
     55             self.is_delaunay = True
     56         else:

RuntimeError: Error in qhull Delaunay triangulation calculation: input inconsistency (exitcode=1); use python verbose option (-v) to see original qhull error.
@lukashergt lukashergt added the bug Something isn't working label Jun 8, 2020
williamjameshandley added a commit that referenced this issue Apr 9, 2024
* Fix for logL_birth

* bump version to 2.1.5

* bump version to 2.1.6

* now avoiding dropna where possible and reducing code repetition

* version bump to 2.2.2

* Dropping all infs with warnings

* Corrected bump_version script

* Added a scatter kind

* fix mistakenly changed DOI in README

* check for inf directly instead of indirectly using isfinite, since nans are actually ok here

* add `pytest.warns` to tests to explicitly check for the existance of the newly added warnings and to keep our pytest output clean

* Update samples.py for pep8

* version bump to 2.5.2

* bump version to 2.7.4

* Removed obselete test for #96

* Update README.rst version to 2.8.5

* Update _version.py to 2.8.5

* bump version to 2.8.6

* bump version to 2.8.7

* bump version to 2.8.8

* bump version to 2.8.9

---------

Co-authored-by: Lukas Hergt <[email protected]>
Co-authored-by: Lukas Hergt <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant