main.py 5.59 KB
Newer Older
Romain Creuzenet's avatar
Romain Creuzenet committed
1
"""File to execute to show results"""
{}'s avatar
{} committed
2
3
4
5
6
7
8
#Data
from parameters import SESSION

#Basics
import re
from datetime import datetime, timedelta
import matplotlib.dates as mdates
Romain Creuzenet's avatar
Romain Creuzenet committed
9
10
import numpy as np
import matplotlib.pyplot as plt
{}'s avatar
{} committed
11
12
13
14
15
16
17
18
19
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
#Stats
import statsmodels.graphics as stm_graphs
import pandas as pd 
import statsmodels.api as stm
#Graph map
from mpl_toolkits.basemaps import Basemap

Romain Creuzenet's avatar
Romain Creuzenet committed
20
21
22
23
24
25
26
27
28
29
30
31
32


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


def ask_q(possibilities, text=">>> "):
    answer = None
    while answer not in possibilities:
        answer = input(text)
    return answer

{}'s avatar
{} committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def ask_d(text=">>> "):
    t = input(text)
    done = False

    date_parser = re.compile(
        r"(?P<year>\d+)-(?P<month>\d+)-(?P<day>\d+) (?P<hour>\d+):(?P<minute>\d+)"
    )
    match = False
    result = None



    while(match == False):
        match = date_parser.match(t)
        if not match:
            print("Veuillez suivre le format demandé")
            continue
        m = match.groupdict()
        result = (int(m['year']), int(m['month']), int(m['day']), int(m['hour']), int(m['minute']))
           
            
    return result
Romain Creuzenet's avatar
Romain Creuzenet committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

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
79
        stations = []
Romain Creuzenet's avatar
Romain Creuzenet committed
80
        print("Choisissez une station parmis celles-ci:")
Romain Creuzenet's avatar
Romain Creuzenet committed
81
82
83
84
85
86
87
        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
88
89
90
91
92
93
94
95
96
97
98
99
        station = ask_q(stations)

        # 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)
        attr = ask_q(decision.keys())

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

        figure = plt.figure(figsize=(25,16))
        axes = plt.subplot()
        axes.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M'))
        _=plt.xticks(rotation=90) 
        
        
        plt.plot(ts, label=attr)
        plt.title("Donnees de " + str(attr) + " pour la station : " + station )
Romain Creuzenet's avatar
Romain Creuzenet committed
116
117
        plt.legend()
        plt.show()
{}'s avatar
{} committed
118
119
120
121
122
123
124
125
126
127
128
        plt.savefig('./out/graph_'+station+'_'+ str(attr) +'.png')

        res = stm.tsa.seasonal_decompose(ts, freq=15, extrapolate_trend= 'freq')
        res.plot()
        plt.show()
        plt.savefig('./out/decompose_'+station+'_'+ str(attr) +'.png')
        

        ACF = stm_graphs.tsaplots.plot_acf(ts,lags=30)
        plt.show()
        plt.savefig('./out/acf_'+station+'_'+ str(attr) +'.png')
Romain Creuzenet's avatar
Romain Creuzenet committed
129
130

    def map(self):
{}'s avatar
{} committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
        self.table = "TABLE_TIME"

        #Ask Date
        print("Entrez une date sous la forme YYYY-MM-DD HH:mm")
        date = ask_d()

        # 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)
        attr = ask_q(decision.keys())


        df = pd.DataFrame()
        query = "SELECT station,lon,lat,{} FROM {} WHERE time={}".format(attr,self.table, date)
        print(query)
        for row in execute_query(query):
            if getattr(row, "station") is None:
                continue
            print(getattr(row,"station"))
            df = df.append({"station": getattr(row, "station"),
                        "lon":getattr(row,"lon"),
                        "lat":getattr(row,"lat"),
                        "val":getattr(row,attr)},
                        ignore_index=True)

        
        map = Basemap(projection='mill',lat_0=26.281898,lon_0=-16.42,lat_1 = 43.08,lon_1 =7.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
177
178
179
180
181
182
183

    def cluster(self):
        print("Cette fonction n'est pas implémentée")


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