Import ASCII data with script

Hi,
I’ve preprocessed my EEG data without Brainstorm and I’ve saved it in ASCII format, now I would like to analyze data with Brainstorm. I realize I can import it from GUI, but I don’t know how I do it with script.
Thanks a lot.

Best wishes.
Ilaria

% Process: Create link to raw file
sFiles = bst_process(‘CallProcess’, ‘process_import_data_raw’, sFiles,[], …
‘subjectname’, SubjectName{1}, …
‘datafile’, {RawFiles, ‘EEG-ASCII’}, …
‘channelreplace’, 1, …
‘channelalign’, 0);
In this case file format is unknown, although ‘EEG-ASCII’ is in the list of supported formats (import_data.m). What can I do?
Thanks a lot

Hi Ilaria,

The function to import ASCII EEG files have not been updated yet to work in the new file import/export framework (that allows continuous files, processes and scripts, SSP projections, etc). Therefore they cannot be used to build scripts with the pipeline editor.
They are outdated and need to be refreshed, I will do it but not immediately.

I can suggest those alternatives:

  1. Use another exchange file format (EDF, EEGLAB .set, EGI .raw, etc).
    What software did you use for the pre-processing and the analysis?
    What are the formats you can use to export the data from there?

  2. Do all the pre-processing in Brainstorm. It can be very fast, and if you are planning to do some source analysis, this is the only way to get a correct noise covariance matrix for the inverse modeling.

  3. Use more “manual” scripts. Example:

ImportOptions = db_template('ImportOptions');
ImportOptions.DisplayMessages = 0;
NewFiles = import_data(DataFile, [], 'EEG-ASCII', iStudy, iSubject, ImportOptions);

% To get the subject and study indices, leave your mouse over the folder in which you want to import the file for a few seconds.
% You can edit the import options from a script with the calls:
% OPTIONS = bst_get('ImportEegRawOptions');
% Edit the OPTIONS fields
% bst_set('ImportEegRawOptions', OPTIONS);

You need to update Brainstorm before it stops showing the import options, I made a quick fix so that it works easily in script mode (menu Help > Update Brainstorm).

Cheers,
Francois

Hello Francois,

I'm trying to import the epoched data into Brainstorm using your alternatives, but so far without luck.

I have an SPM file with the final analyses, and I also exported it as plain .mat (which is not supported by the automatic import), EDF and EEGLAB with fieldtrip function ft_write_data and fieldtrip2eeglab (before I have to convert to fieldtrip bu this is easy).

In the EDF case I have no epochs, even though I specified in the header

hdr.nTrials = nTrials;

But Brainstrom gives me "this file does not contains any epoch"

Do you happen to know some toolbox to export EDF that is compatible with the definition of epochs that Brainstorm expects?

And with EEGLAB, I get these red exclamation marks in my trials (sorry for the silly test names)


and I can't select "Average Files" in the pipeline (like the trials doesn't exist)

I do manage to import the EDF file with the "Time" Version of import instead of "Existing epochs", and use the option to cut every X seconds, but the trials ends up with different times (from 0 to 1.2s, from 1.2 to 2.4s, etc...) and for instance I'm unable to calculate the noise covariance.
If I can fix that perhaps this would solve the problem, although I would feel more confortable importing with the "Existing epochs" version...

I know I could pre-process my data from the beginning with Brainstorm, but since I already have a very stable pipeline outside, with fine tunned high-pass filters and ICA artifcat removal for instance, I would prefer to just import the cleaned epochs if possible..
Also because I have and unusual way of defining conditions, which gives quite a lot of possible contrasts...
And it must be through scripts.. (not this manual one, since I need to provide the Study ID which I hope to be created during the process)

Thank you very much in advance,

Leonardo Barbosa

Hi Leonardo,

In the EDF case I have no epochs, even though I specified in the header

EDF files can only be read as continuous files in Brainstorm.

And with EEGLAB, I get these red exclamation marks in my trials and I can't select "Average Files" in the pipeline

For some reason, those trials are marked as bad in the EEGLAB file. To tag as good trials: select them all > right-click > Accept trials.

I do manage to import the EDF file with the "Time" Version of import instead of "Existing epochs", and use the option to cut every X seconds, but the trials ends up with different times

Drag and drop all the imported trials in the Process1 box and run the process "Standardize > Uniform epoch time". It will read the time definition of the first file of apply it to all the others. This can be executed from a script without any problem, so I guess you could go with the export to EDF option.

Cheers,
Francois

Hi Francois,

Thanks for the quick reply.

