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
32 changes: 23 additions & 9 deletions src/deeptime/covariance/util/_moments.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,13 @@ def _sum(X, xmask=None, xconst=None, Y=None, ymask=None, yconst=None, symmetric=
T = X.shape[0]
# Check if weights are given:
if weights is not None:
X = weights[:, None] * X
if Y is not None:
Y = weights[:, None] * Y
# compute raw sums on variable data
sx_raw = X.sum(axis=0) # this is the mean before subtracting it.
sy_raw = 0
if Y is not None:
sy_raw = Y.sum(axis=0)
# weighted column sums as vector-matrix products, without a weighted copy of the data
sx_raw = weights.dot(X)
sy_raw = weights.dot(Y) if Y is not None else 0
else:
# compute raw sums on variable data
sx_raw = X.sum(axis=0) # this is the mean before subtracting it.
sy_raw = Y.sum(axis=0) if Y is not None else 0

# expand raw sums to full data
if xmask is not None:
Expand Down Expand Up @@ -375,6 +374,10 @@ def _filter_variable_indices(mask, column_selection):
# SECOND MOMENT MATRICES / COVARIANCES
# ====================================================================================

# size of the weighted copy of a block of frames in the weighted second moment
_WEIGHTED_BLOCK_BYTES = 1 << 24


def _M2_dense(X, Y, weights=None, diag_only=False):
""" 2nd moment matrix using dense matrix computations.

Expand All @@ -385,7 +388,18 @@ def _M2_dense(X, Y, weights=None, diag_only=False):
if diag_only:
return np.einsum('i,ij,ij->j', weights, X, Y)
else:
return np.dot((weights[:, None] * X).T, Y)
# accumulate over blocks of frames, so that only one block of X is weighted at a time
dtype = np.result_type(weights, X)
blocksize = max(_WEIGHTED_BLOCK_BYTES // (dtype.itemsize * max(X.shape[1], 1)), 4 * X.shape[1])
if X.shape[0] <= blocksize:
return np.dot((weights[:, None] * X).T, Y)
M2 = np.zeros((X.shape[1], Y.shape[1]), dtype=np.result_type(dtype, Y))
buf = np.empty((blocksize, X.shape[1]), dtype=dtype)
for start in range(0, X.shape[0], blocksize):
block = slice(start, start + blocksize)
wX = np.multiply(weights[block, None], X[block], out=buf[:len(X[block])])
M2 += np.dot(wX.T, Y[block])
return M2
else:
if diag_only:
# contract column-wise without materializing the elementwise product
Expand Down
28 changes: 28 additions & 0 deletions tests/covariance/test_moments.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
__author__ = 'noe'

from deeptime.covariance import moments_XX, moments_XXXY, moments_block
from deeptime.covariance.util import _moments


class TestMoments(unittest.TestCase):
Expand Down Expand Up @@ -602,3 +603,30 @@ def test_moments_block_weighted_sparseconst(self):
self._test_moments_block_weighted(self.X_100_sparseconst, self.Y_100_sparseconst, self.cols_100,
remove_mean=True, sparse_mode='sparse', sparse_tol=self.sparse_tol,
weights=self.weights)


N_COLS = 16
FRAMES_PER_BLOCK = _moments._WEIGHTED_BLOCK_BYTES // (8 * N_COLS)


def _check_weighted_moments(n_frames):
rng = np.random.default_rng(42)
X, Y = rng.standard_normal((n_frames, N_COLS)), rng.standard_normal((n_frames, N_COLS))
weights = rng.random(n_frames)
w, sx, sy, Cxx, Cxy = moments_XXXY(X, Y, weights=weights, sparse_mode='dense')
wX = weights[:, None] * X
np.testing.assert_allclose(w, weights.sum())
np.testing.assert_allclose(sx, wX.sum(axis=0), atol=1e-8)
np.testing.assert_allclose(sy, (weights[:, None] * Y).sum(axis=0), atol=1e-8)
np.testing.assert_allclose(Cxx, wX.T @ X, atol=1e-8)
np.testing.assert_allclose(Cxy, wX.T @ Y, atol=1e-8)


def test_weighted_moments_single_block():
# largest input that is still weighted in one piece
_check_weighted_moments(FRAMES_PER_BLOCK)


def test_weighted_moments_blocked():
# two full blocks and a ragged one
_check_weighted_moments(2 * FRAMES_PER_BLOCK + 1000)
Loading