Skip to content

Commit

Permalink
add progress bar for unit evalution
Browse files Browse the repository at this point in the history
  • Loading branch information
adumasphi committed Jan 16, 2024
1 parent f96c625 commit 1b51259
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on top of `OpenTURNS <http://www.openturns.org>`_, with its users as the target
audience. Documentation is available
`here <http://openturns.github.io/otwrapy/master>`_. The module provides :

- An integrated progress bar for unit evaluation of a sample without parallelization.
- A `Parallelizer` class that converts any
`ot.Function <http://openturns.github.io/openturns/master/user_manual/_generated/openturns.Function.html>`_
into a parallel wrapper using either
Expand Down
4 changes: 3 additions & 1 deletion otwrapy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@
base_dir = os.path.dirname(__file__)

from ._otwrapy import *
from ._progress_bar import *

__all__ = (_otwrapy.__all__)
__all__ = (_otwrapy.__all__ +
_progress_bar.__all__)
42 changes: 38 additions & 4 deletions otwrapy/_otwrapy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import openturns as ot
import numpy as np

from ._progress_bar import update_progress

__author__ = "Felipe Aguirre Martinez"
__copyright__ = "Copyright 2015-2019 Phimeca"
__email__ = "aguirre@phimeca.fr"
Expand Down Expand Up @@ -337,6 +339,36 @@ def __exit__(self, type, value, traceback):
shutil.rmtree(self.dirname)


def _exec_sample_unit_progress_bar(func, verbosity):
"""Return a function that evaluate the sample and provide a progress bar.
Parameters
----------
func : Function or callable
A callable python object, usually a function. The function should take
an input vector as argument and return an output vector.
verbosity : int
If value greater than 0, the progress bar is displayed.
Returns
-------
_exec_sample : Function or callable
The function with the progress bar.
"""

def _exec_sample(X):
X = ot.Sample(X)
Y = ot.Sample(0, func.getOutputDimension())
nb_flush = X.getSize()
for i, x in enumerate(X):
Y.add(func(x))
if verbosity > 0 and X.getSize() > 1:
update_progress(i, X.getSize(), 'Sample evaluation', nb_flush, bar_length=50)
return ot.Sample(Y)

return _exec_sample


def _exec_sample_joblib(func, n_cpus, verbosity):
"""Return a function that executes a sample in parallel using joblib.
Expand Down Expand Up @@ -518,8 +550,10 @@ class Parallelizer(ot.OpenTURNSPythonFunction):
if using 'joblib', pathos or 'multiprocessing' as backend.
verbosity : int (Optional)
verbose parameter when using 'joblib' or 'dask'. Default is 10. When 'dask' is used, 0
means no progress bar, whereas other value activate the progress bar.
verbose parameter when using 'joblib', 'dask' or without parallelization.
Default is 10.
For a unit evaluation, if verbosity > 0, a progress bar is displayed.
When 'dask' is used, 0 means no progress bar, whereas other value activate the progress bar.
dask_args : dict (Optional)
Dictionnary parameters when using 'dask'. It must follow this form:
Expand All @@ -529,7 +563,7 @@ class Parallelizer(ot.OpenTURNSPythonFunction):
The parallelization uses SSHCluster class of dask distributed with 1 thread per worker.
When dask is chosen, the argument n_cpus is not used. The progress bar is enabled if
verbosity != 0.
The dask dashboard is enabled at port 8787.
The dask dashboard is enabled at port 8787.
Examples
--------
Expand Down Expand Up @@ -574,7 +608,7 @@ def __init__(self, wrapper, backend='multiprocessing', n_cpus=-1, verbosity=10,

# This configures how to run samples on the model :
if self.n_cpus == 1:
self._exec_sample = self.wrapper
self._exec_sample = _exec_sample_unit_progress_bar(self.wrapper, verbosity)

elif (backend == 'ipython') or (backend == 'ipyparallel'):
# Check that ipyparallel is installed
Expand Down
52 changes: 52 additions & 0 deletions otwrapy/_progress_bar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
__all__ = ['update_progress']

import sys


def update_progress(i, nb_iter, message='Progress', nb_flush=50, bar_length=50):

"""
Progress bar
Parameters
----------
i : int
The iterative index of the loop
nb_iter : int
The maximum number of iterations
message : str
The message to be display in front the progress bar.
nb_flush : int
Number of progress update to be displayed.
bar_length : int
Size of the progress bar.
Examples
--------
import time
nb_iter = 1000
nb_flush = 51
t0 = time.time()
for i in range(nb_iter):
time.sleep(0.001)
update_progress(i, nb_iter, 'test', nb_flush, bar_length=50)
print('\ntotal time : ', time.time() - t0)
"""
# nb_flush must be at max equal to nb_iter
if nb_flush > nb_iter:
nb_flush = nb_iter
# update the progress bar only everyCheck
everyCheck = round(float(nb_iter) / nb_flush)
if not ((i+1) % everyCheck) or (i+1) == nb_iter:
progress = float(i+1) / nb_iter
status = ""
if progress >= 1:
progress = 1
status = "Done\r\n"
block = int(round(bar_length*progress))
text = "\r{0}: [{1}] {2:0.2f}% {3}".format( message,"="*block + "-"*(bar_length-block),
progress*100, status)
sys.stdout.write(text)
sys.stdout.flush()

0 comments on commit 1b51259

Please sign in to comment.