[FieldTrip] Segmenting data with ft_trialfun_general and with self-made function.

Schoffelen, J.M. (Jan Mathijs) janmathijs.schoffelen at donders.ru.nl
Mon Feb 6 21:22:48 CET 2023


Hi Ruoyi,

I don’t know about the specific file format you are working with, but - occasionally in some file formats - some events may have an empty value. As a consequence, the code that you used in your selfmade function may not have been absolutely robust. Specifically, the three lines:

value2 = {event.value}.';
value=cell2mat(value2)
sample = [event.sample].’

lead to two vectors (value and sample) that are assumed to relate to each other one-by-one, meaning that element -i- in one of the vectors corresponds to element -i- in the other vector.
I would first check if this is really the case in your data. If not, I think that this explains the discrepancy you report, and would be indicative that you may need to change your own code a bit, in order to obtain the expected behavior. You could quickly check this: a = {1 2 [] 3}; b = cell2mat(a); As you will see, ‘a’ has 4 elements, ‘b’ only 3...

Best wishes,
Jan-Mathijs



On 6 Feb 2023, at 20:33, Ruoyi Cao via fieldtrip <fieldtrip at science.ru.nl<mailto:fieldtrip at science.ru.nl>> wrote:

Hello FieldTrip community,
I segmented data by triggers after artifact correction on continuous data , where all deleted parts were replaced with NaN. Then this data mat was segmented with ft_trialfun_general and with a self made function as follows. The NaN value was later deleted after the output was time locked to the trigger.

I am very confused as the outcome from my self-made function was surprisingly not the same. Could anyone kindly give me some ideas for why this is the case and how to fix the problem?


Thank you so much
Ruoyi.

The two methods are as follows:
Standard code:

1:
cfg = [];
cfg.dataset             = M
% cfg.trialdef.pre    =  2.5;
% cfg.trialdef.post<https://urldefense.com/v3/__http://cfg.trialdef.post__;!!HJOPV4FYYWzcc1jazlU!6N0m7ApeHYcgG8eDnFRM6pJm_vl3F8FqPPpOGXDL4KV8-gkpwHh_lgp2Uz4GPQJbDOnl0_UdpSfhZCdJQuEfXasSpsBdSQ$>   =  1.5;
cfg.trialdef.eventtype      = 'STATUS';
cfg.trialdef.prestim        = 2.5; % in seconds
cfg.trialdef.poststim       = 1.5; % in seconds
cfg.trialdef.eventvalue = 101;
cfg_B1 = ft_definetrial(cfg);
segbinding1= ft_redefinetrial(cfg_B1,data);
Reference:
Preprocessing - Reading continuous EEG and MEG data - FieldTrip toolbox<https://urldefense.com/v3/__https://www.fieldtriptoolbox.org/tutorial/continuous/__;!!HJOPV4FYYWzcc1jazlU!6N0m7ApeHYcgG8eDnFRM6pJm_vl3F8FqPPpOGXDL4KV8-gkpwHh_lgp2Uz4GPQJbDOnl0_UdpSfhZCdJQuEfXauX0co87g$>

2:  Self made function according to following instruction :

function [trl, event] =location1(cfg);
hdr   = ft_read_header(cfg.dataset);
event = ft_read_event(cfg.dataset);
% search for "trigger" events
% value  = [event(find(strcmp('trigger', {event.type}))).value]';
value2 = {event.value}.';
value=cell2mat(value2)
% sample = [event(find(strcmp('trigger', {event.type}))).sample]';
sample = [event.sample].'
% determine the number of samples before and after the trigger
pretrig  = -round(cfg.trialdef.pre  * hdr.Fs);
posttrig =  round(cfg.trialdef.post<https://urldefense.com/v3/__http://cfg.trialdef.post__;!!HJOPV4FYYWzcc1jazlU!6N0m7ApeHYcgG8eDnFRM6pJm_vl3F8FqPPpOGXDL4KV8-gkpwHh_lgp2Uz4GPQJbDOnl0_UdpSfhZCdJQuEfXasSpsBdSQ$> * hdr.Fs);
% for each trigger except the last one
trl = [];
for j = 1:(length(value)-3)
           trg1 = value(j);
if (trg1==101);
trlbegin = sample(j) + pretrig;
trlend   = sample(j) + posttrig;
offset   = pretrig;
newtrl   = [trlbegin trlend  offset ];;
 trl      = [trl; newtrl]
end
end

This function was run with :
cfg = [];
cfg.dataset             = M
cfg.trialdef.pre    =  2.5;
cfg.trialdef.post<https://urldefense.com/v3/__http://cfg.trialdef.post__;!!HJOPV4FYYWzcc1jazlU!6N0m7ApeHYcgG8eDnFRM6pJm_vl3F8FqPPpOGXDL4KV8-gkpwHh_lgp2Uz4GPQJbDOnl0_UdpSfhZCdJQuEfXasSpsBdSQ$>   =  1.5;
cfg.trialfun = 'location1';
cfg_B1 = ft_definetrial(cfg);
segbinding1= ft_redefinetrial(cfg_B1,data);
References
Making your own trialfun for conditional trial definition - FieldTrip toolbox<https://urldefense.com/v3/__https://www.fieldtriptoolbox.org/example/making_your_own_trialfun_for_conditional_trial_definition/__;!!HJOPV4FYYWzcc1jazlU!6N0m7ApeHYcgG8eDnFRM6pJm_vl3F8FqPPpOGXDL4KV8-gkpwHh_lgp2Uz4GPQJbDOnl0_UdpSfhZCdJQuEfXatuNjCdqQ$>

The following excels shows two different trial definitions given by these two methods. The highlighted value is the differences between the beginning of each segment for each detected trial between two methods.

<image.png>
_______________________________________________
fieldtrip mailing list
https://mailman.science.ru.nl/mailman/listinfo/fieldtrip
https://urldefense.com/v3/__https://doi.org/10.1371/journal.pcbi.1002202__;!!HJOPV4FYYWzcc1jazlU!_MVGSW6yRgkWvGt4xy4cmB_vlZyD-pBdlbVepuQMeFvwrOtYJgIGHAAo0UbHZSnMQFmpY44JpbC233m34wdGVGr04n82eQoxcxg4aA$

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.science.ru.nl/pipermail/fieldtrip/attachments/20230206/06c54ccc/attachment.htm>


More information about the fieldtrip mailing list