Skip to content

Commit

Permalink
Merge pull request #25 from schoffelen/ft2mne
Browse files Browse the repository at this point in the history
ft2mne
  • Loading branch information
schoffelen authored Dec 15, 2023
2 parents 15b9bbb + 3d56ed6 commit 3da338d
Show file tree
Hide file tree
Showing 19 changed files with 1,395 additions and 713 deletions.
1,205 changes: 634 additions & 571 deletions matlab/fiff_define_constants.m

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions matlab/fiff_read_epochs.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
%
% [epochs] = fiff_read_epochs(fname,setno)
%
% Read eochs from file
% Read epochs from file
%
%
% Author : Martin Luessi, MGH Martinos Center
Expand Down Expand Up @@ -71,12 +71,16 @@
case FIFF.FIFF_MNE_BASELINE_MAX
tag = fiff_read_tag(fid,pos);
bmax = tag.data;
case FIFF.FIFFB_MNE_EPOCHS_SELECTION
case FIFF.FIFF_MNE_EPOCHS_SELECTION
tag = fiff_read_tag(fid,pos);
selection = tag.data;
case FIFF.FIFFB_MNE_EPOCHS_DROP_LOG
case FIFF.FIFF_MNE_EPOCHS_DROP_LOG
tag = fiff_read_tag(fid,pos);
drop_log = tag.data;
%FIXME consider support for the below
%FIFF.FIFF_MNE_EPOCHS_REJECT_FLAT; % rejection and flat params
%FIFF.FIFF_MNE_EPOCHS_RAW_SFREQ; % original raw sfreq

end
end

