Skip to content

Commit 286a056

Browse files
committed
feat: added script
1 parent a35046b commit 286a056

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed

autointent/generation/system_resources/__init__.py

Whitespace-only changes.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
"""CLI tool for processing wandb runs and computing system metrics."""
2+
3+
import argparse
4+
import re
5+
from collections.abc import Sequence
6+
7+
from scipy.integrate import trapezoid
8+
9+
import wandb
10+
11+
12+
def calculate_area(metrics: Sequence[float], timestamps: Sequence[float]) -> float:
13+
"""Calculate the area under the curve using the trapezoidal rule.
14+
15+
Args:
16+
metrics (iterable): Array of metric values.
17+
timestamps (iterable): Array of timestamps corresponding to the metric values.
18+
19+
Returns:
20+
float: The computed area under the curve.
21+
22+
Raises:
23+
ValueError: If the lengths of metrics and timestamps do not match.
24+
"""
25+
if len(metrics) != len(timestamps):
26+
error = "Metrics and timestamps dimensions do not match!"
27+
raise ValueError(error)
28+
return trapezoid(metrics, timestamps)
29+
30+
31+
def sanitize_filename(filename: str) -> str:
32+
"""Sanitize a filename by replacing invalid characters with underscores.
33+
34+
Args:
35+
filename (str): The filename to sanitize.
36+
37+
Returns:
38+
str: A sanitized filename containing only alphanumeric characters, underscores, or hyphens.
39+
"""
40+
return re.sub(r"[^a-zA-Z0-9_\-]", "_", filename)
41+
42+
43+
def process_run(run: wandb.Run) -> dict[str, float]:
44+
"""Process a wandb run to extract system metrics and compute statistics.
45+
46+
Args:
47+
run (Any): A wandb run object containing history data.
48+
49+
Returns:
50+
dict: A dictionary mapping metric columns to their computed statistics,
51+
including max, min, avg, area, and median.
52+
"""
53+
system_metrics = run.history(stream="system_metrics")
54+
results = {}
55+
for column in system_metrics.columns:
56+
if "system" not in column:
57+
continue
58+
metrics_values = system_metrics[column].dropna()
59+
if len(metrics_values) > 0:
60+
timestamps = system_metrics.index[: len(metrics_values)]
61+
results[column] = {
62+
"max": metrics_values.max(),
63+
"min": metrics_values.min(),
64+
"avg": metrics_values.mean(),
65+
"area": calculate_area(metrics_values, timestamps),
66+
"median": metrics_values.median(),
67+
}
68+
return results
69+
70+
71+
def main() -> None:
72+
"""CLI endpoint."""
73+
parser = argparse.ArgumentParser(description="Processing wandb resource data")
74+
parser.add_argument("--project", type=str, required=True, help="Wandb project name")
75+
parser.add_argument("--group", type=str, required=True, help="Wandb group name")
76+
parser.add_argument(
77+
"--metrics",
78+
type=str,
79+
nargs="+",
80+
default=["max", "area", "avg"],
81+
help="Metrics to compute (e.g. min, max, area, avg, median)",
82+
)
83+
args = parser.parse_args()
84+
85+
api = wandb.Api()
86+
runs = api.runs(args.project, filters={"group": args.group})
87+
88+
if not runs:
89+
error = f"No runs found for group {args.group} in project {args.project}."
90+
raise ValueError(error)
91+
92+
for run in runs:
93+
if "final_metrics" not in run.name:
94+
wandb.init(project=args.project, group=args.group, name=f"system_resources_{run.name}")
95+
results = process_run(run)
96+
for column, metrics in results.items():
97+
column_process = column.replace("/", "-")
98+
log_data = {}
99+
for m in args.metrics:
100+
if m in metrics:
101+
log_data[f"system_resources/{column_process}_{m}"] = metrics[m]
102+
wandb.log(log_data)
103+
wandb.finish()
104+
105+
106+
if __name__ == "__main__":
107+
main()

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Documentation = "https://deeppavlov.github.io/AutoIntent/"
5454
[project.scripts]
5555
"basic-aug" = "autointent.generation.utterances.basic.cli:main"
5656
"evolution-aug" = "autointent.generation.utterances.evolution.cli:main"
57+
"system-resource" = "autointent.generation.system_recources.wandb_cli:main"
5758

5859
[tool.poetry.group.dev]
5960
optional = true
@@ -210,4 +211,4 @@ module = [
210211
"autointent._callbacks.*",
211212
"autointent.modules.abc.*",
212213
]
213-
warn_unreachable = false
214+
warn_unreachable = false

0 commit comments

Comments
 (0)