Skip to content

Commit 9b4931a

Browse files
Merge pull request #607 from guillaume-vignal/feature/fix_title_height
Dynamic Title Height Adjustment for Feature Importance Plot
2 parents d4f783c + 3764170 commit 9b4931a

16 files changed

+56
-31
lines changed

shapash/explainer/consistency.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from sklearn.manifold import MDS
1111

1212
from shapash.style.style_utils import colors_loading, define_style, select_palette
13+
from shapash.utils.utils import adjust_title_height
1314

1415

1516
class Consistency:
@@ -668,10 +669,11 @@ def _update_pairwise_consistency_fig(self, fig, top_features, xaxis_title, yaxis
668669
auto_open: bool
669670
open automatically the plot
670671
"""
672+
height = max(500, 40 * len(top_features))
671673
title = "Pairwise comparison of Consistency:"
672674
title += "<span style='font-size: 16px;'>\
673675
<br />How are differences in contributions distributed across features?</span>"
674-
dict_t = self._style_dict["dict_title_stability"] | {"text": title, "yref": "paper"}
676+
dict_t = self._style_dict["dict_title_stability"] | {"text": title, "y": adjust_title_height(height)}
675677
dict_xaxis = self._style_dict["dict_xaxis"] | {"text": xaxis_title}
676678
dict_yaxis = self._style_dict["dict_yaxis"] | {"text": yaxis_title}
677679

@@ -684,7 +686,7 @@ def _update_pairwise_consistency_fig(self, fig, top_features, xaxis_title, yaxis
684686
yaxis_title=dict_yaxis,
685687
yaxis=dict(range=[-0.7, len(top_features) - 0.3]),
686688
yaxis2=dict(range=[-0.7, len(top_features) - 0.3]),
687-
height=max(500, 40 * len(top_features)),
689+
height=height,
688690
)
689691

690692
fig.update_yaxes(automargin=True, zeroline=False)

shapash/explainer/multi_decorator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def summarize(self, s_contribs, var_dicts, xs_sorted, masks, columns_dict, featu
230230
def compute_features_import(self, contributions, norm=1):
231231
"""
232232
Compute a relative features importance, sum of absolute values
233-
\u200b\u200bof the contributions for each
233+
of the contributions for each
234234
features importance compute in base 100
235235
236236
Parameters

shapash/explainer/smart_explainer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class SmartExplainer:
153153
model: model object
154154
model used to check the different values of target estimate predict proba
155155
features_desc: dict
156-
Dictionary that references the numbers of feature values \u200b\u200bin the x_init
156+
Dictionary that references the numbers of feature values in the x_init
157157
features_imp: pandas.Series (regression) or list (classification)
158158
Features importance values
159159
local_neighbors: dict

shapash/explainer/smart_plotter.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from shapash.utils.utils import (
2727
add_line_break,
2828
add_text,
29+
adjust_title_height,
2930
compute_digit_number,
3031
compute_sorted_variables_interactions_list_indices,
3132
maximum_difference_sort_value,
@@ -1511,9 +1512,10 @@ def ordinal(n):
15111512
]
15121513
)
15131514

1515+
height = max(500, 11 * g_df.shape[0] * g_df.shape[1])
15141516
title = f"Comparing local explanations in a neighborhood - Id: <b>{index}</b>"
15151517
title += "<span style='font-size: 16px;'><br />How similar are explanations for closeby neighbours?</span>"
1516-
dict_t = self._style_dict["dict_title_stability"] | {"text": title, "yref": "paper"}
1518+
dict_t = self._style_dict["dict_title_stability"] | {"text": title, "y": adjust_title_height(height)}
15171519
dict_xaxis = self._style_dict["dict_xaxis"] | {"text": "Normalized contribution values"}
15181520
dict_yaxis = self._style_dict["dict_yaxis"] | {"text": ""}
15191521

@@ -1524,7 +1526,7 @@ def ordinal(n):
15241526
yaxis_title=dict_yaxis,
15251527
hovermode="closest",
15261528
barmode="group",
1527-
height=max(500, 11 * g_df.shape[0] * g_df.shape[1]),
1529+
height=height,
15281530
legend={"traceorder": "reversed"},
15291531
xaxis={"side": "bottom"},
15301532
)

