perf: 优化汇总用户量渲染

保持逐市场阈值和颜色效果不变,同时缩小汇总阶段的计算缓冲区并补全文档说明。
This commit is contained in:
RBP15P
2026-07-24 18:53:43 +08:00
parent 7c9e993735
commit 0e3868fb8c
5 changed files with 136 additions and 34 deletions
+10
View File
@@ -128,6 +128,15 @@ QGZ 内的样式数据库、其他图层配置和项目结构保持不变;如
正采样量 Grid 的 `ssb_rsrp_dl > 0` 或空值透明。有效像元 alpha 为 `191`,约 75% 不透明度。
## 图例说明文件
每次运行会在 `output/` 根目录更新两份独立图例文件,不重复放入各市场 ZIP:
- `excellent_heatmap_legend.csv`:说明 4G+5G 合并用户量的四档颜色,依次为前 10% 红、10%~40% 黄、40%~70% 蓝、后 30% 绿。
- `competitor_signal_legend.csv`:说明竟对 MR 总采样点为 `0` 时使用红色,以及正采样量 Grid 按 `ssb_rsrp_dl` 使用的八档固定颜色;未通过筛选或信号无效的区域透明。
QGIS 中竟对信号默认显示、用户量默认关闭。单市场 QGIS 不包含 CellData;全量汇总 QGIS 保留 CellData,并沿用源 GeoPackage 的分类样式和标注。
## 常用配置
常用配置集中在 `main.py` 顶部:
@@ -162,6 +171,7 @@ QGZ 内的样式数据库、其他图层配置和项目结构保持不变;如
- 不同市场网格使用独立进程并行处理,每个进程自行打开只读 GeoPackage 连接并写入独立 zip。
- 单市场范围过大时按 `MAX_RASTER_PIXELS` 自动调整像元大小,结果中记录实际像元尺寸和分辨率调整系数。
- 汇总 TIF 使用独立的 `SUMMARY_MAX_RASTER_PIXELS` 清晰度上限,避免受单市场 2500 万像元限制而过度降采样;其中汇总 `用户量.tif` 复用各单市场阈值逐市场绘制,保证总图和单市场图颜色口径一致。
- 汇总用户量仍按市场逐个计算,但每个市场只分配覆盖自身有效像元的局部缓冲区,并在四周保留与平滑半径相同的边距;像元对齐、市场阈值、平滑结果和写入顺序保持不变,避免为每个市场重复处理整张全量画布。
- zip 和 `summary.json` 使用临时文件完成后原子替换,处理中断时不会先删除已有成果。
- `output/summary.json` 会记录总耗时、实际并发数以及每个市场的 Grid/Building 分阶段耗时。
+2
View File
@@ -166,3 +166,5 @@
- 单市场成果包不再复制 `CellData.gpkg`,其 `GIS.qgz` 也会删除 CellData 的 project layer、图层树、图例及绘制顺序引用;全量汇总包继续保留完整 CellData 和对应 QGIS 图层。
- `楼宇指标.csv`、`栅格指标.csv` 不再生成或写入单市场 ZIP;全量汇总包原本也不包含 CSV。单市场标准包现仅包含 `GIS.qgz`、`BuildingInfo.gpkg`、`用户量.tif` 和 `竟对信号.tif`。
- Building 指标源仅用于已停用的楼宇 CSV,因此主处理流程不再要求或打开 `Building.gpkg`,减少无效 I/O。
- 汇总用户量性能优化保持逐市场计算口径:每个市场继续使用自身 4G+5G 合并结果和市场级阈值,但只分配有效记录像元范围加 `HEATMAP_SMOOTH_RADIUS` 边距的局部数组,再按原顺序写入全量画布。
- 局部渲染不拼接、不重采样单市场 TIFF,汇总像元大小、全量画布原点、阈值、平滑半径和市场覆盖顺序均保持不变;新增 `render_scope = market_local_window` 和逐市场 `render_window`/`render_buffer_pixels` 便于追踪优化效果。
+1 -1
View File
@@ -114,7 +114,7 @@ flowchart LR
| 40% ~ 70% | 蓝色 | 用户量偏低 |
| 后 30% | 绿色 | 用户量最低 |
用户量图主要用于识别 4G+5G 综合用户聚集区域,为网络优化、资源投放和问题优先级判断提供参考。该合并数据只作用于用户量图;竟对信号图仍完全使用 5G Grid 的原始字段。全网汇总成果中的用户量图不会重新按全域计算一套阈值,而是复用各市场网格自身的 10%/30%/30%/30% 分档阈值后绘制到同一张汇总图中,保证总图和单市场图的颜色解释一致。
用户量图主要用于识别 4G+5G 综合用户聚集区域,为网络优化、资源投放和问题优先级判断提供参考。该合并数据只作用于用户量图;竟对信号图仍完全使用 5G Grid 的原始字段。全网汇总成果中的用户量图不会重新按全域计算一套阈值,而是复用各市场网格自身的 10%/30%/30%/30% 分档阈值后绘制到同一张汇总图中。汇总渲染为每个市场建立带平滑边距的局部缓冲区,再写入全量画布;像元对齐、阈值、平滑半径和市场处理顺序保持不变,避免重复处理整张全量矩阵。
### 6. 竟对信号模型
+91 -33
View File
@@ -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,
+32
View File
@@ -78,6 +78,38 @@ class UserCountMergeTest(unittest.TestCase):
self.assertEqual(metadata["four_g_only_grid_count"], 1)
self.assertEqual(metadata["five_g_only_grid_count"], 1)
def test_local_summary_render_matches_full_canvas_colors(self):
records = {}
main.merge_user_count_record(records, "a", (24, 27, 24, 27), 20.0, "5G", True)
main.merge_user_count_record(records, "b", (28, 31, 28, 31), 10.0, "5G", True)
main.merge_user_count_record(records, "c", (32, 35, 32, 35), 5.0, "4G")
thresholds = [15.0, 7.5, 2.5]
full_state = main.build_merged_user_count_state(records, 64, 64)
full_mask = full_state["value_weight"] > 0
full_values = np.zeros((64, 64), dtype=np.float32)
np.divide(
full_state["value_sum"],
full_state["value_weight"],
out=full_values,
where=full_mask,
)
full_ranked, _ = main.ranked_pixel_rgb_for_values_with_thresholds(
full_values,
full_mask,
main.EXCELLENT_COLORS,
thresholds,
)
expected = main.smooth_rgb_inside_mask(full_ranked, full_mask, main.HEATMAP_SMOOTH_RADIUS)
local = main.render_merged_user_count_records(records, 64, 64, thresholds)
actual = np.zeros_like(expected)
row_start, row_stop, col_start, col_stop = local["window"]
actual[row_start:row_stop, col_start:col_stop] = local["rgb"]
np.testing.assert_array_equal(actual, expected)
self.assertLess(local["data_mask"].size, full_mask.size)
class CompetitorSignalTest(unittest.TestCase):
def test_filter_accepts_weak_coverage_or_zero_competitor_samples(self):