From 22edf50da1ac0d311c5283954073644720fc9435 Mon Sep 17 00:00:00 2001 From: Chadwick Boulay Date: Mon, 28 Dec 2020 23:29:50 -0500 Subject: [PATCH 1/2] Fix robust_fit for large timestamps by subtracting offset --- pyxdf/pyxdf.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pyxdf/pyxdf.py b/pyxdf/pyxdf.py index 0727f53..b423a89 100644 --- a/pyxdf/pyxdf.py +++ b/pyxdf/pyxdf.py @@ -611,11 +611,12 @@ def _clock_sync( if range_i[0] != range_i[1]: start, stop = range_i[0], range_i[1] + 1 e = np.ones((stop - start,)) - X = np.column_stack([e, clock_times[start:stop]]) - X /= winsor_threshold - y = clock_values[start:stop] / winsor_threshold + X = np.column_stack([e, np.array(clock_times[start:stop]) / winsor_threshold]) + y = np.array(clock_values[start:stop]) / winsor_threshold # noinspection PyTypeChecker - coef.append(_robust_fit(X, y)) + _coefs = _robust_fit(X, y) + _coefs[0] *= winsor_threshold + coef.append(_coefs) else: coef.append((clock_values[range_i[0]], 0)) @@ -702,6 +703,8 @@ def _robust_fit(A, y, rho=1, iters=1000): http://www.stanford.edu/~boyd/papers/distr_opt_stat_learning_admm.html """ + offset = np.min(A[:, 1]) + A[:, 1] = A[:, 1] - offset # Don't use -= operator here! Aty = np.dot(A.T, y) L = np.linalg.cholesky(np.dot(A.T, A)) U = L.T @@ -715,6 +718,7 @@ def _robust_fit(A, y, rho=1, iters=1000): tmp = np.maximum(0, (1 - (1 + 1 / rho) * np.abs(d_inv))) z = rho / (1 + rho) * d + 1 / (1 + rho) * tmp * d u = d - z + x[0] -= x[1] * offset return x From 710b8771e3095963f17d6d63063256058ae0754c Mon Sep 17 00:00:00 2001 From: Chadwick Boulay Date: Tue, 29 Dec 2020 10:51:42 -0500 Subject: [PATCH 2/2] robust_fit: copy mutable input A. --- pyxdf/pyxdf.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyxdf/pyxdf.py b/pyxdf/pyxdf.py index b423a89..50211b9 100644 --- a/pyxdf/pyxdf.py +++ b/pyxdf/pyxdf.py @@ -703,8 +703,9 @@ def _robust_fit(A, y, rho=1, iters=1000): http://www.stanford.edu/~boyd/papers/distr_opt_stat_learning_admm.html """ + A = np.copy(A) # Don't mutate input. offset = np.min(A[:, 1]) - A[:, 1] = A[:, 1] - offset # Don't use -= operator here! + A[:, 1] -= offset Aty = np.dot(A.T, y) L = np.linalg.cholesky(np.dot(A.T, A)) U = L.T