diff --git a/src/pspm_expand_epochs.m b/src/pspm_expand_epochs.m index dff0a1d3..b9ffa4a2 100644 --- a/src/pspm_expand_epochs.m +++ b/src/pspm_expand_epochs.m @@ -1,9 +1,9 @@ -function [sts, ep_exp] = pspm_expand_epochs(epoches, expansion, options) +function [sts, ep_exp] = pspm_expand_epochs(varargin) % fn passt nicht zu missing epochs -% [sts, channel_index] = pspm_expand_epochs( {data_fn, channel}, expansion , options) % [sts, output_file] = pspm_expand_epochs( missing_epochs_fn, expansion , options) -% [sts, expanded_epochs] = pspm_expand_epochs( missing_epochs, expansion , options) % +% [sts, expanded_epochs] = pspm_expand_epochs( missing_epochs, expansion , options) +% [sts, channel_index] = pspm_expand_epochs( filename, channel, expansion , options) % options.mode = 'datafile' % 'missing_ep_file' % 'missing_ep' @@ -20,100 +20,125 @@ return; end -switch options.mode - % missing epochs - case 'missing_ep' - % Directly expand the given epochs - [sts, ep_exp] = expand(epoches, expansion); - return; - % missing epoches file - case 'missing_ep_file' - % Load missing epochs from file - data = load(epoches); % Load the file without specifying a variable - - % Assuming the file contains a variable called 'epochs', access it - % Right name? - if isfield(data, 'epochs') - missing_epochs = data.epochs; - else - error('File does not contain variable ''epochs''.'); - return; - end - if isempty(missing_epochs) - error('Failed to load missing epochs from file.'); - return; - end - % Expand the loaded epochs - [sts, ep_exp] = expand(missing_epochs, expansion); - if sts == -1 - error('Failed to expand epochs.'); - end +if nargin == 3 + + expansion = varargin{2}; + options = varargin{3}; - % Save expanded missing epoch to a new file with 'e' prefix - [pathstr, name, ext] = fileparts(epoches); - output_file = fullfile(pathstr, ['e' name ext]); - save(output_file, 'ep_exp'); % should i save it as epoch??? - disp(['Expanded epochs saved to: ', output_file]); + switch options.mode + % missing epochs + case 'missing_ep' + % Directly expand the given epochs + epochs = varargin{1}; + [ests, ep_exp] = expand(epochs, expansion); - sts = 0; - return; + if ests == -1 + error("Failed to expand epochs."); + return; + end + + sts = 1; + return; + % missing epoches file + case 'missing_ep_file' + % Load missing epochs from file + filename = varargin{1}; + + [lsts, epochs] = pspm_get_timing('file', filename); + + + if lsts == -1 + error("Epoch could not be loaded"); + return; + end + + % Expand the loaded epochs + [ests, ep_exp] = expand(epochs.epochs, expansion); + if ests == -1 + error('Failed to expand epochs.'); + return; + end + + % Save expanded missing epoch to a new file with 'e' prefix + [pathstr, name, ext] = fileparts(filename); + output_file = fullfile(pathstr, ['e' name ext]); + save(output_file, 'ep_exp'); % should i save it as epoch??? + disp(['Expanded epochs saved to: ', output_file]); + + sts = 1; + return; + end +end - case 'datafile' % rename!! +if nargin == 4 %&& options.mode == 'datafile' % rename!! + + % Load channel data + filename = varargin{1}; + channel = varargin{2}; + expansion = varargin{3}; + [lsts, ~, data] = pspm_load_data(filename, channel); + if lsts == -1 + error('Failed to load data from file.'); + return; + end - % Load channel data - datafile = epoches{1}; - channel = epoches{2}; - [lsts, ~, data] = pspm_load_data(datafile, channel); - if lsts == -1 - error('Failed to load data from file.'); - end + channel_data = data{1}; + sr = channel_data.header.sr; - channel_data = data{1}; - sr = channel_data.header.sr; + % Find NaN indices + nan_indices = isnan(channel_data.data); - % Find NaN indices - nan_indices = isnan(channel_data.data); + % Convert NaN indices to epochs + nan_epochs = pspm_logical2epochs(nan_indices, sr); - % Convert NaN indices to epochs - nan_epochs = pspm_logical2epochs(nan_indices, sr); + % Expand the epochs + [ests, ep_exp] = expand(nan_epochs, expansion); + if ests == -1 + error('Failed to expand epochs.'); + end - % Expand the epochs - [sts, ep_exp] = expand(nan_epochs, expansion); - if sts == -1 - error('Failed to expand epochs.'); - end + % Convert expanded epochs back to logical indices + expanded_indices = pspm_epochs2logical(ep_exp, numel(channel_data.data), sr); - % Convert expanded epochs back to logical indices - expanded_indices = pspm_epochs2logical(ep_exp, numel(channel_data.data), sr); + % Set data to NaN at expanded indices + channel_data.data(logical(expanded_indices)) = NaN; - % Set data to NaN at expanded indices - channel_data.data(expanded_indices) = NaN; + % Save the data back to the file + opt = struct(); + [wsts, ~] = pspm_write_channel(filename, {channel_data}, 'replace',opt); % add to options 'newfile'? + % channel_data.data; - % Save the data back to the file - [sts, out] = pspm_write_channel(datafile, {channel_data}, 'replace'); % add to options 'newfile'? - varargout{2} = 23 % channel_data.data; - return; + if wsts == -1 + error('Failed to write the new channel.'); + return + end + sts = 1; + return; - otherwise - error('Unknown mode in options.'); - return; + +else + error('Unknown mode in options.'); + return; end + end -function [sts, ep_exp] = expand(ep, expansion) + +function [ests, ep_exp] = expand(ep, expansion) % Helper function to expand epochs by the specified pre and post times % and merge overlapping epochs. % Also ensures that no epoch starts before time 0. -% Initialize status -sts = -1; +% Initialize status of expand +ests = -1; +ep_exp = []; % Check if epochs matrix and expansion vector are valid if isempty(ep) || numel(expansion) ~= 2 @@ -126,9 +151,23 @@ post = expansion(2); expanded_epochs_temp = [ep(:,1) - pre, ep(:,2) + post]; -% +% % Ensure that the start of any epoch is not negative -expanded_epochs_temp(expanded_epochs_temp(:,1) < 0, 1) = 0; % or <1??? + +[ksts, expanded_epochs_temp] = pspm_get_timing('epochs',expanded_epochs_temp , 'seconds') ; +if ksts == -1 + error('Offsets must be larger than onsets') + return +end + +% If there is only one epoch, no need for merging +if size(expanded_epochs_temp, 1) == 1 + ep_exp = expanded_epochs_temp; + ests = 1; + return; +end + + % Merge overlapping epochs ep_exp = expanded_epochs_temp(1, :); % Start with the first epoch @@ -142,6 +181,5 @@ end end -% Success -sts = 0; +ests = 1; end diff --git a/src/pspm_remove_epochs.m b/src/pspm_remove_epochs.m index 491dfdf7..7b725542 100644 --- a/src/pspm_remove_epochs.m +++ b/src/pspm_remove_epochs.m @@ -12,11 +12,10 @@ % be in seconds. This parameter is passed to pspm_get_timing(). % * timeunits: timeunits of the epochfile. % ┌───options -% └─.channel_action: [pre, post] -% ['add'/'replace'] Defines whether new channels should be added or -% corresponding channels should be replaced. The default value is 'add'. -% .expand_epochs: -% Expand epochs by 0.5 seconds before and after +% └─.channel_action: ['add'/'replace'] Defines whether new channels should be added or +% corresponding channels should be replaced. The default value is 'add'. +% └─.expand_epochs: [pre, post] +% % ● Output % * channel_index: index of channel containing the processed data % ● History