Skip to content

Commit

Permalink
Fix duplicate X-values in KDE distribution (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-nml authored Feb 12, 2024
1 parent d1a70ea commit fc374e1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions nannyml/distribution/continuous/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ def _get_kde(array, cut=3, clip=(-np.inf, np.inf)):
try: # pragma: no cover
kde = sm.nonparametric.KDEUnivariate(array)
kde.fit(cut=cut, clip=clip)

# Calculation may return duplicate support values in edge cases. These results are not sensible. Treating it as
# an error case and returning None
if len(np.unique(kde.support)) < len(kde.support):
return None

return kde
except Exception:
return None
Expand Down
6 changes: 6 additions & 0 deletions nannyml/plots/components/joy_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def _get_kde(array, cut=3, clip=(-np.inf, np.inf)):
try: # pragma: no cover
kde = sm.nonparametric.KDEUnivariate(array)
kde.fit(cut=cut, clip=clip)

# Calculation may return duplicate support values in edge cases. These results are not sensible. Treating it as
# an error case and returning None
if len(np.unique(kde.support)) < len(kde.support):
return None

return kde
except Exception:
return None
Expand Down

0 comments on commit fc374e1

Please sign in to comment.