Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dask based progress bar #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions esm_analysis/Reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
def progress_bar(*futures, **kwargs):
"""Connect dask futures to tqdm progressbar."""

from .utils import (ProgressBar, NotebookProgress)

notebook = kwargs.pop('notebook', None)
multi = kwargs.pop('multi', True)
complete = kwargs.pop('complete', True)
Expand All @@ -45,8 +47,10 @@ def progress_bar(*futures, **kwargs):
if notebook is None:
notebook = is_kernel() # often but not always correct assumption

progress = tqdm.tqdm_notebook if notebook else tqdm.tqdm
_ = list(progress(as_completed(futures), **kwargs))
#progress = tqdm.tqdm_notebook if notebook else tqdm.tqdm
#_ = list(progress(as_completed(futures), **kwargs))
progress = ProgressBar if notebook else NotebookProgress
return progress(futures, **kwargs)


class _BaseVariables(dict):
Expand Down
5 changes: 4 additions & 1 deletion esm_analysis/tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ def test_client(mock_run, mock_client):

def test_progress_bar(mock_client):
future = mock_client.submit(lambda: 2*6)
progress_bar(future)
progress_bar(future, notebook=False)
assert future.result() == 12
future = mock_client.submit(lambda: 2*6)
progress_bar(future, notebook=True)
assert future.result() == 12

def test_apply_function(mock_run, mock_client):
Expand Down
59 changes: 59 additions & 0 deletions esm_analysis/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import tqdm
from tornado.ioloop import IOLoop
from distributed.utils import LoopRunner, is_kernel
from distributed.client import futures_of
from distributed.diagnostics.progressbar import ProgressBar

class ProgressBar(ProgressBar):
def __init__(
self,
keys,
scheduler=None,
interval="100ms",
loop=None,
complete=True,
start=True,
**tqdm_kwargs
):
super(ProgressBar, self).__init__(
keys, scheduler, interval, complete)
self.tqdm = tqdm.tqdm(keys, **tqdm_kwargs)
self.loop = loop or IOLoop()

if start:
loop_runner = LoopRunner(self.loop)
loop_runner.run_sync(self.listen)

def _draw_bar(self, remaining, all, **kwargs):
update_ct = (all - remaining) - self.tqdm.n
self.tqdm.update(update_ct)

def _draw_stop(self, **kwargs):
self.tqdm.close()

class NotebookProgress(ProgressBar):
def __init__(
self,
keys,
scheduler=None,
interval="100ms",
loop=None,
complete=True,
start=True,
**tqdm_kwargs
):
super(NotebookProgress, self).__init__(
keys, scheduler, interval, complete)
self.tqdm = tqdm.tqdm_notebook(keys, **tqdm_kwargs)
self.loop = loop or IOLoop()

if start:
loop_runner = LoopRunner(self.loop)
loop_runner.run_sync(self.listen)

def _draw_bar(self, remaining, all, **kwargs):
update_ct = (all - remaining) - self.tqdm.n
self.tqdm.update(update_ct)

def _draw_stop(self, **kwargs):
self.tqdm.close()