[FieldTrip] using Matlab instead of Fieldtrip toolboxes

Roy Cox roycox.roycox at gmail.com
Thu Feb 24 18:26:29 CET 2022


Hi Jan Mathijs,

Many thanks for your answer: it's proving to be quite some detective work
but I think/hope I'm almost there.

First, it looks like your suggestion works for Fieldtrip itself. But
perhaps you could clarify for my understanding: ft_defaults, which you call
at the start of your Matlab session but is also called from various ft
functions, typically checks for the presence/contents of the global
ft_defaults var. But the toolbox fields are used to update (in this case,
remove) paths ONLY the very first time ft_defaults is called, so have to
make sure the global var has already been set. Something like that?

A complication I didn't mention is that I'm actually using SleepTrip,
carrying a "Fieldtrip-lite" inside. Here your suggestion doesn't work, but
I think I know why. (I collaborate with SleepTrip's developer so we can
make changes quickly, but want to check first before I make suggestions.)

Inside ft_defaults there's a call


*if ~ft_platform_supports('signal') || ~strcmp(ft_default.toolbox.signal,
'matlab') || ~ft_hastoolbox('signal')
addpath(fullfile(fileparts(which('ft_defaults')), 'external', 'signal'));*
*end*

where *ft_platform_supports* appears to verify that all functions in
fieldtrip/external/signal are actually present as a Matlab builtin. Clever,
but SleepTrip includes at least one function in /external/signal that has
no corresponding function in Matlab's signal toolbox.

So my suspicion is that /external/signal (and /external/stats and
/external/image) should ONLY include function names matching a function
from the respective Matlab's toolbox? If so, that should be a relatively
easy fix.

Many thanks,
Roy

On Wed, Feb 23, 2022 at 12:26 PM Schoffelen, J.M. (Jan Mathijs) via
fieldtrip <fieldtrip at science.ru.nl> wrote:

> Hi Roy,
>
> Just changing the global ft_default variable is not going to the trick
> indeed. You’d need to run ft_defaults once more before the changes have an
> effect. Possibly you’d need to run restoredefaultpath first to be
> absolutely sure.
>
> So, in summary, once you start a matlab session you’d do something like:
>
> global ft_default;
> ft_default.toolbox.signal = ‘matlab’;
>
> addpath(<path-to-fieldtrip>);
> ft_defaults;
>
> This should do it, I think.
>
> Best wishes and happy computing!
>
> JM
>
> PS: looking forward to a speed-improved version of the overloaded
> filtfilt.m
>
>
>
> On 22 Feb 2022, at 18:05, Roy Cox via fieldtrip <fieldtrip at science.ru.nl>
> wrote:
>
> hello,
>
> I'm having trouble getting Fieldtrip (revision 8108d99ee.) to reliably use
> Matlab's builtin signal toolbox, rather than the one in
> Fieldtrip/external/signal. I followed the online suggestions (
> https://www.fieldtriptoolbox.org/faq/should_i_add_fieldtrip_with_all_subdirectories_to_my_matlab_path/
> <https://urldefense.com/v3/__https://www.fieldtriptoolbox.org/faq/should_i_add_fieldtrip_with_all_subdirectories_to_my_matlab_path/__;!!HJOPV4FYYWzcc1jazlU!9W_-dvWnmFYAlMXwYGZwfpXws4MmlqjOSG6WyVaJK6hmqIFPpthFjbCjC3mB-u0gbJolbsCaCy0-mhsceOQlVz4qTPmgPn3zU-vVgQ$>)
> and checked the info surrounding ft_defaults.
>
> Background: Matab's filtfilt is about twice as fast as Fieldtrip's, which
> would make a big difference for our overnight sleep EEG. I tested a
> bandpass filter on a small recording while explicitly adding/removing
> *Fieldtrip/external/signal* from the path (as NOT recommended, I know).
> Doing this right before filtfilt is called (from ft_preprocessing ->
> preproc -> ft_preproc_bandpassfilter -> filter_with_correction, line 73):
>
> Fieldtrip
>
> *tic;filt = filtfilt(B, A, dat')';toc Elapsed time is 7.384431 seconds.*
>
> Matlab
>
> *tic;filt = filtfilt(B, A, dat')';toc Elapsed time is 3.328224 seconds.*
>
> I get similar speedups when timing ft_preprocessing, different filters,
> and for (much) larger datasets, so definitely worth it.
>
> Now I try to do things the recommended way from a fresh Matlab session
> (with all Fieldtrip-related stuff removed from startup.m).
>
> First I check:
>
>
> *which -all filtfilt *
> *C:\Program Files\MATLAB\R2020a\toolbox\signal\signal\filtfilt.m*
>
> Then I initialize Fieldtrip:
>
>
> *addpath(myPathToFieldtrip) ft_defaults*
>
> and check filtfilt again:
>
>
> *which -all filtfilt longPathToFieldtrip\external\signal\filtfilt.m
> C:\Program Files\MATLAB\R2020a\toolbox\signal\signal\filtfilt.m
> %Shadowed*
>
> which makes sense since the default is to use Fieldtrip's filtfilt.
>
> I then do as suggested:
>
>
> *global ft_default ft_default.toolbox.signal='matlab';*
>
> and can verify that the global var is correctly set:
>
> *ft_default.toolbox *
>
> *ans =  *
>
>
>
>
>
> *struct with fields:      images: 'compat'       stats: 'compat'
>  signal: 'matlab'     cleanup: {}*
>
> But calling *which -all filtfilt *again, the Fieldtrip path has not been
> removed. I tried calling *ft_defaults *a second time (perhaps in order
> for the new values in *ft_default* to take effect?), but both filtfilt
> paths remain. And I verified that Fieldtrip in fact continues to use its
> own slow filtfilt implementation when calling *ft_preprocessing*. So it
> appears that the contents of * ft_default.toolbox* aren't actually used
> to update paths, which may or may not be as intended.
>
> I have to add that at one (and only one) point during my lengthy
> explorations Fieldtrip DID pick the requested version (and removed the path
> to *\external\signal\ *). But I haven't been able to reproduce since, so
> this seems to work only under highly specific conditions.
>
> Explicitly removing the path to *\external\signal\ *using *rmpath* seems
> to work, but is also risky because I imagine Fieldtrip may attempt to
> restore this path depending on what functions are called elsewhere in a
> script (especially if the request for using the Matlab version in
> *ft_default.toolbox* is missed/ignored).
>
> Besides physically deleting folders, how do I ensure Fieldtrip ALWAYS uses
> Matlab's signal (or stats/image) toolboxes and will NEVER attempt to add
> its own versions back to the Matlab path?
>
> Thanks,
> Roy
> _______________________________________________
> fieldtrip mailing list
> https://mailman.science.ru.nl/mailman/listinfo/fieldtrip
>
> https://urldefense.com/v3/__https://doi.org/10.1371/journal.pcbi.1002202__;!!HJOPV4FYYWzcc1jazlU!9W_-dvWnmFYAlMXwYGZwfpXws4MmlqjOSG6WyVaJK6hmqIFPpthFjbCjC3mB-u0gbJolbsCaCy0-mhsceOQlVz4qTPmgPn0TBKpvfw$
>
>
> _______________________________________________
> fieldtrip mailing list
> https://mailman.science.ru.nl/mailman/listinfo/fieldtrip
> https://doi.org/10.1371/journal.pcbi.1002202
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.science.ru.nl/pipermail/fieldtrip/attachments/20220224/bd1b68c7/attachment.htm>


More information about the fieldtrip mailing list