mne-python
mne-python copied to clipboard
ENH Add Warning if Coreg Distances are Bad
Fixes #6552
This adds a method to check for bad coregistration. Currently, this will raise a warning if the coregistration distances are greater than 5mm.
This function is now called in compute_dig_mri_distances and in 'dig_mri_distances'
This is a whole function because it should continue to be updated as the definition of a "bad coregistration" is expanded.
This looks like it might be failing exactly where it is supposed to. Since it warns when the coregistration distance is greater than 5mm, and it appears to only be throwing exceptions when the test calls actual data. It may be this just needs an added warning filter, but I don't really want to mess with the test filters without some serious research into how they work. If it is an actual problem with the function, I may need to understand the test datasets - which would require some time to go understand properly. I may need to leave this as is until I have more time to understand that part.
I think we probably want to have compute_dig_mri_distances(dist_limit=np.inf) or something by default, and only for some calls do we want to set coreg.compute_dig_mri_distances(dist_limit=5) (where any distances > 5mm for non-omitted points would emit an error). For example, we don't want a warning emitted into the console when using the mne coreg GUI -- we'd want a visual cue in the GUI itself to warn about this.
It may be this just needs an added warning filter,
Taking one of your failing tests for example:
mne/tests/test_coreg.py:391: in test_coreg_class_gui_match
coreg.fit_fiducials(verbose=True)
<decorator-gen-387>:10: in fit_fiducials
???
mne/coreg.py:1764: in fit_fiducials
self._log_dig_mri_distance('Start')
mne/coreg.py:1728: in _log_dig_mri_distance
errs_nearest = self.compute_dig_mri_distances()
mne/coreg.py:1987: in compute_dig_mri_distances
_warn_bad_coregistration(distances)
mne/surface.py:1765: in _warn_bad_coregistration
warn('Warning: Bad coregistration. Median coregistration ' +
mne/utils/_logging.py:390: in warn
warnings.warn_explicit(
E UserWarning: Warning: Bad coregistration. Median coregistration distance is greater than 5.0 mm
With the new syntax I would do:
coreg.fit_fiducials(verbose=True) # existing code, still okay
with pytest.warns(RuntimeWarning, match='is greater than'):
coreg.fit_fiducials(verbose=True, max_dist=5)
And you do a few of these max_dist=5's in the code. Hopefully for fit_icp you can do it and it doesn't warn (which you can assert just by not wrapping the fit_icp(..., max_dist=5) it in a pytest.warns context manager, because we treat warnings as errors).