main.py 7.43 KB
Newer Older
Romain Creuzenet's avatar
Romain Creuzenet committed
1
"""File to execute to show results"""
Romain Creuzenet's avatar
Romain Creuzenet committed
2
# Data
{}'s avatar
{} committed
3
from parameters import SESSION
Romain Creuzenet's avatar
Romain Creuzenet committed
4
# Basic
{}'s avatar
{} committed
5
6
import re
import matplotlib.dates as mdates
Romain Creuzenet's avatar
Romain Creuzenet committed
7
import matplotlib.pyplot as plt
Romain Creuzenet's avatar
Romain Creuzenet committed
8
9
import warnings
# Stats
{}'s avatar
{} committed
10
11
12
import statsmodels.graphics as stm_graphs
import pandas as pd 
import statsmodels.api as stm
Romain Creuzenet's avatar
Romain Creuzenet committed
13
14
15
16
17
import numpy as np
# Graph map
from mpl_toolkits.basemap import Basemap
from pandas.plotting import register_matplotlib_converters
from datetime import datetime
{}'s avatar
{} committed
18

Romain Creuzenet's avatar
Romain Creuzenet committed
19
20
register_matplotlib_converters()
warnings.filterwarnings("ignore")
Romain Creuzenet's avatar
Romain Creuzenet committed
21
22
23
24
25
26
27
28


def execute_query(query):
    for row in SESSION.execute(query):
        yield row


def ask_q(possibilities, text=">>> "):
Romain Creuzenet's avatar
Romain Creuzenet committed
29
    """Demande une question"""
Romain Creuzenet's avatar
Romain Creuzenet committed
30
31
32
33
34
    answer = None
    while answer not in possibilities:
        answer = input(text)
    return answer

{}'s avatar
{} committed
35

Romain Creuzenet's avatar
Romain Creuzenet committed
36
37
38
39
40
def ask_d(text=">>> "):
    """Demande une date"""
    print("Entrez une date sous la forme YYYY-MM-DD HH:mm")
    date_parser = re.compile(r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2}) (?P<hour>\d{2}):(?P<minute>\d{2})")
    match = None
{}'s avatar
{} committed
41

Romain Creuzenet's avatar
Romain Creuzenet committed
42
43
    while match is None:
        t = input(text)
{}'s avatar
{} committed
44
        match = date_parser.match(t)
Romain Creuzenet's avatar
Romain Creuzenet committed
45
46
47

    m = match.groupdict()
    result = (int(m['year']), int(m['month']), int(m['day']), int(m['hour']), int(m['minute']))
{}'s avatar
{} committed
48
49
            
    return result
Romain Creuzenet's avatar
Romain Creuzenet committed
50

Romain Creuzenet's avatar
Romain Creuzenet committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

def choose_attr():
    """Permet de demander un attribut dans la table"""
    # Search element
    decision = {
        "tmpf": "La témparature",
        "relh": "L'humidité"
    }
    print("Choisissez un élément parmis les suivant :")
    for code, text in decision.items():
        print("\t-", text, ":", code)
    return ask_q(decision.keys())


def ask_cluster(text=">>> "):
    answer = ""
    while not answer or not answer.isdigit():
        answer = input(text)
    return int(answer)


Romain Creuzenet's avatar
Romain Creuzenet committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
class Manager:
    table = None  # table name use by the function

    def run(self):
        """Chose"""
        print("Choisissez ce que vous voulez faire")
        print("\t1 - Pour un point donné de l’espace, je veux pouvoir avoir un historique du passé")
        print("\t2 - À un instant donné je veux pouvoir obtenir une carte me représentant n’importe quel indicateur")
        print("\t3 - Pour une période de temps donnée, je veux pouvoir obtenir clusteriser l’espace, et représenter "
              "cette clusterisation")
        decision = {
            "1": "historic",
            "2": "map",
            "3": "cluster"
        }
        answer = ask_q(decision.keys())
        getattr(self, decision[answer])()

    def historic(self):
        self.table = "TABLE_SPACE"

        # Search station
