diff --git a/pytools/__init__.py b/pytools/__init__.py index ded899c314670ddeb7c8b1721abaed405e4a21b6..d02a558a2480dd38342425541ad4f6c91323b27f 100644 --- a/pytools/__init__.py +++ b/pytools/__init__.py @@ -1447,12 +1447,12 @@ class Table: col_widths = [max(len(row[i]) for row in self.rows) for i in range(columns)] - lines = [ - "|".join([cell.ljust(col_width) - for cell, col_width in zip(row, col_widths)]) + lines = [" | ".join([cell.ljust(col_width) + for cell, col_width in zip(row, col_widths)]) for row in self.rows] - lines[1:1] = ["+".join("-"*col_width - for col_width in col_widths)] + lines[1:1] = ["+".join("-" * (col_width + 1 + (i > 0)) + for i, col_width in enumerate(col_widths))] + return "\n".join(lines) def latex(self, skip_lines=0, hline_after=None): diff --git a/pytools/convergence.py b/pytools/convergence.py index 61d21824fe6a1b74c6df90a81186260b1246eb27..d2fa414d88ea4890959ba0053590b1656d8761bb 100644 --- a/pytools/convergence.py +++ b/pytools/convergence.py @@ -50,7 +50,13 @@ class EOCRecorder(object): def max_error(self): return max(err for absc, err in self.history) - def pretty_print(self, abscissa_label="h", error_label="Error", gliding_mean=2): + def pretty_print(self, + abscissa_label="h", + error_label="Error", + gliding_mean=2, + abscissa_format="%s", + error_format="%s", + eoc_format="%s"): from pytools import Table tbl = Table() @@ -58,14 +64,18 @@ class EOCRecorder(object): gm_eoc = self.estimate_order_of_convergence(gliding_mean) for i, (absc, err) in enumerate(self.history): + absc_str = abscissa_format % absc + err_str = error_format % err if i < gliding_mean-1: - tbl.add_row((str(absc), str(err), "")) + eoc_str = "" else: - tbl.add_row((str(absc), str(err), str(gm_eoc[i-gliding_mean+1, 1]))) + eoc_str = eoc_format % (gm_eoc[i - gliding_mean + 1, 1]) + + tbl.add_row((absc_str, err_str, eoc_str)) if len(self.history) > 1: - return str(tbl) + "\n\nOverall EOC: %s" \ - % self.estimate_order_of_convergence()[0, 1] + return "%s\n\nOverall EOC: %s" % (str(tbl), + self.estimate_order_of_convergence()[0, 1]) else: return str(tbl) diff --git a/test/test_pytools.py b/test/test_pytools.py index 775b68b1e94f69135f19393664e5353852222a49..9783d3401db98a38df67540c9cebe6ba5a6bad74 100644 --- a/test/test_pytools.py +++ b/test/test_pytools.py @@ -199,6 +199,39 @@ def test_processlogger(): sleep(0.3) +def test_table(): + import math + from pytools import Table + + tbl = Table() + tbl.add_row(("i", "i^2", "i^3", "sqrt(i)")) + + for i in range(8): + tbl.add_row((i, i ** 2, i ** 3, math.sqrt(i))) + + print(tbl) + print() + print(tbl.latex()) + + +def test_eoc(): + from pytools.convergence import EOCRecorder + eoc = EOCRecorder() + + for i in range(1, 8): + eoc.add_data_point(1.0 / i, 10 ** (-i)) + + p = eoc.pretty_print() + print(p) + print() + + p = eoc.pretty_print( + abscissa_format="%.5e", + error_format="%.5e", + eoc_format="%5.2f") + print(p) + + if __name__ == "__main__": if len(sys.argv) > 1: exec(sys.argv[1])