From aa4e276ed083036f2aec5c899932c53a67434a92 Mon Sep 17 00:00:00 2001 From: Le Roux Erwan <erwan.le-roux@irstea.fr> Date: Wed, 17 Jun 2020 14:07:40 +0200 Subject: [PATCH] [contrasting]fix limit for some assertion equal. add r2 value written on the plot --- extreme_trend/abstract_gev_trend_test.py | 4 +- .../snowfall_plot.py | 62 +++++++++++++------ .../study_visualizer_for_mean_values.py | 8 ++- projects/exceeding_snow_loads/utils.py | 2 +- 4 files changed, 53 insertions(+), 23 deletions(-) diff --git a/extreme_trend/abstract_gev_trend_test.py b/extreme_trend/abstract_gev_trend_test.py index 13121a30..c8a72be9 100644 --- a/extreme_trend/abstract_gev_trend_test.py +++ b/extreme_trend/abstract_gev_trend_test.py @@ -108,8 +108,8 @@ class AbstractGevTrendTest(object): def aic(self): aic = 2 * self.total_number_of_parameters_for_unconstrained_model + self.unconstrained_model_deviance assert np.equal(self.total_number_of_parameters_for_unconstrained_model, self.unconstrained_estimator.margin_model.nb_params) - npt.assert_almost_equal(self.unconstrained_estimator.result_from_model_fit.aic, aic, decimal=12) - npt.assert_almost_equal(self.unconstrained_estimator.aic(), aic, decimal=12) + npt.assert_almost_equal(self.unconstrained_estimator.result_from_model_fit.aic, aic, decimal=5) + npt.assert_almost_equal(self.unconstrained_estimator.aic(), aic, decimal=5) return aic @property diff --git a/projects/contrasting_trends_in_snow_loads/article2_snowfall_versus_time_and_altitude/snowfall_plot.py b/projects/contrasting_trends_in_snow_loads/article2_snowfall_versus_time_and_altitude/snowfall_plot.py index 38299ac8..e34f8755 100644 --- a/projects/contrasting_trends_in_snow_loads/article2_snowfall_versus_time_and_altitude/snowfall_plot.py +++ b/projects/contrasting_trends_in_snow_loads/article2_snowfall_versus_time_and_altitude/snowfall_plot.py @@ -36,7 +36,8 @@ def plot_snowfall_change_mean(altitude_to_visualizer: Dict[int, StudyVisualizerF visualizer.plot_abstract_fast(massif_name_to_mean_at_2000, label='Time derivative of mean annual maxima \nof {} at 2000 m ({})'.format( SCM_STUDY_CLASS_TO_ABBREVIATION[type(study)], study.variable_unit), - add_x_label=False) + add_x_label=False, + add_text=True, massif_name_to_text={k: str(v) for k,v in massif_name_to_r2_score.items()}) # Altitude for the change of dynamic massif_name_to_altitude_change_dynamic = {m: - massif_name_to_b[m] / a for m, a in massif_name_to_a.items()} # Keep only those that are in a reasonable range @@ -44,11 +45,13 @@ def plot_snowfall_change_mean(altitude_to_visualizer: Dict[int, StudyVisualizerF if 0 < d < 3000} visualizer.plot_abstract_fast(massif_name_to_altitude_change_dynamic, label='Altitude for the change of dynamic (m)', - add_x_label=False, graduation=500) + add_x_label=False, graduation=500, + add_text=True, massif_name_to_text={k: str(v) for k,v in massif_name_to_r2_score.items()}) # R2 score - visualizer.plot_abstract_fast(massif_name_to_r2_score, label='r2 time derivative of the mean', graduation=0.1, - add_x_label=False, - negative_and_positive_values=False) + # visualizer.plot_abstract_fast(massif_name_to_r2_score, label='r2 time derivative of the mean', graduation=0.1, + # add_x_label=False, + # negative_and_positive_values=False, + # ) def plot_snowfall_mean(altitude_to_visualizer: Dict[int, StudyVisualizerForMeanValues]): @@ -56,20 +59,35 @@ def plot_snowfall_mean(altitude_to_visualizer: Dict[int, StudyVisualizerForMeanV study = visualizer.study # Plot the curve for the evolution of the mean massif_name_to_a, massif_name_to_b, massif_name_to_r2_score = plot_mean(altitude_to_visualizer, derivative=False) - # Augmentation every km + # Compute values of interest + massif_name_to_mean_at_2000 = {m: a * 2000 + massif_name_to_b[m] for m, a in massif_name_to_a.items()} + massif_name_to_mean_at_1000 = {m: a * 1000 + massif_name_to_b[m] for m, a in massif_name_to_a.items()} massif_name_to_augmentation_every_km = {m: a * 1000 for m, a in massif_name_to_a.items()} - visualizer.plot_abstract_fast(massif_name_to_augmentation_every_km, - label='Augmentation of mean annual maxima of {} \nfor every km of elevation ({})'.format( - SCM_STUDY_CLASS_TO_ABBREVIATION[type(study)], study.variable_unit), - add_x_label=False, negative_and_positive_values=False) + massif_name_to_relative_augmentation_between_2000_and_3000_every_km = {m: 100 * (a * 1000) / (a * 2000 + massif_name_to_b[m]) for m, a in massif_name_to_a.items()} + massif_name_to_relative_augmentation_between_1000_and_2000_every_km = {m: 100 * (a * 1000) / (a * 1000 + massif_name_to_b[m]) for m, a in massif_name_to_a.items()} + + # Augmentation every km + ds = [massif_name_to_augmentation_every_km, massif_name_to_relative_augmentation_between_1000_and_2000_every_km, + massif_name_to_relative_augmentation_between_2000_and_3000_every_km] + prefixs = ['Augmentation', 'Relative augmentation between 1000m and 2000m', 'Relative augmentation between 2000m and 3000m'] + graduations = [1.0, 10.0, 10.0] + for d, prefix in zip(ds, prefixs): + visualizer.plot_abstract_fast(d, + graduation=1.0, + label=prefix + ' of mean annual maxima of {} \nfor every km of elevation ({})'.format( + SCM_STUDY_CLASS_TO_ABBREVIATION[type(study)], study.variable_unit), + add_x_label=False, negative_and_positive_values=False, + add_text=True, + massif_name_to_text={k: str(v) for k, v in massif_name_to_r2_score.items()} + ) # Value at 2000 m - massif_name_to_mean_at_2000 = {m: a * 2000 + massif_name_to_b[m] for m, a in massif_name_to_a.items()} visualizer.plot_abstract_fast(massif_name_to_mean_at_2000, label='Mean annual maxima of {} at 2000 m ()'.format( SCM_STUDY_CLASS_TO_ABBREVIATION[type(study)], study.variable_unit), - add_x_label=False, negative_and_positive_values=False) - # R2 score - visualizer.plot_abstract_fast(massif_name_to_r2_score, label='r2 mean', graduation=0.1, - add_x_label=False, negative_and_positive_values=False) + add_x_label=False, negative_and_positive_values=False, + add_text=True, massif_name_to_text={k: str(v) for k,v in massif_name_to_r2_score.items()}) + # # R2 score + # visualizer.plot_abstract_fast(massif_name_to_r2_score, label='r2 mean', graduation=0.1, + # add_x_label=False, negative_and_positive_values=False) def plot_mean(altitude_to_visualizer: Dict[int, StudyVisualizerForMeanValues], derivative=False): @@ -90,9 +108,17 @@ def plot_mean(altitude_to_visualizer: Dict[int, StudyVisualizerForMeanValues], d nb_years = 10 res = [(a, t.change_in_mean_for_the_last_x_years(nb_years=nb_years)) for i, (a, t) in enumerate(zip(altitudes_massif, trend_tests)) - if not t.unconstrained_model_is_stationary] - altitudes_values, values = zip(*res) - moment = 'Change in the last {} years \nfor non-stationary models'.format(nb_years) + if t.is_significant] + if len(res) > 0: + altitudes_values, values = zip(*res) + else: + altitudes_values, values = [], [] + moment = 'Change in the last {} years \nfor significative models'.format(nb_years) + # res = [(a, t.change_in_mean_for_the_last_x_years(nb_years=nb_years)) + # for i, (a, t) in enumerate(zip(altitudes_massif, trend_tests)) + # if not t.unconstrained_model_is_stationary] + # altitudes_values, values = zip(*res) + # moment = 'Change in the last {} years \nfor non-stationary models'.format(nb_years) else: moment = 'mean' values = [t.unconstrained_estimator_gev_params_last_year.mean for t in trend_tests] diff --git a/projects/contrasting_trends_in_snow_loads/article2_snowfall_versus_time_and_altitude/study_visualizer_for_mean_values.py b/projects/contrasting_trends_in_snow_loads/article2_snowfall_versus_time_and_altitude/study_visualizer_for_mean_values.py index c480742b..2c33eb43 100644 --- a/projects/contrasting_trends_in_snow_loads/article2_snowfall_versus_time_and_altitude/study_visualizer_for_mean_values.py +++ b/projects/contrasting_trends_in_snow_loads/article2_snowfall_versus_time_and_altitude/study_visualizer_for_mean_values.py @@ -38,8 +38,12 @@ class StudyVisualizerForMeanValues(StudyVisualizerForNonStationaryTrends): assert len(self.non_stationary_trend_test) == len(NON_STATIONARY_TREND_TEST_PAPER_2) def plot_abstract_fast(self, massif_name_to_value, label, graduation=10.0, cmap=plt.cm.coolwarm, add_x_label=True, - negative_and_positive_values=True, add_text=False): - massif_name_to_text = self.massif_name_to_text if add_text else None + negative_and_positive_values=True, add_text=False, massif_name_to_text=None): + if add_text: + if massif_name_to_text is None: + massif_name_to_text = self.massif_name_to_text + else: + massif_name_to_text = None super().plot_abstract(massif_name_to_value, label, label, self.fit_method, graduation, cmap, add_x_label, negative_and_positive_values, massif_name_to_text) diff --git a/projects/exceeding_snow_loads/utils.py b/projects/exceeding_snow_loads/utils.py index 477660cd..496884fb 100644 --- a/projects/exceeding_snow_loads/utils.py +++ b/projects/exceeding_snow_loads/utils.py @@ -33,7 +33,7 @@ NON_STATIONARY_TREND_TEST_PAPER_2 = [ # Gumbel models GumbelVersusGumbel, GumbelLocationTrendTest, GumbelScaleTrendTest, GumbelLocationAndScaleTrendTest, # GEV models with constant shape - GevVersusGev, GevLocationTrendTest, GevScaleTrendTest, GevLocationAndScaleTrendTest, + GevVersusGev, GevLocationTrendTest, GevScaleTrendTest, GevLocationAndScaleTrendTest, # GEV models with linear shape GevShapeTrendTest, GevLocationAndShapeTrendTest, GevScaleAndShapeTrendTest, GevLocationAndScaleAndShapeTrendTest, # Quadratic model for the Gev/Gumbel and for the location/scale -- GitLab