Skip to content

Commit f0927ed

Browse files
anna-grimanna-grim
andauthored
feat: updated progress bar (#60)
Co-authored-by: anna-grim <anna.grim@alleninstitute.org>
1 parent 04e73c3 commit f0927ed

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

src/segmentation_skeleton_metrics/swc_utils.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,29 +110,28 @@ def parse_cloud_paths(cloud_dict, min_size, anisotropy):
110110
zip_paths = utils.list_gcs_filenames(bucket, cloud_dict["path"], ".zip")
111111
print("Downloading predicted swc files from cloud...")
112112
print("# zip files:", len(zip_paths))
113+
114+
# Assign processes
115+
chunk_size = int(len(zip_paths) * 0.02)
116+
cnt = 1
117+
t0, t1 = utils.init_timers()
113118
with ProcessPoolExecutor() as executor:
114119
processes = []
115-
for path in zip_paths:
120+
for i, path in enumerate(zip_paths):
116121
zip_content = bucket.blob(path).download_as_bytes()
117122
processes.append(
118123
executor.submit(download, zip_content, anisotropy, min_size)
119124
)
120-
print("Processes Assigned!\n")
125+
if i > cnt * chunk_size:
126+
cnt, t1 = utils.report_progress(
127+
i, len(zip_paths), chunk_size, cnt, t0, t1
128+
)
121129

122130
# Store results
123-
chunk_size = int(len(zip_paths) * 0.02)
124-
cnt = 1
125131
valid_labels = dict()
126-
t0, t1 = utils.init_timers()
127132
for i, process in enumerate(as_completed(processes)):
128133
valid_labels.update(process.result())
129-
if i > cnt * chunk_size:
130-
utils.progress_bar(i + 1, len(zip_paths))
131-
cnt += 1
132-
133-
# Report Results
134134
print("\n#Valid Labels:", len(valid_labels))
135-
print("")
136135
return valid_labels
137136

138137

src/segmentation_skeleton_metrics/utils.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,29 @@ def init_timers():
439439
return time(), time()
440440

441441

442+
# -- Utils --
443+
def report_progress(current, total, chunk_size, cnt, t0, t1):
444+
eta = get_eta(current, total, chunk_size, t1)
445+
runtime = get_runtime(current, total, chunk_size, t0, t1)
446+
progress_bar2(current, total, eta=eta, runtime=runtime)
447+
return cnt + 1, time()
448+
449+
450+
def get_eta(current, total, chunk_size, t0, return_str=True):
451+
chunk_runtime = time() - t0
452+
remaining = total - current
453+
eta = remaining * (chunk_runtime / chunk_size)
454+
t, unit = time_writer(eta)
455+
return f"{round(t, 4)} {unit}" if return_str else eta
456+
457+
458+
def get_runtime(current, total, chunk_size, t0, t1):
459+
eta = get_eta(current, total, chunk_size, t1, return_str=False)
460+
total_runtime = time() - t0 + eta
461+
t, unit = time_writer(total_runtime)
462+
return f"{round(t, 4)} {unit}"
463+
464+
442465
def progress_bar(current, total, bar_length=50):
443466
"""
444467
Reports the progress of completing some process.
@@ -462,3 +485,11 @@ def progress_bar(current, total, bar_length=50):
462485
f"[{'=' * progress}{' ' * (bar_length - progress)}] {current}/{total}"
463486
)
464487
print(f"\r{bar}", end="", flush=True)
488+
489+
def progress_bar2(current, total, bar_length=50, eta=None, runtime=None):
490+
progress = int(current / total * bar_length)
491+
n_completed = f"Completed: {current}/{total}"
492+
bar = f"[{'=' * progress}{' ' * (bar_length - progress)}]"
493+
eta = f"Time Remaining: {eta}" if eta else ""
494+
runtime = f"Estimated Total Runtime: {runtime}" if runtime else ""
495+
print(f"\r{bar} {n_completed} | {eta} | {runtime} ", end="", flush=True)

0 commit comments

Comments
 (0)