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

ideacmdtool with XA30 - different recording start & stop times for cardiac and respiratory signals #234

Open
wants to merge 2 commits into
base: physio
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 8 additions & 1 deletion PhysIO/code/readin/tapas_physio_read_columnar_textfiles.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
strColumnHeader = '';
parsePatternPerNColumns{3} = '%f %f %f';
nEmptyLinesAfterHeader(3) = 0;
case 'BIDS_SEPARATE'
strColumnHeader = '';
parsePatternPerNColumns{1} = '%f';
nEmptyLinesAfterHeader(1) = 0;
case 'BIOPAC_TXT'
strColumnHeader = '.*RESP.*';
parsePatternPerNColumns{4} = '%f %f %f %d';
Expand Down Expand Up @@ -132,6 +136,9 @@
case 'BIDS' % will be in separate json-file
columnNames = {};
nColumns = 3;
case 'BIDS_SEPARATE' % will be in separate json-file
columnNames = {};
nColumns = 1;
case 'BIOPAC_TXT' % bad column names with spaces...e.g. 'RESP - RSP100C'
columnNames = regexp(strLine, '([\t])', 'split');
nColumns = numel(columnNames);
Expand All @@ -151,7 +158,7 @@
% now read the rest of the file
fid = fopen(fileName);
switch upper(fileType)
case {'ADINSTRUMENTS_TXT', 'LABCHART_TXT', 'BIDS', 'BIOPAC_TXT', 'INFO', 'PULS', 'RESP'}
case {'ADINSTRUMENTS_TXT', 'LABCHART_TXT', 'BIDS', 'BIDS_SEPARATE', 'BIOPAC_TXT', 'INFO', 'PULS', 'RESP'}
C = textscan(fid, parsePatternPerNColumns{nColumns}, 'HeaderLines', nHeaderLines);
case 'PHILIPS' % sometimes odd lines with single # occur within text file
C = textscan(fid, parsePatternPerNColumns{nColumns}, 'HeaderLines', nHeaderLines, 'CommentStyle', '#');
Expand Down
91 changes: 80 additions & 11 deletions PhysIO/code/readin/tapas_physio_read_physlogfiles_bids.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
fileName{2} = log_files.respiration;

[c,r,t,cpulse,acq_codes] = tapas_physio_read_physlogfiles_bids_separate(fileName,log_files,hasExplicitJsonFile,DEBUG,verbose);

elseif hasCardiacFile
fileName = log_files.cardiac;
[c,r,t,cpulse,acq_codes] = tapas_physio_read_physlogfiles_bids_unified(fileName,log_files,hasExplicitJsonFile,DEBUG,verbose);
Expand All @@ -97,6 +97,12 @@
[c,r,t,cpulse,acq_codes] = tapas_physio_read_physlogfiles_bids_unified(fileName,log_files,hasExplicitJsonFile,DEBUG,verbose);
end

% De-mean data
c_mean = mean(c);
r_mean = mean(r);
c = c - c_mean;
r = r - r_mean;

end

