Generate epoching script for MEG data

Dear Francois:
I hope this email finds you well.
I am trying to figure out how to generate a script to epoch all my MEG subjects data at once, instead of manually doing the right-click on individual raw files (and then "Import in database").

So I dragged 2 my subjects files into the Process1 box - I assume I don't need to worry about specifically selecting only the "raw MEG files" right? As long as the left bottom side of the "raw channel file looking image button" is pressed, the Process box would automatically filter in the raw MEG file for me, right?

Then, I clicked on the RUN button at the bottom left, and then I selected "Import > Import recordings > Import MEG/EEG: Events", right?

But the interface here seems not the same as that when I individually clicked on the individual raw data. I can only select one subject name at a time, and I assume this is the way to do it with one subject for now and later I just need to modify the script to loop in all my subjects right?

And what is the "Folder name" for? I found if I write in a folder name here, the output would be all the epoched event files put together into one folder under the first subject's raw file folder, which is not what I want. I want to separate each subject's epoched files either under each subject's folder, or under a separate new folder by itself, not under a specific subject's folder like this. I then tried not to write anything in it, and the outputted result did creating separate new folders with the individual epoched files in it but these new folders are still under the first subject's directory. How do I correct for this? (moving these new folders to a higher directory level, not under any specific subject)?

And, I assume I should click off the "Create one condition for each event type", as well as leave out the "Ignore shorter epochs", correct? Also, should I check or check off the "Use CTF compensation"? My data is .dat format. And the data were already filtered in the beginning as continuous data (with a 1-50Hz filter) so I don't need any more "removing DC offset here but how do I ensure this?

Finally, I assume I should check "use SSP/ICA projectors" that I did for all subjects for sure.

And then I don't need to hit "Run" but should just "Generate .m script" and then work on that script, correct?

Thank you very much for your critical assistance!

Yuwen

But the interface here seems not the same as that when I individually clicked on the individual raw data.

The process version of the data importer offers all the options that might be asked when using the interactive menu "Import to database", but that you don't see it they are not relevant for the files you import.
Note that the "Resample" and "Remove DC offset" are not included here, you need to add them as separate processes if needed.

I can only select one subject name at a time, and I assume this is the way to do it with one subject for now and later I just need to modify the script to loop in all my subjects right?

Yes, the way to proceed is to prototype your analysis only with one subject, generate the corresponding scripts for one subject/one run at a time, then add a loop over all the subjects in your script. This requires a bit of Matlab scripting, there is no way to automate this in Brainstorm (yet).

For examples on how to script a full study with multiple subject, you can refer to the tutorials we published earlier this year:
https://neuroimage.usc.edu/brainstorm/Tutorials/VisualSingle
https://neuroimage.usc.edu/brainstorm/Tutorials/VisualGroup

The corresponding processing scripts are available in the Brainstorm distribution (brainstorm3/toolbox/script/tutorial_visual*.m):
https://github.com/brainstorm-tools/brainstorm3/blob/master/toolbox/script/tutorial_visual_single.m
https://github.com/brainstorm-tools/brainstorm3/blob/master/toolbox/script/tutorial_visual_copy.m
https://github.com/brainstorm-tools/brainstorm3/blob/master/toolbox/script/tutorial_visual_group.m

And what is the "Folder name" for?
And, I assume I should click off the "Create one condition for each event type"

This is an option to override the output folder, which is not available in the interactive menu "Import to database". If you want to reproduce what the GUI does, leave this option empty.
If combined to this, you do not select the option "Create one condition for each event type", it would create a new folder for each continuous input file, with the same name but without the "raw" tag. It's the recommended approach for most studies.

as well as leave out the "Ignore shorter epochs", correct?

This one, you could keep selected if your goal is to compute ERPs. It would ignore epochs that are shorter than the longest epoch. This is useful only in the case of events of interests (the events you use for the epoching) that are too close to the beginning or the end of the recordings, it would skip the events that cannot lead to a full epoch.

Also, should I check or check off the "Use CTF compensation"?

It you are using CTF MEG recordings, yes. Otherwise this option has no impact.

And the data were already filtered in the beginning as continuous data (with a 1-50Hz filter) so I don't need any more "removing DC offset here but how do I ensure this?

In general, if you have applied a high-pass filter, this already removed the arbitrary signal DC components, and all the possible slow drifts that are not physiological.
But this could be a debatable question... not everybody has the same opinion on this question, and it depends on your experimental design, your data quality and your hypotheses.
Consider that you don't necessarily need to do an additional baseline correction at the time of the epoching, but keep in mind that someone else might suggest you to remove the DC based on your pre-stim baseline even with this high-pass filter.

Finally, I assume I should check "use SSP/ICA projectors" that I did for all subjects for sure.

If you have removed some SSP/ICA components, yes, select this option, otherwise your cleaning would be lost.

And then I don't need to hit "Run" but should just "Generate .m script" and then work on that script, correct?

For guidelines on how to write scripts, read this tutorial:
https://neuroimage.usc.edu/brainstorm/Tutorials/Scripting

Dear Francois:
Thank you! I see that there seems to be a new options showing up under Process > Import recordings > Import MEG/EEG: Events, which is the "Align sensors using headpoints" and I wonder if I should check this option when generating the script? I have previously followed the tutorial session "Head Motion Detection" and have successfully used a script to adjust all my subjects' head motions and corrected the head positions, as reflected by the HLU time series. So I wonder here when generating an epoching script, should I check the option of "Algin sensors using headpoints" or not?

Thank you!

Yuwen

Dear Francois:
Sorry - For some reason I thought I saw this option of "Align sensors using headpoints" when working with generating the script of epoching, but now it seems to have disappeared. So I guess it's not a regular option to consider, right?

Thank you,
Yuwen

You get this option when importing new data to the database (creating new links to continuous file, or importing already epoched files).

Repeat in your scripts what you would do interactively. If you have a set of additional head points that correctly distributed and that allows you to get good results with the automatic registration, use it. Otherwise, you'd need to mark the NAS/LPA/RPA points as accurately as possible.
https://neuroimage.usc.edu/brainstorm/Tutorials/ChannelFile#Automatic_registration

For marking multiple MRI files and saving the fiducials in a .m file for later use, you can use the menu File > Batch MRI fiducials.
https://neuroimage.usc.edu/brainstorm/Tutorials/Scripting?highlight=(Batch+MRI+fiducials)#How_to_process_an_entire_study
https://neuroimage.usc.edu/brainstorm/Tutorials/RestingOmega?highlight=(Batch+MRI+fiducials)#Import_the_dataset_.5BTODO.5D

Thank you, Francois.

Yuwen