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
|
return logger
|
||||||
|
|
||||||
|
|
||||||
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def extras(config: DictConfig) -> None:
|
def extras(config: DictConfig) -> None:
|
||||||
"""Optional utilities, controlled by hydra config.
|
"""Optional utilities, controlled by hydra config.
|
||||||
|
|
||||||
@@ -37,8 +40,6 @@ def extras(config: DictConfig) -> None:
|
|||||||
config (DictConfig): Configuration composed by Hydra.
|
config (DictConfig): Configuration composed by Hydra.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
log = get_logger(__name__)
|
|
||||||
|
|
||||||
# disable python warnings if <config.ignore_warnings=True>
|
# disable python warnings if <config.ignore_warnings=True>
|
||||||
if config.get("ignore_warnings"):
|
if config.get("ignore_warnings"):
|
||||||
log.info("Disabling python warnings! <config.ignore_warnings=True>")
|
log.info("Disabling python warnings! <config.ignore_warnings=True>")
|
||||||
@@ -48,15 +49,12 @@ def extras(config: DictConfig) -> None:
|
|||||||
@rank_zero_only
|
@rank_zero_only
|
||||||
def print_config(
|
def print_config(
|
||||||
config: DictConfig,
|
config: DictConfig,
|
||||||
fields: Sequence[str] = (
|
print_order: Sequence[str] = (
|
||||||
"trainer",
|
|
||||||
"model",
|
|
||||||
"datamodule",
|
"datamodule",
|
||||||
|
"model",
|
||||||
"callbacks",
|
"callbacks",
|
||||||
"logger",
|
"logger",
|
||||||
"test_after_training",
|
"trainer",
|
||||||
"seed",
|
|
||||||
"name",
|
|
||||||
),
|
),
|
||||||
resolve: bool = True,
|
resolve: bool = True,
|
||||||
) -> None:
|
) -> None:
|
||||||
@@ -64,21 +62,30 @@ def print_config(
|
|||||||
|
|
||||||
Args:
|
Args:
|
||||||
config (DictConfig): Configuration composed by Hydra.
|
config (DictConfig): Configuration composed by Hydra.
|
||||||
fields (Sequence[str], optional): Determines which main fields from config will
|
print_order (Sequence[str], optional): Determines in what order config components are printed.
|
||||||
be printed and in what order.
|
|
||||||
resolve (bool, optional): Whether to resolve reference fields of DictConfig.
|
resolve (bool, optional): Whether to resolve reference fields of DictConfig.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
style = "dim"
|
style = "dim"
|
||||||
tree = rich.tree.Tree("CONFIG", style=style, guide_style=style)
|
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)
|
branch = tree.add(field, style=style, guide_style=style)
|
||||||
|
|
||||||
config_section = config.get(field)
|
config_group = config[field]
|
||||||
branch_content = str(config_section)
|
if isinstance(config_group, DictConfig):
|
||||||
if isinstance(config_section, DictConfig):
|
branch_content = OmegaConf.to_yaml(config_group, resolve=resolve)
|
||||||
branch_content = OmegaConf.to_yaml(config_section, resolve=resolve)
|
else:
|
||||||
|
branch_content = str(config_group)
|
||||||
|
|
||||||
branch.add(rich.syntax.Syntax(branch_content, "yaml"))
|
branch.add(rich.syntax.Syntax(branch_content, "yaml"))
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user