%% Function for separate physio files (cardiac and resp with different sampling rates)
Expand Down Expand Up @@ -147,7 +153,7 @@
if hasJsonFile
% in BIDS, start of the phys logging is stated relative to the first volume scan start.
% PhysIO defines the scan acquisiton relative to the phys log start
tRelStartScan = -val.StartTime;
tRelStartScanCardiac = -val.StartTime;
else
verbose = tapas_physio_log(...
['No .json file found and empty log_files.relative_start_acquisition. ' ...
Expand All @@ -156,9 +162,9 @@
else
if hasJsonFile
% add both delays
tRelStartScan = log_files.relative_start_acquisition - val.StartTime;
tRelStartScanCardiac = log_files.relative_start_acquisition - val.StartTime;
else
tRelStartScan = log_files.relative_start_acquisition;
tRelStartScanCardiac = log_files.relative_start_acquisition;
end
end

Expand All @@ -175,9 +181,13 @@
end
end

C = tapas_physio_read_columnar_textfiles(fileNameCardiac, 'BIDS');
C = tapas_physio_read_columnar_textfiles(fileNameCardiac, 'BIDS_SEPARATE');
c = double(C{idxCol(1)});
iAcqStart = (double(C{idxCol(2)})~=0); % trigger has 1, rest is 0;
try
iAcqStart = (double(C{idxCol(2)})~=0); % trigger has 1, rest is 0;
catch
iAcqStart = [];
end

%% delete temporary unzipped file
if isZipped
Expand Down Expand Up @@ -228,6 +238,26 @@
end
end

% sum implicit (.json) and explicit relative shifts of log/scan acquisition
if isempty(log_files.relative_start_acquisition)
if hasJsonFile
% in BIDS, start of the phys logging is stated relative to the first volume scan start.
% PhysIO defines the scan acquisiton relative to the phys log start
tRelStartScanRespiratory = -val.StartTime;
else
verbose = tapas_physio_log(...
['No .json file found and empty log_files.relative_start_acquisition. ' ...
'Please specify explicitly.'], verbose, 2);
end
else
if hasJsonFile
% add both delays
tRelStartScanRespiratory = log_files.relative_start_acquisition - val.StartTime;
else
tRelStartScanRespiratory = log_files.relative_start_acquisition;
end
end

% default columns in text file for phys recordings; overruled by JSON file
% 1 = resp, 2 = trigger
bidsColumnNamesRespiration = {'respiratory', 'trigger'};
Expand All @@ -248,8 +278,8 @@
nSamplesCardiac = length(c);
nSamplesRespiration = length(r);

tCardiac = -tRelStartScan + ((0:(nSamplesCardiac-1))*dtCardiac)';
tRespiration = -tRelStartScan + ((0:(nSamplesRespiration-1))*dtRespiration)';
tCardiac = -tRelStartScanCardiac + ((0:(nSamplesCardiac-1))*dtCardiac)';
tRespiration = -tRelStartScanRespiratory + ((0:(nSamplesRespiration-1))*dtRespiration)';

%% Deal with NaNs in c and r timecourse
c(isnan(c)) = interp1(tCardiac(~isnan(c)), c(~isnan(c)), tCardiac(isnan(c)));
Expand All @@ -272,7 +302,9 @@
%dtRespiration = tRespiration(2) - tRespiration(1);

isHigherSamplingCardiac = dtCardiac < dtRespiration;
if isHigherSamplingCardiac
isSameSamplingDiffNSamples = (dtCardiac == dtRespiration) && (nSamplesCardiac ~= nSamplesRespiration);

if isHigherSamplingCardiac && ~isSameSamplingDiffNSamples
t = tCardiac;
rInterp = interp1(tRespiration, r, t);
%racq_codesInterp = interp1(tRespiration, racq_codes, t, 'nearest');
Expand All @@ -285,7 +317,7 @@
end
r = rInterp;

else
elseif ~isHigherSamplingCardiac && ~isSameSamplingDiffNSamples
t = tRespiration;
cInterp = interp1(tCardiac, c, t);
%cacq_codesInterp = interp1(tCardiac, cacq_codes, t, 'nearest');
Expand All @@ -297,7 +329,44 @@
verbose.fig_handles(end+1) = fh;
end
c = cInterp;


elseif isSameSamplingDiffNSamples
% first we must syncronize the time vectors
if tRespiration(1) < tCardiac(1)

[~, indexOfMin] = min(abs(tRespiration-tCardiac(1)));

tRespiration = tRespiration(indexOfMin:end);
r = r(indexOfMin:end);
nSamplesRespiration = length(r);

else

[~, indexOfMin] = min(abs(tCardiac-tRespiration(1)));

tCardiac = tCardiac(indexOfMin:end);
c = c(indexOfMin:end);
nSamplesCardiac = length(c);

end

% % then fill the shorter file with zeros
% if nSamplesRespiration < nSamplesCardiac
% t = tCardiac;
% r(nSamplesRespiration+1:nSamplesCardiac) = 0;
% else
% t = tRespiration;
% c(nSamplesCardiac+1:nSamplesRespiration) = 0;
% end

% then trim the longer file
if nSamplesCardiac > nSamplesRespiration
c = c(1:nSamplesRespiration);
t = tRespiration;
else
r = r(1:nSamplesCardiac);
t = tCardiac;
end
end

%% Recompute acq_codes as for Siemens (volume on/volume off)
Expand Down
35 changes: 31 additions & 4 deletions PhysIO/code/readin/tapas_physio_read_physlogfiles_siemens.m
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@

if isempty(dt)
nSamplesC = size(data_table,1);
dt_c = tLogTotal/(nSamplesC-1);
dt_c = round(tLogTotal/(nSamplesC-1), 4);
else
dt_c = dt(1);
dt_c = round(dt(1), 4);
end

dataCardiac = tapas_physio_siemens_table2cardiac(data_table, ecgChannel, dt_c, ...
Expand Down Expand Up @@ -241,9 +241,9 @@

if isempty(dt)
nSamplesR = size(data_table,1);
dt_r = tLogTotal/(nSamplesR-1);
dt_r = round(tLogTotal/(nSamplesR-1), 4);
else
dt_r = dt(end);
dt_r = round(dt(end), 4);
end

dataResp = tapas_physio_siemens_table2cardiac(data_table, ecgChannel, ...
Expand All @@ -270,6 +270,33 @@
t_r = [];
end

%% As the start times of both signal recordings might be different, we should syncronize them before padding
nSamplesR = numel(r);
nSamplesC = numel(c);

isSameSamplingDiffNSamples = (dt_c == dt_r) && (nSamplesC ~= nSamplesR);

if isSameSamplingDiffNSamples

% first we must syncronize the time vectors
if t_r(1) < t_c(1)

[~, indexOfMin] = min(abs(t_r-t_c(1)));

t_r = t_r(indexOfMin:end);
r = r(indexOfMin:end);
nSamplesR = length(r);

else

[~, indexOfMin] = min(abs(t_c-t_r(1)));

t_c = t_c(indexOfMin:end);
c = c(indexOfMin:end);
nSamplesC = length(c);

end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Adapt time scales resp/cardiac, i.e. zero fill c or r
Expand Down