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

modified progress bar formatting to inherit from flwr formatting #273

Merged
merged 1 commit into from
Oct 31, 2024
Merged
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
21 changes: 18 additions & 3 deletions fl4health/clients/basic_client.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import copy
import datetime
import os
from collections.abc import Iterable, Sequence
from enum import Enum
from logging import INFO, WARNING
from inspect import currentframe, getframeinfo
from logging import INFO, WARNING, LogRecord
from pathlib import Path
from typing import Any, Optional, Tuple, Union

import torch
import torch.nn as nn
from flwr.client import NumPyClient
from flwr.common.logger import LOG_COLORS, log
from flwr.common.logger import LOGGER_NAME, console_handler, log
from flwr.common.typing import Config, NDArrays, Scalar
from torch.nn.modules.loss import _Loss
from torch.optim import Optimizer
Expand Down Expand Up @@ -1264,14 +1266,27 @@ def maybe_progress_bar(self, iterable: Iterable) -> Iterable:
if not self.progress_bar:
return iterable
else:
# We can use the flwr console handler to format progress bar
frame = currentframe()
lineno = 0 if frame is None else getframeinfo(frame).lineno
record = LogRecord(
name=LOGGER_NAME,
pathname=os.path.abspath(os.getcwd()),
lineno=lineno, #
args={},
exc_info=None,
level=INFO,
msg="{l_bar}{bar}{r_bar}",
)
format = console_handler.format(record)
# Create a clean looking tqdm instance that matches the flwr logging
kwargs: Any = {
"leave": True,
"ascii": " >=",
# "desc": f"{LOG_COLORS['INFO']}INFO{LOG_COLORS['RESET']} ",
"unit": "steps",
"dynamic_ncols": True,
"bar_format": f"{LOG_COLORS['INFO']}INFO{LOG_COLORS['RESET']}" + " : {l_bar}{bar}{r_bar}",
"bar_format": format,
}
return tqdm(iterable, **kwargs)

Expand Down