Skip to content

Commit cca4d64

Browse files
committed
Round values between -1e-5 and 1e-5 to 0
1 parent b6da3ab commit cca4d64

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

switch_model/reporting/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ def define_arguments(argparser):
6262
)
6363

6464

65-
def get_cell_formatter(sig_digits):
65+
def get_cell_formatter(sig_digits, zero_cutoff):
6666
sig_digits_formatter = "{0:." + str(sig_digits) + "g}"
6767

6868
def format_cell(c):
6969
if not isinstance(c, float):
7070
return c
71-
if abs(c) < 1e-8:
71+
if abs(c) < zero_cutoff:
7272
return 0
7373
else:
7474
return sig_digits_formatter.format(c)
@@ -86,7 +86,9 @@ def write_table(instance, *indexes, output_file=None, **kwargs):
8686
# don't know what that is.
8787
if output_file is None:
8888
raise Exception("Must specify output_file in write_table()")
89-
cell_formatter = get_cell_formatter(instance.options.sig_figs_output)
89+
cell_formatter = get_cell_formatter(
90+
instance.options.sig_figs_output, instance.options.zero_cutoff_output
91+
)
9092

9193
if "df" in kwargs:
9294
df = kwargs.pop("df")
@@ -153,7 +155,9 @@ def post_solve(instance, outdir):
153155

154156

155157
def save_generic_results(instance, outdir, sorted_output):
156-
cell_formatter = get_cell_formatter(instance.options.sig_figs_output)
158+
cell_formatter = get_cell_formatter(
159+
instance.options.sig_figs_output, instance.options.zero_cutoff_output
160+
)
157161

158162
components = list(instance.component_objects(Var))
159163
# add Expression objects that should be saved, if any

switch_model/solve.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,12 @@ def define_arguments(argparser):
730730
type=int,
731731
help="The number of significant digits to include in the output by default",
732732
)
733+
argparser.add_argument(
734+
"--zero-cutoff-output",
735+
default=1e-5,
736+
type=float,
737+
help="If the magnitude of an output value is less than this value, it is rounded to 0.",
738+
)
733739

734740
argparser.add_argument(
735741
"--sorted-output",

0 commit comments

Comments
 (0)