Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
modules/chart_type_recommender/__init__.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Chart Type Recommender package initialization.
|
| 3 |
+
"""
|
modules/chart_type_recommender/chart_type_recommender.py
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
|
| 4 |
+
"""
|
| 5 |
+
图表类型推荐模块 (chart_type_recommender)
|
| 6 |
+
基于输入数据特征,自动推荐最合适的图表类型
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import json
|
| 10 |
+
import logging
|
| 11 |
+
import argparse
|
| 12 |
+
from typing import Dict, List, Any, Tuple
|
| 13 |
+
|
| 14 |
+
# 配置日志
|
| 15 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 16 |
+
logger = logging.getLogger(__name__)
|
| 17 |
+
|
| 18 |
+
# 支持的图表类型
|
| 19 |
+
CHART_TYPES = [
|
| 20 |
+
"vertical_bar_chart",
|
| 21 |
+
"horizontal_bar_chart",
|
| 22 |
+
"vertical_stacked_bar_chart",
|
| 23 |
+
"horizontal_stacked_bar_chart",
|
| 24 |
+
"grouped_bar_chart",
|
| 25 |
+
"line_chart",
|
| 26 |
+
"area_chart",
|
| 27 |
+
"pie_chart",
|
| 28 |
+
"donut_chart",
|
| 29 |
+
"scatter_plot",
|
| 30 |
+
"bubble_chart",
|
| 31 |
+
"heatmap"
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
def analyze_data_structure(data: Dict[str, Any]) -> Dict[str, Any]:
|
| 35 |
+
"""
|
| 36 |
+
分析数据结构,提取关键特征
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
data: 输入数据对象
|
| 40 |
+
|
| 41 |
+
Returns:
|
| 42 |
+
包含数据特征的字典
|
| 43 |
+
"""
|
| 44 |
+
features = {}
|
| 45 |
+
|
| 46 |
+
# 提取列信息
|
| 47 |
+
columns = data.get("data", {}).get("columns", [])
|
| 48 |
+
features["column_count"] = len(columns)
|
| 49 |
+
|
| 50 |
+
# 分析列类型
|
| 51 |
+
time_columns = []
|
| 52 |
+
number_columns = []
|
| 53 |
+
categorical_columns = []
|
| 54 |
+
|
| 55 |
+
for col in columns:
|
| 56 |
+
data_type = col.get("data_type", "")
|
| 57 |
+
if data_type == "time":
|
| 58 |
+
time_columns.append(col["name"])
|
| 59 |
+
elif data_type == "number":
|
| 60 |
+
number_columns.append(col["name"])
|
| 61 |
+
elif data_type == "categorical":
|
| 62 |
+
categorical_columns.append(col["name"])
|
| 63 |
+
|
| 64 |
+
features["time_columns"] = time_columns
|
| 65 |
+
features["number_columns"] = number_columns
|
| 66 |
+
features["categorical_columns"] = categorical_columns
|
| 67 |
+
|
| 68 |
+
# 分析数据行数
|
| 69 |
+
rows = data.get("data", {}).get("data", [])
|
| 70 |
+
features["row_count"] = len(rows)
|
| 71 |
+
|
| 72 |
+
return features
|
| 73 |
+
|
| 74 |
+
def recommend_chart_types(data_features: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 75 |
+
"""
|
| 76 |
+
基于数据特征推荐合适的图表类型
|
| 77 |
+
|
| 78 |
+
Args:
|
| 79 |
+
data_features: 数据特征字典
|
| 80 |
+
|
| 81 |
+
Returns:
|
| 82 |
+
推荐的图表类型列表,按置信度排序
|
| 83 |
+
"""
|
| 84 |
+
recommendations = []
|
| 85 |
+
|
| 86 |
+
# 检查基本条件
|
| 87 |
+
has_time = len(data_features["time_columns"]) > 0
|
| 88 |
+
has_number = len(data_features["number_columns"]) > 0
|
| 89 |
+
has_category = len(data_features["categorical_columns"]) > 0
|
| 90 |
+
|
| 91 |
+
# 时间序列分析
|
| 92 |
+
if has_time and has_number:
|
| 93 |
+
if has_category:
|
| 94 |
+
# 具有分类的时间序列,推荐堆叠图和分组柱状图
|
| 95 |
+
recommendations.append({
|
| 96 |
+
"type": "vertical_stacked_bar_chart",
|
| 97 |
+
"confidence": 0.92,
|
| 98 |
+
"reasoning": "适合比较不同时间段内多个类别的分布情况,同时展示总量变化趋势"
|
| 99 |
+
})
|
| 100 |
+
|
| 101 |
+
recommendations.append({
|
| 102 |
+
"type": "grouped_bar_chart",
|
| 103 |
+
"confidence": 0.75,
|
| 104 |
+
"reasoning": "适合清晰对比不同时期内各类别的具体数值"
|
| 105 |
+
})
|
| 106 |
+
|
| 107 |
+
recommendations.append({
|
| 108 |
+
"type": "area_chart",
|
| 109 |
+
"confidence": 0.68,
|
| 110 |
+
"reasoning": "适合展示不同类别随时间的变化趋势和累积效应"
|
| 111 |
+
})
|
| 112 |
+
else:
|
| 113 |
+
# 简单时间序列,推荐折线图和柱状图
|
| 114 |
+
recommendations.append({
|
| 115 |
+
"type": "line_chart",
|
| 116 |
+
"confidence": 0.88,
|
| 117 |
+
"reasoning": "适合展示连续时间序列的趋势变化"
|
| 118 |
+
})
|
| 119 |
+
|
| 120 |
+
recommendations.append({
|
| 121 |
+
"type": "vertical_bar_chart",
|
| 122 |
+
"confidence": 0.75,
|
| 123 |
+
"reasoning": "适合比较不同时间点的数值大小"
|
| 124 |
+
})
|
| 125 |
+
|
| 126 |
+
# 分类比较
|
| 127 |
+
elif has_category and has_number and not has_time:
|
| 128 |
+
recommendations.append({
|
| 129 |
+
"type": "horizontal_bar_chart",
|
| 130 |
+
"confidence": 0.85,
|
| 131 |
+
"reasoning": "适合比较不同类别的数值大小"
|
| 132 |
+
})
|
| 133 |
+
|
| 134 |
+
if len(data_features["categorical_columns"]) > 1:
|
| 135 |
+
recommendations.append({
|
| 136 |
+
"type": "heatmap",
|
| 137 |
+
"confidence": 0.72,
|
| 138 |
+
"reasoning": "适合展示两个分类变量之间的关系和数值分布"
|
| 139 |
+
})
|
| 140 |
+
else:
|
| 141 |
+
recommendations.append({
|
| 142 |
+
"type": "Pie Chart",
|
| 143 |
+
"confidence": 0.65,
|
| 144 |
+
"reasoning": "适合展示不同类别的占比情况"
|
| 145 |
+
})
|
| 146 |
+
|
| 147 |
+
# 如果没有匹配的推荐,提供默认选项
|
| 148 |
+
if not recommendations:
|
| 149 |
+
recommendations.append({
|
| 150 |
+
"type": "vertical_bar_chart",
|
| 151 |
+
"confidence": 0.60,
|
| 152 |
+
"reasoning": "通用图表类型,适合大多数数据展示需求"
|
| 153 |
+
})
|
| 154 |
+
|
| 155 |
+
return recommendations
|
| 156 |
+
|
| 157 |
+
def process(input: str, output: str) -> bool:
|
| 158 |
+
"""
|
| 159 |
+
处理输入数据并生成图表类型推荐
|
| 160 |
+
|
| 161 |
+
Args:
|
| 162 |
+
input: 输入JSON文件路径
|
| 163 |
+
output: 输出JSON文件路径
|
| 164 |
+
|
| 165 |
+
Returns:
|
| 166 |
+
处理成功返回True,否则返回False
|
| 167 |
+
"""
|
| 168 |
+
try:
|
| 169 |
+
# 读取输入数据
|
| 170 |
+
logger.info(f"读取输入文件: {input}")
|
| 171 |
+
with open(input, 'r', encoding='utf-8') as f:
|
| 172 |
+
data = json.load(f)
|
| 173 |
+
|
| 174 |
+
# 分析数据特征
|
| 175 |
+
logger.info("分析数据结构和特征")
|
| 176 |
+
data_features = analyze_data_structure(data)
|
| 177 |
+
|
| 178 |
+
# 生成图表类型推荐
|
| 179 |
+
logger.info("生成图表类型推荐")
|
| 180 |
+
chart_type_recommendations = recommend_chart_types(data_features)
|
| 181 |
+
|
| 182 |
+
# 添加推荐结果到原始数据
|
| 183 |
+
data["chart_type"] = chart_type_recommendations
|
| 184 |
+
|
| 185 |
+
# 写入输出文件
|
| 186 |
+
logger.info(f"写入输出文件: {output}")
|
| 187 |
+
with open(output, 'w', encoding='utf-8') as f:
|
| 188 |
+
json.dump(data, f, ensure_ascii=False, indent=2)
|
| 189 |
+
|
| 190 |
+
logger.info("图表类型推荐完成")
|
| 191 |
+
return True
|
| 192 |
+
|
| 193 |
+
except Exception as e:
|
| 194 |
+
logger.error(f"处理失败: {str(e)}")
|
| 195 |
+
return False
|
| 196 |
+
|
| 197 |
+
if __name__ == "__main__":
|
| 198 |
+
parser = argparse.ArgumentParser(description="ChartPipeline - 图表类型推荐模块")
|
| 199 |
+
parser.add_argument("--input", required=True, help="输入JSON文件路径")
|
| 200 |
+
parser.add_argument("--output", required=True, help="输出JSON文件路径")
|
| 201 |
+
|
| 202 |
+
args = parser.parse_args()
|
| 203 |
+
|
| 204 |
+
process(input=args.input, output=args.output)
|