Romain Creuzenet's avatar
Romain Creuzenet committed
94
        stations = []
Romain Creuzenet's avatar
Romain Creuzenet committed
95
        print("Choisissez une station parmis celles-ci:")
Romain Creuzenet's avatar
Romain Creuzenet committed
96
97
98
99
100
101
102
        query = "SELECT DISTINCT station FROM {}".format(self.table)
        for i, row in enumerate(execute_query(query), 1):
            end = "\n" if i % 3 == 0 else ""
            print("\t", row.station, end=end)
            stations.append(row.station)
        print()

Romain Creuzenet's avatar
Romain Creuzenet committed
103
        station = ask_q(stations)
Romain Creuzenet's avatar
Romain Creuzenet committed
104
        attr = choose_attr()
Romain Creuzenet's avatar
Romain Creuzenet committed
105

{}'s avatar
{} committed
106
        ts = pd.Series()
Romain Creuzenet's avatar
Romain Creuzenet committed
107
        query = "SELECT time ,{} FROM {} WHERE station={}".format(attr, self.table, station.__repr__())
Romain Creuzenet's avatar
Romain Creuzenet committed
108
        for row in execute_query(query):
Romain Creuzenet's avatar
Romain Creuzenet committed
109
110
111
            value = getattr(row, attr)
            if value is None:
                continue
{}'s avatar
{} committed
112
113
            ts.loc[datetime(*list(row.time))] = value

Romain Creuzenet's avatar
Romain Creuzenet committed
114
        plt.figure(figsize=(25, 16))
{}'s avatar
{} committed
115
116
        axes = plt.subplot()
        axes.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M'))
Romain Creuzenet's avatar
Romain Creuzenet committed
117
118
        plt.xticks(rotation=90)

{}'s avatar
{} committed
119
        plt.plot(ts, label=attr)
Romain Creuzenet's avatar
Romain Creuzenet committed
120
        plt.title("Donnees de " + str(attr) + " pour la station : " + station)
Romain Creuzenet's avatar
Romain Creuzenet committed
121
122
        plt.legend()
        plt.show()
Romain Creuzenet's avatar
Romain Creuzenet committed
123
        plt.savefig('./out/graph_' + station + '_' + str(attr) + '.png')
{}'s avatar
{} committed
124

Romain Creuzenet's avatar
Romain Creuzenet committed
125
        res = stm.tsa.seasonal_decompose(ts, freq=15, extrapolate_trend='freq')
{}'s avatar
{} committed
126
127
        res.plot()
        plt.show()
Romain Creuzenet's avatar
Romain Creuzenet committed
128
        plt.savefig('./out/decompose_' + station + '_' + str(attr) + '.png')
{}'s avatar
{} committed
129

Romain Creuzenet's avatar
Romain Creuzenet committed
130
        stm_graphs.tsaplots.plot_acf(ts, lags=30)
{}'s avatar
{} committed
131
        plt.show()
Romain Creuzenet's avatar
Romain Creuzenet committed
132
        plt.savefig('./out/acf_' + station + '_' + str(attr) + '.png')
Romain Creuzenet's avatar
Romain Creuzenet committed
133
134

    def map(self):
{}'s avatar
{} committed
135
136
137
        self.table = "TABLE_TIME"

        date = ask_d()
Romain Creuzenet's avatar
Romain Creuzenet committed
138
        attr = choose_attr()
{}'s avatar
{} committed
139

Romain Creuzenet's avatar
Romain Creuzenet committed
140
141
142
        plt.figure(figsize=(14, 14))
        the_map = Basemap(projection='mill', llcrnrlat=25.281898, llcrnrlon=-18.42, urcrnrlat=48.08, urcrnrlon=10.35,
                          resolution='l')
{}'s avatar
{} committed
143
        # draw coastlines, country boundaries, fill continents.
