Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ft2mne #25

Merged
merged 24 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5b26c06
ENH - created writing function for epochs data, as symmetric counterp…
schoffelen Apr 19, 2023
8ff589f
ENH - symmetric writing function for the reading counterpart, returns…
schoffelen Apr 19, 2023
fb645a8
ENH - consistent handling of 3D matrices, which seems allowed as per …
schoffelen Apr 19, 2023
0d758dc
FIF - typos preventing correct reading of some stuff
schoffelen Apr 19, 2023
95a654e
ENH and FIX - allow for double precision writing, fix for the chan info
schoffelen Apr 19, 2023
9c2dd10
ENH - cosmetics
schoffelen Apr 19, 2023
2907e4d
ENH - added sfreq constant
schoffelen Apr 19, 2023
ef86cb0
FIX - constant name change SMSH into IAS
schoffelen Apr 20, 2023
975566b
Merge remote-tracking branch 'origin/master' into ft2mne to pull in the
schoffelen Dec 8, 2023
9bd0af3
ENH - activated assertion
schoffelen Dec 11, 2023
01adb12
ENH - freshly updated from mne-python's constants.py
schoffelen Dec 11, 2023
178d28d
ENH - robustification of test function
schoffelen Dec 11, 2023
42d676c
ENH - robustification becomes a bit of a moving target
schoffelen Dec 11, 2023
5da2048
ENH - further attempt at robustification
schoffelen Dec 12, 2023
7b1ee6e
ENH - robustification of unit tests
schoffelen Dec 12, 2023
5c8775d
ENH - commented out channel alias block
schoffelen Dec 14, 2023
85df21f
ENH - write whole matrix at once
schoffelen Dec 14, 2023
b3e68f4
ENH - updated test cases for double precision and complex valued data
schoffelen Dec 14, 2023
2018f31
cosmetics
schoffelen Dec 14, 2023
16b4bbc
typo doc
schoffelen Dec 14, 2023
d52bd03
doc
schoffelen Dec 14, 2023
c7849c2
ENH - cosmetics
schoffelen Dec 14, 2023
90d4301
ENH - added some tests
schoffelen Dec 14, 2023
3d56ed6
removed commented out code
schoffelen Dec 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 16 additions & 9 deletions matlab/fiff_write_complex.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,22 @@ 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

% this is quite slow
%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
%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;

24 changes: 15 additions & 9 deletions matlab/fiff_write_double_complex.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,21 @@ 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
% this is quite slow
schoffelen marked this conversation as resolved.
Show resolved Hide resolved
% 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
% 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
Loading