Commit 76936786 authored by Le Roux Erwan's avatar Le Roux Erwan
Browse files

[SCM] add title for visualization, add mean/max visualization trend

parent e9edd532
No related merge requests found
Showing with 86 additions and 8 deletions
+86 -8
...@@ -34,7 +34,7 @@ class AbstractStudy(object): ...@@ -34,7 +34,7 @@ class AbstractStudy(object):
@property @property
def df_all_snowfall_concatenated(self) -> pd.DataFrame: def df_all_snowfall_concatenated(self) -> pd.DataFrame:
df_list = [pd.DataFrame(snowfall, columns=self.safran_massif_names) for snowfall in df_list = [pd.DataFrame(time_serie, columns=self.safran_massif_names) for time_serie in
self.year_to_daily_time_serie.values()] self.year_to_daily_time_serie.values()]
df_concatenated = pd.concat(df_list) df_concatenated = pd.concat(df_list)
return df_concatenated return df_concatenated
...@@ -111,7 +111,6 @@ class AbstractStudy(object): ...@@ -111,7 +111,6 @@ class AbstractStudy(object):
""" Visualization methods """ """ Visualization methods """
def visualize(self, ax=None, massif_name_to_fill_kwargs=None, show=True, fill=True): def visualize(self, ax=None, massif_name_to_fill_kwargs=None, show=True, fill=True):
print("here")
if ax is None: if ax is None:
ax = plt.gca() ax = plt.gca()
df_massif = pd.read_csv(op.join(self.map_full_path, 'massifsalpes.csv')) df_massif = pd.read_csv(op.join(self.map_full_path, 'massifsalpes.csv'))
...@@ -135,6 +134,14 @@ class AbstractStudy(object): ...@@ -135,6 +134,14 @@ class AbstractStudy(object):
""" Some properties """ """ Some properties """
@property
def title(self):
return "{} at altitude {}m".format(self.variable_name, self.altitude)
@property
def variable_name(self):
return self.variable_class.NAME
@property @property
def relative_path(self) -> str: def relative_path(self) -> str:
return r'local/spatio_temporal_datasets' return r'local/spatio_temporal_datasets'
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
class AbstractVariable(object): class AbstractVariable(object):
NAME = ''
def __init__(self, dataset): def __init__(self, dataset):
self.dataset = dataset self.dataset = dataset
......
...@@ -16,12 +16,14 @@ class CrocusVariable(AbstractVariable): ...@@ -16,12 +16,14 @@ class CrocusVariable(AbstractVariable):
class CrocusSweVariable(CrocusVariable): class CrocusSweVariable(CrocusVariable):
NAME = 'Snow Water Equivalent'
def __init__(self, dataset): def __init__(self, dataset):
super().__init__(dataset, 'SNOWSWE') super().__init__(dataset, 'SNOWSWE')
class CrocusDepthVariable(CrocusVariable): class CrocusDepthVariable(CrocusVariable):
NAME = 'Snow Depth'
def __init__(self, dataset): def __init__(self, dataset):
super().__init__(dataset, "SNOWDEPTH") super().__init__(dataset, "SNOWDEPTH")
...@@ -9,7 +9,7 @@ from experiment.meteo_france_SCM_study.safran.safran_visualizer import StudyVisu ...@@ -9,7 +9,7 @@ from experiment.meteo_france_SCM_study.safran.safran_visualizer import StudyVisu
def load_all_studies(study_class, only_first_one=False): def load_all_studies(study_class, only_first_one=False):
all_studies = [] all_studies = []
is_safran_study = study_class == Safran is_safran_study = study_class == Safran
nb_days = [1, 5] if is_safran_study else [1] nb_days = [3, 1] if is_safran_study else [1]
for alti, nb_day in product(AbstractStudy.ALTITUDES, nb_days): for alti, nb_day in product(AbstractStudy.ALTITUDES, nb_days):
print('alti: {}, nb_day: {}'.format(alti, nb_day)) print('alti: {}, nb_day: {}'.format(alti, nb_day))
study = Safran(alti, nb_day) if is_safran_study else study_class(alti) study = Safran(alti, nb_day) if is_safran_study else study_class(alti)
...@@ -20,9 +20,10 @@ def load_all_studies(study_class, only_first_one=False): ...@@ -20,9 +20,10 @@ def load_all_studies(study_class, only_first_one=False):
if __name__ == '__main__': if __name__ == '__main__':
for study_class in [Safran, CrocusSwe, CrocusDepth][:]: for study_class in [Safran, CrocusSwe, CrocusDepth][1:2]:
for study in load_all_studies(study_class, only_first_one=True): for study in load_all_studies(study_class, only_first_one=True):
study_visualizer = StudyVisualizer(study) study_visualizer = StudyVisualizer(study)
# safran_visualizer.visualize_independent_margin_fits(threshold=[None, 20, 40, 60][0]) # study_visualizer.visualize_independent_margin_fits(threshold=[None, 20, 40, 60][0])
study_visualizer.visualize_smooth_margin_fit() # study_visualizer.visualize_smooth_margin_fit()
# safran_visualizer.visualize_full_fit() study_visualizer.visualize_all_kde_graphs()
# study_visualizer.visualize_full_fit()
...@@ -12,3 +12,8 @@ class Safran(AbstractStudy): ...@@ -12,3 +12,8 @@ class Safran(AbstractStudy):
def instantiate_variable_object(self, dataset) -> AbstractVariable: def instantiate_variable_object(self, dataset) -> AbstractVariable:
return self.variable_class(dataset, self.nb_days_of_snowfall) return self.variable_class(dataset, self.nb_days_of_snowfall)
@property
def variable_name(self):
return super().variable_name() + 'cumulated over {} days'.format(self.nb_days_of_snowfall)
...@@ -19,6 +19,8 @@ class SafranSnowfallVariable(AbstractVariable): ...@@ -19,6 +19,8 @@ class SafranSnowfallVariable(AbstractVariable):
(but here the problem might be that the x_i are not idnependent, they are highly dependent one from another) (but here the problem might be that the x_i are not idnependent, they are highly dependent one from another)
""" """
NAME = 'Snowfall'
def __init__(self, dataset, nb_consecutive_days_of_snowfall=1): def __init__(self, dataset, nb_consecutive_days_of_snowfall=1):
super().__init__(dataset) super().__init__(dataset)
self.nb_consecutive_days_of_snowfall = nb_consecutive_days_of_snowfall self.nb_consecutive_days_of_snowfall = nb_consecutive_days_of_snowfall
...@@ -46,3 +48,5 @@ class SafranSnowfallVariable(AbstractVariable): ...@@ -46,3 +48,5 @@ class SafranSnowfallVariable(AbstractVariable):
import math
import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import pandas as pd import pandas as pd
...@@ -11,7 +14,6 @@ from extreme_estimator.margin_fits.gev.gev_params import GevParams ...@@ -11,7 +14,6 @@ from extreme_estimator.margin_fits.gev.gev_params import GevParams
from extreme_estimator.margin_fits.gev.gevmle_fit import GevMleFit from extreme_estimator.margin_fits.gev.gevmle_fit import GevMleFit
from extreme_estimator.margin_fits.gpd.gpd_params import GpdParams from extreme_estimator.margin_fits.gpd.gpd_params import GpdParams
from extreme_estimator.margin_fits.gpd.gpdmle_fit import GpdMleFit from extreme_estimator.margin_fits.gpd.gpdmle_fit import GpdMleFit
from experiment.meteo_france_SCM_study.safran.safran import Safran
from extreme_estimator.margin_fits.plot.create_shifted_cmap import get_color_rbga_shifted from extreme_estimator.margin_fits.plot.create_shifted_cmap import get_color_rbga_shifted
from spatio_temporal_dataset.dataset.abstract_dataset import AbstractDataset from spatio_temporal_dataset.dataset.abstract_dataset import AbstractDataset
...@@ -21,6 +23,7 @@ class StudyVisualizer(object): ...@@ -21,6 +23,7 @@ class StudyVisualizer(object):
def __init__(self, study: AbstractStudy, show=True): def __init__(self, study: AbstractStudy, show=True):
self.study = study self.study = study
self.show = show self.show = show
self.window_size_for_smoothing = 21
@property @property
def observations(self): def observations(self):
...@@ -34,6 +37,60 @@ class StudyVisualizer(object): ...@@ -34,6 +37,60 @@ class StudyVisualizer(object):
def dataset(self): def dataset(self):
return AbstractDataset(self.observations, self.coordinates) return AbstractDataset(self.observations, self.coordinates)
def visualize_all_kde_graphs(self, show=True):
massif_names = self.study.safran_massif_names
nb_columns = 5
nb_rows = math.ceil(len(massif_names) / nb_columns)
fig, axes = plt.subplots(nb_rows, nb_columns)
fig.subplots_adjust(hspace=1.0, wspace=1.0)
for i, massif_name in enumerate(massif_names):
row_id, column_id = i // nb_columns, i % nb_columns
ax = axes[row_id, column_id]
self.visualize_kde_graph(ax, i, massif_name)
title = self.study.title
title += ' (mean computed with a sliding window of size {})'.format(self.window_size_for_smoothing)
fig.suptitle(title)
if show:
plt.show()
def visualize_kde_graph(self, ax, i, massif_name):
self.maxima_plot(ax, i)
self.mean_plot(ax, i)
ax.set_xlabel('year')
ax.set_title(massif_name)
def mean_plot(self, ax, i):
# Display the mean graph
# Counting the sum of 3-consecutive days of snowfall does not have any physical meaning,
# as we are counting twice some days
color_mean = 'g'
tuples_x_y = [(year, np.mean(data[:, i])) for year, data in self.study.year_to_daily_time_serie.items()]
x, y = list(zip(*tuples_x_y))
x, y = self.smooth(x, y)
ax.plot(x, y, color=color_mean)
ax.set_ylabel('mean', color=color_mean)
def maxima_plot(self, ax, i):
# Display the graph of the max on top
color_maxima = 'r'
tuples_x_y = [(year, annual_maxima[i]) for year, annual_maxima in self.study.year_to_annual_maxima.items()]
x, y = list(zip(*tuples_x_y))
ax2 = ax.twinx()
ax2.plot(x, y, color=color_maxima)
ax2.set_ylabel('maxima', color=color_maxima)
def smooth(self, x, y):
# Average on windows of size 2*M+1 (M elements on each side)
filter = np.ones(self.window_size_for_smoothing) / self.window_size_for_smoothing
y = np.convolve(y, filter, mode='valid')
assert self.window_size_for_smoothing % 2 == 1
nb_to_delete = int(self.window_size_for_smoothing // 2)
x = np.array(x)[nb_to_delete:-nb_to_delete]
assert len(x) == len(y)
return x, y
def fit_and_visualize_estimator(self, estimator): def fit_and_visualize_estimator(self, estimator):
estimator.fit() estimator.fit()
axes = estimator.margin_function_fitted.visualize(show=False) axes = estimator.margin_function_fitted.visualize(show=False)
......
Supports Markdown
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