nds-statistics-platform / representative_gallery.py
AutoZYX's picture
Deploy PostgreSQL statistics 8cb2438
9a9908d verified
Raw History Blame Contribute Delete
26.7 kB
"""One exact matched representative per automatically grouped collection point.
Safe batch membership is joined only after the dashboard's metadata and ODD
filters. All details describe the selected current frame, never merged labels.
"""
from __future__ import annotations
from collections import OrderedDict
from collections.abc import Callable, Iterable, Mapping
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from datetime import date
from functools import lru_cache
from itertools import zip_longest
import hashlib
from http.client import HTTPException
import html
import math
from pathlib import Path
import re
from threading import Lock
from time import monotonic
from urllib.request import Request
import streamlit as st
from annotation_statistics import AnnotationRecord, definitions
from classic_statistics import selection_label
from city_labels import city_display_name
_PAGE_SIZE = 12
_LOCATION_CODE = re.compile(r'L-[0-9a-f]{16}\Z')
_BATCH_CODE = re.compile(r'B-[0-9a-f]{16}\Z')
_FRAME_KEY = re.compile(r'[0-9a-f]{64}\.jpg\Z')
_PAGE_KEY = 'representative_gallery_page'
_CITIES_KEY = 'representative_gallery_cities'
_LEGACY_CITY_KEY = 'representative_gallery_city'
_SELECTION_KEY = '_representative_gallery_selection'
_POPULATION_KEY = '_representative_gallery_population'
_MAX_FRAME_BYTES = 4 * 1024 * 1024
_THUMBNAIL_TIMEOUT_SECONDS = 5
_IMAGE_FAILURE_COOLDOWN_SECONDS = 30
_IMAGE_FAILURES: OrderedDict[str, float] = OrderedDict()
_IMAGE_FAILURE_LOCK = Lock()
# Gallery visibility only; source records and duration statistics remain intact.
_HIDDEN_LOCATION_CODES = frozenset({'L-8a9f37a3fe972582'})
_STYLE = """
<style>
.st-key-representative_gallery_grid [class*="st-key-representative_example_"] {
background: var(--nds-surface, #fff); border-radius: var(--nds-radius, 8px);
border-color: var(--nds-border, #e5e9ee); box-shadow: none;
}
.st-key-representative_gallery_grid [data-testid="stImage"] img {
display: block; width: 100%; aspect-ratio: 16 / 9; object-fit: contain;
background: var(--nds-ground, #f5f7fa); border-radius: 6px;
}
.st-key-representative_gallery_controls { margin: 0 0 4px; }
.st-key-representative_gallery_controls [data-testid="stHorizontalBlock"] { gap: 16px; }
.st-key-representative_gallery_controls [data-testid="stSelectbox"],
.st-key-representative_gallery_controls [data-testid="stMultiSelect"] { min-width: 0; }
.example-method { color: var(--nds-muted, #586574); font-size: 12px; line-height: 1.6; }
.example-method summary { width: fit-content; cursor: pointer; }
.example-method summary:focus-visible { outline: 2px solid var(--nds-blue, #3d6b8e); outline-offset: 3px; }
.example-method p { max-width: 84ch; margin: 6px 0 0; font-size: 12px; }
.example-page-summary {
color: var(--nds-ink, #17212f); font-size: 14px; font-weight: 500;
font-variant-numeric: tabular-nums; line-height: 1.5;
}
.st-key-representative_gallery_controls [data-testid="stCaptionContainer"] p {
color: var(--nds-muted, #586574); font-size: 12px; line-height: 1.5;
}
.example-placeholder {
aspect-ratio: 16 / 9; display: flex; align-items: center; justify-content: center;
background: var(--nds-ground, #f5f7fa); border: 1px dashed var(--nds-border, #e5e9ee);
border-radius: 6px; color: var(--nds-muted, #586574); font-size: 12px;
line-height: 1.5; text-align: center; padding: 12px;
}
.example-meta { padding: 2px 0 0; color: var(--nds-ink, #17212f); }
.example-city { font-size: 14px; font-weight: 600; line-height: 1.5; }
.example-code {
color: var(--nds-muted, #586574); font-size: 12px; line-height: 1.5;
font-variant-numeric: tabular-nums; overflow-wrap: anywhere; margin-top: 3px;
}
.example-date {
color: var(--nds-muted, #586574); font-size: 12px; line-height: 1.5;
font-variant-numeric: tabular-nums; margin-top: 3px;
}
.example-summary { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 8px; }
.example-tag {
background: var(--nds-ground, #f5f7fa); color: var(--nds-ink, #17212f);
font-size: 12px; line-height: 1.5; border-radius: 4px; padding: 2px 6px;
}
.example-provenance { margin-top: 8px; }
.example-duration {
margin-top: 8px; font-size: 14px; font-weight: 500; line-height: 1.5;
font-variant-numeric: tabular-nums;
}
.example-label-row {
color: var(--nds-muted, #586574); font-size: 12px; line-height: 1.6;
padding: 6px 0; border-bottom: 1px solid var(--nds-border, #e5e9ee);
overflow-wrap: anywhere;
}
.example-label-row:last-child { border-bottom: 0; }
.example-label-row b {
display: block; color: var(--nds-ink, #17212f); font-size: 12px; font-weight: 500;
}
.st-key-representative_gallery_grid [data-testid="stExpander"] {
border-color: var(--nds-border, #e5e9ee); border-radius: 6px; box-shadow: none;
}
.st-key-representative_gallery_grid [data-testid="stExpander"] summary p { font-size: 12px; }
@media (max-width: 1100px) and (min-width: 641px) {
.st-key-representative_gallery_grid [data-testid="stHorizontalBlock"] { flex-wrap: wrap; gap: 16px; }
.st-key-representative_gallery_grid [data-testid="stColumn"] {
min-width: calc(50% - 8px); flex: 1 1 calc(50% - 8px);
}
}
@media (max-width: 640px) {
.example-method summary { min-height: 44px; padding: 12px 0; line-height: 20px; box-sizing: border-box; }
.st-key-representative_gallery_controls [data-testid="stHorizontalBlock"] { flex-wrap: wrap; gap: 12px; }
.st-key-representative_gallery_controls [data-testid="stColumn"] {
min-width: 0; width: auto; flex: 1 1 calc(50% - 6px);
}
.st-key-representative_gallery_controls [data-testid="stColumn"]:last-child { flex-basis: 100%; }
.st-key-representative_gallery_controls [data-baseweb="select"] > div { min-height: 44px; }
.st-key-representative_gallery_grid [data-testid="stHorizontalBlock"] { flex-wrap: wrap; gap: 12px; }
.st-key-representative_gallery_grid [data-testid="stColumn"] {
min-width: 0; width: auto; flex: 1 1 calc(50% - 6px);
}
.st-key-representative_gallery_grid [class*="st-key-representative_example_"] { padding: 10px; }
.st-key-representative_gallery_grid [data-testid="stExpander"] summary { min-height: 44px; }
}
@media (max-width: 359px) {
.st-key-representative_gallery_grid [data-testid="stColumn"] { flex-basis: 100%; }
}
</style>
"""
@dataclass(frozen=True)
class LocationExample:
"""Filtered members of one safe location, in representative priority order."""
code: str
city: str
records: tuple[AnnotationRecord, ...]
total_batches: int
@property
def record(self) -> AnnotationRecord:
return self.records[0]
@property
def matching_batches(self) -> int:
return len(self.records)
@property
def collected_on(self) -> date | None:
return self.record.collected_on
@property
def batch_code(self) -> str:
return self.record.batch_code
def location_examples(records: Iterable[AnnotationRecord], locations
) -> tuple[tuple[LocationExample, ...], int]:
"""Join exact safe batch codes after filtering; unmapped batches stay pending."""
records = tuple(records)
matched = {record.batch_code: record for record in records
if isinstance(getattr(record, 'batch_code', None), str)
and _BATCH_CODE.fullmatch(record.batch_code)}
grouped = set()
examples = []
for group in locations.groups:
if not isinstance(group.code, str) or not _LOCATION_CODE.fullmatch(group.code):
continue
members = tuple(dict.fromkeys(code for code in group.batch_codes
if isinstance(code, str) and _BATCH_CODE.fullmatch(code)))
candidates = tuple(sorted((matched[code] for code in members
if code in matched and code not in grouped
and matched[code].city == group.city), key=lambda record: (
not (isinstance(getattr(record, 'frame_key', None), str)
and _FRAME_KEY.fullmatch(record.frame_key)),
-(record.collected_on or date.min).toordinal(), record.batch_code)))
if candidates:
examples.append(LocationExample(group.code, group.city, candidates, len(members)))
grouped.update(record.batch_code for record in candidates)
return tuple(examples), sum(getattr(record, 'batch_code', None) not in grouped for record in records)
def _reset_gallery_page() -> None:
st.session_state[_PAGE_KEY] = 1
def gallery_page(records: Iterable[AnnotationRecord], page: int = 1
) -> tuple[tuple[AnnotationRecord, ...], int, int]:
"""Interleave cities, retaining newest-date/safe-code priority within each."""
by_city = {}
for record in sorted(records, key=lambda record: (
-(record.collected_on or date.min).toordinal(),
getattr(record, 'batch_code', None) or '',
)):
by_city.setdefault(record.city, []).append(record)
ordered = [record for row in zip_longest(*(by_city[city] for city in sorted(by_city)))
for record in row if record is not None]
pages = (len(ordered) + _PAGE_SIZE - 1) // _PAGE_SIZE
try:
page = int(page)
except (TypeError, ValueError, OverflowError):
page = 1
selected = min(max(1, page), max(1, pages))
start = (selected - 1) * _PAGE_SIZE
return tuple(ordered[start:start + _PAGE_SIZE]), selected, pages
def _location_hours(example: LocationExample, batch_hours: Mapping[str, float | None] | None
) -> tuple[float | None, int, int]:
"""Sum distinct filtered members; missing coverage never becomes measured zero."""
codes = {record.batch_code for record in example.records}
known = []
for code in codes:
value = batch_hours.get(code) if batch_hours is not None else None
if (not isinstance(value, bool) and isinstance(value, (int, float))
and math.isfinite(value) and value >= 0):
known.append(value)
return math.fsum(known) if known else None, len(known), len(codes)
@lru_cache(maxsize=256)
def _public_gallery_image(key: str) -> bytes:
"""Cache verified JPEG bytes; briefly defer failed transfers on cache misses."""
from public_snapshot import _open_public_url
from representative_frames import public_frame_url
# A successful LRU hit bypasses this check, including when an overlapping
# request failed after another request successfully cached the same key.
with _IMAGE_FAILURE_LOCK:
if _IMAGE_FAILURES.get(key, 0) > monotonic():
raise OSError('Representative frame is temporarily unavailable')
_IMAGE_FAILURES.pop(key, None)
try:
request = Request(public_frame_url(key), headers={'Accept': 'image/jpeg', 'User-Agent': 'NDS-statistics/1'})
with _open_public_url(request, timeout=_THUMBNAIL_TIMEOUT_SECONDS) as response:
length = response.headers.get('Content-Length')
if length is not None and not 0 <= int(length) <= _MAX_FRAME_BYTES:
raise ValueError('Representative frame transfer is too large')
content = response.read(_MAX_FRAME_BYTES + 1)
if (len(content) > _MAX_FRAME_BYTES or not content.startswith(b'\xff\xd8\xff')
or not content.endswith(b'\xff\xd9') or hashlib.sha256(content).hexdigest() + '.jpg' != key):
raise ValueError('Representative frame integrity check failed')
except (OSError, HTTPException, ValueError, TypeError, OverflowError):
with _IMAGE_FAILURE_LOCK:
_IMAGE_FAILURES[key] = monotonic() + _IMAGE_FAILURE_COOLDOWN_SECONDS
_IMAGE_FAILURES.move_to_end(key)
while len(_IMAGE_FAILURES) > 256:
_IMAGE_FAILURES.popitem(last=False)
raise
_retry_gallery_images((key,))
return content
def _retry_gallery_images(keys: Iterable[str]) -> None:
"""Retry visible unavailable images without discarding successful bytes."""
with _IMAGE_FAILURE_LOCK:
for key in keys:
_IMAGE_FAILURES.pop(key, None)
def gallery_image(key: str | None, *, cache_root: Path | None = None) -> bytes | None:
"""Serve sanitized local or public JPEG bytes from the dashboard's domain."""
if not isinstance(key, str) or not _FRAME_KEY.fullmatch(key):
return None
from representative_frames import frame_file
cached = frame_file(key, cache_root=cache_root)
if cached is not None:
try:
return cached.read_bytes()
except OSError:
pass
try:
return _public_gallery_image(key)
except (OSError, HTTPException, ValueError, TypeError, OverflowError):
return None
def _page_images(records: Iterable[AnnotationRecord], image_loader: Callable) -> dict:
"""Load only the visible page's unique valid keys, with four requests at once."""
keys = tuple(dict.fromkeys(key for record in records
if isinstance(key := getattr(record, 'frame_key', None), str)
and _FRAME_KEY.fullmatch(key)))
if not keys:
return {}
def load(key):
try:
return image_loader(key)
except (OSError, HTTPException, ValueError, TypeError, OverflowError):
return None
with ThreadPoolExecutor(max_workers=4) as executor:
return dict(zip(keys, executor.map(load, keys)))
@st.fragment
def render_representative_gallery(
records: Iterable[AnnotationRecord], translate: Callable[[str], str], *,
locations=None, location_status: str = 'not_published',
language: str = 'zh', field_label: Callable[[str], str] | None = None,
image_loader: Callable[[str], bytes | str | None] | None = None,
batch_hours: Mapping[str, float | None] | None = None,
incomplete_batches: Iterable[str] = (),
core: bool | None = None,
) -> None:
"""Show one matched frame and filtered member hours per point, twelve per page."""
records = tuple(records)
incomplete_batches = frozenset(incomplete_batches)
core = any(label.field == 'scene.type' for record in records for label in record.labels) if core is None else core
if core:
from core_annotations import definitions_v2
fields = definitions_v2()
else:
fields = definitions()
def tr(zh: str, en: str) -> str:
translated = translate(zh)
return en if language == 'en' and translated == zh else translated
def label_value(label) -> str:
if label is None:
return tr('未标注', 'Unlabeled')
if label.state != 'observed':
return translate(selection_label('@' + label.state, language))
return '、'.join(translate(value) for value in label.values)
st.markdown(_STYLE, unsafe_allow_html=True)
st.markdown("<div id='nav-examples' class='dash-section'><h2 class='dash-section-title'>"
+ html.escape(tr('筛选结果 · 场景示例', 'Filter results · Scene examples')) + '</h2></div>', unsafe_allow_html=True)
if locations is None or location_status != 'ok':
if location_status in ('not_published', 'not_configured'):
st.info(tr('地点归组尚未发布,暂不展示地点示例。',
'Location grouping has not been published; point examples are not available yet.'))
else:
st.warning(tr('地点归组暂不可用,请稍后刷新。', 'Location grouping is unavailable. Please refresh later.'))
return
examples, pending = location_examples(records, locations)
examples = tuple(example for example in examples if example.code not in _HIDDEN_LOCATION_CODES)
pending_summary = tr(f'全部城市匹配 {len(examples):,} 个采集点 · 待归组 {pending:,} 个批次',
f'All cities: {len(examples):,} matching points · {pending:,} matching batches await grouping')
st.markdown("<details class='example-method'><summary>"
+ html.escape(tr('归组与时长说明', 'Grouping and duration details'))
+ '</summary><p>' + html.escape(tr(
'采集点为自动归组结果,同城组内最大距离 100 米,未经人工确认为路口。每点展示一张匹配代表帧,标签仅描述该帧。'
'场景时长汇总当前城市、日期、编码、时段及场景/ODD 筛选后,同一点不同匹配批次的关联视频已知时长(h);'
'它不是条件持续时长。未知时长不计入已知小计,缺少定位信息或待归组的批次不列入地点示例。',
'Collection points are automatically grouped with a maximum within-group distance of 100 metres, '
'not reviewed intersection identities. Each point shows one matching frame; its labels describe only that frame. '
'Scene hours sum known associated video hours for distinct matching batches at that point after current city, '
'date, codec, period and scene/ODD filters; they do not measure time spent in a condition. Unknown hours are '
'excluded from known subtotals. Batches without location data or awaiting grouping are omitted.'))
+ '</p><p>' + html.escape(pending_summary) + '</p></details>', unsafe_allow_html=True)
# Value equality retains city/page across unchanged refreshes. Any filtered
# population or membership change resets to all cities and the first page.
population = (tuple(sorted(records, key=lambda record: (
-(record.collected_on or date.min).toordinal(), getattr(record, 'batch_code', None) or ''))),
tuple(sorted(examples, key=lambda example: example.code)))
cities = sorted({example.city for example in examples})
st.session_state.pop(_LEGACY_CITY_KEY, None)
if not cities:
st.session_state[_POPULATION_KEY] = population
st.session_state[_SELECTION_KEY] = {}
st.session_state[_PAGE_KEY] = 1
st.session_state.pop(_CITIES_KEY, None)
st.info(tr('当前筛选没有可展示的采集点示例。', 'No collection-point examples match these filters.'))
return
saved = st.session_state.get(_SELECTION_KEY, {})
if st.session_state.get(_POPULATION_KEY) != population:
st.session_state[_CITIES_KEY] = []
st.session_state[_PAGE_KEY] = 1
else:
st.session_state.setdefault(_CITIES_KEY, saved.get('cities', []))
st.session_state.setdefault(_PAGE_KEY, saved.get('page', 1))
st.session_state[_POPULATION_KEY] = population
selected_cities = st.session_state[_CITIES_KEY]
valid_cities = ([city for city in selected_cities if city in cities]
if isinstance(selected_cities, (list, tuple)) else [])
if valid_cities != selected_cities:
st.session_state[_CITIES_KEY] = valid_cities
st.session_state[_PAGE_KEY] = 1
with st.container(key='representative_gallery_controls'):
city_column, page_column, summary_column = st.columns((2, 1.2, 2.4), vertical_alignment='bottom')
with city_column:
selected_cities = st.multiselect(tr('示例城市', 'Example cities'), cities, key=_CITIES_KEY,
placeholder=tr('全部城市', 'All cities'),
format_func=city_display_name, on_change=_reset_gallery_page)
city_examples = tuple(example for example in examples
if not selected_cities or example.city in selected_cities)
_, selected_page, pages = gallery_page(city_examples, st.session_state[_PAGE_KEY])
st.session_state[_PAGE_KEY] = selected_page
with page_column:
st.selectbox(tr('示例页码', 'Example page'), list(range(1, pages + 1)), key=_PAGE_KEY,
format_func=str,
disabled=pages == 1)
cards, selected_page, _ = gallery_page(city_examples, st.session_state[_PAGE_KEY])
st.session_state[_SELECTION_KEY] = {'cities': list(selected_cities), 'page': selected_page}
first = (selected_page - 1) * _PAGE_SIZE + 1
with summary_column:
st.markdown("<div class='example-page-summary'>"
+ html.escape(tr(f'显示 {first:,}–{first + len(cards) - 1:,} / {len(city_examples):,} 个采集点 · 共 {pages} 页',
f'Showing {first:,}–{first + len(cards) - 1:,} / {len(city_examples):,} collection points · {pages} pages'))
+ '</div>', unsafe_allow_html=True)
load_image = image_loader or gallery_image
images = _page_images((example.record for example in cards), load_image)
failed_keys = tuple(key for key, image in images.items() if image is None)
if failed_keys:
st.button(tr('重试不可用图片', 'Retry unavailable images'), key='representative_gallery_retry',
on_click=_retry_gallery_images, args=(failed_keys,))
# Up to four peer images share the canvas. Balance the actual page count over
# its rows (5 -> 3 + 2, 9 -> 3 + 3 + 3) rather than leave one orphan image.
# A single point stays at a readable thumbnail width, aligned to the canvas.
row_count = (len(cards) + 3) // 4
row_size, extra = divmod(len(cards), row_count)
with st.container(key='representative_gallery_grid', width=640 if len(cards) == 1 else 'stretch'):
start = 0
for row_index in range(row_count):
row = cards[start:start + row_size + (row_index < extra)]
for offset, (column, example) in enumerate(zip(st.columns(len(row)), row)):
with column, st.container(border=True, key=f'representative_example_{first + start + offset}'):
record = example.record
key = getattr(record, 'frame_key', None)
image = images.get(key) if isinstance(key, str) else None
if image is not None:
st.image(image, width='stretch')
else:
st.markdown("<div class='example-placeholder'>"
+ html.escape(tr('代表帧暂不可用', 'Representative frame unavailable'))
+ '</div>', unsafe_allow_html=True)
code = getattr(record, 'batch_code', None)
duration, known_members, matching_members = _location_hours(example, batch_hours)
duration_text = f'{duration:,.2f} h' if duration is not None else '— h'
partial = duration is not None and (
known_members < matching_members or
any(member.batch_code in incomplete_batches for member in example.records))
partial_text = ("<div class='example-date'>"
+ html.escape(tr('部分时长未知', 'Some duration is unknown')) + '</div>'
if partial else '')
code = code if isinstance(code, str) and _BATCH_CODE.fullmatch(code) else tr('批次编号暂不可用', 'Batch code unavailable')
collected = record.collected_on.isoformat() if record.collected_on else tr('采集日期未知', 'Collection date unknown')
labels = {label.field: label for label in record.labels}
road = (label_value(labels.get('scene.type')) if core else
' · '.join(label_value(labels.get(field)) for field in ('top_road_category', 'top_road_subcategory')))
weather = label_value(labels.get('environment.light.intensity' if core else 'environment.weather'))
source = getattr(record, 'schema_version', 'aerial_annotation_v1')
if source == 'aerial_annotation_v2':
provenance = tr('来源:V2 核心标注', 'Source: V2 core annotation')
elif core:
provenance = tr('来源:旧版 V1 只读映射;不是 V2 重新标注',
'Source: read-only legacy V1 mapping; not V2 reannotation')
else:
provenance = tr('来源:旧版 V1 原始标签', 'Source: original legacy V1 labels')
st.markdown(
"<div class='example-meta'>"
f"<div class='example-city'>{html.escape(city_display_name(record.city))}</div>"
f"<div class='example-code'>{html.escape(example.code)}</div>"
f"<div class='example-date'>{html.escape(collected)}</div>"
f"<div class='example-summary'><span class='example-tag'>{html.escape(road)}</span>"
f"<span class='example-tag'>{html.escape(weather)}</span></div>"
f"<div class='example-duration'>{html.escape(tr('场景时长', 'Scene duration'))} {html.escape(duration_text)}</div>"
+ partial_text + '</div>',
unsafe_allow_html=True)
with st.expander(tr(f'查看全部 {len(fields)} 项标签', f'View all {len(fields)} labels')):
st.markdown(
"<div class='example-provenance'>"
f"<div class='example-date'>{html.escape(provenance)}</div>"
f"<div class='example-code'>{html.escape(tr('代表批次', 'Representative batch'))}: {html.escape(code)}</div>"
f"<div class='example-date'>{html.escape(tr('匹配 / 全部批次', 'Matching / total batches'))}: {example.matching_batches:,} / {example.total_batches:,}</div>"
f"<div class='example-date'>{html.escape(tr('已知时长批次 / 匹配批次', 'Batches with known duration / matching batches'))}: {known_members:,} / {matching_members:,}</div>"
'</div>', unsafe_allow_html=True)
for definition in fields:
field = definition['code']
name = field_label(field) if field_label else translate(definition['name'])
st.markdown(f"<div class='example-label-row' data-example-field='{field}'>"
f"<b>{html.escape(name)}</b>{html.escape(label_value(labels.get(field)))}</div>",
unsafe_allow_html=True)
start += len(row)