[FieldTrip] [full-message] Failure to import cortical surface extracted with Freesurfer into Matlab using ft_read_headshape
Schoffelen, J.M. (Jan Mathijs)
jan.schoffelen at donders.ru.nl
Thu May 2 09:47:03 CEST 2019
Hi Darinka,
If I comment out one of the ‘fgets’ I can read the file. I suggest you check whether your reported issue is an isolated one, i.e. it only occurs in this specific file, or a more general. If the latter, I’d contact the freesurfer people to make them aware of the problem. Even better would be to provide them with a patched version of read_surf.m (based on the slightly more recent version they host on https://github.com/freesurfer/freesurfer), where code improvements can be suggested by means of a Pull Request. A not so elegant solution would be, to check whether the vnum and fnum are integers, and if not, rewind to the beginning of the file and do the reading with a single fgets.
Below screenshot shows some (old) documentation about the fileformat I found online. It seems that in your file the comment is terminated by a single “\n”, and not by two of them, as is expected currently in read_surf. It may be that Freeview etc. did not suffer, because it is more robust.
Further below, I have pasted a version of read_surf (based on the latest version of the freesurfer repo) that should work in your case.
Best wishes,
Jan-Mathijs
[cid:5F2F73D4-01CB-4D1A-9780-1332CB0D98D0 at dccn.nl]
%%%%%%%%%%%%%%%%%%%%%
% PATCHED VERSION
function [vertex_coords, faces, magic] = read_surf(fname)
%
% [vertex_coords, faces] = read_surf(fname)
% reads a the vertex coordinates and face lists from a surface file
% note that reading the faces from a quad file can take a very long
% time due to the goofy format that they are stored in. If the faces
% output variable is not specified, they will not be read so it
% should execute pretty quickly.
%
%
% read_surf.m
%
% Original Author: Bruce Fischl
% CVS Revision Info:
% $Author: fischl $
% $Date: 2014/04/30 12:59:03 $
% $Revision: 1.7 $
%
% Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH"
%
% Terms and conditions for use, reproduction, distribution and contribution
% are found in the 'FreeSurfer Software License Agreement' contained
% in the file 'LICENSE' found in the FreeSurfer distribution, and here:
%
% https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense
%
% Reporting: freesurfer at nmr.mgh.harvard.edu<mailto:freesurfer at nmr.mgh.harvard.edu>
%
%fid = fopen(fname, 'r') ;
%nvertices = fscanf(fid, '%d', 1);
%all = fscanf(fid, '%d %f %f %f %f\n', [5, nvertices]) ;
%curv = all(5, :)' ;
% open it as a big-endian file
%QUAD_FILE_MAGIC_NUMBER = (-1 & 0x00ffffff) ;
%NEW_QUAD_FILE_MAGIC_NUMBER = (-3 & 0x00ffffff) ;
TRIANGLE_FILE_MAGIC_NUMBER = 16777214 ;
QUAD_FILE_MAGIC_NUMBER = 16777215 ;
NEW_QUAD_FILE_MAGIC_NUMBER = 16777213 ;
fid = fopen(fname, 'rb', 'b') ;
if (fid < 0)
str = sprintf('could not open surface file %s.', fname) ;
error(str) ;
end
magic = fread3(fid) ;
if((magic == QUAD_FILE_MAGIC_NUMBER) | (magic == NEW_QUAD_FILE_MAGIC_NUMBER))
vnum = fread3(fid) ;
fnum = fread3(fid) ;
vertex_coords = fread(fid, vnum*3, 'int16') ./ 100 ;
if (nargout > 1)
for i=1:fnum
for n=1:4
faces(i,n) = fread3(fid) ;
end
end
end
elseif (magic == TRIANGLE_FILE_MAGIC_NUMBER)
fgets(fid) ;
fgets(fid) ;
vnum = fread(fid, 1, 'int32') ;
fnum = fread(fid, 1, 'int32') ;
% possibly the comment line was not followed by two \n’s, this patch is suggested by J.M.Schoffelen, 20190502
if ~isa(vnum, 'int32') || ~isa(fnum, 'int32’)
frewind(fid);
fread3(fid);
fgets(fid);
vnum = fread(fid, 1, 'int32') ;
fnum = fread(fid, 1, 'int32') ;
end
vertex_coords = fread(fid, vnum*3, 'float32') ;
if (nargout > 1)
faces = fread(fid, fnum*3, 'int32') ;
faces = reshape(faces, 3, fnum)' ;
end
else
fprintf('ERROR: magic number %d unknown\n',magic);
vertex_coords = [];
faces = [];
return;
end
vertex_coords = reshape(vertex_coords, 3, vnum)' ;
fclose(fid) ;
On 1 May 2019, at 16:56, Darinka Trübutschek <darinkat87 at gmail.com<mailto:darinkat87 at gmail.com>> wrote:
Dear Jan,
thanks for your reply.
Yes, I can confirm that I am calling the read_surf function included with the fieldtrip toolbox. I think the problem (perhaps specific to me?) might arise from the fact that the surface files I recently generated with freesurfer (e.g., the attached lh.pial from sample subject bert) differ slightly in their format from previous ones?
For example, when reading in a previously generated file (that works) into matlab using the read_surf function from fieldtrip, I get the following:
fid = fopen(fname, 'rb', 'b') ;
if (fid < 0)
str = sprintf('could not open curvature file %s.', fname) ;
error(str) ;
end
magic = fread3(fid) ;
magic = 16777214;
>> a = fgets(fid);
>> a
a =
'created by dante on Fri Jul 20 21:37:20 2018
'
>> b = fgets(fid);
>> b
b =
'
'
>> vnum = fread(fid, 1, 'int32') ;
fnum = fread(fid, 1, 'int32') ;
>> vnum
vnum =
147285
>> fnum
fnum =
294566
When I do the same with the surfaces I just generated (for example, for the attached lh_pial from sample subject bert), variable b no longer corresponds to a newline, but actually already contains some information.
fname =
'/home/darinka/Test_Freesurfer/subjects/bert/surf/lh.pial'
>> fid = fopen(fname, 'rb', 'b') ;
if (fid < 0)
str = sprintf('could not open curvature file %s.', fname) ;
error(str) ;
end
magic = fread3(fid) ;
>> a = fgets(fid);
>> a
a =
'created by darinka on Fri Apr 5 15:33:41 2019
'
>> b = fgets(fid);
>> b
b =
' XI ������� ����Z ��s����%��g� D�\ ¾=��ٴ���� "�ZR�B@¾����(�¿5����.{#�T��� ¾~��Z���P½���o��$¾@��h ���¾� ��
'
>>
>> vnum = fread(fid, 1, 'int32') ;
fnum = fread(fid, 1, 'int32') ;
>> vnum
vnum =
-1.0516e+09
>> fnum
fnum =
-1.0276e+09
I am not sure as to why exactly this discrepancy seems to exist (especially given that the visualization in freeview works fine for either one) and whether it is specific to my setup or something more general, but I was hoping that someone with more expertise in these specific functions might be able to point me in the right direction?
Again, many thanks for your help and all the best,
Darinka
On Wed, May 1, 2019 at 3:28 PM Schoffelen, J.M. (Jan Mathijs) <jan.schoffelen at donders.ru.nl<mailto:jan.schoffelen at donders.ru.nl>> wrote:
Hi Darinka,
I cannot confirm your problem, although admittedly I have used different software versions (matlab/fieldtrip/freesurfer) etc.
The problem seems to occur in read_surf, which is not maintained by us, but comes from the freesurfer folks. Can you confirm that the version of read_surf that the call to ft_read_headshape ends up using is in external/freesurfer?
best wishes,
Jan-Mathijs
On 1 May 2019, at 14:56, Darinka Trübutschek <darinkat87 at gmail.com<mailto:darinkat87 at gmail.com>> wrote:
Dear fieldtrip community,
Sorry for double-posting - my original message somehow seems to have been cut short...
I am currently trying to import cortical surfaces extracted with freesurfer (version: freesurfer-linux-centos7_x86_64-dev-20190403-dacefd1) into Matlab (version: R2018b) using the fieldtrip function ft_read_headshape (version: 20190329). All of this is run in an Ubuntu 18.04.2 LTS environment. While all of the extracted surfaces seem fine upon visual inspection in freeview, the import into Matlab fails, throwing the following error message:
pial = ft_read_headshape('freesurfer/surf/lh.pial');
Error using fread
Invalid size.
Error in read_surf (line 72)
vertex_coords = fread(fid, vnum*3, 'float32') ;
Error in ft_read_headshape (line 909)
[pos, tri] = read_surf(filename);
It is most certainly related to the fact that, both for the number of vertices (vnum) and the number of faces (fnum), the function read_surf returns non-sensible (?) values (i.e., vnum = -1.0263e+09 and fnum = -1.0529e+09). The same code produces no error when run on the same pial surface extracted with a different version of freesurfer (freesurfer-Linux-centos6_x86_64-stable-pub-v6.0.0-2beb96c) in a different Ubuntu environment (Ubuntu 16.04 LTS).
As visual inspection in Freeview of the 2 surfaces from the two different Freesurfer versions reveals no obvious differences, I am therefore at a loss as where exactly to start to fix this problem and would be grateful for any help.
Many thanks!
Darinka
--
Darinka Trübutschek, PhD
Department of Experimental Psychology
Anna Watts Building
Radcliffe Observatory Quarter
Woodstock Road
Oxford, OX2 6GG
UK
website: https://sites.google.com/site/dtruebutschek/
_______________________________________________
fieldtrip mailing list
https://mailman.science.ru.nl/mailman/listinfo/fieldtrip
https://doi.org/10.1371/journal.pcbi.1002202
_______________________________________________
fieldtrip mailing list
https://mailman.science.ru.nl/mailman/listinfo/fieldtrip
https://doi.org/10.1371/journal.pcbi.1002202
--
Darinka Trübutschek, PhD
Department of Experimental Psychology
Anna Watts Building
Radcliffe Observatory Quarter
Woodstock Road
Oxford, OX2 6GG
UK
website: https://sites.google.com/site/dtruebutschek/
<lh.pial>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.science.ru.nl/pipermail/fieldtrip/attachments/20190502/5f47f387/attachment-0002.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screen Shot 2019-05-02 at 09.26.55.png
Type: image/png
Size: 359603 bytes
Desc: Screen Shot 2019-05-02 at 09.26.55.png
URL: <http://mailman.science.ru.nl/pipermail/fieldtrip/attachments/20190502/5f47f387/attachment-0002.png>
-------------- next part --------------
_______________________________________________
fieldtrip mailing list
https://mailman.science.ru.nl/mailman/listinfo/fieldtrip
https://doi.org/10.1371/journal.pcbi.1002202
More information about the fieldtrip
mailing list