Expand Down
2 changes: 1 addition & 1 deletion matlab/fiff_read_evoked.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
is_smsh = [ is_smsh zeros(1,sets(k).naspect) ];
naspect = naspect + sets(k).naspect;
end
saspects = fiff_dir_tree_find(evoked(k), FIFF.FIFFB_SMSH_ASPECT);
saspects = fiff_dir_tree_find(evoked(k), FIFF.FIFFB_IAS_ASPECT);
nsaspects = length(saspects);
if nsaspects > 0
sets(k).naspect = sets(k).naspect + nsaspects;
Expand Down
2 changes: 1 addition & 1 deletion matlab/fiff_read_evoked_all.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
is_smsh = [ is_smsh zeros(1,sets(k).naspect) ];
naspect = naspect + sets(k).naspect;
end
saspects = fiff_dir_tree_find(evoked(k), FIFF.FIFFB_SMSH_ASPECT);
saspects = fiff_dir_tree_find(evoked(k), FIFF.FIFFB_IAS_ASPECT);
nsaspects = length(saspects);
if nsaspects > 0
sets(k).naspect = sets(k).naspect + nsaspects;
Expand Down
2 changes: 1 addition & 1 deletion matlab/fiff_setup_read_raw.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
raw = fiff_dir_tree_find(meas,FIFF.FIFFB_CONTINUOUS_DATA);
end
if isempty(raw) && allow_maxshield
raw = fiff_dir_tree_find(meas,FIFF.FIFFB_SMSH_RAW_DATA);
raw = fiff_dir_tree_find(meas,FIFF.FIFFB_IAS_RAW_DATA);
if ~isempty(raw)
disp([10 '--------' 10 ...
'WARNING: This file contains raw Internal Active Shielding data. It may be distorted.' 10 ...
Expand Down
13 changes: 4 additions & 9 deletions matlab/fiff_write_complex.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,10 @@ function fiff_write_complex(fid,kind,data)
if count ~= 1
error(me,'write failed');
end
for k = 1:nel
count = fwrite(fid,real(data(k)),'single');
if count ~= 1
error(me,'write failed');
end
count = fwrite(fid,imag(data(k)),'single');
if count ~= 1
error(me,'write failed');
end
data = reshape(single([real(data(:)) imag(data(:))]).', [], 1);
count = fwrite(fid, data, 'single');
if count ~= nel*2
error(me, 'write failed');
end
return;

67 changes: 46 additions & 21 deletions matlab/fiff_write_complex_matrix.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@ function fiff_write_complex_matrix(fid,kind,mat)
me='MNE:fiff_write_complex_matrix';

if nargin ~= 3
error(me,'Incorrect number of arguments');
error(me,'Incorrect number of arguments');
end

ndim = ndims(mat);
if ndim < 2 || ndim > 3
error(me,'Input should be a two-dimensional or three-dimensional matrix');
end

FIFFT_COMPLEX_FLOAT = 20;
FIFFT_MATRIX = bitshift(1,30);
FIFFT_MATRIX_COMPLEX = bitor(FIFFT_COMPLEX_FLOAT,FIFFT_MATRIX);
FIFFV_NEXT_SEQ=0;

datasize = 2*4*numel(mat) + 4*3;
datasize = 2*4*numel(mat) + 4*(ndim+1);

count = fwrite(fid,int32(kind),'int32');
if count ~= 1
Expand All @@ -49,27 +54,47 @@ function fiff_write_complex_matrix(fid,kind,mat)
if count ~= 1
error(me,'write failed');
end
nrow = size(mat,1);
ncol = size(mat,2);
for j = 1:nrow
for k = 1:ncol
count = fwrite(fid,real(mat(j,k)),'single');
if count ~= 1
error(me,'write failed');
end
count = fwrite(fid,imag(mat(j,k)),'single');
if count ~= 1
error(me,'write failed');
end
end
end
dims(1) = size(mat,2);
dims(2) = size(mat,1);
dims(3) = 2;
count = fwrite(fid,int32(dims),'int32');
if count ~= 3
% nrow = size(mat,1);
% ncol = size(mat,2);
% for j = 1:nrow
% for k = 1:ncol
% count = fwrite(fid,real(mat(j,k)),'single');
% if count ~= 1
% error(me,'write failed');
% end
% count = fwrite(fid,imag(mat(j,k)),'single');
% if count ~= 1
% error(me,'write failed');
% end
% end
% end

% the matrix is written last dimension first, and then each element's real part followed by its imaginary part
siz = size(mat);
mat = permute(mat,ndim:-1:1);
mat = [real(mat(:)) imag(mat(:))]';
count = fwrite(fid,single(mat(:)),'single');
if count ~= numel(mat)
error(me,'write failed');
end
if ndim==2
dims(1) = siz(2);
dims(2) = siz(1);
dims(3) = 2;
count = fwrite(fid,int32(dims),'int32');
if count ~= 3
error(me,'write failed');
end
elseif ndim==3
dims(1) = siz(3);
dims(2) = siz(2);
dims(3) = siz(1);
dims(4) = 3;
count = fwrite(fid,int32(dims),'int32');
if count ~= 4
error(me,'write failed');
end
end

return;

13 changes: 4 additions & 9 deletions matlab/fiff_write_double_complex.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,10 @@ function fiff_write_double_complex(fid,kind,data)
if count ~= 1
error(me,'write failed');
end
for k = 1:nel
count = fwrite(fid,real(data(k)),'double');
if count ~= 1
error(me,'write failed');
end
count = fwrite(fid,imag(data(k)),'double');
if count ~= 1
error(me,'write failed');
end
data = reshape([real(data(:)) imag(data(:))].', [], 1);
count = fwrite(fid, data, 'double');
if count ~= nel*2
error(me, 'write failed');
end
return;

67 changes: 46 additions & 21 deletions matlab/fiff_write_double_complex_matrix.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@ function fiff_write_double_complex_matrix(fid,kind,mat)
me='MNE:fiff_write_double_complex_matrix';

if nargin ~= 3
error(me,'Incorrect number of arguments');
error(me,'Incorrect number of arguments');
end

ndim = ndims(mat);
if ndim < 2 || ndim > 3
error(me,'Input should be a two-dimensional or three-dimensional matrix');
end

FIFFT_COMPLEX_DOUBLE = 21;
FIFFT_MATRIX = bitshift(1,30);
FIFFT_MATRIX_COMPLEX_DOUBLE = bitor(FIFFT_COMPLEX_DOUBLE,FIFFT_MATRIX);
FIFFV_NEXT_SEQ=0;

datasize = 2*8*numel(mat) + 4*3;
datasize = 2*8*numel(mat) + 4*(ndim+1);

count = fwrite(fid,int32(kind),'int32');
if count ~= 1
Expand All @@ -49,27 +54,47 @@ function fiff_write_double_complex_matrix(fid,kind,mat)
if count ~= 1
error(me,'write failed');
end
nrow = size(mat,1);
ncol = size(mat,2);
for j = 1:nrow
for k = 1:ncol
count = fwrite(fid,real(mat(j,k)),'double');
if count ~= 1
error(me,'write failed');
end
count = fwrite(fid,imag(mat(j,k)),'double');
if count ~= 1
error(me,'write failed');
end
end
end
dims(1) = size(mat,2);
dims(2) = size(mat,1);
dims(3) = 2;
count = fwrite(fid,int32(dims),'int32');
if count ~= 3
% nrow = size(mat,1);
% ncol = size(mat,2);
% for j = 1:nrow
% for k = 1:ncol
% count = fwrite(fid,real(mat(j,k)),'double');
% if count ~= 1
% error(me,'write failed');
% end
% count = fwrite(fid,imag(mat(j,k)),'double');
% if count ~= 1
% error(me,'write failed');
% end
% end
% end

% the matrix is written last dimension first, and then each element's real part followed by its imaginary part
siz = size(mat);
mat = permute(mat,ndim:-1:1);
mat = [real(mat(:)) imag(mat(:))]';
count = fwrite(fid,double(mat(:)),'double');
if count ~= numel(mat)
error(me,'write failed');
end
if ndim==2
dims(1) = siz(2);
dims(2) = siz(1);
dims(3) = 2;
count = fwrite(fid,int32(dims),'int32');
if count ~= 3
error(me,'write failed');
end
elseif ndim==3
dims(1) = siz(3);
dims(2) = siz(2);
dims(3) = siz(1);
dims(4) = 3;
count = fwrite(fid,int32(dims),'int32');
if count ~= 4
error(me,'write failed');
end
end

return;

32 changes: 22 additions & 10 deletions matlab/fiff_write_double_matrix.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ function fiff_write_double_matrix(fid,kind,mat)
error(me,'Incorrect number of arguments');
end

if length(size(mat)) ~= 2
error(me,'Input should be a two-dimensional matrix');
ndim = ndims(mat);
if ndim < 2 || ndim > 3
error(me,'Input should be a two-dimensional or three-dimensional matrix');
end

FIFFT_DOUBLE = 5;
FIFFT_MATRIX = bitshift(1,30);
FIFFT_MATRIX_DOUBLE = bitor(FIFFT_DOUBLE,FIFFT_MATRIX);
FIFFV_NEXT_SEQ=0;

datasize = 8*numel(mat) + 4*3;
datasize = 8*numel(mat) + 4*(ndim+1);

count = fwrite(fid,int32(kind),'int32');
if count ~= 1
Expand All @@ -53,16 +54,27 @@ function fiff_write_double_matrix(fid,kind,mat)
if count ~= 1
error(me,'write failed');
end
count = fwrite(fid,double(mat'),'double');
count = fwrite(fid,double(permute(mat,ndim:-1:1)),'double');
if count ~= numel(mat)
error(me,'write failed');
end
dims(1) = size(mat,2);
dims(2) = size(mat,1);
dims(3) = 2;
count = fwrite(fid,int32(dims),'int32');
if count ~= 3
error(me,'write failed');
if ndim==2
dims(1) = size(mat,2);
dims(2) = size(mat,1);
dims(3) = 2;
count = fwrite(fid,int32(dims),'int32');
if count ~= 3
error(me,'write failed');
end
elseif ndim==3
dims(1) = size(mat,3);
dims(2) = size(mat,2);
dims(3) = size(mat,1);
dims(4) = 3;
count = fwrite(fid,int32(dims),'int32');
if count ~= 4
error(me,'write failed');
end
end

return;
Expand Down
Loading

0 comments on commit 3da338d

Please sign in to comment.