Skip to content

Add option to show single progress bar for all workers #203

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

Open
wants to merge 3 commits 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
9 changes: 7 additions & 2 deletions pandarallel/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Any, Callable, Dict, Iterator, Optional, Tuple, Type, cast
from functools import partial

import dill
import pandas as pd
Expand Down Expand Up @@ -205,6 +206,7 @@ def parallelize_with_memory_file_system(
nb_requested_workers: int,
data_type: Type[DataType],
progress_bars_type: ProgressBarsType,
single_bar
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to have type hints for function arguments.

):
def closure(
data: Any,
Expand Down Expand Up @@ -239,7 +241,7 @@ def closure(

show_progress_bars = progress_bars_type != ProgressBarsType.No

progress_bars = get_progress_bars(progresses_length, show_progress_bars)
progress_bars = get_progress_bars(progresses_length, show_progress_bars, single_bar)
progresses = [0] * nb_workers
workers_status = [WorkerStatus.Running] * nb_workers

Expand Down Expand Up @@ -344,6 +346,7 @@ def parallelize_with_pipe(
nb_requested_workers: int,
data_type: Type[DataType],
progress_bars_type: ProgressBarsType,
single_bar
):
def closure(
data: Any,
Expand Down Expand Up @@ -380,7 +383,7 @@ def closure(

show_progress_bars = progress_bars_type != ProgressBarsType.No

progress_bars = get_progress_bars(progresses_length, show_progress_bars)
progress_bars = get_progress_bars(progresses_length, show_progress_bars, single_bar)
progresses = [0] * nb_workers
workers_status = [WorkerStatus.Running] * nb_workers

Expand Down Expand Up @@ -444,6 +447,7 @@ def initialize(
shm_size_mb=None,
nb_workers=NB_PHYSICAL_CORES,
progress_bar=False,
single_progress_bar=False,
verbose=2,
use_memory_fs: Optional[bool] = None,
) -> None:
Expand All @@ -459,6 +463,7 @@ def initialize(
if use_memory_fs
else parallelize_with_pipe
)
parallelize = partial(parallelize, single_bar=single_progress_bar)

if use_memory_fs and not is_memory_fs_available:
raise SystemError("Memory file system is not available")
Expand Down
23 changes: 16 additions & 7 deletions pandarallel/progress_bars.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@


class ProgressBarsConsole(ProgressBars):
def __init__(self, maxs: List[int], show: bool) -> None:
def __init__(self, maxs: List[int], show: bool, single_bar=True) -> None:
self.__show = show
self.__single_bar = single_bar
if self.__single_bar:
maxs = [sum(maxs)]

Check warning on line 61 in pandarallel/progress_bars.py

View check run for this annotation

Codecov / codecov/patch

pandarallel/progress_bars.py#L61

Added line #L61 was not covered by tests
self.__bars = [[0, max] for max in maxs]
self.__width = self.__get_width()

Expand Down Expand Up @@ -107,6 +110,8 @@
if not self.__show:
return

if self.__single_bar:
values = [sum(values)]

Check warning on line 114 in pandarallel/progress_bars.py

View check run for this annotation

Codecov / codecov/patch

pandarallel/progress_bars.py#L114

Added line #L114 was not covered by tests
for index, value in enumerate(values):
self.__bars[index][0] = value

Expand All @@ -118,7 +123,7 @@


class ProgressBarsNotebookLab(ProgressBars):
def __init__(self, maxs: List[int], show: bool) -> None:
def __init__(self, maxs: List[int], show: bool, single_bar=True) -> None:
"""Initialization.
Positional argument:
maxs - List containing the max value of each progress bar
Expand All @@ -131,6 +136,9 @@
from IPython.display import display
from ipywidgets import HBox, IntProgress, Label, VBox

self.__single_bar = single_bar
if self.__single_bar:
maxs = [sum(maxs)]

Check warning on line 141 in pandarallel/progress_bars.py

View check run for this annotation

Codecov / codecov/patch

pandarallel/progress_bars.py#L139-L141

Added lines #L139 - L141 were not covered by tests
self.__bars = [
HBox(
[
Expand All @@ -150,7 +158,8 @@
"""
if not self.__show:
return

if self.__single_bar:
values = [sum(values)]

Check warning on line 162 in pandarallel/progress_bars.py

View check run for this annotation

Codecov / codecov/patch

pandarallel/progress_bars.py#L161-L162

Added lines #L161 - L162 were not covered by tests
for index, value in enumerate(values):
bar, label = self.__bars[index].children

Expand All @@ -166,18 +175,18 @@
"""Set a bar on error"""
if not self.__show:
return

if self.__single_bar: index = 0

Check warning on line 178 in pandarallel/progress_bars.py

View check run for this annotation

Codecov / codecov/patch

pandarallel/progress_bars.py#L178

Added line #L178 was not covered by tests
bar, _ = self.__bars[index].children
bar.bar_style = "danger"


def get_progress_bars(
maxs: List[int], show
maxs: List[int], show, single_bar
) -> Union[ProgressBarsNotebookLab, ProgressBarsConsole]:
return (
ProgressBarsNotebookLab(maxs, show)
ProgressBarsNotebookLab(maxs, show, single_bar)
if is_notebook_lab()
else ProgressBarsConsole(maxs, show)
else ProgressBarsConsole(maxs, show, single_bar)
)


Expand Down