Loading Freesurfer 6 .mgz files crashes mrview & mrconvert

Not sure how high this is on the priority list, and it could be me (usually is :confused:). I’m trying to use Freesurfer 6 aseg to seed tractography as an experiment, but MRtrix is not happy with freesurfer’s output. This really isn’t a big deal, freesurfer’s mri_convert does a fine job converting to .nii. I have not tried this with Freesurfer 5.

I can provide any and all files, I generated them from the Human Connectome Project.

Freesurfer version

cat /Applications/freesurfer/VERSION
freesurfer-i386-apple-darwin11.4.2-stable6-20170119

MRView version

235: mrview --version
== mrview 382393bb-dirty ==
64 bit release version, built Mar  3 2017, using Eigen 3.3.3

mrconvert segfault

236: mrconvert freesurfer/mri/brain.mgz test.mif
Segmentation fault: 11

Looks like your version has been modified, given the ‘dirty’ label here. Any chance that could have interfered?

Otherwise, can you post the full output of the failing command with the -debug option, it might give us some insight as to where the error might be occurring.

Also, it might be worth trying the mgh_mgz_write_fix branch (this pull request), since I’ve also added a few changes to the how the FreeSurfer tags are loaded - might conceivably be related.

Failing that, we’d need to look at the file to see where things might be going wrong. Unless you’re willing to do:

$ ./configure -assert -debug 
$ ./build release/bin/mrconvert 
$ gdb --args release/bin/mrconvert freesurfer/mri/brain.mgz test.mif
# ...
(gdb) r
# wait for segfault
(gdb) bt

and paste the full terminal session here.

Don’t forget to ./configure && ./build afterwards to reset everything to how it was…

I’m running from a brew installed version of MRtrix, not my local built version, so perhaps that is the culprit?

Brew installed version

134: /usr/local/bin//mrconvert freesurfer/mri/brain.mgz test.mif
Segmentation fault: 11
135: /usr/local/bin//mrconvert -version
== mrconvert 382393bb-dirty ==
64 bit release version, built Mar  3 2017, using Eigen 3.3.3
Author(s): J-Donald Tournier (jdtournier@gmail.com)
Copyright (c) 2008-2016 the MRtrix3 contributors
# ...

Local build

10: git checkout master && git pull
# ...
03:45 PM{mra9161:R5095654(master)}:mrtrix3
11: ./configure -assert -debug -nogui

MRtrix build type requested: debug with asserts [command-line only]
# ...
writing configuration to file './release/config': ok

03:45 PM{mra9161:R5095654(master)}:mrtrix3
12: ./build release/bin/mrconvert
( 1/61) [CC] release/lib/thread.o
( 2/61) [CC] release/lib/header.o
# ...

Builds fine, now to test!

138: ~/Source/mrtrix3/release/bin/mrconvert freesurfer/mri/brain.mgz test.mif
mrconvert: [100%] uncompressing image "freesurfer/mri/brain.mgz"
mrconvert: [100%] copying from "freesurfer/mri/brain.mgz" to "test.mif"
03:54 PM{mra9161:R5095654}:100307

Works fine, looks like the brew version is broken!

141: brew info mrtrix3
mrtrix3/mrtrix3/mrtrix3: stable 0.3.15-522-gbc67f1a, HEAD
MRtrix3: tools to perform various types of diffusion MRI analyses.
http://mrtrix.org
/usr/local/Cellar/mrtrix3/0.3.15-500-g382393b_2 (336 files, 45.1MB) *
  Built from source on 2017-03-03 at 08:03:38
From: https://github.com/MRtrix3/homebrew-mrtrix3/blob/master/mrtrix3.rb
==> Dependencies
Build: eigen ✔, pkg-config ✔
==> Requirements
Build: git ✔
Recommended: python ✘, qt5 ✔
==> Options
--stable
	Install latest tagged stable version. Default is last commit on master branch.
--with-copy_src_from_home
	Use MRtrix3 source code from ~/mrtrix3. This settting is for developers and testing purposes!
