Locating the FilterSources in BCI2k/Transcoding struct 's'

Forum for software developers to discuss BCI2000 software development
Locked
TokyoBCI
Posts: 6
Joined: 31 Jul 2012, 05:24

Locating the FilterSources in BCI2k/Transcoding struct 's'

Post by TokyoBCI » 31 Jul 2012, 05:57

Hello,

I hope I have chosen the correct subforum to post my question.

I have recorded several Onlinesessions with 14 Subjects and my aim is to use the data and develop an improved filter chain fitted to our scenario (a p300 speller with wholly new Application and several other additions).

These are only available as compiled froms in the cmdline subfolder in the "tools" section. I am wondering how I should go about my goal to rapidly design and test my filtering ideas through Matlab in several offline sessions on our recorded data.

I have read http://www.bci2000.org/wiki/index.php/P ... sed_Filter
as well as
http://www.bci2000.org/wiki/index.php/P ... sed_Filter

but I find myself none the wiser.

I think I am just confused about the order of filtering happening, but according to the schematic, MATLAB filters can be used between normalization and spatial filtering (which I want to control as well...how do i go about it?) but I didn"t find the introductino in the wiki helpful to me. Can anyone set me upon the right path?
Last edited by TokyoBCI on 06 Aug 2012, 01:50, edited 1 time in total.

mellinger
Posts: 1056
Joined: 12 Feb 2003, 11:06

Re: Locating the Filters in BCI2k

Post by mellinger » 31 Jul 2012, 09:08

Hi,

the MatlabFilter is useful if you want to process data online, using Matlab code.

What you want to do is to process data offline, using existing BCI2000 filters. This can be done from the command line, without using Matlab:
http://www.bci2000.org/wiki/index.php/U ... Processing

In addition, there exists a Matlab wrapper function for command line processing, which makes it very comfortable to use:
http://www.bci2000.org/wiki/index.php/U ... i2000chain

I'm sorry that you got on the wrong track using the documentation, and I would like to improve the wiki. So I would appreciate if you could drop me a note how it happened, i.e. how you found the MatlabFilter related wiki pages, and missed the offline analysis related ones.

Thanks,
Juergen

TokyoBCI
Posts: 6
Joined: 31 Jul 2012, 05:24

Re: Locating the Filters in BCI2k

Post by TokyoBCI » 06 Aug 2012, 01:50

mellinger wrote:Hi,

the MatlabFilter is useful if you want to process data online, using Matlab code.

What you want to do is to process data offline, using existing BCI2000 filters. This can be done from the command line, without using Matlab:
http://www.bci2000.org/wiki/index.php/U ... Processing

In addition, there exists a Matlab wrapper function for command line processing, which makes it very comfortable to use:
http://www.bci2000.org/wiki/index.php/U ... i2000chain

I'm sorry that you got on the wrong track using the documentation, and I would like to improve the wiki. So I would appreciate if you could drop me a note how it happened, i.e. how you found the MatlabFilter related wiki pages, and missed the offline analysis related ones.

Thanks,
Juergen
Thank you for the input.

I aim to change the SpatialFiltertype to the Laplacian types, but do not now where I have to edit the source to achieve it.

