Commit b1dc8612 authored by Romain Creuzenet's avatar Romain Creuzenet

cluster meterologique

parent dfbf01eb
......@@ -2,6 +2,7 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="dec891dc-2fad-4291-af33-64d4fd64029d" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/main.py" beforeDir="false" afterPath="$PROJECT_DIR$/main.py" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
......@@ -25,7 +26,7 @@
<entry file="file://$PROJECT_DIR$/main.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="273">
<caret line="313" selection-start-line="313" selection-end-line="313" />
<caret line="328" column="34" selection-start-line="328" selection-start-column="34" selection-end-line="328" selection-end-column="34" />
<folding>
<element signature="e#45#96#0" expanded="true" />
</folding>
......@@ -36,7 +37,7 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/download_data.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="19">
<state relative-caret-position="95">
<caret line="5" column="52" selection-start-line="5" selection-start-column="52" selection-end-line="5" selection-end-column="52" />
</state>
</provider>
......@@ -45,8 +46,8 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/parameters.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="817">
<caret line="67" column="11" lean-forward="true" selection-start-line="67" selection-start-column="5" selection-end-line="67" selection-end-column="16" />
<state relative-caret-position="1216">
<caret line="67" column="11" selection-start-line="67" selection-start-column="5" selection-end-line="67" selection-end-column="16" />
</state>
</provider>
</entry>
......@@ -196,45 +197,45 @@
<entry file="file://$PROJECT_DIR$/data/LFGA_2001_2010.csv" />
<entry file="file://$PROJECT_DIR$/data/LFLA_2001_2010.csv" />
<entry file="file://$PROJECT_DIR$/data/LFOI_2001_2010.csv" />
<entry file="file://$PROJECT_DIR$/create_table.py">
<entry file="file://$PROJECT_DIR$/env_nf26/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="912">
<caret line="51" column="17" selection-start-line="51" selection-start-column="17" selection-end-line="51" selection-end-column="17" />
<state relative-caret-position="-12363">
<caret line="585" column="8" selection-start-line="585" selection-start-column="8" selection-end-line="585" selection-end-column="8" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/download_data.py">
<entry file="file://$PROJECT_DIR$/env_nf26/lib/python3.6/site-packages/matplotlib/pyplot.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="19">
<caret line="5" column="52" selection-start-line="5" selection-start-column="52" selection-end-line="5" selection-end-column="52" />
<state relative-caret-position="291">
<caret line="2785" column="4" selection-start-line="2785" selection-start-column="4" selection-end-line="2785" selection-end-column="4" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/env_nf26/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py">
<entry file="file://$PROJECT_DIR$/create_table.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-12363">
<caret line="585" column="8" selection-start-line="585" selection-start-column="8" selection-end-line="585" selection-end-column="8" />
<state relative-caret-position="912">
<caret line="51" column="17" selection-start-line="51" selection-start-column="17" selection-end-line="51" selection-end-column="17" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/env_nf26/lib/python3.6/site-packages/matplotlib/pyplot.py">
<entry file="file://$PROJECT_DIR$/download_data.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="291">
<caret line="2785" column="4" selection-start-line="2785" selection-start-column="4" selection-end-line="2785" selection-end-column="4" />
<state relative-caret-position="95">
<caret line="5" column="52" selection-start-line="5" selection-start-column="52" selection-end-line="5" selection-end-column="52" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/parameters.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="817">
<caret line="67" column="11" lean-forward="true" selection-start-line="67" selection-start-column="5" selection-end-line="67" selection-end-column="16" />
<state relative-caret-position="1216">
<caret line="67" column="11" selection-start-line="67" selection-start-column="5" selection-end-line="67" selection-end-column="16" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/main.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="273">
<caret line="313" selection-start-line="313" selection-end-line="313" />
<caret line="328" column="34" selection-start-line="328" selection-start-column="34" selection-end-line="328" selection-end-column="34" />
<folding>
<element signature="e#45#96#0" expanded="true" />
</folding>
......
......@@ -63,8 +63,8 @@ def chose_attr():
"""Permet de demander un attribut dans la table"""
# Search element
decision = {
"tmpf": "La témparature",
"relh": "L'humidité"
"tmpf": "La témparature (en Fahrenheit)",
"relh": "L'humidité ( en %)"
}
print("Choisissez un élément parmis les suivant :")
for code, text in decision.items():
......@@ -80,30 +80,12 @@ def ask_int(text=">>> "):
return int(answer)
def generate_color():
return "#{:06x}".format(random.randint(0, 0xFFFFFF))
def clear_zone(zone):
"""
Permet de néttoyer une zone
:param zone: list de tuple x, y
:return: lats, lons
"""
xs, ys = [], []
for x, y in zone:
xs.append(x)
ys.append(y)
x_min = min(xs)
y_min = min(ys)
x_max = max(xs)
y_max = max(ys)
lons = [x_min, x_max, x_max, x_min, x_min]
lats = [y_max, y_max, y_min, y_min, y_max]
return lats, lons
def generate_color(i):
colors = 'bgrcmykw'
if i < len(colors):
return colors[i]
else:
return "#{:06x}".format(random.randint(0, 0xFFFFFF))
class Manager:
......@@ -254,8 +236,9 @@ class Manager:
# Ask information from user
print("=== Choix 3 : CLUSTER ===")
print("Vous allez devoir choisir une période de temps. Chaque station aura une pondérence suivant le nombre de"
" mesures prises durant cette péridoe")
attr = chose_attr()
print("Vous allez devoir choisir une période de temps. On considéra la moyenne de l'attribut sur cette "
"période de temps")
date_begin = date_end = None
while date_begin is None or date_begin >= date_end:
print("La date de départ :")
......@@ -265,50 +248,61 @@ class Manager:
print("Entrez le nombre de cluster voulus")
nb_cluster = ask_int()
# Initialisation
query = "SELECT lon, lat FROM {} WHERE time >= {} AND time <= {} ALLOW FILTERING" \
"".format(self.table, date_begin, date_end)
# Calc of mean
query = "SELECT station, lon, lat, {attr} FROM {table} WHERE time >= {begin} AND time <= {end} ALLOW FILTERING" \
"".format(
attr=attr,
table=self.table,
begin=date_begin,
end=date_end
)
stations = {} # station: {'nb': 3, 'sum': 5, 'lon': 3.27, 'lat': 12}
for row in execute_query(query):
if None in (row.station, row.lon, row.lat, getattr(row, attr)):
continue
if row.station in stations:
stations[row.station]['sum'] += getattr(row, attr)
stations[row.station]['nb'] += 1
else:
stations[row.station] = {'nb': 1, 'sum': getattr(row, attr), 'lon': row.lon, 'lat': row.lat}
for value in stations.values():
value['mean'] = value['sum'] / value['nb']
means = [elt['mean'] for elt in stations.values()]
# Initialisation mean
mini = int(min(means))
maxi = int(max(means))
old_centroids = None
new_centroids = [
(
random.randint(int(self.x_min * 10000), int(self.x_max * 10000)) / 10000, # x with 4 decimals
random.randint(int(self.y_min * 10000), int(self.y_max * 10000)) / 10000, # x with 4 decimals
)
random.randint(mini, maxi)
for _ in range(nb_cluster)
]
while old_centroids != new_centroids:
old_centroids = new_centroids
data = [
{'x': 0, 'y': 0, 'nb': 0}
{'sum': 0, 'nb': 0}
for _ in range(nb_cluster)
]
# could be parallelize
for row in execute_query(query):
for m in means:
distances = [
(x - row.lon) ** 2 + (y - row.lat) ** 2
for x, y in old_centroids
(m - centroid) ** 2
for centroid in old_centroids
]
i = distances.index(min(distances))
data[i]['x'] += row.lon
data[i]['y'] += row.lat
data[i]['sum'] += m
data[i]['nb'] += 1
# end calc parallelize
if 0 in [value['nb'] for value in data]:
# cluster empty do it again
new_centroids = [
(
random.randint(int(self.x_min * 10000), int(self.y_min * 10000)) / 10000, # x with 4 decimals
random.randint(int(self.y_min * 10000), int(self.y_max * 10000)) / 10000, # x with 4 decimals
)
random.randint(int(min(means), int(max(means))))
for _ in range(nb_cluster)
]
else:
new_centroids = [
(
float("{0:.4f}".format(elt['x'] / elt['nb'])),
float("{0:.4f}".format(elt['y'] / elt['nb'])),
)
float("{0:.2f}".format(elt['sum'] / elt['nb']))
for elt in data
]
......@@ -332,51 +326,27 @@ class Manager:
the_map.drawmeridians(np.arange(0, 360, 30))
the_map.drawparallels(np.arange(-90, 90, 30))
colors = [generate_color() for _ in range(nb_cluster)]
# Add centroids
for i, (lon, lat) in enumerate(old_centroids):
x, y = the_map(lon, lat)
# plt.plot(x, y, 'go')
plt.annotate("Cluster {}".format(i), (x, y), color=colors[i])
the_map.plot(x, y, marker='D', color=colors[i])
colors = [generate_color(i) for i in range(nb_cluster)]
# Add all points
query = "SELECT station, lon, lat FROM {} WHERE time >= {} AND time <= {} ALLOW FILTERING" \
"".format(self.table, date_begin, date_end)
stations = set()
zones = [[] for _ in range(nb_cluster)]
for row in execute_query(query):
if row.station in stations:
continue
else:
stations.add(row.station)
for station, value in stations.items():
# Analyse the point
distances = [
(row.lon - x_centroid) ** 2 + (row.lat - y_centroid) ** 2
for x_centroid, y_centroid in old_centroids
(value['mean'] - centroid) ** 2
for centroid in old_centroids
]
i = distances.index(min(distances))
zones[i].append((row.lon, row.lat))
# Add the point
x, y = the_map(row.lon, row.lat)
x, y = the_map(value['lon'], value['lat'])
the_map.plot(x, y, marker=".", color=colors[i])
plt.annotate(row.station, (x, y), color=colors[i])
# add zones
for i, zone in enumerate(zones):
# Remote point inside box
lats, lons = clear_zone(zone)
x, y = the_map(lons, lats)
the_map.plot(x, y, marker=None, color=colors[i])
plt.annotate("{} : {:.2f}".format(station, value['mean']), (x, y), color=colors[i])
title = "{} clusters du {} au {}".format(
nb_cluster,
datetime(*list(date_begin)).strftime('%Y-%m-%d %H:%M'),
datetime(*list(date_end)).strftime('%Y-%m-%d %H:%M')
title = "{nb_cluster} clusters de {attr} du {begin} au {end}".format(
nb_cluster=nb_cluster,
attr=attr,
begin=datetime(*list(date_begin)).strftime('%Y-%m-%d %H:%M'),
end=datetime(*list(date_end)).strftime('%Y-%m-%d %H:%M')
)
plt.title(title)
for elt in ' :-':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment