Repository navigation
Closed
Description
Describe the bug
Meshes reduced in size using decimate_surface are sometime invalid and crash the BEM computation generating a RuntimeError exception with message like Surface inner skull has topological defects: 3 / 5119 vertices have fewer than three neighboring triangles [1381, 1382, 1383].
Steps to reproduce
The following code:
from mne.surface import decimate_surface, read_surface
path = "/usr/local/freesurfer/subjects/ANTS7-5Months3T/bem/inner_skull_large.surf"
from mne.surface import _triangle_neighbors
# Code extracted and slighly modified from mne.surface.complete_surface_info
# for compactness of the example
def check_topological_defect(faces, vertices):
# Find neighboring triangles, accumulate vertex normals, normalize
neighbor_tri = _triangle_neighbors(faces, vertices)
# Check for topological defects
zero, fewer = list(), list()
for ni, n in enumerate(neighbor_tri):
if len(n) < 3:
if len(n) == 0:
zero.append(ni)
else:
fewer.append(ni)
if len(zero) > 0:
print(' Vertices do not have any neighboring '
'triangles: [%s]' % ', '.join(str(z) for z in zero))
if len(fewer) > 0:
print(' Vertices have fewer than three neighboring '
'triangles, removing neighbors: [%s]'
% ', '.join(str(f) for f in fewer))
vertices, faces = read_surface(path)
print("Checking the full mesh:")
check_topological_defect(faces, len(vertices))
vertices, faces = decimate_surface(vertices, faces, n_triangles=5120)
print("Checking the decimated mesh:")
check_topological_defect(faces, len(vertices))
Produces the following output:
Checking the full mesh:
Checking the decimated mesh:
Vertices have fewer than three neighboring triangles, removing neighbors: [1381, 1382, 1383]
I can send inner_skull_large.surf by email for you to reproduce this issue.
Expected results
No vertices with fewer than three neighbouring triangles should be introduced by decimate_surface.
Additional information
Platform: Linux-5.3.0-26-generic-x86_64-with-debian-buster-sid
Python: 3.7.4 (default, Aug 13 2019, 20:35:49) [GCC 7.3.0]
Executable: /home/christian/anaconda3/bin/python
CPU: x86_64: 12 cores
Memory: 31.3 GB
mne: 0.20.dev0
numpy: 1.17.4 {blas=mkl_rt, lapack=mkl_rt}
scipy: 1.4.1
matplotlib: 3.1.2 {backend=Qt5Agg}
sklearn: 0.22.1
numba: Not found
nibabel: 3.0.0
cupy: Not found
pandas: 0.25.3
dipy: Not found
mayavi: 4.6.2 {qt_api=pyqt5, PyQt5=5.14.1}
pyvista: Not found
vtk: 8.1.2
Metadata
Metadata
Assignees
Labels
No labels
Activity
christian-oreilly commentedon Jan 27, 2020
Hmm... a similar example can be produce with _assert_complete_surface which raise an exception on some decimated surfaces but not on the original surfaces. I can document this also if you'd like.
larsoner commentedon Jan 28, 2020
I wrote some of this on GitHub, but here are some suggestions (eventually we should add these to https://mne.tools/dev/overview/faq.html#my-watershed-bem-meshes-look-incorrect):
mri_tessellate,mris_extract_main_component,mri_smooth,mris_inflate,mris_sphere, thenmne.surface._get_ico_surface(4)I think what we might want to do eventually is make an alternative mode for
decimate_surfacethat uses the last four of those things (three Freesurfer commands and our internalicostuff) to decimate a given surface. But let's see if it actually works for your use case first.christian-oreilly commentedon Jan 28, 2020
Thanks for this infor @larsoner I am not so familiar with these functions. I got the following working
but then
generated
Obviously, smoothwm has nothing to do with what I am doing here. I am using this functions on a head surface that has not much to do with wm surfaces computed on a separate pipeline (i.e., the bem surfaces I am processing have not been obtained from the watershed algorithm used by the recon_all pipeline). Also, it is not specific to a given hemisphere... In any case, mris_sphere documentation is quite small so I have not much to work with.
Then, looking at the code from mne.surface._get_ico_surface(4)
it seems to return a generic ico suface, not related to my specific surface. How do I "apply" the output of this function to my surface to obtain the downsampled version?
In any case, with the file I had the issue reported herein, using the output
mris_extract_main_componentalready fix the issue. Apparently, there was quite a few detached componentsI still have an issue from
_assert_complete_surfacecomplaining aboutSurface is not complete (sum of solid angles yielded 1.00004, should be 1.)on another files though. This is not resolved by using the output ofmris_extract_main_componentandmris_smooth.christian-oreilly commentedon Jan 28, 2020
Nevermind the question about how use
mne.surface._get_ico_surface(4), I just seen your comments on Gitterchristian-oreilly commentedon Jan 28, 2020
@larsoner Do you have a pointer of something I could read to get a conceptual explanation of what is the idea here
Surface is not complete (sum of solid angles yielded 1.00004, should be 1.)(i.e., what is this sum of solid angle and why it should be 1 for a complete surface)? It would make it easier for me to understand what could create departure from this in my mesh.Also, since the check in _assert_inside (that fails for two other of my meshes) is also based on the _get_solids output, it seems that better understanding what is going on there might be the solution for the issues I have with my three remaining problematic meshes.
larsoner commentedon Jan 29, 2020
This suggests that our current version of
decimate_surfacedoes not preserve the topological properties of the surface. In this case it seems to have probably created a hole (?) in the previously complete mesh.This suggests that the Freesurfer way of doing things (which will maintain a complete triangulation) might indeed be better, if we can get it to work.
larsoner commentedon Jan 29, 2020
Looks like you are using John Richards public Infants NITRC dataset. So I can test locally.
Looks like you:
-qparameter from the description in ENH: API for subcortical surface source spaces #6784, and in your particular case youHere is a gist. Run the Freesurfer bash commands at the top of the Python file, then actually run the Python file:
And see:
If you can confirm that this works for you, I can work on making some helper functions that would you to do the
mri_tesselatethroughmris_sphere(plus Python downsampling) steps in Python. I think it makes sense to leave themri_binarizestep to the user.christian-oreilly commentedon Jan 29, 2020
@larsoner Seems to work pretty well indeed. It is a shame that I had to loose so many time trying to accomplish the same things with approaches...! I'll check that it works fine for all subjects. My goal (and it is almost completed) is to produce population-average for infants at 3, 4.5, 6, 7.5, 9, 12, 18, and 24 months old so that they could be used for analyses in infants, much as fsaverage is used for analyses in adults when no MRI is available for source reconstruction. Pial and WM surfaces have been obtained using John Richards averages and Lilla Zollei FS pipeline for infants. It took quite a bit of debuging but it seems to work fine now, minus some last bugs...
I though it could be nice to add these to python-MNE, so that other researchers can use it. I know I missed it for my own research in any cases, and that other researchers also do. I can prepare a PR for that when I am done if you think it is a good idea to add it to MNE.
larsoner commentedon Jan 29, 2020
That would be great. I've worked with those data a bit (as well as the infant pipeline) and ended up deciding to go the volumetric source space rather than surface source space route. We should chat at some point to figure this stuff out.
christian-oreilly commentedon Jan 29, 2020
Sure. I would be happy to hear about your experience with this and why you chose in the end not to go for surface sources.
It looks like the freesurfer code produces valid meshes but at the price of a very approximative fit to the source data. On this figure, the BEM mask is color-coded as a lookup table, the meshes I obtained with marching cube + Laplacian smoothing + decimation are in green, the surfaces obtained with freesurfer as in purple :

On a more fundamental level, I am a bit doubtful about the BEM mask to start with. As can be seen on this picture, the green surfaces that were following quite closely the BEM segmentation do not seem to follow very well the skull. I am not very knowledgeable about MRI but this seems very obvious even for my untrained eye.
This is probably an issue to discuss with John Richard...
larsoner commentedon Jan 29, 2020
Yes let's coordinate on a time to chat about this, see my responses on gitter. Too much ground to cover here...
christian-oreilly commentedon Jan 29, 2020
FYI, I corrected the meshes that had topological defects or that were not watertight using
christian-oreilly commentedon Jan 30, 2020
FYI, the last problematic mesh I had was causing problem because the BEM file was problematic to start with. There were no skull voxel at some places
I corrected these by hands and it is now working... So all my meshes works now! :)