Skip to content

Commit

Permalink
patch values stacked plot (pymc-labs#1181)
Browse files Browse the repository at this point in the history
* clip values

* tested soluton

* return figure
  • Loading branch information
juanitorduz authored Nov 7, 2024
1 parent 5c08f7f commit 94a8096
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
13 changes: 12 additions & 1 deletion pymc_marketing/mmm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
"""Base class for Marketing Mix Models (MMM)."""

import warnings
from collections.abc import Callable
from inspect import (
getattr_static,
Expand Down Expand Up @@ -1126,7 +1127,17 @@ def plot_grouped_contribution_breakdown_over_time(
area_params = dict(stacked=True, ax=ax)
if area_kwargs is not None:
area_params.update(area_kwargs)
all_contributions_over_time.plot.area(**area_params)
try:
all_contributions_over_time.plot.area(**area_params)
except ValueError:
warnings.warn(
"""
Each contribution value must be either all positive or all negative.
Try deselecting variables with negative contributions.
""",
stacklevel=2,
)
return fig
ax.legend(title="groups", loc="center left", bbox_to_anchor=(1, 0.5))
return fig

Expand Down
17 changes: 9 additions & 8 deletions tests/mmm/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
rng: np.random.Generator = np.random.default_rng(seed=seed)


@pytest.fixture(scope="module")
def toy_X() -> pd.DataFrame:
@pytest.fixture(scope="module", params=[0, 42], ids=["seed_0", "seed_42"])
def toy_X(request) -> pd.DataFrame:
local_rng: np.random.Generator = np.random.default_rng(seed=request.param)
date_data: pd.DatetimeIndex = pd.date_range(
start="2019-06-01", end="2021-12-31", freq="W-MON"
)
Expand All @@ -42,12 +43,12 @@ def toy_X() -> pd.DataFrame:
return pd.DataFrame(
data={
"date": date_data,
"channel_1": rng.integers(low=0, high=400, size=n),
"channel_2": rng.integers(low=0, high=50, size=n),
"control_1": rng.gamma(shape=1000, scale=500, size=n),
"control_2": rng.gamma(shape=100, scale=5, size=n),
"other_column_1": rng.integers(low=0, high=100, size=n),
"other_column_2": rng.normal(loc=0, scale=1, size=n),
"channel_1": local_rng.integers(low=0, high=400, size=n),
"channel_2": local_rng.integers(low=0, high=50, size=n),
"control_1": local_rng.gamma(shape=1000, scale=500, size=n),
"control_2": local_rng.normal(loc=0, scale=2, size=n),
"other_column_1": local_rng.integers(low=0, high=100, size=n),
"other_column_2": local_rng.normal(loc=0, scale=1, size=n),
}
)

Expand Down

0 comments on commit 94a8096

Please sign in to comment.