ggpx_statistics/app.py

116 lines
3.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from threading import Lock
import pandas as pd
import requests
from flask import Flask, send_file, make_response, session, request
from matplotlib import rcParams
from redis import Redis
from pca import plot_pca_scatter
from radarref import plot_radar
from similarity import row_correlations, row_euclidean_distances, calculate_similarity, plot_similarity_heatmap, \
plot_similarity_network
# 全局设置中文字体
rcParams['font.sans-serif'] = ['SimHei'] # 设置中文字体为黑体
rcParams['axes.unicode_minus'] = False # 解决坐标轴负号显示问题
app = Flask(__name__)
redis_client = Redis(host='localhost', port=6379, db=0, password="7ZH2jxM1")
lock = Lock()
def handleGetData(taskId, ids):
# 获取键对应的值
content = redis_client.get(f'{taskId}:{ids}')
if content:
print(f"获取到了{{taskId}}数据---------------")
return content
if ids.find(",") != -1:
new_ids = [int(item) for item in ids.split(",")]
else:
new_ids = [] if ids == '' else [int(ids)]
resp = requests.post(f"http://localhost:8069/prod-api/ggpx/TaskData/toEXCEL2",
{"taskId": taskId,
'ids': new_ids})
# 设置键值对
redis_client.set(f'{taskId}:{ids}', resp.content, 5)
return resp.content
@app.route('/')
def index():
return 'Hello World'
@app.route('/statistics/radar/<id>')
def radar(id):
ids = request.args.get('ids', '')
# 一个行名为样本名列名为指标名的pd.dataframe
data = pd.read_excel(handleGetData(id, ids), index_col=0)
# 任务设置的每个指标的最大值
max_values = [10, 15, 15, 10, 15, 5, 10, 10, 5, 5] # 示例最大值向量
while True:
with lock:
# 使用这个函数
img_bytes = plot_radar(data, max_values)
# 创建响应对象
response = make_response(img_bytes.read())
response.headers['Content-Type'] = 'image/png'
return response
@app.route('/statistics/pca/<id>')
def pca(id):
ids = request.args.get('ids', '')
# 一个行名为样本名列名为指标名的pd.dataframe
data = pd.read_excel(handleGetData(id, ids), index_col=0)
while True:
with lock:
# 生成散点图,带上标签
img_bytes = plot_pca_scatter(data, labels=data.index)
# 创建响应对象
response = make_response(img_bytes.read())
response.headers['Content-Type'] = 'image/png'
return response
@app.route('/statistics/similarity/<id>')
def similarity(id):
ids = request.args.get('ids', '')
# 一个行名为样本名列名为指标名的pd.dataframe
matrix = pd.read_excel(handleGetData(id, ids), index_col=0)
# 调用函数
correlation_result = row_correlations(matrix)
while True:
with lock:
# 绘制相关系数热力图
img_bytes = plot_similarity_heatmap(correlation_result)
# 创建响应对象
response = make_response(img_bytes.read())
response.headers['Content-Type'] = 'image/png'
return response
@app.route('/statistics/similarity2/<id>')
def similarity2(id):
ids = request.args.get('ids', '')
# 示例矩阵,和之前的扇形图要求一样
matrix = pd.read_excel(handleGetData(id, ids), index_col=0)
# 调用函数
correlation_result = row_correlations(matrix)
distance_result = row_euclidean_distances(matrix)
similarity_result = calculate_similarity(correlation_result, distance_result)
while True:
with lock:
# 绘制相似度网络图
img_bytes = plot_similarity_network(similarity_result, threshold=0.5)
# 创建响应对象
response = make_response(img_bytes.read())
response.headers['Content-Type'] = 'image/png'
return response
if __name__ == '__main__':
app.run(threaded=True, host='0.0.0.0')