Spaces:
Paused
Paused
File size: 2,094 Bytes
dac3297 | 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 | import json, glob, re
from collections import Counter
from pathlib import Path
files = sorted(glob.glob('output/floor_*_detections.json'))
print(f'{len(files)} floors processed:\n')
# Schedule for comparison
with open(r'output/355 E 86th St Mechanical 100% CD Set_flattened_schedule_cache.json') as f:
sched = json.load(f)
schedule_totals = {tag: info.get('qty') or 0 for tag, info in sched.items()}
all_tags_seen = set()
floor_counts = {}
for fp in files:
m = re.match(r'.*floor_(\d+)_(.+)_detections\.json', fp)
if not m:
continue
page_num = int(m.group(1))
floor_name = m.group(2).replace('_', ' ')
with open(fp) as f:
hits = json.load(f)['hits']
counts = Counter(h['tag'] for h in hits)
floor_counts[(page_num, floor_name)] = counts
all_tags_seen.update(counts.keys())
tags_present = sorted(t for t in schedule_totals if schedule_totals.get(t, 0) > 0 or t in all_tags_seen)
# Build table
header = ['Floor'] + tags_present + ['Total']
rows = []
det_totals = Counter()
for (pn, fn), counts in sorted(floor_counts.items()):
row = [f'{pn:>2} {fn}']
total = 0
for tag in tags_present:
cnt = counts.get(tag, 0)
det_totals[tag] += cnt
total += cnt
row.append(str(cnt) if cnt else '-')
row.append(str(total))
rows.append(row)
# Schedule and detected total rows
sched_row = ['Schedule'] + [str(schedule_totals.get(t, 0)) for t in tags_present] + [str(sum(schedule_totals.get(t,0) for t in tags_present))]
det_row = ['Detected'] + [str(det_totals.get(t, 0)) for t in tags_present] + [str(sum(det_totals.values()))]
# Print table
col_widths = [max(len(r[i]) for r in (rows + [header, sched_row, det_row])) for i in range(len(header))]
def fmt_row(row):
return ' '.join(c.rjust(w) if i > 0 else c.ljust(w) for i, (c, w) in enumerate(zip(row, col_widths)))
print(fmt_row(header))
print('-' * (sum(col_widths) + 2 * (len(col_widths) - 1)))
for r in rows:
print(fmt_row(r))
print('-' * (sum(col_widths) + 2 * (len(col_widths) - 1)))
print(fmt_row(sched_row))
print(fmt_row(det_row))
|