--without-matlab
	Do not add MRtrix scripts to matlab path.
--without-multithreaded_build
	This is useful if your computer has many cores but not enough RAM to build MRtrix using multiple threads.
--without-python
	Build without python support
--without-qt5
	Build without qt5 support
--HEAD
	Install HEAD version

Rabbit hole now… if this version is broken, I’ll report!

143: brew upgrade mrtrix3
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 3 taps (homebrew/core, homebrew/science, mrtrix3/mrtrix3).

==> Upgrading 1 outdated package, with result:
mrtrix3/mrtrix3/mrtrix3 0.3.15-532-gc27886f_2
==> Upgrading mrtrix3/mrtrix3/mrtrix3
# ...

Well, funny, the brew version == mrconvert c27886fc-dirty == matches the head commit, but it segfaults:

462: /usr/local/bin/mrconvert freesurfer/mri/brain.mgz /tmp/test.mif
Segmentation fault: 11
04:09 PM{mra9161:R5095654}:100307
463: /usr/local/bin/mrconvert -version
== mrconvert c27886fc-dirty ==
64 bit release version, built Apr 11 2017, using Eigen 3.3.3

Looks like the brew build is the problem. I’m not going to worry about it. Thanks!

OK, that’s a bit weird… I don’t understand why ‘dirty’ would show up in the version string - that’s taken direct from git describe --dirty, and would only show that label if tracked content had been modified and not committed. But otherwise, I don’t think the brew formula would do anything unusual in terms of how the code is installed - it’s still going to configure and build on the target system, as you did manually. Maybe @maxpietsch might be able to figure this out…

Git reports dirty because homebrew adds a directory to the build directory .brew_home which acts as a sandbox home directory (overwrites ENV["HOME"]). Oddly, homebrew also adds .brew_home/Library/Python/2.7/lib/python/site-packages/homebrew.pth to the git repo. I am not sure if resetting to head and ignoring .brew_home/Library/Python/2.7/lib/python/site-packages/homebrew.pth is safe to do.

I have no idea why mrconvert segfaults. I’ve updated the homebrew formula:

brew update
brew uninstall mrtrix3
brew install mrtrix3 -debug -verbose --debug --assert --mrconvert --test

This tests mrconvert with our test data before installing it to /usr/local/bin. The test log is written to /usr/local/Cellar/mrtrix3/0.3.15-532-gc27886f_3/share/mrtrix3. If this is all fine then I’d continue as Donald suggested:

gdb --args /usr/local/bin/mrconvert freesurfer/mri/brain.mgz test.mif
# ...
(gdb) r
# wait for segfault
(gdb) bt

Happy to give it a spin if you provide me with a .mgz file.

@maxpietsch, here’s a link to the problem file. Let me know how you make out!

@blezek Thanks, I can’t reproduce the problem though.

Could you check if there are differences between /usr/local/Cellar/mrtrix3/0.3.15-532-gc27886f_3/share/mrtrix3/config and ~/Source/mrtrix3/config?

My config (in /usr/local/Cellar/mrtrix3/0.3.15-532-gc27886f_3/share/mrtrix3) is:

# configure output:
#
# MRtrix build type requested: debug with asserts
#
# Detecting OS: darwin
# OS X deployment target: 10.12
# Checking for C++11 compliant compiler [clang++]: 4.2.1 - tested ok
# Detecting pointer size: 64 bit
# Detecting byte order: little-endian
# Checking for variable-length array support: yes
# Checking for non-POD variable-length array support: yes
# Checking for zlib compression library: 1.2.8
# Checking for Eigen 3 library: 3.3.3
# Checking Eigen 3 memory alignment requirements: OK
# Checking shared library generation: yes
# Checking for Qt moc: moc (version 5.8.0)
# Checking for Qt qmake: qmake (version 5.8.0)
# Checking for Qt rcc: rcc (version 5.8.0)
# Checking for Qt: 5.8.0


