Commit 86c8b86e by Oscar Roisin

### clean code + add map to kmeans

parent 24cbaffa
 ... ... @@ -3,15 +3,17 @@ from datetime import datetime from sklearn.cluster import KMeans import numpy as np import folium import loading as l import history as h colours = ['blue', 'red', 'green', 'orange', 'pink', 'white', 'purple', 'gray'] def getDatasForPeriod(startPeriod, endPeriod, indicators): datas = [] for i in range(int(startPeriod[0:4]), int(endPeriod[0:4]) + 1): datas += session.execute(f"SELECT year, month, day, station, {indicators} FROM {l.table_name_date} where year = {i}") datas += session.execute(f"SELECT year, month, day, station, lat, lon, {indicators} FROM {l.table_name_date} where year = {i}") return datas ... ... @@ -35,12 +37,12 @@ def getDecileForAllStations(startPeriod, endPeriod, table, nb_indicators, indica if t[3] not in l.keys(): l[t[3]] = [] for i in range(nb_indicators): if t[4 + i] != None: l[t[3]].append({indicators_list[i] : [float(t[4 + i])]}) if t[6 + i] != None: l[t[3]].append({indicators_list[i] : [float(t[6 + i])]}) else: for i in range(nb_indicators): if t[4 + i] != None: l[t[3]][i][indicators_list[i]].append(float(t[4 + i])) if t[6 + i] != None: l[t[3]][i][indicators_list[i]].append(float(t[6 + i])) # Sort all lists of values for station in l.keys(): ... ... @@ -77,7 +79,7 @@ def applyKmeans(deciles, nb_indicators, indicators_list, startPeriod, endPeriod) t += deciles[station][i][indicators_list[i]] table.append(t) nb_clusters = 3 nb_clusters = 4 if len(stations_name) < nb_clusters: print(f"Le nombre de villes ayant des données est trop inférieur ({len(stations_name)}) pour appliquer les kmeans pour la période du {startPeriod} au {endPeriod}") return None ... ... @@ -124,14 +126,26 @@ def kmeans(startPeriod, endPeriod, indicators_list): table = getDatasForPeriod(startPeriod, endPeriod, indicators) table = list(table) # Get coordinates coord = dict() for t in table: coord[t[3]]=(t[4], t[5]) # Get the map with all deciles for all stations and indicators table_deciles = getDecileForAllStations(startPeriod, endPeriod, table, nb_indicators, indicators_list_numeric) station_with_center = applyKmeans(table_deciles, nb_indicators, indicators_list_numeric, startPeriod, endPeriod) if station_with_center != None: print(f"Voici les villes et le cluster auxquelles elles appartiennent:") print(f"{station_with_center}") file_name = f"{startPeriod} to {endPeriod}.html" # Create map m = folium.Map(location=[64.2815, 27.6753]) # Add Marker for each station for key, value in station_with_center.items(): folium.Marker([coord[key][0], coord[key][1]], popup=f"{key}", icon=folium.Icon(color=colours[value])).add_to(m) # Save map m.save(file_name) print(f"La carte a été enregistrée à {file_name}") else: print(f"Aucune clusterisation déterminée") if __name__ == '__main__': cluster = Cluster() ... ...
 ... ... @@ -307,7 +307,7 @@ def createTableQueryPartitionningByDate(table): peak_wind_drct decimal, peak_wind_time decimal, metar varchar, PRIMARY KEY((year, month), day, station, hour, minute) PRIMARY KEY((year), month, day, station, hour, minute) )""" return query ... ... @@ -358,7 +358,7 @@ def insertQueryData(row, table): """ return query # Not to execute when importing file # Not to execute when importing file if __name__ == '__main__': cluster = Cluster() ... ...
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!