|
| 1 | +"""MDIO Dataset information command.""" |
| 2 | + |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +from typing import TYPE_CHECKING |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +from click import STRING |
| 10 | +from click import Choice |
| 11 | +from click import argument |
| 12 | +from click import command |
| 13 | +from click import option |
| 14 | + |
| 15 | + |
| 16 | +if TYPE_CHECKING: |
| 17 | + from mdio.core import Grid |
| 18 | + |
| 19 | + |
| 20 | +@command(name="info") |
| 21 | +@argument("mdio-path", type=STRING) |
| 22 | +@option( |
| 23 | + "-access", |
| 24 | + "--access-pattern", |
| 25 | + required=False, |
| 26 | + default="012", |
| 27 | + help="Access pattern of the file", |
| 28 | + type=STRING, |
| 29 | + show_default=True, |
| 30 | +) |
| 31 | +@option( |
| 32 | + "-format", |
| 33 | + "--output-format", |
| 34 | + required=False, |
| 35 | + default="pretty", |
| 36 | + help="Output format. Pretty console or JSON.", |
| 37 | + type=Choice(["pretty", "json"]), |
| 38 | + show_default=True, |
| 39 | + show_choices=True, |
| 40 | +) |
| 41 | +def info( |
| 42 | + mdio_path: str, |
| 43 | + output_format: str, |
| 44 | + access_pattern: str, |
| 45 | +) -> None: |
| 46 | + """Provide information on a MDIO dataset. |
| 47 | +
|
| 48 | + By default, this returns human-readable information about the grid and stats for |
| 49 | + the dataset. If output-format is set to json then a json is returned to |
| 50 | + facilitate parsing. |
| 51 | + """ |
| 52 | + from mdio import MDIOReader |
| 53 | + |
| 54 | + reader = MDIOReader( |
| 55 | + mdio_path, |
| 56 | + access_pattern=access_pattern, |
| 57 | + return_metadata=True, |
| 58 | + ) |
| 59 | + |
| 60 | + grid_dict = parse_grid(reader.grid) |
| 61 | + stats_dict = cast_stats(reader.stats) |
| 62 | + |
| 63 | + mdio_info = { |
| 64 | + "path": mdio_path, |
| 65 | + "stats": stats_dict, |
| 66 | + "grid": grid_dict, |
| 67 | + } |
| 68 | + |
| 69 | + if output_format == "pretty": |
| 70 | + pretty_print(mdio_info) |
| 71 | + |
| 72 | + if output_format == "json": |
| 73 | + json_print(mdio_info) |
| 74 | + |
| 75 | + |
| 76 | +def cast_stats(stats_dict: dict[str, Any]) -> dict[str, float]: |
| 77 | + """Normalize all floats to JSON serializable floats.""" |
| 78 | + return {k: float(v) for k, v in stats_dict.items()} |
| 79 | + |
| 80 | + |
| 81 | +def parse_grid(grid: Grid) -> dict[str, dict[str, int | str]]: |
| 82 | + """Extract grid information per dimension.""" |
| 83 | + grid_dict = {} |
| 84 | + for dim_name in grid.dim_names: |
| 85 | + dim = grid.select_dim(dim_name) |
| 86 | + min_ = str(dim.coords[0]) |
| 87 | + max_ = str(dim.coords[-1]) |
| 88 | + size = str(dim.coords.shape[0]) |
| 89 | + grid_dict[dim_name] = {"name": dim_name, "min": min_, "max": max_, "size": size} |
| 90 | + return grid_dict |
| 91 | + |
| 92 | + |
| 93 | +def json_print(mdio_info: dict[str, Any]) -> None: |
| 94 | + """Convert MDIO Info to JSON and pretty print.""" |
| 95 | + from json import dumps as json_dumps |
| 96 | + |
| 97 | + from rich import print |
| 98 | + |
| 99 | + print(json_dumps(mdio_info, indent=2)) |
| 100 | + |
| 101 | + |
| 102 | +def pretty_print(mdio_info: dict[str, Any]) -> None: |
| 103 | + """Print pretty MDIO Info table to console.""" |
| 104 | + from rich.console import Console |
| 105 | + from rich.table import Table |
| 106 | + |
| 107 | + console = Console() |
| 108 | + |
| 109 | + grid_table = Table(show_edge=False) |
| 110 | + grid_table.add_column("Dimension", justify="right", style="cyan", no_wrap=True) |
| 111 | + grid_table.add_column("Min", justify="left", style="magenta") |
| 112 | + grid_table.add_column("Max", justify="left", style="magenta") |
| 113 | + grid_table.add_column("Size", justify="left", style="green") |
| 114 | + |
| 115 | + for _, axis_dict in mdio_info["grid"].items(): |
| 116 | + name, min_, max_, size = axis_dict.values() |
| 117 | + grid_table.add_row(name, min_, max_, size) |
| 118 | + |
| 119 | + stat_table = Table(show_edge=False) |
| 120 | + stat_table.add_column("Stat", justify="right", style="cyan", no_wrap=True) |
| 121 | + stat_table.add_column("Value", justify="left", style="magenta") |
| 122 | + |
| 123 | + for stat, value in mdio_info["stats"].items(): |
| 124 | + stat_table.add_row(stat, f"{value:.4f}") |
| 125 | + |
| 126 | + master_table = Table(title=f"File Information for {mdio_info['path']}") |
| 127 | + master_table.add_column("MDIO Grid", justify="center") |
| 128 | + master_table.add_column("MDIO Statistics", justify="center") |
| 129 | + master_table.add_row(grid_table, stat_table) |
| 130 | + |
| 131 | + console.print(master_table) |
| 132 | + |
| 133 | + |
| 134 | +cli = info |
0 commit comments