perf: 优化汇总用户量渲染
保持逐市场阈值和颜色效果不变,同时缩小汇总阶段的计算缓冲区并补全文档说明。
This commit is contained in:
@@ -1396,7 +1396,7 @@ def collect_user_count_source(
|
||||
}
|
||||
|
||||
|
||||
def build_merged_user_count_state(records, height, width):
|
||||
def build_merged_user_count_state(records, height, width, row_offset=0, col_offset=0):
|
||||
value_sum = np.zeros((height, width), dtype=np.float32)
|
||||
value_weight = np.zeros((height, width), dtype=np.uint16)
|
||||
painted_cells = 0
|
||||
@@ -1406,6 +1406,10 @@ def build_merged_user_count_state(records, height, width):
|
||||
skipped_no_value += 1
|
||||
continue
|
||||
row_start, row_stop, col_start, col_stop = record["window"]
|
||||
row_start -= row_offset
|
||||
row_stop -= row_offset
|
||||
col_start -= col_offset
|
||||
col_stop -= col_offset
|
||||
value_sum[row_start:row_stop, col_start:col_stop] += record["value_sum"]
|
||||
value_weight[row_start:row_stop, col_start:col_stop] += 1
|
||||
painted_cells += 1
|
||||
@@ -1417,6 +1421,70 @@ def build_merged_user_count_state(records, height, width):
|
||||
}
|
||||
|
||||
|
||||
def render_merged_user_count_records(records, canvas_height, canvas_width, thresholds=None):
|
||||
valid_records = [record for record in records.values() if record["has_value"]]
|
||||
if not valid_records:
|
||||
empty_mask = np.zeros((0, 0), dtype=bool)
|
||||
empty_rgb = np.zeros((0, 0, 3), dtype=np.uint8)
|
||||
return {
|
||||
"window": (0, 0, 0, 0),
|
||||
"data_mask": empty_mask,
|
||||
"rgb": empty_rgb,
|
||||
"thresholds": [],
|
||||
"rank_pixel_counts": [],
|
||||
"painted_cells": 0,
|
||||
"skipped_no_value": len(records),
|
||||
}
|
||||
|
||||
row_start = min(record["window"][0] for record in valid_records)
|
||||
row_stop = max(record["window"][1] for record in valid_records)
|
||||
col_start = min(record["window"][2] for record in valid_records)
|
||||
col_stop = max(record["window"][3] for record in valid_records)
|
||||
padding = HEATMAP_SMOOTH_RADIUS
|
||||
row_start = max(0, row_start - padding)
|
||||
row_stop = min(canvas_height, row_stop + padding)
|
||||
col_start = max(0, col_start - padding)
|
||||
col_stop = min(canvas_width, col_stop + padding)
|
||||
|
||||
state = build_merged_user_count_state(
|
||||
records,
|
||||
row_stop - row_start,
|
||||
col_stop - col_start,
|
||||
row_offset=row_start,
|
||||
col_offset=col_start,
|
||||
)
|
||||
value_sum = state["value_sum"]
|
||||
value_weight = state["value_weight"]
|
||||
data_mask = value_weight > 0
|
||||
value_raster = np.zeros_like(value_sum)
|
||||
np.divide(value_sum, value_weight, out=value_raster, where=data_mask)
|
||||
if thresholds:
|
||||
ranked_rgb, rank_pixel_counts = ranked_pixel_rgb_for_values_with_thresholds(
|
||||
value_raster,
|
||||
data_mask,
|
||||
EXCELLENT_COLORS,
|
||||
thresholds,
|
||||
)
|
||||
applied_thresholds = [float(value) for value in thresholds]
|
||||
else:
|
||||
ranked_rgb, applied_thresholds, rank_pixel_counts = ranked_pixel_rgb_for_values(
|
||||
value_raster,
|
||||
data_mask,
|
||||
EXCELLENT_COLORS,
|
||||
EXCELLENT_PERCENT_STEPS,
|
||||
)
|
||||
smoothed_rgb = smooth_rgb_inside_mask(ranked_rgb, data_mask, HEATMAP_SMOOTH_RADIUS)
|
||||
return {
|
||||
"window": (row_start, row_stop, col_start, col_stop),
|
||||
"data_mask": data_mask,
|
||||
"rgb": smoothed_rgb,
|
||||
"thresholds": applied_thresholds,
|
||||
"rank_pixel_counts": rank_pixel_counts,
|
||||
"painted_cells": state["painted_cells"],
|
||||
"skipped_no_value": state["skipped_no_value"],
|
||||
}
|
||||
|
||||
|
||||
def merged_user_count_metadata(records, source_stats):
|
||||
shared_grids = 0
|
||||
five_g_only_grids = 0
|
||||
@@ -1482,38 +1550,24 @@ def render_summary_user_count_by_market(
|
||||
source_name,
|
||||
prefer_geometry=source_name == "5G",
|
||||
)
|
||||
state = build_merged_user_count_state(records, height, width)
|
||||
value_sum = state["value_sum"]
|
||||
value_weight = state["value_weight"]
|
||||
candidates = sum(stats["candidates"] for stats in source_stats.values())
|
||||
painted_cells = state["painted_cells"]
|
||||
skipped_no_value = state["skipped_no_value"]
|
||||
|
||||
data_mask = value_weight > 0
|
||||
thresholds = []
|
||||
rank_pixel_counts = []
|
||||
if np.any(data_mask):
|
||||
value_raster = np.zeros_like(value_sum)
|
||||
np.divide(value_sum, value_weight, out=value_raster, where=data_mask)
|
||||
thresholds = (market_thresholds or {}).get(market.code)
|
||||
if thresholds:
|
||||
ranked_rgb, rank_pixel_counts = ranked_pixel_rgb_for_values_with_thresholds(
|
||||
value_raster,
|
||||
data_mask,
|
||||
EXCELLENT_COLORS,
|
||||
thresholds,
|
||||
)
|
||||
thresholds = [float(value) for value in thresholds]
|
||||
else:
|
||||
ranked_rgb, thresholds, rank_pixel_counts = ranked_pixel_rgb_for_values(
|
||||
value_raster,
|
||||
data_mask,
|
||||
EXCELLENT_COLORS,
|
||||
EXCELLENT_PERCENT_STEPS,
|
||||
)
|
||||
smoothed_rgb = smooth_rgb_inside_mask(ranked_rgb, data_mask, HEATMAP_SMOOTH_RADIUS)
|
||||
image[data_mask, :3] = smoothed_rgb[data_mask]
|
||||
image[data_mask, 3] = HEATMAP_ALPHA
|
||||
render_result = render_merged_user_count_records(
|
||||
records,
|
||||
height,
|
||||
width,
|
||||
(market_thresholds or {}).get(market.code),
|
||||
)
|
||||
painted_cells = render_result["painted_cells"]
|
||||
skipped_no_value = render_result["skipped_no_value"]
|
||||
data_mask = render_result["data_mask"]
|
||||
thresholds = render_result["thresholds"]
|
||||
rank_pixel_counts = render_result["rank_pixel_counts"]
|
||||
data_pixels = int(np.count_nonzero(data_mask))
|
||||
if data_pixels:
|
||||
row_start, row_stop, col_start, col_stop = render_result["window"]
|
||||
target = image[row_start:row_stop, col_start:col_stop]
|
||||
target[data_mask, :3] = render_result["rgb"][data_mask]
|
||||
target[data_mask, 3] = HEATMAP_ALPHA
|
||||
|
||||
total_candidates += candidates
|
||||
total_painted_cells += painted_cells
|
||||
@@ -1523,11 +1577,13 @@ def render_summary_user_count_by_market(
|
||||
"market_name": market.name,
|
||||
"candidates": candidates,
|
||||
"painted_cells": painted_cells,
|
||||
"data_pixels": int(np.count_nonzero(data_mask)),
|
||||
"data_pixels": data_pixels,
|
||||
"skipped_no_value": skipped_no_value,
|
||||
"thresholds": thresholds,
|
||||
"threshold_source": "market_zip" if (market_thresholds or {}).get(market.code) else "summary_raster",
|
||||
"rank_pixel_counts": rank_pixel_counts,
|
||||
"render_window": list(render_result["window"]),
|
||||
"render_buffer_pixels": int(data_mask.size),
|
||||
}
|
||||
market_result.update(merged_user_count_metadata(records, source_stats))
|
||||
market_results.append(market_result)
|
||||
@@ -1547,6 +1603,8 @@ def render_summary_user_count_by_market(
|
||||
"percent_steps": list(EXCELLENT_PERCENT_STEPS),
|
||||
"color_scope": "per_market",
|
||||
"threshold_source": "market_zip",
|
||||
"render_scope": "market_local_window",
|
||||
"render_padding_pixels": HEATMAP_SMOOTH_RADIUS,
|
||||
"market_results": market_results,
|
||||
"width": width,
|
||||
"height": height,
|
||||
|
||||
Reference in New Issue
Block a user