From e069f597b53d288170f462b107711e70c58aa6d7 Mon Sep 17 00:00:00 2001 From: Nathan Weinberg <31703736+nathan-weinberg@users.noreply.github.com> Date: Tue, 19 Mar 2024 16:26:42 -0400 Subject: [PATCH] Add handling for ZeroDivisionError (#76) * Add handling for ZeroDivisionError * Changes from session with Mehul --- .gitignore | 1 + README.md | 4 +-- src/touchstone/decision_maker/__init__.py | 31 +++++++++++++++-------- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 08ce389..67a8feb 100644 --- a/.gitignore +++ b/.gitignore @@ -47,3 +47,4 @@ MANIFEST # Per-project virtualenvs .venv*/ +venv*/ diff --git a/README.md b/README.md index 5a9887a..5a5be43 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,8 @@ It is suggested to use a venv to install and run touchstone. ```shell python -m venv /virtual/environment source /virtual/environment/bin/activate -git clone https://github.com/cloud-bulldozer/touchstone -cd touchstone +git clone https://github.com/cloud-bulldozer/benchmark-comparison +cd benchmark-comparison python setup.py develop touchstone_compare -h usage: touchstone_compare [-h] [--version] [--database {elasticsearch}] [--identifier-key IDENTIFIER] -u UUID [UUID ...] [-a ALIASES [ALIASES ...]] [-o {json,yaml,csv}] --config CONFIG [--output-file OUTPUT_FILE] diff --git a/src/touchstone/decision_maker/__init__.py b/src/touchstone/decision_maker/__init__.py index 6124e10..8c69dd2 100644 --- a/src/touchstone/decision_maker/__init__.py +++ b/src/touchstone/decision_maker/__init__.py @@ -29,18 +29,29 @@ def _compare(self, input_dict, compare_dict): # baseline value is the current value plus the tolerancy base_val = input_dict[self.baseline_uuid] + input_dict[self.baseline_uuid] * self.tolerancy / 100 for u, v in input_dict.items(): + # skip input_dict values that are part of the baseline uuid (no comparison to self) if u == self.baseline_uuid: continue - metric_percent = v * 100 / input_dict[self.baseline_uuid] - # If percentage is greater than 100, sustract 100 from it else substract it from 100 - deviation = metric_percent - 100 if metric_percent > 100 else 100 - metric_percent - deviation = -deviation if v < input_dict[self.baseline_uuid] else deviation - if (self.tolerancy >= 0 and v > base_val) or (self.tolerancy < 0 and v < base_val): - result = "Fail" - self.passed = False - self.fails += 1 - else: - result = "Pass" + try: + metric_percent = v * 100 / input_dict[self.baseline_uuid] + except ZeroDivisionError: + # both values are 0 + if (v == 0) and (input_dict[self.baseline_uuid] == 0): + metric_percent = 100 + # just baseline value is 0 + else: + metric_percent = 0 + finally: + # If percentage is greater than 100, sustract 100 from it else substract it from 100 + deviation = metric_percent - 100 if metric_percent > 100 else 100 - metric_percent + deviation = -deviation if v < input_dict[self.baseline_uuid] else deviation + print(f"deviation is {deviation}") + if (self.tolerancy >= 0 and v > base_val) or (self.tolerancy < 0 and v < base_val): + result = "Fail" + self.passed = False + self.fails += 1 + else: + result = "Pass" if result not in compare_dict: compare_dict[result] = {} compare_dict[result] = {