Romain Creuzenet's avatar
Romain Creuzenet committed
144
145
146
        the_map.drawcoastlines(linewidth=0.25)
        the_map.drawcountries(linewidth=0.25)
        the_map.fillcontinents(color='coral', lake_color='aqua')
{}'s avatar
{} committed
147
        # draw the edge of the map projection region (the projection limb)
Romain Creuzenet's avatar
Romain Creuzenet committed
148
        the_map.drawmapboundary(fill_color='aqua')
{}'s avatar
{} committed
149
        # draw lat/lon grid lines every 30 degrees.
Romain Creuzenet's avatar
Romain Creuzenet committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
        the_map.drawmeridians(np.arange(0, 360, 30))
        the_map.drawparallels(np.arange(-90, 90, 30))

        try:
            query = "SELECT station, lon, lat, {} FROM {} WHERE time={}".format(attr, self.table, date)
            for row in execute_query(query):
                if getattr(row, "station") is None:
                    continue
                x, y = the_map(row.lon, row.lat)
                value = getattr(row, attr)
                plt.plot(x, y, 'go')
                plt.annotate(round(value, 1), (x, y))
            plt.title('Map')
            plt.show()
            plt.savefig('./out/map.png')
        except:
            dateb = list(date)
            dateb[3] = 0
            dateb[4] = 0
            dateb = tuple(dateb)

            datee = list(date)
            datee[3] = 23
            datee[4] = 59
            datee = tuple(dateb)
            print("Seul ces heures sont disponibles pour ce jour")
            query = "SELECT DISTINCT time FROM {} WHERE time>={} AND time <= {} ALLOW FILTERING".format(self.table,
                                                                                                        dateb, datee)
            for row in execute_query(query):
                print(getattr(row, "time"))
{}'s avatar
{} committed
180

Romain Creuzenet's avatar
Romain Creuzenet committed
181
182
    def cluster(self):
        self.table = "TABLE_TIME"
{}'s avatar
{} committed
183

Romain Creuzenet's avatar
Romain Creuzenet committed
184
185
186
187
188
189
190
        # Ask Date
        print("Entrez la date de départ")
        date_b = ask_d()
        print("Entrez la date de fin")
        date_e = ask_d()
        print("Entrez le nombre de cluster voulus")
        nb_clust = ask_cluster()
Romain Creuzenet's avatar
Romain Creuzenet committed
191

Romain Creuzenet's avatar
Romain Creuzenet committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
        query = "SELECT * FROM {} WHERE time>={} AND time <= {}".format(self.table, date_b, date_e)
        for row in execute_query(query):
            if getattr(row, "station") is None:
                continue
            x, y = map(getattr(row, "lon"), getattr(row, "lat"))
            value = getattr(row, attr)
            plt.plot(x, y, 'go')
            plt.annotate(round(value, 1), (x, y))

        if False:
            plt.figure(figsize=(14, 14))
            map = Basemap(projection='mill', llcrnrlat=25.281898, llcrnrlon=-18.42, urcrnrlat=48.08, urcrnrlon=10.35,
                          resolution='l')
            # draw coastlines, country boundaries, fill continents.
            map.drawcoastlines(linewidth=0.25)
            map.drawcountries(linewidth=0.25)
            map.fillcontinents(color='coral', lake_color='aqua')
            # draw the edge of the map projection region (the projection limb)
            map.drawmapboundary(fill_color='aqua')
            # draw lat/lon grid lines every 30 degrees.
            map.drawmeridians(np.arange(0, 360, 30))
            map.drawparallels(np.arange(-90, 90, 30))

            plt.title('Map')
            plt.show()
            plt.savefig('./out/map.png')
Romain Creuzenet's avatar
Romain Creuzenet committed
218
219
220
221


if __name__ == "__main__":
    Manager().run()