Skip to content
This repository was archived by the owner on Mar 19, 2021. It is now read-only.

Commit 9f9370e

Browse files
committed
Format inf and nan in fit summaries.
Thanks to @ahartikainen and @danielrosenbloom for discussing a solution. Fixes #249
1 parent 57b47a5 commit 9f9370e

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

pystan/misc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ def _number_width(n):
124124

125125
def _format_number_si(num, n_signif_figures):
126126
"""Format a number using scientific notation to given significant figures"""
127+
if math.isnan(num) or math.isinf(num):
128+
return str(num)
127129
leading, exp = '{:E}'.format(num).split('E')
128130
leading = round(float(leading), n_signif_figures - 1)
129131
exp = exp[:1] + exp[2:] if exp[1] == '0' else exp

pystan/tests/test_misc.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ def test_format_number(self):
141141
np.testing.assert_equal(misc._format_number(9.94598693e-01, 2, 6), '0.99')
142142
np.testing.assert_equal(misc._format_number(-9.94598693e-01, 2, 6), '-0.99')
143143

144+
def test_format_number_inf(self):
145+
np.testing.assert_equal(misc._format_number_si(float('inf'), 2), 'inf')
146+
np.testing.assert_equal(misc._format_number_si(float('-inf'), 2), '-inf')
147+
144148
def test_array_to_table(self):
145149
rownames = np.array(['alpha', 'beta[0]', 'beta[1]', 'beta[2]', 'beta[3]', 'sigma', 'lp__'])
146150
colnames = ('mean', 'se_mean', 'sd', '2.5%', '25%', '50%', '75%', '97.5%', 'n_eff', 'Rhat')

0 commit comments

Comments
 (0)