Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions shapash/plots/plot_contribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,34 +148,44 @@ def plot_interactions_scatter(
x_series = pd.Series(x_values.values.flatten())
y_series = pd.Series(y_values.values.flatten())

color_column = col_name
if color_column == x_name:
color_column = f"{col_name}__color"
while color_column in (x_name, y_name, "__x_hover__"):
color_column += "_"
Comment on lines +151 to +155

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think this is really necessary? you could simply add a fixed internal column name such as __color__ (as it is already done for __x_hover__) inside data_df and labels:

color_column = "__color__"
data_df = pd.DataFrame({
    x_name: x_series.values,
    y_name: y_series.values,
    color_column: col_values.values.flatten(),
    "__x_hover__": x_values_hover.values.flatten(),
})
labels = {"__x_hover__": x_name, color_column: col_name}


data_df = pd.DataFrame(
{
x_name: x_series.values,
y_name: y_series.values,
col_name: col_values.values.flatten(),
color_column: col_values.values.flatten(),
"__x_hover__": x_values_hover.values.flatten(),
}
)
labels = {"__x_hover__": x_name, color_column: col_name}
custom_data = [x_values_hover.values.flatten(), x_values.index.to_numpy()]

if isinstance(col_values.values.flatten()[0], str):
fig = px.scatter(
data_df,
x=x_name,
y=y_name,
color=col_name,
color=color_column,
color_discrete_sequence=style_dict["interactions_discrete_colors"],
hover_data={x_name: False, "__x_hover__": True},
labels={"__x_hover__": x_name},
labels=labels,
custom_data=custom_data,
)
else:
scatter_args = {
"data_frame": data_df,
"x": x_name,
"y": y_name,
"color": col_name,
"color": color_column,
"color_continuous_scale": col_scale,
"hover_data": {x_name: False, "__x_hover__": True},
"labels": {"__x_hover__": x_name},
"labels": labels,
"custom_data": custom_data,
}
if cmin is not None and cmax is not None:
scatter_args["range_color"] = [cmin, cmax]
Expand All @@ -184,10 +194,12 @@ def plot_interactions_scatter(
if x_values_hover.equals(x_values):
_add_density_trace(fig, x_series, y_series, style_dict, include_na_for_categories=True)

interaction_customdata = np.stack((x_values_hover.values.flatten(), x_values.index.values), axis=-1).tolist()
for trace in fig.data:
if trace.fill != "toself":
trace.customdata = interaction_customdata
trace_customdata = trace.customdata.tolist()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need a list here? if yes can you explain why? otherwise I don't think the whole loop is needed

# Plotly may keep the array when assigned an equal list.
trace.customdata = None
trace.customdata = trace_customdata

return _build_secondary_y_scatter_figure(fig, y_series)

Expand Down
53 changes: 53 additions & 0 deletions tests/unit_tests/plots/test_plot_contribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import numpy as np
import pandas as pd
import plotly.io as pio
from sklearn.tree import DecisionTreeRegressor

from shapash import SmartExplainer
from shapash.plots.plot_contribution import (
plot_interactions_scatter,
plot_interactions_violin,
Expand Down Expand Up @@ -195,5 +197,56 @@ def test_interactions_violin_yaxis_grid_is_enabled_and_no_title_leaks(self):
self.assertIsNone(fig.layout.yaxis.title.text)


class TestSameFeatureInteraction(unittest.TestCase):
def test_violin_scatter_positions_and_rows_with_same_feature(self):
style = define_style(get_palette("default"))
for values in ([10, 30] * 10, ["low", "high"] * 10):
with self.subTest(values=values[:2]):
index = [f"row_{i}" for i in range(len(values))]
x_values = pd.DataFrame({"feature": list(map(str, values))}, index=index)
col_values = pd.DataFrame({"feature": values}, index=index)
y_values = pd.DataFrame({"interaction": np.arange(len(values))}, index=index)

fig = plot_interactions_violin(
x_name="feature",
y_name="interaction",
col_name="feature",
x_values=x_values,
y_values=y_values,
col_values=col_values,
col_scale=style["interactions_col_scale"],
style_dict=style,
)

scatter = [trace for trace in fig.data if trace.type == "scatter"]
self.assertEqual(sum(len(trace.x) for trace in scatter), len(values))
for trace in scatter:
self.assertTrue(np.all(np.asarray(trace.x, dtype=float) >= -0.6))
self.assertTrue(np.all(np.asarray(trace.x, dtype=float) <= 1.6))
for x, y, customdata in zip(trace.x, trace.y, trace.customdata):
row = int(y)
modality = sorted(set(map(str, values))).index(str(values[row]))
self.assertLessEqual(abs(float(x) - modality), 0.5)
self.assertEqual(customdata[0], str(values[row]))
self.assertEqual(customdata[1], index[row])
self.assertEqual(customdata[2], str(values[row]))
self.assertGreater(len(set(float(x) for trace in scatter for x in trace.x)), 2)

def test_interactions_plot_same_numeric_feature_shows_dispersed_points(self):
x = pd.DataFrame({"feature": [10, 30] * 30})
model = DecisionTreeRegressor(random_state=0).fit(x, x["feature"] * 3)
explainer = SmartExplainer(model=model)
explainer.compile(x=x)

fig = explainer.plot.interactions_plot("feature", "feature")

scatter = [trace for trace in fig.data if trace.type == "scatter"]
self.assertEqual(sum(len(trace.x) for trace in scatter), len(x))
self.assertTrue(all(-0.6 <= point <= 1.6 for trace in scatter for point in trace.x))
self.assertGreater(len(set(float(point) for trace in scatter for point in trace.x)), 2)
np.testing.assert_array_equal(scatter[0].marker.color, x["feature"])
np.testing.assert_allclose(scatter[0].y, np.where(x["feature"] == 10, -30, 30))


if __name__ == "__main__":
unittest.main()
Loading