Skip to content

Commit

Permalink
ENH+ACK: add support for FieldTrip MEEG data structures without "dim"…
Browse files Browse the repository at this point in the history
… field. Issue reported by #Evelyn Muschter#
  • Loading branch information
nno committed Sep 12, 2017
1 parent b181a06 commit 191fea3
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
16 changes: 10 additions & 6 deletions mvpa/cosmo_map2meeg.m
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,6 @@ function write_eeglab_txt(fn, hdr)
[arr, dim_labels]=cosmo_unflatten(ds,[],'set_missing_to',NaN,...
'matrix_labels',{'pos'});

is_without_samples_dim=size(ds.samples,1);

if cosmo_isfield(ds,'a.meeg.samples_label')
samples_label={ds.a.meeg.samples_label};
else
Expand Down Expand Up @@ -485,12 +483,18 @@ function write_eeglab_txt(fn, hdr)

if cosmo_isfield(ds,'a.meeg.dim')
ft.dim=ds.a.meeg.dim;
else
% use regular grid to determine
ds_vol=cosmo_vol_grid_convert(ds,'tovol');
ft.dim=ds_vol.a.vol.dim(:)';
end

if cosmo_isfield(ds,'a.meeg.tri');
ft.tri=ds.a.meeg.tri;
end
%
% else
% % use regular grid to determine
% ds_vol=cosmo_vol_grid_convert(ds,'tovol');
% ft.dim=ds_vol.a.vol.dim(:)';
% end



function ft=build_ft(ds,unused)
Expand Down
18 changes: 15 additions & 3 deletions mvpa/cosmo_meeg_dataset.m
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,13 @@ function verify_trial_params(nsamples,trial_idx)
ds.a.meeg.samples_field=samples_field;

if is_ft_source_struct(ft)
ds=apply_ft_source_inside(ds,fdim.labels,fdim.values,...
if isfield(ft,'inside')
ds=apply_ft_source_inside(ds,fdim.labels,fdim.values,...
ft.inside);
ds.a.meeg.dim=ft.dim;
end

optional_fields={'dim','tri'};
ds.a.meeg=copy_optional_fields(ds.a.meeg,ft,optional_fields);
end

ds.a.meeg=set_samples_label_explicitly_if_necessary(ds.a.meeg,ft);
Expand All @@ -290,6 +294,14 @@ function verify_trial_params(nsamples,trial_idx)
ds=posthoc_slice_dataset_if_necessary(ds,opt);


function s=copy_optional_fields(s,source,optional_fields)
for k=1:numel(optional_fields)
key=optional_fields{k};
if isfield(source,key)
s.(key)=source.(key);
end
end

function a_meeg=set_samples_label_explicitly_if_necessary(a_meeg,ft)
% deal with grand average data
if ~cosmo_isfield(ft,'dimord')
Expand Down Expand Up @@ -625,7 +637,7 @@ function verify_trial_params(nsamples,trial_idx)


function tf=is_ft_source_struct(ft)
tf=isstruct(ft) && isfield(ft,'pos') && isfield(ft,'inside');
tf=isstruct(ft) && isfield(ft,'pos');

function tf=is_ft_sensor_struct(ft)
tf=isfield(ft,'dimord') && isfield(ft,'label');
Expand Down
48 changes: 48 additions & 0 deletions tests/test_meeg_io.m
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ function test_synthetic_meeg_dataset()
assertExceptionThrown(@()cosmo_meeg_dataset(ds,'targets',[1 2]),'');



function test_meeg_eeglab_txt_io()
ds=cosmo_synthetic_dataset('type','meeg');

Expand Down Expand Up @@ -721,6 +722,53 @@ function test_dimord_label()
end


function test_meeg_source_dataset_pos_dim_inside_fields()
% mapping back and forth should be fine whether or not the
% 'pos', 'dim' and 'inside' fields are there or not
ds_orig=cosmo_synthetic_dataset('type','source','size','huge');
ft_orig=cosmo_map2meeg(ds_orig);

for has_dim=[false,true]
for has_tri=[false,true]
for has_inside=[false,true]
ft=ft_orig;

if has_dim
ft.dim=[2 2 2];
end

n_pos=size(ft.pos,1);

if has_tri
ft.tri=ceil(rand(5,3)*max(n_pos));
end

if ~has_inside
ft=rmfield(ft,'inside');
end

func=@()cosmo_meeg_dataset(ft);

% verify fields are there
ds=func();
assertEqual(has_dim,isfield(ds.a.meeg,'dim'));
assertEqual(has_tri,isfield(ds.a.meeg,'tri'));

% map back
ft_back=cosmo_map2meeg(ds);

% cosmo_map2meeg always returns an inside field, so for
% now we remove it if it is present here
if ~has_inside
ft_back=rmfield(ft_back,'inside');
end

% ensure expected fields are preserved
assertEqual(ft,ft_back);
end
end
end


function x=randint()
x=ceil(rand()*10+5);
Expand Down

0 comments on commit 191fea3

Please sign in to comment.