0001 function phys = proc_physio(ps)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 insane = 0;
0018 phys = [];
0019
0020
0021 sinfo = ps.sinfo;
0022 sid = sinfo.id;
0023 pp = ps.pp;
0024 logfid = ps.logfid;
0025
0026 phys_fname = fullfile(ps.datapath, sid, sprintf('%s%s%s', sid,ps.phys_data_stub,ps.phys_data_suffix));
0027 check_exist(phys_fname);
0028
0029
0030 ri = read_physmon(phys_fname, ps.pp);
0031
0032
0033 if ~isempty(ps.elim_runs)
0034 fprintf(logfid, 'Eliminating run(s) %s\n', sprintf('%d ', ps.elim_runs));
0035 ri(ps.elim_runs) = [];
0036 end
0037
0038
0039
0040 use_runs = sinfo.use_runs;
0041 nruns = length(use_runs);
0042
0043 if nruns ~= length(ri)
0044 error_str = sprintf('ERROR:proc_physio:Expected %d runs, detected %d\n', nruns, length(ri));
0045 fprintf(logfid, error_str);
0046 error(error_str);
0047 return
0048 else
0049 for irun = 1:nruns
0050 ri(irun).id = use_runs(irun);
0051 end
0052 end
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062 fprintf(logfid, 'Performing data integrity checks ...\n');
0063
0064
0065 ps.expect.nslice = sum(sinfo.nvol(use_runs)*ps.nslice_per_vol);
0066
0067 if nruns ~= ps.expect.nruns
0068 fprintf(logfid,'WARNING:proc_physio:Really wanted %d runs; found %d\n', ps.expect.nruns, nruns);
0069 insane = 0;
0070
0071
0072
0073 ps.expect.pos_events = nruns*ps.expect.pos_events_per_run;
0074 ps.expect.neg_events = nruns*ps.expect.neg_events_per_run;
0075 end
0076
0077 npos = length(cat(1,ri.pos_events));
0078 if npos ~= ps.expect.pos_events
0079 fprintf(logfid,'WARNING:proc_physio:Expected %d positive markers, detected %d\n', ps.expect.pos_events, npos);
0080 insane = 1;
0081 end
0082
0083 nneg = length(cat(1,ri.neg_events));
0084 if ~any(ismember(nneg, ps.expect.neg_events))
0085 fprintf(logfid,'WARNING:proc_physio:Expected %d negative markers, detected %d\n', ps.expect.neg_events(1), nneg);
0086 insane = 1;
0087 end
0088
0089 nslice = length(cat(1,ri.slice_onsets));
0090 nslice_per_vol = ps.nslice_per_vol;
0091 if nslice ~= ps.expect.nslice
0092 for irun = 1:nruns
0093 run_idx = ri(irun).id;
0094 expect_nslice = sinfo.nvol(run_idx)*nslice_per_vol;
0095 actual_nslice = length(ri(irun).slice_onsets);
0096 if actual_nslice ~= expect_nslice
0097 fprintf(logfid,'WARNING:proc_physio:Run %d: Expected %d slices, detected %d\n', irun, expect_nslice, actual_nslice);
0098
0099 if expect_nslice == actual_nslice-nslice_per_vol
0100 ndiff = actual_nslice-expect_nslice;
0101 fprintf(logfid,'Eliminating %d excess slices from beginning of run %d\n', ndiff, irun);
0102
0103
0104
0105 ri(irun).slice_onsets(1:ndiff) = [];
0106
0107 new_start_time = ri(irun).slice_onsets(1);
0108
0109 ri(irun).slice_onsets = ri(irun).slice_onsets - new_start_time;
0110
0111 ri(irun).pos_events(2:end) = ...
0112 ri(irun).pos_events(2:end)-new_start_time;
0113 ri(irun).neg_events = ri(irun).neg_events - new_start_time;
0114 ri(irun).key_events = ri(irun).key_events - new_start_time;
0115
0116 ri(irun).cardiac = ri(irun).cardiac - new_start_time;
0117 elim_idx = find(ri(irun).cardiac < 0);
0118 ri(irun).cardiac(elim_idx) = [];
0119
0120 elim_idx = find(((0:length(ri(irun).respir)-1)/pp.Fs ...
0121 -new_start_time) < 0);
0122 ri(irun).respir(elim_idx) = [];
0123 elseif expect_nslice < actual_nslice
0124
0125 ndiff = actual_nslice-expect_nslice;
0126 fprintf(logfid,'Eliminating %d excess slices from end of run %d\n', ndiff, irun);
0127 ri(irun).slice_onsets(actual_nslice ...
0128 -(actual_nslice-expect_nslice)+1:end) = [];
0129
0130
0131
0132 stop_time = ri(irun).slice_onsets(end);
0133 bad_idx = find(ri(irun).pos_events > stop_time);
0134 ri(irun).pos_events(bad_idx) = [];
0135
0136 bad_idx = find(ri(irun).neg_events > stop_time);
0137 ri(irun).neg_events(bad_idx) = [];
0138
0139 bad_idx = find(ri(irun).key_events > stop_time);
0140 ri(irun).key_events(bad_idx) = [];
0141
0142
0143
0144 bad_idx = find(ri(irun).cardiac > stop_time + ps.cardiac_slop_s);
0145 ri(irun).cardiac(bad_idx) = [];
0146
0147 bad_idx = find((0:length(ri(irun).respir)-1)/pp.Fs > stop_time);
0148 ri(irun).respir(bad_idx) = [];
0149
0150 else
0151 insane = 1;
0152 end
0153 end
0154 end
0155 end
0156
0157 ps.insane = insane;
0158 if insane
0159 fprintf(logfid,'\tErrors detected!\n');
0160 else
0161 fprintf(logfid,'\tNo errors found\n');
0162 end
0163
0164
0165 phys.ps = ps;
0166 phys.ri = ri;
0167
0168 if pp.savedata
0169 phys_matname = fullfile(ps.datapath, sid, sprintf('%s_phys.mat', sid));
0170 fprintf(logfid,'Saving physio information to: %s\n', phys_matname);
0171 save(phys_matname, 'phys')
0172 end
0173
0174 return