I also want to introduce my own filter which I want to introduce into the filter chain. Is there any hint as to what the preferable location would be - I would like to apply the GSVD based Signal Fraction proposed by the Department of Mathematics of CSU (http://www.vissta.ncsu.edu/workshop/pre ... y/BCI5.ppt) on the "mixed signal - the raw recorded signal, to my understanding, after referencing has taken place.

LAst but not least, I would like to know how I can translate the output of the bci2000chain (the struct 's') into usable data for offline p300classification, to see how the new filtering has affected the feature extraction and subsequently classificatino performance.

----

edit: As I see, the Mario.exe had a nice GUI for slecting laplacian spatial filters. That is essentially what I was looking for: http://www.bci2000.org/wiki/index.php/P ... with_Mario

Any ideas how to achieve my aims ?

boulay
Posts: 382
Joined: 25 Dec 2011, 21:14

Re: Locating the Filters in BCI2k

Post by boulay » 06 Aug 2012, 05:38

TokyoBCI wrote: I aim to change the SpatialFiltertype to the Laplacian types, but do not now where I have to edit the source to achieve it. Using anything else for the SpatialFilterType var in the BCI-chain just gives errors.
Most BCI researchers use a simple linear kernel as an approximation of a Laplacian spatial filter. See the attached screenshot. You can ignore EDC and Trig. Also, that should be T7, not Tz.
LLP_spatial_filter.JPG
LLP_spatial_filter.JPG (20.34 KiB) Viewed 5988 times
TokyoBCI wrote: I also want to introduce my own filter which I want to introduce into the filter chain. Is there any hint as to what the preferable location would be - I would like to apply the GSVD based Signal Fraction proposed by the Department of Mathematics of CSU (http://www.vissta.ncsu.edu/workshop/pre ... y/BCI5.ppt) on the "mixed signal - the raw recorded signal, to my understanding, after referencing has taken place.
If you want your filter to be useful online, you'll need to program a module. See here:
http://www.bci2000.org/wiki/index.php/P ... ing_Filter

If you don't ever want to use it online, and if you want to use the other OfflineAnalysis tool for P300 analysis ANYWAY, and you have MATLAB, then I suggest the following:
  • Find BCI2000\tools\matlab\ExampleBCI2000Chain.m . Write your own version that uses the appropriate signal processing steps. You probably want only TransmissionFilter and SpatialFilter
    Modify s.Signal using GSVD based Signal Fraction (sorry, can't help you there)
    Use BCI2000\tools\mex\save_bcidat.m to write the changed signal to a new bci2000 dat file
    Use BCI2000\tools\OfflineAnalysis\OfflineAnalysis.m to do your P300 analysis.
-Chad

TokyoBCI
Posts: 6
Joined: 31 Jul 2012, 05:24

Re: Locating the Filters in BCI2k

Post by TokyoBCI » 07 Aug 2012, 00:02

boulay wrote:
TokyoBCI wrote: I aim to change the SpatialFiltertype to the Laplacian types, but do not now where I have to edit the source to achieve it. Using anything else for the SpatialFilterType var in the BCI-chain just gives errors.
Most BCI researchers use a simple linear kernel as an approximation of a Laplacian spatial filter. See the attached screenshot. You can ignore EDC and Trig. Also, that should be T7, not Tz.
LLP_spatial_filter.JPG
TokyoBCI wrote: I also want to introduce my own filter which I want to introduce into the filter chain. Is there any hint as to what the preferable location would be - I would like to apply the GSVD based Signal Fraction proposed by the Department of Mathematics of CSU (http://www.vissta.ncsu.edu/workshop/pre ... y/BCI5.ppt) on the "mixed signal - the raw recorded signal, to my understanding, after referencing has taken place.
If you want your filter to be useful online, you'll need to program a module. See here:
http://www.bci2000.org/wiki/index.php/P ... ing_Filter

If you don't ever want to use it online, and if you want to use the other OfflineAnalysis tool for P300 analysis ANYWAY, and you have MATLAB, then I suggest the following:
  • Find BCI2000\tools\matlab\ExampleBCI2000Chain.m . Write your own version that uses the appropriate signal processing steps. You probably want only TransmissionFilter and SpatialFilter
    Modify s.Signal using GSVD based Signal Fraction (sorry, can't help you there)
    Use BCI2000\tools\mex\save_bcidat.m to write the changed signal to a new bci2000 dat file
    Use BCI2000\tools\OfflineAnalysis\OfflineAnalysis.m to do your P300 analysis.
-Chad
This was very helpful, even though it's only a bird's eye view of what I need. Thank you again. I will post here once I successfully implement my ideas.

- David

TokyoBCI
Posts: 6
Joined: 31 Jul 2012, 05:24

Re: Locating the Filters in BCI2k

Post by TokyoBCI » 08 Aug 2012, 00:38

boulay wrote: Most BCI researchers use a simple linear kernel as an approximation of a Laplacian spatial filter. See the attached screenshot. You can ignore EDC and Trig. Also, that should be T7, not Tz.
LLP_spatial_filter.JPG
-Chad
How do I get to this interesting window? The plethora of tools without a common GUI still confuses me at times. As far as i know, the Matrix itself should be stored in .prm file accompanying the recorded data. What tool did you use to display and change the matrix? Where does that fit into modifying filters for offline use?

I also wonder whether the bci2000chain.m covers only the Signalprocessing module between (but not including) the Alignment Filter and the Connector Input or whether one needs to specify the lot to get usable data in the end. Which is it?

The bci2000wiki states that dense to full matrix spatial filtering will result in loss of channel labels.

How is it possible to use just the moving average filter used by the p300temporal filter? Is there an isolated form somewhere?

boulay
Posts: 382
Joined: 25 Dec 2011, 21:14

Re: Locating the Filters in BCI2k

Post by boulay » 08 Aug 2012, 05:45

TokyoBCI wrote: How do I get to this interesting window? The plethora of tools without a common GUI still confuses me at times. As far as i know, the Matrix itself should be stored in .prm file accompanying the recorded data. What tool did you use to display and change the matrix? Where does that fit into modifying filters for offline use?
I use \BCI2000\tools\BCI2000FileInfo\BCI2000FileInfo.exe to load a data file, then I click on View Parameters.
The new window is the same as the GUI for the BCI2000 operator Config menu. If you've ever used BCI2000 for online feedback of brain signals then you have probably encountered this window.
You can modify the parameters as you see fit, then click on "Configure Save..." to choose which parameters you want to write to the .prm file, then click on "Save Parameters..." to write the prm file. In the future, you can also use this window to load a .prm file, make some modifications, and then save it again. If I only need to make one simple modification then I simply use a text editor to edit the .prm file. If I need to make several chnages, and if I can't remember the exact format for the line of text, then I use this tool.

I'm not sure about the loss of channel labels. I use full matrix spatial filtering and yet I can still access the signals in my application module by name (instead of index).

TokyoBCI
Posts: 6
Joined: 31 Jul 2012, 05:24

Re: Locating the FilterSources in BCI2k/Transcoding struct '

Post by TokyoBCI » 13 Aug 2012, 03:40

Error messages wizardry needed:

Code: Select all

s = 

             FileName: [1x81 char]
              DateStr: '2012-04-29 17:04:53'
              DateNum: 7.3499e+005
          FilterChain: {'TransmissionFilter'  'SpatialFilter'}
         ToolVersions: [1x1 struct]
           ShellInput: [1x319 char]
          ShellOutput: ''
            ChainTime: 43.7690
     ChainSpeedFactor: 12.7081
            Megabytes: 14.0422
                Parms: [1x1 struct]
               Blocks: 17799
      BlocksPerSecond: 32
      SecondsPerBlock: 0.0313
             Channels: 9
        ChannelLabels: {1x9 cell}
             Elements: 16
        ElementLabels: {1x16 cell}
        ElementValues: [1x16 single]
          ElementUnit: 's'
                 Time: [17799x1 single]
             FullTime: [284784x1 single]
    FullElementValues: [284784x1 single]
               Signal: [284784x9 single]
               States: [1x1 struct]
save_bcidat('C:\Save.dat',s.Signal,s.States,s.Parms)
??? Error using ==> save_bcidat
Too little samples in state "PauseApplication": state variables
should provide a value for each sample point.
 
All I did is shorten the filter chain by throwing out the ARFilter.

Now: is my parameterfile corrupted or what happened? I believe that the problem lies in the discrepancy between TIme and Fulltime. What did I disregard to be faced with this error?

boulay
Posts: 382
Joined: 25 Dec 2011, 21:14

Re: Locating the FilterSources in BCI2k/Transcoding struct '

Post by boulay » 13 Aug 2012, 11:41

I don't have the BCI2000 mex files or any .dat files on this computer, and I'm locked out of campus for the holiday, but here's a guess:

Code: Select all

>> [n_samples,n_channels]=size(s.Signal);
>> temp_states = s.States;
>> temp_states = setfield(temp_states, 'PauseApplication', false(n_samples,1));
>> s = setfield(s, 'States', temp_states);
Do that before trying to save your data.

Are you using save_bcidat or the wrapper (I can't look for it right now, but it should be in BCI2000/Tools/cmdline)? I think there's a wrapper that Jeremy Hill (jhill) wrote that reduces the requirements of save_bcidat.

Sorry I can't be more specific, but I figured this vague piece of information might be more useful than waiting until I get back to the office next Monday.
-Chad

jhill
Posts: 31
Joined: 17 Nov 2009, 15:15

Re: Locating the FilterSources in BCI2k/Transcoding struct '

Post by jhill » 17 Aug 2012, 19:23

For a start, don't worry about the difference between s.Time and s.FullTime. It does not indicate corruption: they are intentionally different sizes, and their different sizes reflect their different names. BCI2000 (and hence bci2000chain, which mimics it) fundamentally processes signals one "SampleBlock" at a time, with the number of samples per block dictated by the SampleBlockSize parameter. States are generally updated once per block. Therefore some things (e.g. s.States subfields) are best plotted against s.Time, i.e. the number of seconds corresponding to each block. Other things (e.g. s.Signal when bci2000chain runs in '2d' mode, as it does in your example) are best plotted against s.FullTime, i.e. the number of seconds corresponding to each sample. Simply choose the one that is the correct size. And if you want to look at everything (even states) at one-sample resolution, override the SampleBlockSize parameter and change it to 1 in your bci2000chain call (and go and make a cup of tea).

Having understood that, your problem with save_bcidat is that bci2000chain has left you with one state value per block, whereas save_bcidat expects them to be in full per-sample format. This is because of the way that the underlying command-line tools (and most BCI2000 Filters themselves) handle state information, in contrast to the way load_bcidat (and the DataIOFilter that saved it originally) do it.

The simplest workaround would be to do an additional step to load the full per-sample states, and use that structure instead:

Code: Select all

[ans, perSampleStates] = load_bcidat(s.FileName);
save_bcidat('blah.dat', s.Signal, perSampleStates, s.Parms)
Note that this is only possible because your filters (TransmissionFilter and SpatialFilter) do not change the block size in mid-chain, and hence the total number of samples stays the same as in the file. If any filters were to change the block size (as LinearClassifier, P3TemporalFilter and ExpressionFilter do), let alone convert the block into a different representation (say, to frequency bins instead of samples), then perSampleStates would still be the wrong size.

Chad mentioned that there is a wrapper around save_bcidat. That's true—it's called create_bcidat—but it has the disadvantage of discarding state variables entirely. If the states are unimportant to you, however, you could use it:

Code: Select all

create_bcidat('blah.dat', s)  % warning: s.States are discarded

Locked

Who is online

Users browsing this forum: Bing [Bot] and 1 guest