def test_array_to_table(): rownames = np.array(['alpha', 'beta[0]', 'beta[1]', 'beta[2]', 'beta[3]', 'sigma', 'lp__']) colnames = ('mean', 'se_mean', 'sd', '2.5%', '25%', '50%', '75%', '97.5%', 'n_eff', 'Rhat') arr = np.array([[-1655, 625, 1654, -4378, -3485, -927, -27, -9, 7, 2], [ -47, 59, 330, -770, -69, -2, 2, 1060, 31, 1], [ -18, 70, 273, -776, -30, 0, 35, 608, 15, 1], [ 8, 38, 230, -549, -26, 0, 17, 604, 36, 1], [ 23, 32, 303, -748, -34, 0, 49, 751, 92, 1], [ 405, 188, 974, 0, 4, 73, 458, 2203, 27, 1], [ -13, 3, 8, -23, -20, -15, -5, 1, 8, 2]]) n_digits = 2 result = misc._array_to_table(arr, rownames, colnames, n_digits) desired = "alpha -1655 625 1654 -4378 -3485 -927 -27 -9 7 2" desired_py2 = "alpha -1655 625.0 1654.0 -4378 -3485 -927.0 -27.0 -9.0 7.0 2.0" # round() behaves differently in Python 3 if PY2: np.testing.assert_equal(result.split('\n')[1], desired_py2) else: np.testing.assert_equal(result.split('\n')[1], desired) arr = np.array([[-1655325, 625.25, 1654.25, -4378.25, -3485.25, -927.25, -27.25, -9.25, 7.25, 2], [ -47.25, 59.25, 330.25, -770.25, -69.25, -2.25, 2.25, 1060.25, 31.25, 1], [ -18.25, 70.25, 273.25, -776.25, -30.25, 0.25, 35.25, 608.25, 15.25, 1], [ 8.25, 38.25, 230.25, -549.25, -26.25, 0.25, 17.25, 604.25, 36.25, 1], [ 23.25, 32.25, 303.25, -748.25, -34.25, 0.25, 49.25, 751.25, 92.25, 1], [ 405.25, 188.25, 974.25, 0.25, 4.25, 73.25, 458.25, 2203.25, 27.25, 1], [ -13.25, 3.25, 8.25, -23.25, -20.25, -15.25, -5.25, 1.25, 8.25, 2]]) result = misc._array_to_table(arr, rownames, colnames, n_digits) desired = "alpha -1.7e6 625.25 1654.2 -4378 -3485 -927.2 -27.25 -9.25 7 2.0" desired_py2 = "alpha -1.7e6 625.25 1654.2 -4378 -3485 -927.2 -27.25 -9.25 7.0 2.0" # round() behaves differently in Python 3 if PY2: np.testing.assert_equal(result.split('\n')[1], desired_py2) else: np.testing.assert_equal(result.split('\n')[1], desired)
def test_array_to_table(self): rownames = np.array([ 'alpha', 'beta[0]', 'beta[1]', 'beta[2]', 'beta[3]', 'sigma', 'lp__' ]) colnames = ('mean', 'se_mean', 'sd', '2.5%', '25%', '50%', '75%', '97.5%', 'n_eff', 'Rhat') arr = np.array([[-1655, 625, 1654, -4378, -3485, -927, -27, -9, 7, 2], [-47, 59, 330, -770, -69, -2, 2, 1060, 31, 1], [-18, 70, 273, -776, -30, 0, 35, 608, 15, 1], [8, 38, 230, -549, -26, 0, 17, 604, 36, 1], [23, 32, 303, -748, -34, 0, 49, 751, 92, 1], [405, 188, 974, 0, 4, 73, 458, 2203, 27, 1], [-13, 3, 8, -23, -20, -15, -5, 1, 8, 2]]) n_digits = 2 result = misc._array_to_table(arr, rownames, colnames, n_digits) desired = "alpha -1655 625 1654 -4378 -3485 -927 -27 -9 7 2" desired_py2 = "alpha -1655 625.0 1654.0 -4378 -3485 -927.0 -27.0 -9.0 7.0 2.0" # round() behaves differently in Python 3 if PY2: np.testing.assert_equal(result.split('\n')[1], desired_py2) else: np.testing.assert_equal(result.split('\n')[1], desired) arr = np.array([[ -1655325, 625.25, 1654.25, -4378.25, -3485.25, -927.25, -27.25, -9.25, 7.25, 2 ], [ -47.25, 59.25, 330.25, -770.25, -69.25, -2.25, 2.25, 1060.25, 31.25, 1 ], [ -18.25, 70.25, 273.25, -776.25, -30.25, 0.25, 35.25, 608.25, 15.25, 1 ], [ 8.25, 38.25, 230.25, -549.25, -26.25, 0.25, 17.25, 604.25, 36.25, 1 ], [ 23.25, 32.25, 303.25, -748.25, -34.25, 0.25, 49.25, 751.25, 92.25, 1 ], [ 405.25, 188.25, 974.25, 0.25, 4.25, 73.25, 458.25, 2203.25, 27.25, 1 ], [ -13.25, 3.25, 8.25, -23.25, -20.25, -15.25, -5.25, 1.25, 8.25, 2 ]]) result = misc._array_to_table(arr, rownames, colnames, n_digits) desired = "alpha -1.7e6 625.25 1654.2 -4378 -3485 -927.2 -27.25 -9.25 7 2.0" desired_py2 = "alpha -1.7e6 625.25 1654.2 -4378 -3485 -927.2 -27.25 -9.25 7.0 2.0" # round() behaves differently in Python 3 if PY2: np.testing.assert_equal(result.split('\n')[1], desired_py2) else: np.testing.assert_equal(result.split('\n')[1], desired)
def fit2latex(fit, model_name, params, proj_dir): s = _summary(fit, params, [0.025, 0.975]) body = _array_to_table(s['summary'], s['summary_rownames'], s['summary_colnames'], 2) # Convert string to dataframe df = pd.read_fwf(StringIO(body), dtype=str, index_col=0) # Drop columns we don't care about df = df.drop(["se_mean", "sd", "Rhat"], axis=1) df = format_cols(df) # Round the ESS to the nearest 10 df["n_eff"] = df["n_eff"].astype(int) // 100 * 100 # Tidy up column and row names for tex df = rename_cols(df) df = rename_rows(df) df = df.rename_axis('{Parameter}', axis=1) # Column formatting. The S column type is provided by siunitx col_format = "@{}cS[table-format=2.2]S[table-format=2.2]S[table-format=2.2]r@{}" # Print dataframe to latex tex = df.to_latex(escape=False, column_format=col_format) save_path = path.join(proj_dir, 'out', '{}'.format(model_name)) with open(save_path + '.tab', "w") as file: file.write(tex)
def gr2summary_str(gr, ndigits=2): summary = [ gr['model_name'].value, gr.file.filename, prnDict(gr2datadict(gr), braces=False), _array_to_table(gr['summary'][:], gr['summary_rownames'][:], gr['summary_colnames'][:], ndigits) ] return '\n\n'.join(summary)