time_series_analysis.py 4.38 KB
Newer Older
李明杰's avatar
李明杰 committed
1 2 3 4 5 6 7
# -*- coding: utf-8 -*-
# author:Li Mingjie time:2019/2/28
# Brief:Time Series Data Analysis
import pandas as pd
import matplotlib.pyplot as plt
import datetime as dt
import bottom_function.data_read as dr
李明杰's avatar
李明杰 committed
8 9 10 11 12

# import json
# from flask import Flask
# from flask import request
# from flask_cors import CORS
李明杰's avatar
李明杰 committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66


def datetime_data_plot(timetype, starttime, endtime, graphtype):
    gree_data = dr.read_domain_data(datatype="control", starttime=starttime, endtime=endtime)
    tent_data = dr.read_domain_data(datatype="application", starttime=starttime, endtime=endtime)

    # gree_data['datetime'] = gree_data['datetime'].apply(lambda x: dt.datetime.strftime(x, "%Y-%m-%d %H "))
    # tent_data['datetime'] = tent_data['datetime'].apply(lambda x: dt.datetime.strftime(x, "%Y-%m-%d %H "))

    gree_data = gree_data.set_index('datetime', drop=True)
    tent_data = tent_data.set_index('datetime', drop=True)
    mg_data = gree_data.apply(sum, axis=1)
    mt_data = tent_data.apply(sum, axis=1)

    g_data = pd.DataFrame()
    t_data = pd.DataFrame()
    all_data = pd.DataFrame()
    if timetype == "hour":
        g_data = mg_data.resample('H').sum()
        t_data = mt_data.resample('H').sum()
        all_data = pd.concat([g_data, t_data], axis=1)
        all_data.columns = ['control', 'application']
        index_data = all_data.index.tolist()
        for i in range(len(index_data)):
            index_data[i] = dt.datetime.strftime(index_data[i], "%Y-%m-%d %H ")
        all_data.index = index_data
    if timetype == "day":
        g_data = mg_data.resample('D').sum()
        t_data = mt_data.resample('D').sum()
        all_data = pd.concat([g_data, t_data], axis=1)
        all_data.columns = ['control', 'application']
        index_data = all_data.index.tolist()
        for i in range(len(index_data)):
            index_data[i] = dt.datetime.strftime(index_data[i], "%Y-%m-%d")
        all_data.index = index_data
    if timetype == "month":
        g_data = mg_data.resample('M').sum()
        t_data = mt_data.resample('M').sum()
        all_data = pd.concat([g_data, t_data], axis=1)
        all_data.columns = ['control', 'application']
        index_data = all_data.index.tolist()
        for i in range(len(index_data)):
            index_data[i] = dt.datetime.strftime(index_data[i], "%Y-%m")
        all_data.index = index_data
    if timetype == "year":
        g_data = mg_data.resample('Y').sum()
        t_data = mt_data.resample('Y').sum()
        all_data = pd.concat([g_data, t_data], axis=1)
        all_data.columns = ['control', 'application']
        index_data = all_data.index.tolist()
        for i in range(len(index_data)):
            index_data[i] = dt.datetime.strftime(index_data[i], "%Y")
        all_data.index = index_data

李明杰's avatar
李明杰 committed
67 68
    # fig = plt.figure(figsize=(16, 6))
    all_data.plot(kind=graphtype, stacked=True, use_index=True,figsize=(10, 8))
李明杰's avatar
李明杰 committed
69 70 71 72
    plt.xticks(rotation=45)

    plt.title(str(starttime) + ' to ' + str(
        endtime) + ' ' + timetype + ' datetime domain analysis of ' + graphtype + ' graph',
李明杰's avatar
李明杰 committed
73
              fontsize=15)
李明杰's avatar
李明杰 committed
74 75 76
    plt.tight_layout(5)
    path = '/roobo/soft/phpmyadmin/plot_time.jpg'
    plt.savefig(path)
李明杰's avatar
李明杰 committed
77
    return 'http://120.79.171.145:8000/plot_time.jpg'
李明杰's avatar
李明杰 committed
78 79


李明杰's avatar
李明杰 committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
# app = Flask(__name__)
# CORS(app, supports_credentials=True)
#
#
# @app.route('/SPDAS/time_series_analysis1', methods=['POST'])
# def domain():
#     param = ({"time_type": [{"value": "hour", "id": 1}, {"value": "day", "id": 2},
#                             {"value": "month", "id": 3}, {"value": "year", "id": 4}],
#               "time": "2018-12-01 00:00:00/2018-12-02 00:00:00",
#               "graph_type": [{"value": "bar"}, {"value": "pie"}]})
#     return json.JSONEncoder().encode(param)
#
#
# @app.route('/SPDAS/time_series_analysis2', methods=['POST'])
# def domain_form():
#     # 需要从request对象读取表单内容:
#     data = request.get_data()
#     json_re = json.loads(data)
#     print(json_re)
#     timetype = json_re['time_type']
#     m_time = json_re['time']
#     graphtype = json_re['graph_type']
#     str_time = str(m_time)
#     m_time = str_time.split('/')
#     starttime = m_time[0]
#     endtime = m_time[1]
#     image_path = datetime_data_plot(timetype=timetype, starttime=starttime, endtime=endtime, graphtype=graphtype)
#     path = ({"time_image": image_path})
#     return json.JSONEncoder().encode(path)
#
#
# if __name__ == '__main__':
#     app.run(debug=True, host='10.7.19.129', port=5000)