Refactor rich config printing (#249)
- Refactor `print_config()` method - Modify `print_config()` so all config groups are always printend
This commit is contained in:
1 parent
19f5454d13
commit
02d3b65aaa
1 file changed
+22
-15
+22
-15
@@ -30,6 +30,9 @@ def get_logger(name=__name__) -> logging.Logger:
|
||||
return logger
|
||||
|
||||
|
||||
log = get_logger(__name__)
|
||||
|
||||
|
||||
def extras(config: DictConfig) -> None:
|
||||
"""Optional utilities, controlled by hydra config.
|
||||
|
||||
@@ -37,8 +40,6 @@ def extras(config: DictConfig) -> None:
|
||||
config (DictConfig): Configuration composed by Hydra.
|
||||
"""
|
||||
|
||||
log = get_logger(__name__)
|
||||
|
||||
# disable python warnings if <config.ignore_warnings=True>
|
||||
if config.get("ignore_warnings"):
|
||||
log.info("Disabling python warnings! <config.ignore_warnings=True>")
|
||||
@@ -48,15 +49,12 @@ def extras(config: DictConfig) -> None:
|
||||
@rank_zero_only
|
||||
def print_config(
|
||||
config: DictConfig,
|
||||
fields: Sequence[str] = (
|
||||
"trainer",
|
||||
"model",
|
||||
print_order: Sequence[str] = (
|
||||
"datamodule",
|
||||
"model",
|
||||
"callbacks",
|
||||
"logger",
|
||||
"test_after_training",
|
||||
"seed",
|
||||
"name",
|
||||
"trainer",
|
||||
),
|
||||
resolve: bool = True,
|
||||
) -> None:
|
||||
@@ -64,21 +62,30 @@ def print_config(
|
||||
|
||||
Args:
|
||||
config (DictConfig): Configuration composed by Hydra.
|
||||
fields (Sequence[str], optional): Determines which main fields from config will
|
||||
be printed and in what order.
|
||||
print_order (Sequence[str], optional): Determines in what order config components are printed.
|
||||
resolve (bool, optional): Whether to resolve reference fields of DictConfig.
|
||||
"""
|
||||
|
||||
style = "dim"
|
||||
tree = rich.tree.Tree("CONFIG", style=style, guide_style=style)
|
||||
|
||||
for field in fields:
|
||||
quee = []
|
||||
|
||||
for field in print_order:
|
||||
quee.append(field) if field in config else log.info(f"Field '{field}' not found in config")
|
||||
|
||||
for field in config:
|
||||
if field not in quee:
|
||||
quee.append(field)
|
||||
|
||||
for field in quee:
|
||||
branch = tree.add(field, style=style, guide_style=style)
|
||||
|
||||
config_section = config.get(field)
|
||||
branch_content = str(config_section)
|
||||
if isinstance(config_section, DictConfig):
|
||||
branch_content = OmegaConf.to_yaml(config_section, resolve=resolve)
|
||||
config_group = config[field]
|
||||
if isinstance(config_group, DictConfig):
|
||||
branch_content = OmegaConf.to_yaml(config_group, resolve=resolve)
|
||||
else:
|
||||
branch_content = str(config_group)
|
||||
|
||||
branch.add(rich.syntax.Syntax(branch_content, "yaml"))
|
||||
|
||||
|
||||
Reference in new issue
Block a user