Skip to content
Merged
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
8 changes: 8 additions & 0 deletions formulae/terms/terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from formulae.terms.variable import Variable

_log = logging.getLogger("formulae")
_NON_INTERACTABLE_TRANSFORMS = {"hsgp", "cr", "cc", "bs", "tp"}


# XTODO: Components have 'value' and terms have 'data'... which one should be kept?
Expand Down Expand Up @@ -226,6 +227,13 @@ def __init__(self, *components):
for component in components:
if component not in self.components:
self.components.append(component)
if len(self.components) > 1:
for component in self.components:
if (
isinstance(component, Call)
and component.call.callee in _NON_INTERACTABLE_TRANSFORMS
):
raise TypeError(f"Interaction with '{component.call.callee}' is not supported.")
self.data = None
self.kind = None
self.name = ":".join([str(component.name) for component in self.components])
Expand Down
34 changes: 0 additions & 34 deletions tests/test_design_matrices.py
Original file line number Diff line number Diff line change
Expand Up @@ -1179,40 +1179,6 @@ def test_categorical_ordered_series():
assert levels == list("bcd")


def test_bs_categorical_interaction():
data = pd.DataFrame(
{
"state": np.tile(
["lonely", "depressed", "hopeful", "stressed", "positive", "isolated"], 10
),
"time": np.repeat(np.arange(0, 5), 12),
}
)

formula = "0 + bs(time, degree=2, df=3) : state"
dm = design_matrices(formula, data)
assert dm.common.terms["bs(time, degree=2, df=3):state"].levels == [
"0, depressed",
"0, hopeful",
"0, isolated",
"0, lonely",
"0, positive",
"0, stressed",
"1, depressed",
"1, hopeful",
"1, isolated",
"1, lonely",
"1, positive",
"1, stressed",
"2, depressed",
"2, hopeful",
"2, isolated",
"2, lonely",
"2, positive",
"2, stressed",
]


def test_calls_with_lazy_values():
# https://github.com/bambinos/formulae/issues/87
value = np.arange(5)
Expand Down
7 changes: 7 additions & 0 deletions tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ def test_term_interaction():
assert desc == comp


@pytest.mark.parametrize("transform", ["hsgp", "cr", "cc", "bs", "tp"])
@pytest.mark.parametrize("operator", [":", "*"])
def test_spline_transforms_reject_interactions(transform, operator):
with pytest.raises(TypeError, match=f"Interaction with '{transform}' is not supported"):
model_description(f"y{operator}{transform}(x)")


def test_term_power_interaction():
desc = model_description("x*y")
comp = Model(
Expand Down
Loading