Commit 528c2840 authored by Le Roux Erwan's avatar Le Roux Erwan
Browse files

[SCM] tight subplots for max stable display for advisors'meeting

parent f662f7b9
No related merge requests found
Showing with 56 additions and 19 deletions
+56 -19
...@@ -138,9 +138,7 @@ class AbstractStudy(object): ...@@ -138,9 +138,7 @@ class AbstractStudy(object):
massif_name = self.coordinate_id_to_massif_name[coordinate_id] massif_name = self.coordinate_id_to_massif_name[coordinate_id]
fill_kwargs = massif_name_to_fill_kwargs[massif_name] if massif_name_to_fill_kwargs is not None else {} fill_kwargs = massif_name_to_fill_kwargs[massif_name] if massif_name_to_fill_kwargs is not None else {}
ax.fill(*l, **fill_kwargs) ax.fill(*l, **fill_kwargs)
ax.scatter(self.massifs_coordinates.x_coordinates, self.massifs_coordinates.y_coordinates) ax.scatter(self.massifs_coordinates.x_coordinates, self.massifs_coordinates.y_coordinates, s=1)
ax.axis('off')
if show: if show:
plt.show() plt.show()
......
...@@ -20,11 +20,13 @@ class Crocus(AbstractStudy): ...@@ -20,11 +20,13 @@ class Crocus(AbstractStudy):
suffix = '' if self.altitude == 2400 else ' average of data observed every 6 hours' suffix = '' if self.altitude == 2400 else ' average of data observed every 6 hours'
return super().variable_name + suffix return super().variable_name + suffix
class CrocusSwe(Crocus): class CrocusSwe(Crocus):
def __init__(self, altitude=1800): def __init__(self, altitude=1800):
super().__init__(CrocusSweVariable, altitude) super().__init__(CrocusSweVariable, altitude)
class ExtendedCrocusSwe(AbstractExtendedStudy, CrocusSwe): class ExtendedCrocusSwe(AbstractExtendedStudy, CrocusSwe):
pass pass
......
...@@ -35,9 +35,9 @@ def study_iterator(study_class, only_first_one=False, both_altitude=False, verbo ...@@ -35,9 +35,9 @@ def study_iterator(study_class, only_first_one=False, both_altitude=False, verbo
def extended_visualization(): def extended_visualization():
save_to_file = True save_to_file = True
only_first_one = True only_first_one = True
for study_class in SCM_EXTENDED_STUDIES[:1]: for study_class in SCM_EXTENDED_STUDIES[:]:
for study in study_iterator(study_class, only_first_one=only_first_one): for study in study_iterator(study_class, only_first_one=only_first_one):
study_visualizer = StudyVisualizer(study, save_to_file=save_to_file, only_one_graph=True, year_for_kde_plot=1958, vertical_kde_plot=True) study_visualizer = StudyVisualizer(study, save_to_file=save_to_file, only_one_graph=True, plot_block_maxima_quantiles=True)
# study_visualizer.visualize_all_mean_and_max_graphs() # study_visualizer.visualize_all_mean_and_max_graphs()
study_visualizer.visualize_all_experimental_law() study_visualizer.visualize_all_experimental_law()
# for study_class in SCM_EXTENDED_STUDIES[:]: # for study_class in SCM_EXTENDED_STUDIES[:]:
...@@ -54,7 +54,7 @@ def normal_visualization(): ...@@ -54,7 +54,7 @@ def normal_visualization():
for study in study_iterator(study_class, only_first_one=only_first_one): for study in study_iterator(study_class, only_first_one=only_first_one):
study_visualizer = StudyVisualizer(study, save_to_file=save_to_file) study_visualizer = StudyVisualizer(study, save_to_file=save_to_file)
# study_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_linear_margin_fit(only_first_max_stable=only_first_one) study_visualizer.visualize_linear_margin_fit(only_first_max_stable=False)
def complete_analysis(only_first_one=False): def complete_analysis(only_first_one=False):
...@@ -73,6 +73,6 @@ def complete_analysis(only_first_one=False): ...@@ -73,6 +73,6 @@ def complete_analysis(only_first_one=False):
if __name__ == '__main__': if __name__ == '__main__':
# normal_visualization() normal_visualization()
extended_visualization() # extended_visualization()
# complete_analysis() # complete_analysis()
...@@ -43,14 +43,18 @@ class StudyVisualizer(object): ...@@ -43,14 +43,18 @@ class StudyVisualizer(object):
self.year_for_kde_plot = year_for_kde_plot self.year_for_kde_plot = year_for_kde_plot
self.plot_block_maxima_quantiles = plot_block_maxima_quantiles self.plot_block_maxima_quantiles = plot_block_maxima_quantiles
self.show = False if self.save_to_file else show
self.window_size_for_smoothing = 21 self.window_size_for_smoothing = 21
# PLOT ARGUMENTS
self.show = False if self.save_to_file else show
if self.only_one_graph: if self.only_one_graph:
self.figsize = (6.0, 4.0) self.figsize = (6.0, 4.0)
elif self.only_first_row: elif self.only_first_row:
self.figsize = (8.0, 6.0) self.figsize = (8.0, 6.0)
else: else:
self.figsize = (16.0, 10.0) self.figsize = (16.0, 10.0)
self.subplot_space = 0.05
self.coef_zoom_map = 0
@property @property
def observations(self): def observations(self):
...@@ -74,7 +78,7 @@ class StudyVisualizer(object): ...@@ -74,7 +78,7 @@ class StudyVisualizer(object):
nb_columns = 5 nb_columns = 5
nb_rows = 1 if self.only_first_row else math.ceil(len(self.study.safran_massif_names) / nb_columns) nb_rows = 1 if self.only_first_row else math.ceil(len(self.study.safran_massif_names) / nb_columns)
fig, axes = plt.subplots(nb_rows, nb_columns, figsize=self.figsize) fig, axes = plt.subplots(nb_rows, nb_columns, figsize=self.figsize)
fig.subplots_adjust(hspace=1.0, wspace=1.0) fig.subplots_adjust(hspace=self.subplot_space, wspace=self.subplot_space)
if self.only_first_row: if self.only_first_row:
for massif_id, massif_name in enumerate(self.study.safran_massif_names[:nb_columns]): for massif_id, massif_name in enumerate(self.study.safran_massif_names[:nb_columns]):
ax = axes[massif_id] ax = axes[massif_id]
...@@ -149,13 +153,16 @@ class StudyVisualizer(object): ...@@ -149,13 +153,16 @@ class StudyVisualizer(object):
xlabel = 'x = {}'.format(self.study.title) if self.only_one_graph else 'x' xlabel = 'x = {}'.format(self.study.title) if self.only_one_graph else 'x'
label_function = ax.set_ylabel if self.vertical_kde_plot else ax.set_xlabel label_function = ax.set_ylabel if self.vertical_kde_plot else ax.set_xlabel
label_function(xlabel) label_function(xlabel)
sorted_x_levels = sorted(list([x_level for x_level, _ in name_to_xlevel_and_color.values()]))
# Take all the ticks # Take all the ticks
# sorted_x_levels = sorted(list([x_level for x_level, _ in name_to_xlevel_and_color.values()]))
# extraticks = [float(float_to_str_with_only_some_significant_digits(x, nb_digits=2)) # extraticks = [float(float_to_str_with_only_some_significant_digits(x, nb_digits=2))
# for x in sorted_x_levels] # for x in sorted_x_levels]
# Display only some specific ticks # Display only some specific ticks
extraticks = [name_to_xlevel_and_color['mean'][0], name_to_xlevel_and_color[AbstractParams.QUANTILE_100][0]] extraticks_names = ['mean', AbstractParams.QUANTILE_100]
if self.plot_block_maxima_quantiles:
extraticks_names += [name for name in name_to_xlevel_and_color.keys() if BLOCK_MAXIMA_DISPLAY_NAME in name]
extraticks = [name_to_xlevel_and_color[name][0] for name in extraticks_names]
set_ticks_function = ax.set_yticks if self.vertical_kde_plot else ax.set_xticks set_ticks_function = ax.set_yticks if self.vertical_kde_plot else ax.set_xticks
# Round up the ticks with a given number of significative digits # Round up the ticks with a given number of significative digits
...@@ -192,13 +199,15 @@ class StudyVisualizer(object): ...@@ -192,13 +199,15 @@ class StudyVisualizer(object):
ax.set_title(self.study.safran_massif_names[massif_id]) ax.set_title(self.study.safran_massif_names[massif_id])
def visualize_linear_margin_fit(self, only_first_max_stable=False): def visualize_linear_margin_fit(self, only_first_max_stable=False):
self.plot_name = 'Full Likelihood with Linear marginals and max stable dependency structure'
default_covariance_function = CovarianceFunction.cauchy default_covariance_function = CovarianceFunction.cauchy
plot_name = 'Full Likelihood with Linear marginals and max stable dependency structure'
plot_name += '\n(with {} covariance structure when a covariance is needed)'.format(str(default_covariance_function).split('.')[-1])
self.plot_name = plot_name
max_stable_models = load_test_max_stable_models(default_covariance_function=default_covariance_function) max_stable_models = load_test_max_stable_models(default_covariance_function=default_covariance_function)
if only_first_max_stable: if only_first_max_stable:
max_stable_models = max_stable_models[:1] max_stable_models = max_stable_models[:1]
fig, axes = plt.subplots(len(max_stable_models) + 1, len(GevParams.SUMMARY_NAMES), figsize=self.figsize) fig, axes = plt.subplots(len(max_stable_models) + 1, len(GevParams.SUMMARY_NAMES), figsize=self.figsize)
fig.subplots_adjust(hspace=1.0, wspace=1.0) fig.subplots_adjust(hspace=self.subplot_space, wspace=self.subplot_space)
margin_class = LinearAllParametersAllDimsMarginModel margin_class = LinearAllParametersAllDimsMarginModel
# Plot the smooth margin only # Plot the smooth margin only
margin_model = margin_class(coordinates=self.coordinates) margin_model = margin_class(coordinates=self.coordinates)
...@@ -209,16 +218,44 @@ class StudyVisualizer(object): ...@@ -209,16 +218,44 @@ class StudyVisualizer(object):
margin_model = margin_class(coordinates=self.coordinates) margin_model = margin_class(coordinates=self.coordinates)
estimator = FullEstimatorInASingleStepWithSmoothMargin(self.dataset, margin_model, max_stable_model) estimator = FullEstimatorInASingleStepWithSmoothMargin(self.dataset, margin_model, max_stable_model)
title = get_display_name_from_object_type(type(max_stable_model)) title = get_display_name_from_object_type(type(max_stable_model))
if isinstance(max_stable_model, AbstractMaxStableModelWithCovarianceFunction): # if isinstance(max_stable_model, AbstractMaxStableModelWithCovarianceFunction):
title += ' ' + str(default_covariance_function).split('.')[-1] # title += ' ' + str(default_covariance_function).split('.')[-1]
self.fit_and_visualize_estimator(estimator, axes[i], title=title) self.fit_and_visualize_estimator(estimator, axes[i], title=title)
# Add the label
self.show_or_save_to_file() self.show_or_save_to_file()
def fit_and_visualize_estimator(self, estimator, axes=None, title=None): def fit_and_visualize_estimator(self, estimator, axes=None, title=None):
estimator.fit() estimator.fit()
axes = estimator.margin_function_fitted.visualize_function(show=False, axes=axes, title=title)
margin_fct = estimator.margin_function_fitted
axes = margin_fct.visualize_function(show=False, axes=axes, title='')
def get_lim_array(ax):
return np.array([np.array(ax.get_xlim()), np.array(ax.get_ylim())])
for ax in axes: for ax in axes:
old_lim = get_lim_array(ax)
self.study.visualize(ax, fill=False, show=False) self.study.visualize(ax, fill=False, show=False)
new_lim = get_lim_array(ax)
assert 0 <= self.coef_zoom_map <= 1
updated_lim = new_lim * self.coef_zoom_map + (1 - self.coef_zoom_map) * old_lim
for i, method in enumerate([ax.set_xlim, ax.set_ylim]):
method(updated_lim[i, 0], updated_lim[i, 1])
ax.tick_params(axis=u'both', which=u'both', length=0)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax.set_aspect('equal')
ax0 = axes[0]
ax0.get_yaxis().set_visible(True)
# todo: manage to remove ticks on ylabel
# finally it's good because it differntiate it from the other labels
# maybe i could put it in bold
ax0.set_ylabel(title)
ax0.tick_params(axis=u'both', which=u'both', length=0)
def show_or_save_to_file(self): def show_or_save_to_file(self):
assert self.plot_name is not None assert self.plot_name is not None
...@@ -252,7 +289,7 @@ class StudyVisualizer(object): ...@@ -252,7 +289,7 @@ class StudyVisualizer(object):
if axes is None: if axes is None:
fig, axes = plt.subplots(1, len(params_names)) fig, axes = plt.subplots(1, len(params_names))
fig.subplots_adjust(hspace=1.0, wspace=1.0) fig.subplots_adjust(hspace=self.subplot_space, wspace=self.subplot_space)
for i, gev_param_name in enumerate(params_names): for i, gev_param_name in enumerate(params_names):
ax = axes[i] ax = axes[i]
......
...@@ -30,7 +30,7 @@ def plot_extreme_param(ax, gev_param_name, values): ...@@ -30,7 +30,7 @@ def plot_extreme_param(ax, gev_param_name, values):
shifted_cmap = shiftedColorMap(cmap, midpoint=midpoint, name='shifted') shifted_cmap = shiftedColorMap(cmap, midpoint=midpoint, name='shifted')
norm = mpl.colors.Normalize(vmin=vmin, vmax=vmax) norm = mpl.colors.Normalize(vmin=vmin, vmax=vmax)
divider = make_axes_locatable(ax) divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size='5%', pad=0.05) cax = divider.append_axes('right', size='5%', pad=0.03)
cb = cbar.ColorbarBase(cax, cmap=shifted_cmap, norm=norm) cb = cbar.ColorbarBase(cax, cmap=shifted_cmap, norm=norm)
cb.set_label(gev_param_name) cb.set_label(gev_param_name)
return norm, shifted_cmap return norm, shifted_cmap
......
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