PATH = r'/Users/mp/.pyenv/pyenv/bin:/usr/local/Homebrew/Library/Homebrew/shims/super:/usr/local/opt/pkg-config/bin:/usr/local/opt/python/bin:/usr/local/opt/qt/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/opt/qt5/bin'
obj_suffix = '.o'
exe_suffix = ''
lib_prefix = 'lib'
lib_suffix = '.dylib'
cpp = [ 'clang++', '-c', 'CFLAGS', 'SRC', '-o', 'OBJECT' ]
cpp_flags = [ '-std=c++11', '-mno-avx', '-DMRTRIX_MACOSX', '-fPIC', '-mmacosx-version-min=10.12', '-DMRTRIX_WORD64', '-isystem', '/usr/local/Cellar/eigen/3.3.3/include/eigen3', '-Wall', '-O0', '-g', '-D_GLIBCXX_DEBUG=1', '-D_GLIBCXX_DEBUG_PEDANTIC=1' ]
ld = [ 'clang++', 'OBJECTS', 'LDFLAGS', '-o', 'EXECUTABLE' ]
ld_flags = [ '-mmacosx-version-min=10.12', '-lz', '-g' ]
runpath = '-Wl,-rpath,@loader_path/'
ld_enabled = True
ld_lib = [ 'clang++', 'OBJECTS', 'LDLIB_FLAGS', '-o', 'LIB' ]
ld_lib_flags = [ '-dynamiclib', '-install_name', '@rpath/LIBNAME', '-mmacosx-version-min=10.12', '-mmacosx-version-min=10.12', '-lz', '-g' ]
eigen_cflags = [ '-isystem', '/usr/local/Cellar/eigen/3.3.3/include/eigen3' ]
moc = 'moc'
rcc = 'rcc'
qt_cflags = [ '-pipe', '-stdlib=libc++', '-isysroot', '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk', '-mmacosx-version-min=10.9', '-g', '-std=gnu++11', '-fPIC', '-DQT_OPENGL_LIB', '-DQT_SVG_LIB', '-DQT_WIDGETS_LIB', '-DQT_GUI_LIB', '-DQT_CORE_LIB', '-isystem', '/usr/local/Cellar/qt/5.8.0_2/lib/QtOpenGL.framework/Headers', '-isystem', '/usr/local/Cellar/qt/5.8.0_2/lib/QtSvg.framework/Headers', '-isystem', '/usr/local/Cellar/qt/5.8.0_2/lib/QtWidgets.framework/Headers', '-isystem', '/usr/local/Cellar/qt/5.8.0_2/lib/QtGui.framework/Headers', '-isystem', '/usr/local/Cellar/qt/5.8.0_2/lib/QtCore.framework/Headers', '-isystem', '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/OpenGL.framework/Headers', '-isystem', '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/AGL.framework/Headers', '-isystem', '/usr/local/Cellar/qt/5.8.0_2/mkspecs/macx-clang', '-F/usr/local/Cellar/qt/5.8.0_2/lib' ]
qt_ldflags = [ '-headerpad_max_install_names', '-stdlib=libc++', '-Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk', '-mmacosx-version-min=10.9', '-F/usr/local/Cellar/qt/5.8.0_2/lib', '-framework', 'QtOpenGL', '-framework', 'QtWidgets', '-framework', 'QtGui', '-framework', 'QtCore', '-framework', 'DiskArbitration', '-framework', 'IOKit', '-framework', 'QtSvg', '-framework', 'OpenGL', '-framework', 'AGL' ]
nogui = False

Just realised, there are special characters in the header comments that mrview (on Ubuntu and macOS) renders as ?. mrinfo shows them as whitespace.

Also tested mrconvert on that file on ubuntu and another macbook. Both work fine.

@maxpietsch, not sure if it’s worth your time to continue to poke at this problem… Since my local build works, and I don’t need to use mrconvert for the most part, I’m happy at this point. Here’s some debugging info:

Config differences

Essentially the same but my build has a different PATH (is to be expected).

