Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct handling of linear model fitting failure #187

Merged
merged 1 commit into from
Apr 7, 2023
Merged
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
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