shapash/explainer/smart_state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def summarize(self, s_contrib, var_dict, x_sorted, mask, columns_dict, features_
305305
def compute_features_import(self, contributions, norm=1):
306306
"""
307307
Compute a relative features importance, sum of absolute values
308-
\u200b\u200bof the contributions for each
308+
of the contributions for each
309309
features importance compute in base 100
310310
Parameters
311311
----------

shapash/manipulation/summarize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def summarize_el(dataframe, mask, prefix):
4747
def compute_features_import(dataframe, norm=1):
4848
"""
4949
Compute a relative features importance, sum of absolute values
50-
\u200b\u200bof the contributions for each
50+
of the contributions for each
5151
features importance compute in base 100
5252
Parameters
5353
----------

shapash/plots/plot_bar_chart.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from plotly import graph_objs as go
22
from plotly.offline import plot
33

4-
from shapash.utils.utils import add_line_break, truncate_str
4+
from shapash.utils.utils import add_line_break, adjust_title_height, truncate_str
55

66

77
def plot_bar_chart(
@@ -83,7 +83,7 @@ def plot_bar_chart(
8383
if subtitle:
8484
title += "<br><sup>" + subtitle + "</sup>"
8585
topmargin += 15
86-
dict_t = style_dict["dict_title"] | {"text": title, "yref": "paper"}
86+
dict_t = style_dict["dict_title"] | {"text": title, "y": adjust_title_height(height)}
8787
dict_xaxis = style_dict["dict_xaxis"] | {"text": "Contribution"}
8888
dict_yaxis = style_dict["dict_yaxis"] | {"text": None}
8989
dict_local_plot_colors = style_dict["dict_local_plot_colors"] | {"text": None}

shapash/plots/plot_compacity.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from plotly.offline import plot
33
from plotly.subplots import make_subplots
44

5+
from shapash.utils.utils import adjust_title_height
6+
57

68
def plot_compacity(
79
features_needed, distance_reached, style_dict, approx=0.9, nb_features=5, file_name=None, auto_open=False
@@ -104,7 +106,7 @@ def plot_compacity(
104106
title += (
105107
"<span style='font-size: 16px;'><br />How many variables are enough to produce accurate explanations?</span>"
106108
)
107-
dict_t = style_dict["dict_title_stability"] | {"text": title, "yref": "paper"}
109+
dict_t = style_dict["dict_title_stability"] | {"text": title, "y": adjust_title_height()}
108110

109111
fig.update_layout(
110112
template="none",

shapash/plots/plot_contribution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from plotly import graph_objs as go
66
from plotly.offline import plot
77

8-
from shapash.utils.utils import add_line_break, truncate_str
8+
from shapash.utils.utils import add_line_break, adjust_title_height, truncate_str
99
from shapash.webapp.utils.utils import round_to_k
1010

1111

@@ -493,7 +493,7 @@ def _update_contributions_fig(
493493
title += "<br><sup>" + subtitle + "</sup>"
494494
else:
495495
title += "<br><sup>" + addnote + "</sup>"
496-
dict_t = style_dict["dict_title"] | {"text": title, "yref": "paper"}
496+
dict_t = style_dict["dict_title"] | {"text": title, "y": adjust_title_height(height)}
497497
dict_xaxis = style_dict["dict_xaxis"] | {"text": truncate_str(feature_name, 110)}
498498
dict_yaxis = style_dict["dict_yaxis"] | {"text": "Contribution"}
499499

shapash/plots/plot_correlations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from plotly.subplots import make_subplots
77

88
from shapash.manipulation.summarize import compute_corr
9-
from shapash.utils.utils import compute_top_correlations_features, suffix_duplicates
9+
from shapash.utils.utils import adjust_title_height, compute_top_correlations_features, suffix_duplicates
1010

1111

1212
def plot_correlations(
@@ -203,7 +203,7 @@ def prepare_corr_matrix(df_subset):
203203
if len(list_features) < len(df.drop(features_to_hide, axis=1).columns):
204204
subtitle = f"Top {len(list_features)} correlations"
205205
title += f"<span style='font-size: 12px;'><br />{subtitle}</span>"
206-
dict_t = style_dict["dict_title"] | {"text": title, "yref": "paper"}
206+
dict_t = style_dict["dict_title"] | {"text": title, "y": adjust_title_height(height)}
207207

208208
fig.update_layout(
209209
coloraxis=dict(colorscale=["rgb(255, 255, 255)"] + style_dict["init_contrib_colorscale"][5:-1]),

0 commit comments

Comments
 (0)