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
2 changes: 1 addition & 1 deletion .github/workflows/build-wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-22.04, ubuntu-22.04-arm, windows-2022, macos-13, macos-14]
os: [ubuntu-22.04, ubuntu-22.04-arm, windows-2022, macos-14, macos-15]

steps:
- uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ Changelog
=========


4.1.4 - 2025-12-16
------------------

**Other changes:**

- :func:`tabmat.from_df` now avoids unnecessary copies of dense arrays, but still ensures that the results are contiguous (C or F order).
- We now use `narwhals`' v2 API for data frame handling.


4.1.3 - 2025-11-12
------------------

Expand Down
2 changes: 1 addition & 1 deletion src/tabmat/categorical_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ def matvec(
is_int = np.issubdtype(other.dtype, np.signedinteger)

if is_int:
other_m = other.astype(float) # type: ignore
other_m: np.ndarray = other.astype(float)
else:
other_m = other

Expand Down
2 changes: 1 addition & 1 deletion src/tabmat/constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def from_df(
if dense_dfidx:
matrices.append(
DenseMatrix(
df[:, dense_dfidx].to_numpy().astype(dtype),
df[:, dense_dfidx].to_numpy().astype(dtype, copy=False),
column_names=np.asarray(df.columns)[dense_dfidx],
term_names=np.asarray(df.columns)[dense_dfidx],
)
Expand Down
14 changes: 14 additions & 0 deletions src/tabmat/dense_matrix.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import textwrap
import warnings
from typing import Optional, Union

import numpy as np
Expand Down Expand Up @@ -43,6 +44,19 @@ def __init__(self, input_array, column_names=None, term_names=None):
elif input_array.ndim > 2:
raise ValueError("Input array must be 1- or 2-dimensional")

# Ensure array is contiguous (C or F order) for Cython operations
# Only copy if necessary
if (
not input_array.flags["C_CONTIGUOUS"]
and not input_array.flags["F_CONTIGUOUS"]
):
warnings.warn(
"Input array is not contiguous; making a copy.",
UserWarning,
stacklevel=2,
)
input_array = np.asfortranarray(input_array)

self._array = input_array
width = self._array.shape[1]

Expand Down
2 changes: 1 addition & 1 deletion src/tabmat/formula.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _encode_numerical(self, values, metadata, encoder_state, spec, drop_rows):
if drop_rows:
values = values.drop(index=values.index[drop_rows])
if isinstance(values, pd.Series):
values = values.to_numpy().astype(self.dtype)
values = values.to_numpy().astype(self.dtype, copy=False)
if (values != 0).mean() <= self.sparse_threshold:
return _InteractableSparseVector(sps.csc_matrix(values[:, np.newaxis]))
else:
Expand Down
29 changes: 17 additions & 12 deletions tests/test_fast_sandwich.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,30 @@ def test_fast_sandwich_dense():


def test_dense_sandwich_on_non_contiguous():
"""Non-regression test for #208"""
"""Non-regression test for #208

DenseMatrix now automatically ensures arrays are contiguous,
so non-contiguous inputs are automatically copied and made contiguous.
"""
rng = np.random.default_rng(seed=123)
X = rng.standard_normal(size=(100, 20))

# Xd wraps a not-contiguous array.
Xd = DenseMatrix(X[:, :10])
Xs = SparseMatrix(csc_matrix(X[:, 10:]))
Xm = SplitMatrix([Xd, Xs])
# Column slicing creates a non-contiguous array, but DenseMatrix
# automatically makes it contiguous (copying only if necessary).
non_contiguous_array = X[:, :10]
assert not non_contiguous_array.flags["C_CONTIGUOUS"]
assert not non_contiguous_array.flags["F_CONTIGUOUS"]

# Making the sandwich product fail.
with pytest.raises(Exception, match="The matrix X is not contiguous"):
Xm.sandwich(np.ones(X.shape[0]))
Xd = DenseMatrix(non_contiguous_array)
# The internal array should now be contiguous
assert Xd.A.flags["C_CONTIGUOUS"] or Xd.A.flags["F_CONTIGUOUS"]

# Xd wraps a copy, which makes the data contiguous.
Xd = DenseMatrix(X[:, :10].copy())
Xs = SparseMatrix(csc_matrix(X[:, 10:]))
Xm = SplitMatrix([Xd, Xs])

# The sandwich product works without problem here.
Xm.sandwich(np.ones(X.shape[0]))
# The sandwich product should work without problem
result = Xm.sandwich(np.ones(X.shape[0]))
assert result is not None


def check(A, d, cols):
Expand Down
Loading