Download src/telemetry_logger.py from ezharjan/SemanticPotentialRoutingTelemetry: direct link, hf CLI and curl.
- Browser
- Download file 9.1 kB
-
https://huggingface.co/datasets/ezharjan/SemanticPotentialRoutingTelemetry/resolve/main/src/telemetry_logger.py
- Command line
-
hf download hf://datasets/ezharjan/SemanticPotentialRoutingTelemetry/src/telemetry_logger.py
-
curl -L -o telemetry_logger.py https://huggingface.co/datasets/ezharjan/SemanticPotentialRoutingTelemetry/resolve/main/src/telemetry_logger.py
9.1 kB
| """Streaming Parquet serialisation and the dataset manifest. | |
| The dataset is a relational schema of eight tables. Each shard of episodes is written as one | |
| ``data/<table>/part-XXXXX.parquet`` file (zstd, modest row groups so readers can stream), through | |
| a temporary file that is renamed only once complete. A shard is therefore either fully present or | |
| absent, which makes an interrupted sweep resumable and keeps memory bounded to one shard. | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import platform | |
| import sys | |
| from datetime import datetime, timezone | |
| from pathlib import Path | |
| from typing import Dict, List, Sequence | |
| import numpy as np | |
| import pyarrow as pa | |
| import pyarrow.parquet as pq | |
| ROW_GROUP_SIZE = 8192 | |
| SCHEMAS: Dict[str, pa.Schema] = { | |
| # One row per episode: design cell, split, static graph, flows and traffic parameters. | |
| "episodes": pa.schema([ | |
| ("episode_id", pa.int32()), ("cell_id", pa.int16()), ("replicate", pa.int16()), ("split", pa.string()), | |
| ("topology", pa.string()), ("size", pa.int16()), ("traffic_profile", pa.string()), | |
| ("load_level", pa.string()), ("dynamics_level", pa.string()), | |
| ("n_nodes", pa.int16()), ("n_edges", pa.int32()), ("n_flows", pa.int16()), ("tracked_flows", pa.int16()), | |
| ("steps", pa.int32()), ("field_stride", pa.int16()), | |
| ("offered_load", pa.float32()), ("total_capacity", pa.float32()), | |
| ("edge_u", pa.list_(pa.int16())), ("edge_v", pa.list_(pa.int16())), | |
| ("capacity", pa.list_(pa.int16())), ("latency", pa.list_(pa.int16())), | |
| ("node_role", pa.list_(pa.int8())), ("node_x", pa.list_(pa.float32())), ("node_y", pa.list_(pa.float32())), | |
| ("flow_source", pa.list_(pa.int16())), ("flow_sink", pa.list_(pa.int16())), | |
| ("flow_mean_rate", pa.list_(pa.float32())), ("flow_idle_rate", pa.list_(pa.float32())), | |
| ("flow_burst_rate", pa.list_(pa.float32())), | |
| ("p_idle_to_burst", pa.float32()), ("p_burst_to_idle", pa.float32()), | |
| ]), | |
| # One row per topology event; active for start <= step < end. | |
| "events": pa.schema([ | |
| ("episode_id", pa.int32()), ("kind", pa.string()), ("start", pa.int32()), ("end", pa.int32()), | |
| ("node", pa.int16()), ("edge_u", pa.int16()), ("edge_v", pa.int16()), ("factor", pa.float32()), | |
| ]), | |
| # One row per (episode, router): network-level benchmark figures. | |
| "router_summary": pa.schema([ | |
| ("episode_id", pa.int32()), ("router", pa.string()), | |
| ("offered", pa.int32()), ("delivered", pa.int32()), ("dropped", pa.int32()), ("in_flight", pa.int32()), | |
| ("loss_ratio", pa.float32()), ("mean_delay", pa.float32()), ("p99_delay", pa.float32()), | |
| ("mean_queue", pa.float32()), ("max_queue", pa.int32()), | |
| ("link_utilisation", pa.float32()), ("link_saturation", pa.float32()), | |
| ("route_changes", pa.int32()), | |
| ]), | |
| # One row per (episode, router, flow): end-to-end benchmark figures. | |
| "flow_summary": pa.schema([ | |
| ("episode_id", pa.int32()), ("router", pa.string()), ("flow", pa.int16()), | |
| ("source", pa.int16()), ("sink", pa.int16()), ("mean_rate", pa.float32()), | |
| ("min_hops", pa.int16()), ("min_latency", pa.int16()), | |
| ("offered", pa.int32()), ("delivered", pa.int32()), ("dropped", pa.int32()), ("in_flight", pa.int32()), | |
| ("loss_ratio", pa.float32()), ("mean_delay", pa.float32()), ("delay_std", pa.float32()), | |
| ("p50_delay", pa.float32()), ("p95_delay", pa.float32()), ("p99_delay", pa.float32()), | |
| ("max_delay", pa.int32()), ("mean_queueing_delay", pa.float32()), ("mean_path_latency", pa.float32()), | |
| ("mean_hops", pa.float32()), ("route_changes", pa.int32()), | |
| ]), | |
| # One row per (episode, router, step, tracked flow). | |
| "flow_telemetry": pa.schema([ | |
| ("episode_id", pa.int32()), ("router", pa.string()), ("step", pa.int32()), ("flow", pa.int16()), | |
| ("mmpp_state", pa.int8()), ("offered", pa.int32()), ("admitted", pa.int32()), | |
| ("delivered", pa.int32()), ("dropped", pa.int32()), ("queued", pa.int32()), ("in_transit", pa.int32()), | |
| ("mean_delay", pa.float32()), ("route_changes", pa.int32()), | |
| ]), | |
| # One row per (episode, router, step): network totals plus occupancy and drops of every node. | |
| "network_telemetry": pa.schema([ | |
| ("episode_id", pa.int32()), ("router", pa.string()), ("step", pa.int32()), | |
| ("offered", pa.int32()), ("admitted", pa.int32()), ("delivered", pa.int32()), ("dropped", pa.int32()), | |
| ("queued", pa.int32()), ("in_transit", pa.int32()), ("mean_delay", pa.float32()), ("route_changes", pa.int32()), | |
| ("queue_depth", pa.list_(pa.int16())), ("node_dropped", pa.list_(pa.int16())), | |
| ]), | |
| # One row per (episode, router, step): packets forwarded over every link, per direction. | |
| "link_telemetry": pa.schema([ | |
| ("episode_id", pa.int32()), ("router", pa.string()), ("step", pa.int32()), | |
| ("load_uv", pa.list_(pa.int16())), ("load_vu", pa.list_(pa.int16())), | |
| ]), | |
| # One row per logged step of the potential router: φ of the tracked flows, flattened (tracked × N). | |
| "potential_field": pa.schema([ | |
| ("episode_id", pa.int32()), ("step", pa.int32()), ("potential", pa.list_(pa.float32())), | |
| ]), | |
| } | |
| def table_names(routers: Sequence[str]) -> List[str]: | |
| return [t for t in SCHEMAS if t != "potential_field" or "potential" in routers] | |
| def list_column(matrix: np.ndarray, value_type: pa.DataType) -> pa.ListArray: | |
| """A (rows, width) array as a list<value_type> column with one fixed-width list per row.""" | |
| rows, width = matrix.shape | |
| offsets = np.arange(0, (rows + 1) * width, width, dtype=np.int32) | |
| return pa.ListArray.from_arrays(pa.array(offsets), pa.array(np.ascontiguousarray(matrix).ravel(), type=value_type)) | |
| def shard_path(data_dir: Path, table: str, shard: int) -> Path: | |
| return data_dir / table / f"part-{shard:05d}.parquet" | |
| def shard_files(data_dir: Path, table: str) -> List[Path]: | |
| return sorted((data_dir / table).glob("part-*.parquet")) | |
| def completed_shards(data_dir: Path, n_shards: int, routers: Sequence[str]) -> List[int]: | |
| for stale in data_dir.glob("*/*.tmp"): | |
| stale.unlink() | |
| return [s for s in range(n_shards) | |
| if all(shard_path(data_dir, t, s).exists() for t in table_names(routers))] | |
| def write_shard(data_dir: Path, shard: int, episodes: List[Dict[str, pa.Table]]) -> None: | |
| for table in episodes[0]: | |
| final = shard_path(data_dir, table, shard) | |
| final.parent.mkdir(parents=True, exist_ok=True) | |
| tmp = final.with_name(final.name + ".tmp") | |
| pq.write_table(pa.concat_tables([e[table] for e in episodes]), tmp, | |
| compression="zstd", row_group_size=ROW_GROUP_SIZE) | |
| tmp.replace(final) | |
| def read_table(data_dir: Path, table: str, columns=None, filters=None) -> pa.Table: | |
| return pq.read_table(shard_files(data_dir, table), columns=columns, filters=filters) | |
| def table_stats(data_dir: Path, table: str) -> Dict[str, int]: | |
| files = shard_files(data_dir, table) | |
| return {"files": len(files), | |
| "rows": sum(pq.read_metadata(f).num_rows for f in files), | |
| "bytes": sum(f.stat().st_size for f in files)} | |
| def write_manifest(data_dir: Path, cfg, dataset_version: str) -> Dict: | |
| """Provenance, design coverage and table statistics of the shards present in `data_dir`.""" | |
| import networkx, pandas, scipy # noqa: E401 (versions only) | |
| episodes = read_table(data_dir, "episodes", | |
| columns=["episode_id", "cell_id", "split", "topology", "size", | |
| "traffic_profile", "load_level", "dynamics_level"]).to_pandas() | |
| coverage = {name: episodes[name].value_counts().sort_index().to_dict() | |
| for name in ("topology", "size", "traffic_profile", "load_level", "dynamics_level", "split")} | |
| per_cell = episodes.cell_id.value_counts() | |
| manifest = { | |
| "dataset_version": dataset_version, | |
| "created_utc": datetime.now(timezone.utc).isoformat(timespec="seconds"), | |
| "provenance": { | |
| "python": sys.version.split()[0], "platform": platform.platform(), | |
| "numpy": np.__version__, "scipy": scipy.__version__, "pyarrow": pa.__version__, | |
| "pandas": pandas.__version__, "networkx": networkx.__version__, | |
| }, | |
| "config": json.loads(cfg.to_json()), | |
| "design": {"cells": len(cfg.cells), "episodes_planned": cfg.episodes, | |
| "episodes_present": int(len(episodes)), | |
| "episodes_per_cell_min": int(per_cell.min()), "episodes_per_cell_max": int(per_cell.max())}, | |
| "coverage": {k: {str(kk): int(vv) for kk, vv in v.items()} for k, v in coverage.items()}, | |
| "tables": {name: table_stats(data_dir, name) for name in table_names(cfg.routers)}, | |
| } | |
| (data_dir / "manifest.json").write_text(json.dumps(manifest, indent=2), encoding="utf-8") | |
| return manifest | |