-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
164 lines (137 loc) · 4.87 KB
/
utils.py
File metadata and controls
164 lines (137 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""Utility functions."""
from __future__ import annotations
import functools
import json
import logging
import time
from pathlib import Path
from typing import Any, Callable
import numpy as np
from sklearn.metrics import (
accuracy_score,
confusion_matrix,
f1_score,
precision_score,
recall_score,
roc_auc_score,
)
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
def timer(unit: str = "seconds", log_level: int = logging.INFO) -> Callable:
"""Decorator to measure execution time."""
def decorator(func: Callable) -> Callable:
@functools.wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
start_time = time.time()
try:
result = func(*args, **kwargs)
end_time = time.time()
elapsed_time = (
(end_time - start_time) * 1000
if unit == "milliseconds"
else (end_time - start_time)
)
unit_label = "ms" if unit == "milliseconds" else "s"
logger.log(
log_level,
f"'{func.__name__}' executed in {elapsed_time:.2f} {unit_label}",
)
if log_level == logging.DEBUG:
logger.debug(f"Args: {args}, kwargs: {kwargs}")
return result
except Exception as e:
end_time = time.time()
elapsed_time = (
(end_time - start_time) * 1000
if unit == "milliseconds"
else (end_time - start_time)
)
unit_label = "ms" if unit == "milliseconds" else "s"
logger.error(
f"'{func.__name__}' failed after {elapsed_time:.2f} {unit_label}"
)
logger.exception(e)
raise
return wrapper
return decorator
def get_metrics(
y_true: np.ndarray, y_pred: np.ndarray, y_proba: np.ndarray | None = None
) -> dict[str, float]:
"""Calculate classification metrics."""
metrics = {
"accuracy": accuracy_score(y_true, y_pred),
"precision": precision_score(
y_true, y_pred, average="weighted", zero_division=0
),
"recall": recall_score(y_true, y_pred, average="weighted", zero_division=0),
"f1": f1_score(y_true, y_pred, average="weighted", zero_division=0),
}
if y_proba is not None:
unique_classes = np.unique(y_true)
if len(unique_classes) == 2:
metrics["roc_auc"] = roc_auc_score(y_true, y_proba)
return metrics
def validate_input(data: Any, expected_type: Any, var_name: str) -> None:
"""Validate input type and emptiness."""
if not isinstance(data, expected_type):
raise TypeError(
f"{var_name} must be {expected_type.__name__}, got {type(data).__name__}"
)
try:
if len(data) == 0:
raise ValueError(f"{var_name} cannot be empty")
except TypeError:
pass # data doesn't support len()
def get_confusion_matrix(y_true: np.ndarray, y_pred: np.ndarray) -> np.ndarray:
"""Calculate confusion matrix."""
return confusion_matrix(y_true, y_pred)
def save_json(data: dict[str, Any], filepath: str | Path) -> None:
"""Save dictionary to JSON file."""
try:
path = Path(filepath)
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("w") as f:
json.dump(data, f, indent=2)
logger.info(f"Saved to {filepath}")
except Exception as e:
logger.error(f"Save failed: {e}")
raise
def load_json(filepath: str | Path) -> dict[str, Any]:
"""Load JSON file to dictionary."""
try:
path = Path(filepath)
if not path.exists():
raise FileNotFoundError(f"File not found: {filepath}")
with path.open("r") as f:
data = json.load(f)
logger.info(f"Loaded from {filepath}")
return data
except FileNotFoundError:
logger.error(f"File not found: {filepath}")
raise
except json.JSONDecodeError as e:
logger.error(f"Invalid JSON: {e}")
raise
except Exception as e:
logger.error(f"Load failed: {e}")
raise
def format_time(seconds: float) -> str:
"""Format seconds into human-readable time."""
if seconds < 60:
return f"{seconds:.2f}s"
elif seconds < 3600:
minutes = seconds / 60
return f"{minutes:.2f}m"
else:
hours = seconds / 3600
return f"{hours:.2f}h"
def ensure_dir(path: str | Path) -> Path:
"""Ensure directory exists."""
p = Path(path)
p.mkdir(parents=True, exist_ok=True)
return p
def get_size_mb(filepath: str | Path) -> float:
"""Get file size in MB."""
return Path(filepath).stat().st_size / (1024 * 1024)