From 6d2bf1fabb3c72fa7216eb7c7e6da7627832752f Mon Sep 17 00:00:00 2001 From: Ben Butler-Cole Date: Fri, 5 Aug 2022 16:21:38 +0100 Subject: [PATCH 1/2] Decimal outputs can be rounded Adds a command line argument to specify whether to round the outputs (and to how many decimal places). ``` -R ROUND_TO, --round-to ROUND_TO number of digits to round outputs to ``` This addresses two rough edges with models that produce decimal outputs. Firstly they may produce outputs with spurious or uninteresting accuracy, secondly floating point arithmetic may produce unhelpful decimal representations. Both are apparent in the example below. Without rounding: ``` $ cat pipeline.sys | systems-run | cut -f1,14- | expand --tabs=20 Options Initiatives Deliverables Observables Done 0 0 0 0 0 0 1 5 0 0 0 0 2 6 2 0 0 0 3 6 2 2 0 0 4 5.94 2.0 2.16 1.8 0 5 6.832000000000001 2.0 2.2880000000000003 1.8 1.8 6 6.6856 2.0 2.3904 1.8 3.6 7 6.5084800000000005 2.0 2.47232 1.8 5.4 8 6.306784 2.0 2.5378559999999997 1.8 7.2 9 6.0854272 2.0 2.5902847999999996 1.8 9.0 10 5.848341759999999 2.0 2.6322278399999997 1.8 10.8 ``` With rounding: ``` $ cat pipeline.sys | systems-run --round-to 2 | cut -f1,14- | expand --tabs=14 Options Initiatives Deliverables Observables Done 0 0 0 0 0 0 1 5 0 0 0 0 2 6 2 0 0 0 3 6 2 2 0 0 4 5.94 2.0 2.16 1.8 0 5 6.83 2.0 2.29 1.8 1.8 6 6.69 2.0 2.39 1.8 3.6 7 6.51 2.0 2.47 1.8 5.4 8 6.31 2.0 2.54 1.8 7.2 9 6.09 2.0 2.59 1.8 9.0 10 5.85 2.0 2.63 1.8 10.8 ``` --- docs/tutorial.md | 6 ++++-- systems/models.py | 7 +++++-- systems/parse.py | 7 +++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/docs/tutorial.md b/docs/tutorial.md index 52e1a43..22f0756 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -1,4 +1,3 @@ - # Tutorial Please follow the [installation instructions](../) before continuing with these steps. @@ -39,9 +38,12 @@ Then you could run the simulation for 10 rounds: 9 15 45 22 11 5 18 1 10 15 45 22 11 5 22 1 +Sometimes it may be clearer to round the outputs for display: + + systems-run --round-to 2 + You can also get the output as CSV: cat examples/links.txt | systems-run -r10 --csv Which you could... load into a spreadsheet or something to graph! - diff --git a/systems/models.py b/systems/models.py index b112eda..866bb74 100644 --- a/systems/models.py +++ b/systems/models.py @@ -325,7 +325,7 @@ def render_html(self, results): rows += ["", ""] return "\n".join(rows) - def render(self, results, sep='\t', pad=True): + def render(self, results, sep='\t', pad=True, round_to=None): "Render results to string from Model run." lines = [] @@ -338,7 +338,10 @@ def render(self, results, sep='\t', pad=True): for i, snapshot in enumerate(results): row = "%s" % i for j, col in enumerate(col_stocks): - num = str(snapshot[col.name]) + value = snapshot[col.name] + if round_to: + value = round(value, round_to) + num = str(value) if pad: num = num.ljust(col_size[j]) diff --git a/systems/parse.py b/systems/parse.py index 29d9edc..37ff1d0 100644 --- a/systems/parse.py +++ b/systems/parse.py @@ -121,6 +121,11 @@ def main(): help="number of rounds to run evaluation", default=10) p.add_argument('--csv', action='store_true', default=False) + p.add_argument( + '-R', + '--round-to', + type=int, + help="number of digits to round outputs to") args = p.parse_args() txt = sys.stdin.read() @@ -136,6 +141,8 @@ def main(): if args.csv: kwargs['sep'] = ',' kwargs['pad'] = False + if args.round_to: + kwargs['round_to'] = args.round_to print(model.render(results, **kwargs)) From a581610479ca4d8f4e9f88f2968fb3c3002ff4aa Mon Sep 17 00:00:00 2001 From: Ben Butler-Cole Date: Fri, 5 Aug 2022 16:34:59 +0100 Subject: [PATCH 2/2] Include help output in README --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2c63a45..fd657fa 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ - # Systems `systems` is a set of tools for describing, running and visualizing @@ -68,6 +67,17 @@ systems/ `systems-run` is used to run models: + $ systems-run --help + usage: parse.py [-h] [-r ROUNDS] [--csv] [-R ROUND_TO] + + options: + -h, --help show this help message and exit + -r ROUNDS, --rounds ROUNDS + number of rounds to run evaluation + --csv + -R ROUND_TO, --round-to ROUND_TO + number of digits to round outputs to + $ cat examples/hiring.txt | systems-run -r 3 PhoneScreens Onsites Offers Hires Employees Departures 0 0 0 0 0 5 0