Skip to content

Commit

Permalink
fix parsing of Triangular distribution (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
j-faria committed Nov 15, 2022
1 parent 9afafca commit 62a6e70
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
6 changes: 6 additions & 0 deletions kima/vendor/DNest4/code/Distributions/Triangular.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class Triangular:public ContinuousDistribution
double cdf(double x) const;
double cdf_inverse(double x) const;
double log_pdf(double x) const;
// ostream representation of Triangular class
virtual std::ostream& print(std::ostream& out) const override
{
out << "Triangular(" << lower << "; " << centre << "; " << upper << ")";
return out;
}
};


Expand Down
16 changes: 11 additions & 5 deletions pykima/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ def _prior_to_dist():
'Cauchy': stats.cauchy,
'InvGamma': lambda shape, scale: stats.invgamma(shape, scale=scale),
'Fixed': Fixed,
'Triangular': lambda lo, ce, up: stats.triang(c=(ce-lo)/(up-lo),
loc=lo, scale=up-lo),
}
return d

Expand Down Expand Up @@ -415,10 +417,13 @@ def find_prior_parameters(prior):
name = name.replace('Truncated', '')
truncated = True

twopars = ('LogUniform', 'ModifiedLogUniform', 'Gaussian', 'Kumaraswamy',
'Cauchy', 'InvGamma')
threepars = ['Triangular']
twopars = [
'LogUniform', 'ModifiedLogUniform', 'Gaussian', 'Kumaraswamy',
'Cauchy', 'InvGamma'
]

if name in twopars:
if name in threepars + twopars:
r = [float(v) for v in inparens.split(';')]
elif name == 'Uniform':
v1, v2 = inparens.split(';')
Expand All @@ -445,9 +450,10 @@ def get_prior(prior):
a, b = find_prior_limits(prior)
loc, scale, _ = pars
a, b = (a - loc) / scale, (b - loc) / scale

try:
d = _prior_to_dist()

if 'T' in pars:
return d[name](a=a, b=b, loc=loc, scale=scale)
else:
Expand Down Expand Up @@ -648,7 +654,7 @@ def read_big_file(filename):
data = np.genfromtxt(filename)
return data

except ImportError: # no pandas, use np.genfromtxt
except (ImportError, Exception): # no pandas, use np.genfromtxt
return np.genfromtxt(filename)


Expand Down

0 comments on commit 62a6e70

Please sign in to comment.