Skip to content

Commit

Permalink
Adding progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlee94 committed Nov 22, 2023
1 parent 7b99a10 commit 269a097
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
29 changes: 27 additions & 2 deletions idaes/core/util/parameter_sweep.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
IDAES Parameter Sweep API and sequential workflow runner.
"""

import sys
import json

from pandas import DataFrame
Expand All @@ -32,7 +33,6 @@
_log = idaeslog.getLogger(__name__)

# TODO: Timeouts
# TODO: Progress bar/indicator
# TODO: Re-initialize option/callback


Expand Down Expand Up @@ -480,7 +480,7 @@ def execute_single_sample(self, sample_id: int):

solved = check_optimal_termination(status)
if not solved:
_log.error(f"Sample: {sample_id} failed to converge.")
_log.warning(f"Sample {sample_id} failed to converge.")
except Exception as e:
if self.config.halt_on_error:
raise
Expand All @@ -495,6 +495,8 @@ def execute_single_sample(self, sample_id: int):
# Catch any Exception for recourse
results, solved = self.handle_error(model)

_log.info(f"Sample {sample_id} finished.")

return results, solved, error

def get_initialized_model(self):
Expand Down Expand Up @@ -762,6 +764,25 @@ def from_json_file(self, filename: str):
self.from_dict(json.load(f))
f.close()

@staticmethod
def progress_bar(fraction, msg: str, length: int = 20):
"""
Prints a progress bar to stdout
Args:
fraction: fraction of total samples executed
msg: string to append to progress bar
length: length of progress bar (default=20)
Returns:
None
"""
n_complete = int(length * fraction)
n_remaining = length - n_complete
characters = f"{'*' * n_complete}{'-' * n_remaining}"
sys.stdout.write(f"{fraction * 100:.1f}% {characters} {msg}\n")
sys.stdout.flush()


class SequentialSweepRunner(ParameterSweepBase):
"""
Expand All @@ -781,8 +802,12 @@ def execute_parameter_sweep(self):
self._results = {}
samples = self.get_input_samples()

count = 1
for s in samples.index:
sresults, solved, error = self.execute_single_sample(s)
self._results[s] = {"solved": solved, "results": sresults, "error": error}

self.progress_bar(float(count) / float(len(samples)), "Complete")
count += 1

return self.results
16 changes: 13 additions & 3 deletions idaes/core/util/tests/test_parameter_sweep.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import pytest
import os
import os.path

from pandas import DataFrame, Series
from pandas.testing import assert_frame_equal, assert_series_equal
Expand All @@ -31,7 +30,6 @@
assert_optimal_termination,
)
from pyomo.common.fileutils import this_file_dir
from pyomo.solvers.plugins.solvers.IPOPT import IPOPT
from pyomo.common.tempfiles import TempfileManager

from idaes.core.util.parameter_sweep import (
Expand All @@ -47,6 +45,7 @@
CVTSampling,
)
from idaes.core.util.exceptions import ConfigurationError
import idaes.logger as idaeslog

currdir = this_file_dir()

Expand Down Expand Up @@ -984,7 +983,9 @@ def dummy_recourse(model, arg1=None, arg2=None):

@pytest.mark.component
@pytest.mark.solver
def test_execute_single_sample(self):
def test_execute_single_sample(self, caplog):
caplog.set_level(idaeslog.DEBUG)

def build_model():
m = ConcreteModel()
m.v1 = Var(initialize=1)
Expand Down Expand Up @@ -1018,6 +1019,8 @@ def collect_results(model, status, run_stats):
assert solved
assert error is None

assert "Sample 1 finished." in caplog.text

@pytest.mark.component
def test_execute_single_sample_recourse_none(self):
def build_model():
Expand Down Expand Up @@ -1287,6 +1290,13 @@ def test_get_sample_values(self, psweep_with_results):
vals = psweep_with_results.get_sample_values(0)
assert_series_equal(expected, vals)

@pytest.mark.unit
def test_progress_bar(self, capsys):
psweep = ParameterSweepBase()
psweep.progress_bar(0.25, "testing", 16)
out, err = capsys.readouterr()
assert "25.0% ****------------ testing\n" in out


class TestSequentialSweepRunner:
@pytest.mark.component
Expand Down

0 comments on commit 269a097

Please sign in to comment.