-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils_progress.py
More file actions
301 lines (265 loc) · 11.1 KB
/
Copy pathutils_progress.py
File metadata and controls
301 lines (265 loc) · 11.1 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
from collections import deque
from dataclasses import dataclass
from math import e, ceil
from rich.table import Column
from rich.text import Text
from typing import Any, Optional
import rich.progress as rp
def resume_task(progress: rp.Progress, task_id: rp.TaskID) -> None:
with progress._lock:
task = progress._tasks[task_id]
if task.start_time is None:
progress.start_task(task_id)
elif task.stop_time is not None:
current_time = progress.get_time()
delta = current_time - task.stop_time
def foo(x):
return x._replace(timestamp=x.timestamp + delta)
task._progress = deque(map(foo, task._progress))
task.start_time = task.start_time + delta
task.stop_time = None
class ProgressEMA(rp.Progress):
def update(
self,
task_id: rp.TaskID,
*,
total: Optional[float] = None,
completed: Optional[float] = None,
advance: Optional[float] = None,
description: Optional[str] = None,
visible: Optional[bool] = None,
refresh: bool = False,
**fields: Any,
) -> None:
"""Update information associated with a task.
Args:
task_id (TaskID): Task id (returned by add_task).
total (float, optional): Updates task.total if not None.
completed (float, optional): Updates task.completed if not None.
advance (float, optional): Add a value to task.completed if not None.
description (str, optional): Change task description if not None.
visible (bool, optional): Set visible flag if not None.
refresh (bool): Force a refresh of progress information. Default is False.
**fields (Any): Additional data fields required for rendering.
"""
with self._lock:
task = self._tasks[task_id]
completed_start = task.completed
if total is not None and total != task.total:
task.total = total
task._reset()
if advance is not None:
task.completed += advance
if completed is not None:
task.completed = completed
if description is not None:
task.description = description
if visible is not None:
task.visible = visible
task.fields.update(fields)
update_completed = task.completed - completed_start
current_time = self.get_time()
old_sample_time = current_time - self.speed_estimate_period
_progress = task._progress
popleft = _progress.popleft
while _progress and _progress[0].timestamp < old_sample_time:
popleft()
if update_completed > 0:
_progress.append(rp.ProgressSample(current_time, update_completed))
if (
task.total is not None
and task.completed >= task.total
and task.finished_time is None
):
task.finished_time = task.elapsed
if refresh:
self.refresh()
def advance(self, task_id: rp.TaskID, advance: float = 1) -> None:
"""Advance task by a number of steps.
Args:
task_id (TaskID): ID of task.
advance (float): Number of steps to advance. Default is 1.
"""
current_time = self.get_time()
with self._lock:
task = self._tasks[task_id]
completed_start = task.completed
task.completed += advance
update_completed = task.completed - completed_start
old_sample_time = current_time - self.speed_estimate_period
_progress = task._progress
popleft = _progress.popleft
while _progress and _progress[0].timestamp < old_sample_time:
popleft()
while len(_progress) > 1000:
popleft()
_progress.append(rp.ProgressSample(current_time, update_completed))
if (
task.total is not None
and task.completed >= task.total
and task.finished_time is None
):
task.finished_time = task.elapsed
task.finished_speed = task.speed
class TimeRemainingColumn(rp.ProgressColumn):
"""Renders estimated time remaining.
Args:
compact (bool, optional): Render MM:SS when time remaining is less than an hour. Defaults to False.
elapsed_when_finished (bool, optional): Render time elapsed when the task is finished. Defaults to False.
exponential_moving_average (bool, optional): Estimate using an exponential moving average instead of averaging the time over a past window. Defaults to False.
"""
# Only refresh twice a second to prevent jitter
max_refresh = 0.5
def __init__(
self,
compact: bool = False,
elapsed_when_finished: bool = False,
exponential_moving_average: bool = False,
table_column: Optional[Column] = None,
):
self.compact = compact
self.elapsed_when_finished = elapsed_when_finished
self.exponential_moving_average = exponential_moving_average
super().__init__(table_column=table_column)
def render(self, task: "Task") -> Text: #type: ignore
"""Show time remaining."""
if self.elapsed_when_finished and task.finished:
task_time = task.finished_time
style = "progress.elapsed"
else:
if self.exponential_moving_average:
task_time = task.time_remaining_ema
else:
task_time = task.time_remaining
style = "progress.remaining"
if task.total is None:
return Text("", style=style)
if task_time is None:
return Text("--:--" if self.compact else "-:--:--", style=style)
# Based on https://github.com/tqdm/tqdm/blob/master/tqdm/std.py
minutes, seconds = divmod(int(task_time), 60)
hours, minutes = divmod(minutes, 60)
if self.compact and not hours:
formatted = f"{minutes:02d}:{seconds:02d}"
else:
formatted = f"{hours:d}:{minutes:02d}:{seconds:02d}"
return Text(formatted, style=style)
@dataclass
class TaskEMA(rp.Task):
speed_ema: Optional[float] = None
"""Optional[float]: The current speed estimated using an exponential moving average."""
def update_speed_ema(
self, update_completed: float, update_time: float, alpha: float = 0.1
) -> None:
update_speed = update_completed / update_time
if self.speed_ema is None:
self.speed_ema = update_speed
else:
weight_old_speed = (1 - alpha) ** update_completed
self.speed_ema = 1 / (
(1 - weight_old_speed) / update_speed
+ weight_old_speed / self.speed_ema
) # Harmonic mean of the speeds
@property
def time_remaining_ema(self) -> Optional[float]:
"""Optional[float]: Get estimated time to completion using an exponential moving average, or ``None`` if no data."""
if self.finished:
return 0.0
speed = self.speed_ema
if not speed:
return None
remaining = self.remaining
if remaining is None:
return None
estimate = remaining / speed
current_step_progress = 0.0
if self.stop_time is None:
with self._lock:
progress = self._progress
if progress:
current_step_progress = self.get_time() - progress[-1].timestamp
elif self.start_time is not None:
current_step_progress = self.get_time() - self.start_time
current_step_progress = (
1 / speed * (1 - e ** (-current_step_progress * speed))
)
return ceil(estimate - current_step_progress)
def _reset(self) -> None:
"""Reset progress."""
super()._reset()
self.speed_ema = None
class Progress2(rp.Progress):
def __init__(
self,
*args: Any,
speed_estimate_alpha: float = 0.1,
**kwargs: Any
) -> None:
super().__init__(*args, **kwargs)
self.speed_estimate_alpha = speed_estimate_alpha
def update(
self,
task_id: rp.TaskID,
*,
total: Optional[float] = None,
completed: Optional[float] = None,
advance: Optional[float] = None,
description: Optional[str] = None,
visible: Optional[bool] = None,
refresh: bool = False,
**fields: Any,
) -> None:
"""Update information associated with a task.
Args:
task_id (TaskID): Task id (returned by add_task).
total (float, optional): Updates task.total if not None.
completed (float, optional): Updates task.completed if not None.
advance (float, optional): Add a value to task.completed if not None.
description (str, optional): Change task description if not None.
visible (bool, optional): Set visible flag if not None.
refresh (bool): Force a refresh of progress information. Default is False.
**fields (Any): Additional data fields required for rendering.
"""
with self._lock:
task = self._tasks[task_id]
completed_start = task.completed
if total is not None and total != task.total:
task.total = total
task._reset()
if advance is not None:
task.completed += advance
if completed is not None:
task.completed = completed
if description is not None:
task.description = description
if visible is not None:
task.visible = visible
task.fields.update(fields)
update_completed = task.completed - completed_start
current_time = self.get_time()
old_sample_time = current_time - self.speed_estimate_period
_progress = task._progress
update_time = None
if _progress:
update_time = current_time - _progress[-1].timestamp
elif task.start_time is not None:
update_time = current_time - task.start_time
popleft = _progress.popleft
while len(_progress) > 2 and _progress[0].timestamp < old_sample_time:
popleft()
if update_completed > 0:
_progress.append(rp.ProgressSample(current_time, update_completed))
if update_completed > 0 and update_time is not None and update_time > 0:
task.update_speed_ema(
update_completed,
update_time,
max(self.speed_estimate_alpha, 1 / (1 + completed_start)),
)
if (
task.total is not None
and task.completed >= task.total
and task.finished_time is None
):
task.finished_time = task.elapsed
if refresh:
self.refresh()