I found the problem during the EEGLAB import. For some reason the “isRaw” is true in the EEGLAB .set that I generated manually (probably some default value that I didn’t set). So in the line 58 of in_fopen_eeglab.m

if ~hdr.isRaw

it jumps to line 89 and set

    iGoodTrials = 1;

Later, since I do have epochs in hdr.EEG.trials, it enters the loop in line 239 and does this

    sFile.epochs(i).bad     = ~ismember(i, iGoodTrials);

So of course all the trials but the first is marked as bad

I don’t know if iRaw == 1 is supposed to happen in epoched datasets, but if so an easy fix would be in line 89

    iGoodTrials = 1:nEpochs

I did that and it’s working fine.

I adapted EEGLAB’s function fieldtrip2eeglab.m and created this mat2eeglab.m that takes a matrix with [channels x time x trials], vector with time in seconds, sampling rate and data set name.

I cannot find this function in the current EEGLAB, I followed fieldtrip’s wiki reference and found it with google, but at least to convert mat to brainstorm it seams to be working…

Thank you very much for the help and hope this is useful,
Leonardo

Hi Leonardo,

Thanks for the update and the suggestions.
There are apparently a few missing cases in the function in_fopen_eeglab. It considers that if the recordings are saved in a separate file (not in the .set), it’s necessarily a continuous file.
I’ll fix this at some point, but not immediately.

For now, do you have an option to save directly the recordings in the field .EEG.epoch of the EEGLAB .set files?
And what about the EDF option?

Cheers,
Francois

Hi François,

[QUOTE=Francois;7417]
There are apparently a few missing cases in the function in_fopen_eeglab. It considers that if the recordings are saved in a separate file (not in the .set), it’s necessarily a continuous file.
[/QUOTE]

So if I export in a single file the hdr.isRaw would be false? This could be easy to change…

[QUOTE=Francois;7417]
For now, do you have an option to save directly the recordings in the field .EEG.epoch of the EEGLAB .set files?
[/QUOTE]

In the file I’ve put in github you can update the EEG.epoch and EEG.events without a problem. I even left in comments how the structure should look like… But since I saw that I just needed the EEG.trials correctly exported in the in_fopen_eeglab, I left these other 2 variables empty, since I’m not planing to do any other condition labeling anymore for different events ( as I mentioned I have a complicated labeling, where there is only one event type but they have extra parameters in en external event file, that I read to define my conditions “by hand” )

If I export the epochs/events, EEGLAB wiil save the hdr.iRaw as false? Sorry if I’m insisting on that but to me it seams to be the source of the trials being imported as bad, no?. Is there another problem in the export/import that is related to the epoch/events properties being present?

[QUOTE=Francois;7417]
And what about the EDF option?
[/QUOTE]

I have the impression that the function I’m using, ft_write_data, is not writing in the format Brainstorm is expecting… not the “Existing epochs” version at least.
And as I said I would like to avoid the << concatenating in export / importing as blocks of N seconds / Standardize > Uniform epoch time >> steps to avoid export / import errors (trial size, etc)

Do you happen to know another EDF export tool that can export epoched files that works fine with Brainstorm? I could test that…

Thank you again for all the help,
Leonardo

If you save your file as one single .set file instead of a combination .set/.dat, it would work correctly.

It is not EEGLAB’s fault, it’s mine. I wrote a function that can process either continuous recordings in .dat format (isRaw=1) OR epoched recordings in .set format (isRaw=0).
The file you have now is saved in a format that is not properly handled by Brainstorm.
There is also no support at all for epoched EDF files.
Note that this isRaw value is not saved in the EEGLAB file, it’s coming from a test at the beginning of my function:

hdr.isRaw = isempty(hdr.EEG.epoch) && ~isempty(hdr.EEG.data);

Can you please send me an example of those .set/.dat file? This way I would be able to add a proper support for them.
You can update this file somewhere (eg. dropbox) and send me a link to the file in a separate email (click on my username on this forum).

Cheers,
Francois

Sorry, my bad, didn’t see this line.

I’m not sure it’s worth the trouble, since probably when you do epoching with EEGLAB you always get the trails field and the epochs field, so it would be a fix to use this mat2eeglab function (or anyone generating a EEGLAB structure like that…)!
But anyway I’ll send you the email with one example.
Also I’ll fix the github code to accept and epoch / events structure in any case…

Cheers,
Leonardo

Ok, so if you have another solution and it’s not a standard structure for EEGLAB files, I will skip this modification.
I already have enough urgent and necessary things to implement =)