-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbar.py
More file actions
207 lines (177 loc) · 7.12 KB
/
bar.py
File metadata and controls
207 lines (177 loc) · 7.12 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import sys
import asyncio
import numpy as np
import numpy.core.defchararray as chars
import traceback
from typing import Dict, Optional, Union
import warnings
class Bar:
"""
Async progress bar for terminal display during long-running operations.
Creates visual progress indicators showing completion percentage, processing
speed, and estimated time remaining. Supports custom metrics display and
integrates with async/await patterns. Automatically handles cleanup when
operations complete or encounter errors.
The progress bar updates in-place on the terminal and can display additional
metrics like loss values or accuracy scores alongside the main progress.
"""
def __init__(
self, iterations: int, title: str = "Loading", steps: int = 40
) -> None:
"""
Initialize progress bar with operation parameters.
Args:
iterations: Total number of items to process
title: Display label for the progress operation
steps: Character width of progress bar (higher = smoother updates)
"""
# Total number of operations to complete
self.iterations: int = iterations
# Display label for this progress bar
self.title: str = title
# Visual width of progress bar in characters
self.steps: int = steps
# Storage for additional metrics to display
self.items: Dict[str, str] = {}
async def update(self, batch: int, time: float, final: bool = False) -> None:
"""
Refresh progress display with current completion status.
Calculates completion percentage, processing throughput, and estimated
time remaining. Renders updated progress bar to terminal with all
current metrics.
Args:
batch: Number of items completed so far
time: Operation start timestamp for speed calculation
final: Whether this is the final update (adds newline)
"""
# Calculate elapsed time since start
elapsed: float = np.subtract(
asyncio.get_event_loop().time(), time
)
# Calculate completion percentage
percentage: float = np.divide(batch, self.iterations)
# Calculate processing rate in items per second
throughput: np.array = np.where(
np.greater(elapsed, 0),
np.floor_divide(batch, elapsed),
0
)
# Estimate remaining time based on current rate
eta: np.array = np.where(
np.greater(batch, 0),
np.divide(
np.multiply(elapsed, np.subtract(self.iterations, batch)),
batch
),
0,
)
# Construct visual progress bar representation
bar: str = chars.add(
"|",
chars.add(
# Filled portion using hash characters
"".join(np.repeat("#", np.ceil(np.multiply(percentage, self.steps)))),
chars.add(
# Empty portion using spaces
"".join(
np.repeat(
" ",
np.subtract(
self.steps,
np.ceil(np.multiply(percentage, self.steps))
),
)
),
# Progress counter display
f"| {batch:03d}/{self.iterations:03d}",
),
),
)
# Output complete progress line to terminal
sys.stdout.write(
chars.add(
chars.add(
chars.add(
# Core progress information with timing
f"\r{self.title}: {bar} [{np.multiply(percentage, 100):.2f}%] in {elapsed:.1f}s "
f"({throughput:.1f}/s, ETA: {eta:.1f}s)",
# Additional metrics if available
np.where(
np.greater(np.size(self.items), 0),
chars.add(
" (",
chars.add(
# Format custom metrics as comma-separated list
", ".join(
[
f"{name}: {value}"
for name, value in self.items.items()
]
),
")",
),
),
"",
),
),
"",
),
"",
)
)
# Add newline for final update to prevent overwriting
if final:
sys.stdout.write("\n")
# Force immediate output to terminal
sys.stdout.flush()
async def postfix(self, **kwargs: Union[str, int, float]) -> None:
"""
Update additional metrics displayed with progress bar.
Accepts arbitrary key-value pairs for displaying supplementary
information like training loss, accuracy, or other relevant metrics
alongside the main progress indicator.
Args:
**kwargs: Metric names and values to display
Example:
await bar.postfix(loss=0.234, accuracy=0.891)
"""
# Update metrics dictionary with new values
self.items.update(kwargs)
async def __aenter__(self) -> "Bar":
"""
Enable usage as async context manager.
Returns:
Bar instance for use within async context block
Example:
async with Bar(total_items, "Processing") as progress:
# processing logic here
"""
return self
async def __aexit__(
self,
exc_type: Optional[type],
exc_val: Optional[BaseException],
exc_tb: Optional[traceback.TracebackException],
) -> None:
"""
Handle cleanup when exiting async context manager.
On successful completion, displays final progress state. On exception,
shows warning message about the encountered error.
Args:
exc_type: Exception class if error occurred
exc_val: Exception instance that was raised
exc_tb: Traceback information for the exception
"""
# Handle normal completion
if exc_type is None:
# Display final completion state
await self.update(
self.iterations,
asyncio.get_event_loop().time(),
final=True
)
else:
# Handle error case with warning message
warnings.warn(
f"\n{self.title} encountered error: {exc_val}"
)