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

[SCM][HYPERCUBE] refactor trend types for hypercube visualization. add the...

[SCM][HYPERCUBE] refactor trend types for hypercube visualization. add the difference between real_trend_type & display_trend_type
parent f1f75ac8
No related merge requests found
Showing with 50 additions and 29 deletions
+50 -29
...@@ -16,12 +16,12 @@ class AltitudeHypercubeVisualizer(AbstractHypercubeVisualizer): ...@@ -16,12 +16,12 @@ class AltitudeHypercubeVisualizer(AbstractHypercubeVisualizer):
return self.tuple_values(idx=0) return self.tuple_values(idx=0)
@property @property
def trend_type_to_style(self): def display_trend_type_to_style(self):
return self.trend_test_class.trend_type_to_style() return self.trend_test_class.display_trend_type_to_style()
@property @property
def trend_types(self): def display_trend_types(self):
return self.trend_type_to_style.keys() return self.display_trend_type_to_style.keys()
@property @property
def nb_axes(self): def nb_axes(self):
...@@ -29,12 +29,12 @@ class AltitudeHypercubeVisualizer(AbstractHypercubeVisualizer): ...@@ -29,12 +29,12 @@ class AltitudeHypercubeVisualizer(AbstractHypercubeVisualizer):
def trend_type_to_series(self, reduction_function): def trend_type_to_series(self, reduction_function):
# Map each trend type to its serie with percentages # Map each trend type to its serie with percentages
return {trend_type: self.trend_type_reduction(reduction_function, trend_type)[0] return {display_trend_type: self.trend_type_reduction(reduction_function, display_trend_type)[0]
for trend_type in self.trend_types} for display_trend_type in self.display_trend_types}
def trend_type_reduction(self, reduction_function, trend_type): def trend_type_reduction(self, reduction_function, display_trend_type):
# Reduce df_bool df to a serie s_trend_type_percentage # Reduce df_bool df to a serie s_trend_type_percentage
df_bool = self.df_hypercube_trend_type.isin(AbstractUnivariateTest.get_trend_types(trend_type)) df_bool = self.df_hypercube_trend_type.isin(AbstractUnivariateTest.get_real_trend_types(display_trend_type))
s_trend_type_percentage = reduction_function(df_bool) s_trend_type_percentage = reduction_function(df_bool)
assert isinstance(s_trend_type_percentage, pd.Series) assert isinstance(s_trend_type_percentage, pd.Series)
assert not isinstance(s_trend_type_percentage.index, pd.MultiIndex) assert not isinstance(s_trend_type_percentage.index, pd.MultiIndex)
...@@ -75,8 +75,8 @@ class AltitudeHypercubeVisualizer(AbstractHypercubeVisualizer): ...@@ -75,8 +75,8 @@ class AltitudeHypercubeVisualizer(AbstractHypercubeVisualizer):
trend_type_to_series = self.trend_type_to_series(reduction_function) trend_type_to_series = self.trend_type_to_series(reduction_function)
for ax_idx, ax in enumerate(axes): for ax_idx, ax in enumerate(axes):
for trend_type in self.trend_types: for trend_type in self.display_trend_types:
style = self.trend_type_to_style[trend_type] style = self.display_trend_type_to_style[trend_type]
percentages_values = trend_type_to_series[trend_type][ax_idx] percentages_values = trend_type_to_series[trend_type][ax_idx]
ax.plot(xlabel_values, percentages_values, style + marker, label=trend_type) ax.plot(xlabel_values, percentages_values, style + marker, label=trend_type)
...@@ -101,7 +101,7 @@ class AltitudeHypercubeVisualizer(AbstractHypercubeVisualizer): ...@@ -101,7 +101,7 @@ class AltitudeHypercubeVisualizer(AbstractHypercubeVisualizer):
def visualize_trend_test_repartition(self, reduction_function, axes=None, subtitle=''): def visualize_trend_test_repartition(self, reduction_function, axes=None, subtitle=''):
if axes is None: if axes is None:
nb_trend_type = len(self.trend_test_class.trend_type_to_style()) nb_trend_type = len(self.display_trend_type_to_style)
fig, axes = plt.subplots(self.nb_axes, nb_trend_type, figsize=self.study_visualizer.figsize) fig, axes = plt.subplots(self.nb_axes, nb_trend_type, figsize=self.study_visualizer.figsize)
for i, axes_row in enumerate(axes): for i, axes_row in enumerate(axes):
...@@ -109,7 +109,7 @@ class AltitudeHypercubeVisualizer(AbstractHypercubeVisualizer): ...@@ -109,7 +109,7 @@ class AltitudeHypercubeVisualizer(AbstractHypercubeVisualizer):
vmax = max([s.max() for s in trend_type_to_serie.values()]) vmax = max([s.max() for s in trend_type_to_serie.values()])
vmin = min([s.min() for s in trend_type_to_serie.values()]) vmin = min([s.min() for s in trend_type_to_serie.values()])
vmax = max(vmax, 0.01) vmax = max(vmax, 0.01)
for ax, trend_type in zip(axes_row, self.trend_types): for ax, trend_type in zip(axes_row, self.display_trend_types):
s_percentages = trend_type_to_serie[trend_type] s_percentages = trend_type_to_serie[trend_type]
massif_to_value = dict(s_percentages) massif_to_value = dict(s_percentages)
cmap = self.trend_test_class.get_cmap_from_trend_type(trend_type) cmap = self.trend_test_class.get_cmap_from_trend_type(trend_type)
...@@ -183,8 +183,8 @@ class Altitude_Hypercube_Year_Visualizer(AltitudeHypercubeVisualizer): ...@@ -183,8 +183,8 @@ class Altitude_Hypercube_Year_Visualizer(AltitudeHypercubeVisualizer):
# Take the mean with respect to the level of interest # Take the mean with respect to the level of interest
return df.mean(level=level) return df.mean(level=level)
def trend_type_reduction(self, reduction_function, trend_type): def trend_type_reduction(self, reduction_function, display_trend_type):
series, df_bool = super().trend_type_reduction(reduction_function, trend_type) series, df_bool = super().trend_type_reduction(reduction_function, display_trend_type)
# Create df argmax # Create df argmax
df = df_bool.copy() df = df_bool.copy()
df = (df * df.columns)[df_bool] df = (df * df.columns)[df_bool]
......
...@@ -132,8 +132,8 @@ def fast_quantity_altitude_hypercube(): ...@@ -132,8 +132,8 @@ def fast_quantity_altitude_hypercube():
def main_run(): def main_run():
# fast_altitude_hypercube() # fast_altitude_hypercube()
# fast_altitude_year_hypercube() fast_altitude_year_hypercube()
full_altitude_year_hypercube() # full_altitude_year_hypercube()
# fast_quantity_altitude_hypercube() # fast_quantity_altitude_hypercube()
# full_quantity_altitude_hypercube() # full_quantity_altitude_hypercube()
......
...@@ -237,7 +237,7 @@ class AltitudeVisualizer(object): ...@@ -237,7 +237,7 @@ class AltitudeVisualizer(object):
s = study_visualizer.df_trend_test_count(trend_test_class, starting_year_to_weights).mean(axis=1) s = study_visualizer.df_trend_test_count(trend_test_class, starting_year_to_weights).mean(axis=1)
altitude_to_serie_with_mean_percentages[altitude] = s altitude_to_serie_with_mean_percentages[altitude] = s
# Plot weighted percentages over the years # Plot weighted percentages over the years
for trend_type, style in trend_test_class.trend_type_to_style().items(): for trend_type, style in trend_test_class.display_trend_type_to_style().items():
weighted_percentages = [v.loc[trend_type] if trend_type in v.index else 0.0 weighted_percentages = [v.loc[trend_type] if trend_type in v.index else 0.0
for v in altitude_to_serie_with_mean_percentages.values()] for v in altitude_to_serie_with_mean_percentages.values()]
......
...@@ -44,6 +44,17 @@ class AbstractGevChangePointTest(AbstractUnivariateTest): ...@@ -44,6 +44,17 @@ class AbstractGevChangePointTest(AbstractUnivariateTest):
except SafeRunException: except SafeRunException:
self.crashed = True self.crashed = True
@classmethod
def real_trend_types(cls):
return super().real_trend_types() + [cls.RRunTimeError_TREND]
@classmethod
def get_real_trend_types(cls, display_trend_type):
real_trend_types = super().get_real_trend_types(display_trend_type)
if display_trend_type is cls.NON_SIGNIFICATIVE_TREND:
real_trend_types.append(cls.RRunTimeError_TREND)
return real_trend_types
@property @property
def likelihood_ratio(self): def likelihood_ratio(self):
return 2 * (self.non_stationary_estimator.result_from_fit.deviance - return 2 * (self.non_stationary_estimator.result_from_fit.deviance -
......
...@@ -21,7 +21,9 @@ class AbstractUnivariateTest(object): ...@@ -21,7 +21,9 @@ class AbstractUnivariateTest(object):
SIGNIFICATIVE_ALL_TREND = SIGNIFICATIVE + ' ' + ALL_TREND SIGNIFICATIVE_ALL_TREND = SIGNIFICATIVE + ' ' + ALL_TREND
SIGNIFICATIVE_POSITIVE_TREND = SIGNIFICATIVE + ' ' + POSITIVE_TREND SIGNIFICATIVE_POSITIVE_TREND = SIGNIFICATIVE + ' ' + POSITIVE_TREND
SIGNIFICATIVE_NEGATIVE_TREND = SIGNIFICATIVE + ' ' + NEGATIVE_TREND SIGNIFICATIVE_NEGATIVE_TREND = SIGNIFICATIVE + ' ' + NEGATIVE_TREND
NON_SIGNIFICATIVE_TREND = 'non ' + SIGNIFICATIVE + ' trend'
# this is the most common level of significance
SIGNIFICANCE_LEVEL = 0.05 SIGNIFICANCE_LEVEL = 0.05
def __init__(self, years, maxima, starting_year): def __init__(self, years, maxima, starting_year):
...@@ -43,29 +45,37 @@ class AbstractUnivariateTest(object): ...@@ -43,29 +45,37 @@ class AbstractUnivariateTest(object):
return self.maxima[self.idx_for_starting_year:] return self.maxima[self.idx_for_starting_year:]
@classmethod @classmethod
def trend_type_to_style(cls): def real_trend_types(cls):
return [cls.POSITIVE_TREND, cls.NEGATIVE_TREND,
cls.SIGNIFICATIVE_POSITIVE_TREND, cls.SIGNIFICATIVE_NEGATIVE_TREND, cls.NO_TREND]
@classmethod
def display_trend_type_to_style(cls):
d = OrderedDict() d = OrderedDict()
# d[cls.POSITIVE_TREND] = 'g--'
# d[cls.NEGATIVE_TREND] = 'r--'
d[cls.ALL_TREND] = 'k--' d[cls.ALL_TREND] = 'k--'
d[cls.POSITIVE_TREND] = 'g--' d[cls.NON_SIGNIFICATIVE_TREND] = 'b--'
d[cls.NEGATIVE_TREND] = 'r--' # d[cls.SIGNIFICATIVE_ALL_TREND] = 'k-'
d[cls.SIGNIFICATIVE_ALL_TREND] = 'k-'
d[cls.SIGNIFICATIVE_POSITIVE_TREND] = 'g-' d[cls.SIGNIFICATIVE_POSITIVE_TREND] = 'g-'
d[cls.SIGNIFICATIVE_NEGATIVE_TREND] = 'r-' d[cls.SIGNIFICATIVE_NEGATIVE_TREND] = 'r-'
# d[cls.NO_TREND] = 'k--' # d[cls.NO_TREND] = 'k--'
return d return d
@classmethod @classmethod
def get_trend_types(cls, trend_type): def get_real_trend_types(cls, display_trend_type):
if trend_type is cls.ALL_TREND: if display_trend_type is cls.ALL_TREND:
return cls.get_trend_types(cls.POSITIVE_TREND) + cls.get_trend_types(cls.NEGATIVE_TREND) return cls.real_trend_types()
elif trend_type is cls.SIGNIFICATIVE_ALL_TREND: elif display_trend_type is cls.SIGNIFICATIVE_ALL_TREND:
return [cls.SIGNIFICATIVE_POSITIVE_TREND, cls.SIGNIFICATIVE_NEGATIVE_TREND] return [cls.SIGNIFICATIVE_POSITIVE_TREND, cls.SIGNIFICATIVE_NEGATIVE_TREND]
if trend_type is cls.POSITIVE_TREND: if display_trend_type is cls.POSITIVE_TREND:
return [cls.POSITIVE_TREND, cls.SIGNIFICATIVE_POSITIVE_TREND] return [cls.POSITIVE_TREND, cls.SIGNIFICATIVE_POSITIVE_TREND]
elif trend_type is cls.NEGATIVE_TREND: elif display_trend_type is cls.NEGATIVE_TREND:
return [cls.NEGATIVE_TREND, cls.SIGNIFICATIVE_NEGATIVE_TREND] return [cls.NEGATIVE_TREND, cls.SIGNIFICATIVE_NEGATIVE_TREND]
elif display_trend_type is cls.NON_SIGNIFICATIVE_TREND:
return [cls.POSITIVE_TREND, cls.NEGATIVE_TREND, cls.NO_TREND]
else: else:
return [trend_type] return [display_trend_type]
@classmethod @classmethod
def get_cmap_from_trend_type(cls, trend_type): def get_cmap_from_trend_type(cls, trend_type):
...@@ -94,7 +104,7 @@ class AbstractUnivariateTest(object): ...@@ -94,7 +104,7 @@ class AbstractUnivariateTest(object):
trend_type = self.POSITIVE_TREND if test_sign > 0 else self.NEGATIVE_TREND trend_type = self.POSITIVE_TREND if test_sign > 0 else self.NEGATIVE_TREND
if self.is_significant: if self.is_significant:
trend_type = self.SIGNIFICATIVE + ' ' + trend_type trend_type = self.SIGNIFICATIVE + ' ' + trend_type
assert trend_type in self.trend_type_to_style() assert trend_type in self.real_trend_types(), trend_type
return trend_type return trend_type
@property @property
......
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