|
5 | 5 | import pandas as pd |
6 | 6 | import matplotlib.pyplot as plt |
7 | 7 | from typing import Union, List, Dict, Optional |
| 8 | +import sys |
8 | 9 |
|
9 | 10 | class PerformanceLogger: |
10 | 11 | def __init__(self, log_path: str, sep: str = "|"): |
@@ -32,32 +33,35 @@ def log_to_dataframe(log_paths: Union[str, List[str]], sep: str = "|") -> pd.Dat |
32 | 33 |
|
33 | 34 | rows = [] |
34 | 35 | for log_id, path in enumerate(log_paths): |
35 | | - with open(path) as f: |
36 | | - for row_id, line in enumerate(f): |
37 | | - parts = [x.strip() for x in line.strip().split(sep)] |
38 | | - if len(parts) < 5: |
39 | | - continue |
40 | | - timestamp, step, elapsed_str, rss_str, user, host = parts[:6] |
41 | | - row = { |
42 | | - "timestamp": timestamp, |
43 | | - "step": step, |
44 | | - "elapsed_sec": float(elapsed_str), |
45 | | - "rss_gb": float(rss_str), |
46 | | - "user": user, |
47 | | - "host": host, |
48 | | - "logfile": path, |
49 | | - "rowID": row_id, |
50 | | - "logID": log_id |
51 | | - } |
52 | | - |
53 | | - if "[" in step and "]" in step: |
54 | | - base, idx = step.split("[") |
55 | | - row["step"] = base |
56 | | - idx = idx.rstrip("]") |
57 | | - for i, val in enumerate(idx.split(",")): |
58 | | - if val.strip().isdigit(): |
59 | | - row[f"index_{i}"] = int(val.strip()) |
60 | | - rows.append(row) |
| 36 | + try: |
| 37 | + with open(path) as f: |
| 38 | + for row_id, line in enumerate(f): |
| 39 | + parts = [x.strip() for x in line.strip().split(sep)] |
| 40 | + if len(parts) < 5: |
| 41 | + continue |
| 42 | + timestamp, step, elapsed_str, rss_str, user, host = parts[:6] |
| 43 | + row = { |
| 44 | + "timestamp": timestamp, |
| 45 | + "step": step, |
| 46 | + "elapsed_sec": float(elapsed_str), |
| 47 | + "rss_gb": float(rss_str), |
| 48 | + "user": user, |
| 49 | + "host": host, |
| 50 | + "logfile": path, |
| 51 | + "rowID": row_id, |
| 52 | + "logID": log_id |
| 53 | + } |
| 54 | + |
| 55 | + if "[" in step and "]" in step: |
| 56 | + base, idx = step.split("[") |
| 57 | + row["step"] = base |
| 58 | + idx = idx.rstrip("]") |
| 59 | + for i, val in enumerate(idx.split(",")): |
| 60 | + if val.strip().isdigit(): |
| 61 | + row[f"index_{i}"] = int(val.strip()) |
| 62 | + rows.append(row) |
| 63 | + except FileNotFoundError: |
| 64 | + continue |
61 | 65 |
|
62 | 66 | return pd.DataFrame(rows) |
63 | 67 |
|
@@ -134,11 +138,11 @@ def plot(df: pd.DataFrame, |
134 | 138 | plt.xlabel(xlabel) |
135 | 139 | plt.ylabel(ylabel) |
136 | 140 | plt.tight_layout() |
137 | | - |
| 141 | + is_testing = "pytest" in sys.modules |
138 | 142 | if output_pdf: |
139 | 143 | pdf.savefig() |
140 | 144 | plt.close() |
141 | | - else: |
| 145 | + elif not is_testing: |
142 | 146 | plt.show() |
143 | 147 |
|
144 | 148 | if output_pdf: |
|
0 commit comments