fix: 调整用户量热力图分位规则

This commit is contained in:
RBP15P
2026-07-16 09:48:13 +08:00
parent 7b5cf5f82e
commit c6383dffdc
2 changed files with 60 additions and 48 deletions
+49 -36
View File
@@ -41,7 +41,7 @@ HEATMAP_SMOOTH_RADIUS = 10 # 热力图平滑半径,数值越大边界感越
HEATMAP_ALPHA = 191 # 热力图有效像元透明度;191 约等于 75% 不透明度。
USER_COUNT_OUTPUT_FILENAME = "用户量.tif" # 采样点数量热力图输出文件名。
WEAK_COVER_OUTPUT_FILENAME = "信号差用户量.tif" # 信号差用户量热力图输出文件名。
EXCELLENT_PERCENT_STEPS = (0.2, 0.4, 0.6) # 去重值降序分位阈值,默认按前 20%、40%、60% 作为连续渐变控制点。
EXCELLENT_PERCENT_STEPS = (0.2, 0.4, 0.6, 0.8) # 不去重,按所有匹配值降序取前 20%、40%、60%、80% 作为连续渐变控制点。
EXCELLENT_COLORS = (
(255, 0, 0), # 高值端,红色。
(255, 255, 0), # 偏高值,黄色。
@@ -518,45 +518,58 @@ def heatmap_rgb_for_values(values):
return np.clip(result, 0, 255).astype(np.uint8)
def percentile_thresholds_from_unique(values, percent_steps):
unique_values = sorted({value for value in values if value is not None}, reverse=True)
if not unique_values:
def percentile_thresholds_from_values(values, percent_steps):
sorted_values = sorted((value for value in values if value is not None), reverse=True)
if not sorted_values:
return []
thresholds = []
for percent in percent_steps:
index = max(0, math.ceil(len(unique_values) * percent) - 1)
index = min(index, len(unique_values) - 1)
thresholds.append(unique_values[index])
index = max(0, math.ceil(len(sorted_values) * percent) - 1)
index = min(index, len(sorted_values) - 1)
thresholds.append(sorted_values[index])
return thresholds
def palette_colors_for_steps(step_count):
if step_count <= 0:
return []
if step_count == 1:
return [EXCELLENT_COLORS[0]]
colors = []
palette_max = len(EXCELLENT_COLORS) - 1
for index in range(step_count):
position = index / (step_count - 1)
scaled = position * palette_max
start_index = int(math.floor(scaled))
end_index = min(start_index + 1, palette_max)
ratio = scaled - start_index
color = interpolate_color(EXCELLENT_COLORS[start_index], EXCELLENT_COLORS[end_index], ratio)
colors.append(tuple(int(round(channel)) for channel in color))
return colors
def excellent_rgb_for_values(values, thresholds):
result = np.zeros((*values.shape, 3), dtype=np.float32)
if len(thresholds) < 3:
if not thresholds:
result[:] = EXCELLENT_COLORS[-1]
return result.astype(np.uint8)
red_threshold, yellow_threshold, blue_threshold = thresholds[:3]
red, yellow, blue, green = EXCELLENT_COLORS
high = values >= red_threshold
high_middle = (values < red_threshold) & (values >= yellow_threshold)
low_middle = (values < yellow_threshold) & (values >= blue_threshold)
low = values < blue_threshold
control_colors = palette_colors_for_steps(len(thresholds))
result[:] = control_colors[-1]
high = values >= thresholds[0]
if np.any(high):
result[high] = red
if np.any(high_middle):
ratio = (values[high_middle] - yellow_threshold) / max(red_threshold - yellow_threshold, 1e-9)
result[high_middle] = interpolate_color(yellow, red, ratio)
if np.any(low_middle):
ratio = (values[low_middle] - blue_threshold) / max(yellow_threshold - blue_threshold, 1e-9)
result[low_middle] = interpolate_color(blue, yellow, ratio)
if np.any(low):
min_value = float(np.min(values[low])) if np.any(low) else blue_threshold
ratio = (values[low] - min_value) / max(blue_threshold - min_value, 1e-9)
result[low] = interpolate_color(green, blue, ratio)
result[high] = control_colors[0]
for index in range(1, len(thresholds)):
upper_threshold = thresholds[index - 1]
lower_threshold = thresholds[index]
between = (values < upper_threshold) & (values >= lower_threshold)
if np.any(between):
ratio = (values[between] - lower_threshold) / max(upper_threshold - lower_threshold, 1e-9)
result[between] = interpolate_color(control_colors[index], control_colors[index - 1], ratio)
return np.clip(result, 0, 255).astype(np.uint8)
@@ -858,7 +871,7 @@ def export_market_excellent_heatmap_tif(
value_weight[row_start:row_stop, col_start:col_stop] += 1.0
matched_values.append(value)
thresholds = percentile_thresholds_from_unique(matched_values, EXCELLENT_PERCENT_STEPS)
thresholds = percentile_thresholds_from_values(matched_values, EXCELLENT_PERCENT_STEPS)
data_mask = value_weight > 0
if np.any(data_mask):
smoothed_sum = gaussian_blur(value_sum, HEATMAP_SMOOTH_RADIUS)
@@ -877,7 +890,7 @@ def export_market_excellent_heatmap_tif(
"painted_cells": len(matched_values),
"data_pixels": int(np.count_nonzero(image[..., 3] > 0)),
"skipped_no_value": skipped_no_value,
"unique_value_count": len(set(matched_values)),
"threshold_value_count": len(matched_values),
"thresholds": thresholds,
"percent_steps": list(EXCELLENT_PERCENT_STEPS),
"width": width,
@@ -902,10 +915,10 @@ def write_excellent_heatmap_legend(output_dir, field):
with legend_path.open("w", newline="", encoding="utf-8-sig") as file:
writer = csv.writer(file)
writer.writerow(["field", "control_point", "red", "green", "blue", "alpha", "meaning"])
writer.writerow([field, "threshold_20_percent", *EXCELLENT_COLORS[0], HEATMAP_ALPHA, "high end"])
writer.writerow([field, "threshold_40_percent", *EXCELLENT_COLORS[1], HEATMAP_ALPHA, "high-middle control"])
writer.writerow([field, "threshold_60_percent", *EXCELLENT_COLORS[2], HEATMAP_ALPHA, "low-middle control"])
writer.writerow([field, "minimum", *EXCELLENT_COLORS[3], HEATMAP_ALPHA, "low end"])
for percent, color in zip(EXCELLENT_PERCENT_STEPS, palette_colors_for_steps(len(EXCELLENT_PERCENT_STEPS))):
writer.writerow(
[field, f"threshold_{int(round(percent * 100))}_percent", *color, HEATMAP_ALPHA, "sorted-value control"]
)
writer.writerow([field, "nodata", 0, 0, 0, 0, "transparent no data"])
return legend_path
@@ -915,10 +928,10 @@ def write_weak_cover_heatmap_legend(output_dir, field):
with legend_path.open("w", newline="", encoding="utf-8-sig") as file:
writer = csv.writer(file)
writer.writerow(["field", "control_point", "red", "green", "blue", "alpha", "meaning"])
writer.writerow([field, "threshold_20_percent", *EXCELLENT_COLORS[0], HEATMAP_ALPHA, "high end"])
writer.writerow([field, "threshold_40_percent", *EXCELLENT_COLORS[1], HEATMAP_ALPHA, "high-middle control"])
writer.writerow([field, "threshold_60_percent", *EXCELLENT_COLORS[2], HEATMAP_ALPHA, "low-middle control"])
writer.writerow([field, "minimum", *EXCELLENT_COLORS[3], HEATMAP_ALPHA, "low end"])
for percent, color in zip(EXCELLENT_PERCENT_STEPS, palette_colors_for_steps(len(EXCELLENT_PERCENT_STEPS))):
writer.writerow(
[field, f"threshold_{int(round(percent * 100))}_percent", *color, HEATMAP_ALPHA, "sorted-value control"]
)
writer.writerow([field, "nodata", 0, 0, 0, 0, "transparent no data"])
return legend_path