40: diff /usr/local/Cellar/mrtrix3/0.3.15-532-gc27886f_3/share/mrtrix3/config ~/Source/mrtrix3/release/config
26c26
< PATH = r'/Users/mra9161/anaconda/envs/py27/bin:/usr/local/Homebrew/Library/Homebrew/shims/super:/usr/local/opt/pkg-config/bin:/usr/local/opt/python/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/mra9161/anaconda/bin'
---
> PATH = r'/opt/local/bin:/opt/local/sbin:/Users/mra9161/Applications/workbench/bin_macosx64:/Applications/blender/blender.app/Contents/MacOS:/usr/local/sbin:/Library/TeX/texbin:/Users/mra9161/Applications:/Users/mra9161/Applications/MRIcron:/Users/mra9161/anaconda/bin:/Applications/freesurfer/bin:/Applications/freesurfer/fsfast/bin:/Applications/freesurfer/tktools:/Users/mra9161/.macosx/fsl/bin:/Applications/freesurfer/mni/bin:/usr/local/go/bin:/Users/mra9161/Source/go/bin:/Users/mra9161/.macosx/bin:/Users/mra9161/.macosx/npm/bin:./node_modules/.bin:/Users/mra9161/.macosx/fsl/bin:/Users/mra9161/.macosx/node_modules/.bin/:/Users/mra9161/.macosx/bin:/Users/mra9161/Source/bin:/usr/local/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/TeX/texbin:/Applications/VMware OVF Tool/'

Debugger

Not sure if this helps?

46: lldb
(lldb) file /usr/local/bin/mrconvert
Current executable set to '/usr/local/bin/mrconvert' (x86_64).
(lldb) run freesurfer/mri/brain.mgz test.mif
Process 17651 launched: '/usr/local/bin/mrconvert' (x86_64)
Process 17651 stopped
* thread #1: tid = 0x40c2b, 0x00007fff84771132 libsystem_c.dylib`strlen + 18, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x00007fff84771132 libsystem_c.dylib`strlen + 18
libsystem_c.dylib`strlen:
->  0x7fff84771132 <+18>: pcmpeqb (%rdi), %xmm0
    0x7fff84771136 <+22>: pmovmskb %xmm0, %esi
    0x7fff8477113a <+26>: andq   $0xf, %rcx
    0x7fff8477113e <+30>: orq    $-0x1, %rax
(lldb) bt
* thread #1: tid = 0x40c2b, 0x00007fff84771132 libsystem_c.dylib`strlen + 18, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
  * frame #0: 0x00007fff84771132 libsystem_c.dylib`strlen + 18
    frame #1: 0x00007fff8f9eaee1 libc++.1.dylib`std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::append(char const*) + 21
    frame #2: 0x00000001000c91e3 libmrtrix-c27886fcf857128701b81eefff7c0a5513a7aa51.dylib`MR::File::GZ::getline() + 467
    frame #3: 0x00000001000fe2ab libmrtrix-c27886fcf857128701b81eefff7c0a5513a7aa51.dylib`MR::Formats::MGZ::read(MR::Header&) const + 539
    frame #4: 0x000000010017e83f libmrtrix-c27886fcf857128701b81eefff7c0a5513a7aa51.dylib`MR::Header::open(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) + 671
    frame #5: 0x0000000100003f03 mrconvert`run() + 115
    frame #6: 0x0000000100001f0d mrconvert`main + 45
    frame #7: 0x00007fff874835ad libdyld.dylib`start + 1
(lldb)

My mac

47: uname -a
Darwin R5095654 15.6.0 Darwin Kernel Version 15.6.0: Fri Feb 17 10:21:18 PST 2017; root:xnu-3248.60.11.4.1~1/RELEASE_X86_64 x86_64

I’d guess it has to do with character encoding issues but not sure. :no_mouth:

I’d be more inclined to believe it might be Anaconda getting in the way yet again… Depends what libraries it brings in, and whether subtly different versions end up being used at runtime or something…?