Skip to content

Commit

Permalink
Correct handling of linear model fitting failure (labsyspharm#187)
Browse files Browse the repository at this point in the history
Now we look at the determinant of the matrix of coefficients, not just
whether all the individual values are zero. Also the intercept is now
zeroed out which fixes the single tile placed far from the rest
(even though this was a sign of complete EdgeAligner failure). A
warning is emitted in the failure case to alert the user of the problem.
  • Loading branch information
jmuhlich authored Apr 7, 2023
1 parent 6eeda68 commit 0176ba5
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions ashlar/reg.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ def check_overlaps(self):
warn_data("Some neighboring tiles have zero overlap.")

def compute_threshold(self):
if self.max_error:
if self.max_error is not None:
if self.verbose:
print(" using explicit error threshold")
return
Expand Down Expand Up @@ -674,9 +674,18 @@ def fit_model(self):
cc0 = list(components[0])
self.lr = sklearn.linear_model.LinearRegression()
self.lr.fit(self.metadata.positions[cc0], self.positions[cc0])
# Fix up degenerate transform matrix (e.g. when we have only one tile).
if (self.lr.coef_ == 0).all():
# Fix up degenerate transform matrix. This happens when the spanning
# tree is completely edgeless or cc0's metadata positions fall in a
# straight line. In this case we fall back to the identity transform.
if np.linalg.det(self.lr.coef_) < 1e-3:
# FIXME We should probably exit here, not just warn. We may provide
# an option to force it anyway.
warn_data(
"Could not align enough edges, proceeding anyway with original"
" stage positions."
)
self.lr.coef_ = np.diag(np.ones(2))
self.lr.intercept_ = np.zeros(2)
# Adjust position of remaining components so their centroids match
# the predictions of the model.
for cc in components[1:]:
Expand Down

0 comments on commit 0176ba5

Please sign in to comment.