Example #1
0
def get_rich_list(list: list, tree=None):
    import rich.tree

    if all(isinstance(item, dict) for item in list):
        return get_rich_table(list)
    if tree is None:
        tree = rich.tree.Tree(str(type(list)))
    for item in list:
        tree.add(get_rich(item))
    return tree
Example #2
0
    def _rich(self, parent):
        if parent:
            tree = parent.add(f'[bold]{self.data}[/bold]')
        else:
            import rich.tree
            tree = rich.tree.Tree(self.data)

        for c in self.children:
            if isinstance(c, Tree):
                c._rich(tree)
            else:
                tree.add(f'[green]{c}[/green]')

        return tree
Example #3
0
def print_config(
    config: DictConfig,
    fields: Sequence[str] = (
        "trainer",
        "model",
        "datamodule",
        "callbacks",
        "logger",
        "seed",
    ),
    resolve: bool = True,
) -> None:
    """Prints content of DictConfig using Rich library and its tree structure.

    Args:
        config (DictConfig): Configuration composed by Hydra.
        fields (Sequence[str], optional): Determines which main fields from config will
        be printed and in what order.
        resolve (bool, optional): Whether to resolve reference fields of DictConfig.
    """

    style = "dim"
    tree = rich.tree.Tree(":gear: CONFIG", style=style, guide_style=style)

    for field in fields:
        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)

        branch.add(rich.syntax.Syntax(branch_content, "yaml"))

    rich.print(tree)
Example #4
0
def get_rich_dict(dict: dict, tree=None):
    import rich.tree

    p = schemata.base.Properties.forms(dict)
    for k, v in p.items():
        if not isinstance(dict[k], p[k]):
            dict[v] = p[k](dict[k])

    a = schemata.base.AdditionalProperties.forms(dict)
    if a is not None:
        for k, v in dict.items():
            if k in p:
                continue
            dict[k] = a(dict[k])
    if tree is None:
        tree = rich.tree.Tree(get_rich(type(dict)))
    for key, value in dict.items():
        tree.add(rich.tree.Tree(label=key))
        tree.children[-1].add(get_rich(value))
    return tree
Example #5
0
def log_results_rich(
    console: rich.console.Console,
    device: ophyd.Device,
    severity: Severity,
    config: Union[PVConfiguration, DeviceConfiguration],
    results: List[Result],
    *,
    severity_to_rich: Optional[Dict[Severity, str]] = None,
    verbose: int = 0,
):
    """Log check results to the module logger."""
    severity_to_rich = severity_to_rich or default_severity_to_rich

    desc = f" ({config.description}) " if config.description else ""

    tree = rich.tree.Tree(
        f"{severity_to_rich[severity]} [default]{device.name}{desc}")
    for result in results:
        if result.severity > Severity.success or verbose > 0:
            tree.add(
                f"{severity_to_rich[result.severity]}[default]: {result.reason}"
            )
    console.print(tree)
Example #6
0
def print_config(
    config: DictConfig,
    print_order: Sequence[str] = (
        "datamodule",
        "model",
        "callbacks",
        "logger",
        "trainer",
    ),
    resolve: bool = True,
) -> None:
    """Prints content of DictConfig using Rich library and its tree structure.

    Args:
        config (DictConfig): Configuration composed by Hydra.
        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)

    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_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"))

    rich.print(tree)

    with open("config_tree.log", "w") as file:
        rich.print(tree, file=file)
Example #7
0
def print_config(
    config: DictConfig,
    fields: Sequence[str] = ("callbacks", "datamodule", "eval", "mode",
                             "model", "trainer", "work_dir",
                             "test_after_training", "seed", "name",
                             "upload_best_model"),
    resolve: bool = True,
) -> None:
    """Prints content of DictConfig using Rich library and its tree structure.

    Args:
        config (DictConfig): Configuration composed by Hydra.
        fields (Sequence[str], optional): Determines which main fields from config will
        be printed and in what order.
        resolve (bool, optional): Whether to resolve reference fields of DictConfig.
    """

    config_copy = deepcopy(config)
    if 'augmentations' in config_copy.datamodule:
        config_copy.datamodule.augmentations = '...'

    style = "dim"
    tree = rich.tree.Tree("CONFIG", style=style, guide_style=style)

    for field in fields:
        branch = tree.add(field, style=style, guide_style=style)

        config_section = config_copy.get(field)
        branch_content = str(config_section)
        if isinstance(config_section, DictConfig):
            branch_content = OmegaConf.to_yaml(config_section, resolve=resolve)

        branch.add(rich.syntax.Syntax(branch_content, "yaml"))

    rich.print(tree)

    with open("config_tree.txt", "w") as fp:
        rich.print(tree, file=fp)