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
6 changes: 4 additions & 2 deletions formulaic/model_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,9 @@ def get_model_matrix(
running `ModelSpec.update(**attr_overrides)`.
"""
if attr_overrides:
return self.update(**attr_overrides).get_model_matrix(data, context=context)
return self.update(**attr_overrides).get_model_matrix(
data, context=context, drop_rows=drop_rows
)
return cast(
"ModelMatrix",
self.get_materializer(data, context=context).get_model_matrix(
Expand Down Expand Up @@ -738,7 +740,7 @@ def get_model_matrix(
materializer = FormulaMaterializer.for_materializer(materializer)
return materializer( # type: ignore
data, context=context, **(materializer_params or {})
).get_model_matrix(self)
).get_model_matrix(self, drop_rows=drop_rows)

return cast(
ModelMatrices,
Expand Down
29 changes: 29 additions & 0 deletions tests/test_model_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,35 @@ def test_get_slice(self, model_spec):
):
model_spec.get_slice("missing")

@pytest.mark.parametrize("output", [None, "pandas", "numpy", "sparse"])
@pytest.mark.parametrize("structured", [False, True])
@pytest.mark.parametrize("missing", [False, True])
def test_subset_get_model_matrix_drop_rows(self, output, structured, missing):
train = pandas.DataFrame({"y": [5, 7, 9], "x": [1, 2, 3], "z": [4, 5, 6]})
spec = Formula("y ~ x + z").get_model_matrix(train).model_spec
spec = spec.subset("y ~ 0 + x") if structured else spec.rhs.subset(["x"])
query = pandas.DataFrame(
{"y": [10, 20, 30], "x": [4, 5, numpy.nan if missing else 6]},
index=[10, 20, 30],
)
drop_rows = {1}
matrices = spec.get_model_matrix(
query, drop_rows=drop_rows, **({"output": output} if output else {})
)
matrix = matrices.rhs if structured else matrices
values = matrix.toarray() if output == "sparse" else numpy.asarray(matrix)
numpy.testing.assert_array_equal(values, [[4]] if missing else [[4], [6]])
assert matrix.model_spec.column_names == ("x",)
assert drop_rows == ({1, 2} if missing else {1})
if output in (None, "pandas"):
assert list(matrix.index) == ([10] if missing else [10, 30])
if structured:
lhs = matrices.lhs
values = lhs.toarray() if output == "sparse" else numpy.asarray(lhs)
numpy.testing.assert_array_equal(
values, [[10]] if missing else [[10], [30]]
)

def test_get_model_matrix(self, model_spec, data2):
m = model_spec.get_model_matrix(data2)

Expand Down