Raw continuous recording split into epochs and then exported into images

Hi everyone,

I have a 2 hr-recording of nap data (EEG, EOG, and EMG) which I need to sleep-score and export in an image form (or pdf or whatever form necessary) so that I can cross-check my sleep scoring with another researcher. The data is raw continuous data which I need to export into 30 seconds epochs (after adding all the necessary events of course).

To do this I first made a custom montage as below.

And below is the resulting raw continuous data.

I specified the time axis (30 sec epochs) and the amplitude axis (50 uV/cm) correctly according to my needs. Therefore what I need Brainstorm to do now is just export (i.e. take the image) whatever I'm looking at currently in 30-second epochs sequentially until the end of the file.

I found two different ways to do this so far but both are very slow because they require manual clicking.

  1. At each epoch view save as image (ctrl + I) OR
  2. Using the process Process > File > Save snapshot (is there any Brainstorm tutorial on this that I can follow?)

Therefore is there a faster way to do this rather than this manual way?
Many thanks in advance.

If you are interested in using the Brainstorm viewer for sleep staging:

To make screen captures with all the properties you need, you can write a script that scans all the file:

% Open viewer
hFig = view_timeseries(RawFile);

% Get file duration
DataMat = in_bst_data(RawFile);
WinLength = 30;
nWin = floor(DataMat.Time(end) / WinLength);

% Get figure references
[~, iFig, iDS] = bst_figures('GetFigure', hFig);
% Configure window
figure_timeseries('SetDisplayMode', hFig, 'column');
figure_timeseries('SetResolution', iDS, iFig, 400, 50);  % Note that the time resolution depends on the size of the window
% panel_record('SetTimeLength', WinLength);  % Useless if defining resolution 

% Loop on windows
for iWin = 1:nWin
    startTime = (iWin - 1) * WinLength;
    panel_record('SetStartTime', startTime);
    imgDir = './';
    out_figure_image(hFig, bst_fullfile(imgDir, sprintf('img_%04d.jpg', iWin)));
    % YOu can use different file formats: TIF, PNG, GIF...

% Close window

Thank you Francois for this suggestion,

Now I am trying to use this code to develop a new Brainstorm Process so that I can just drop the Raw file into Process 1 box and run the process to export the images that I want. Your code reads like it would go under the ====RUN==== section within the example custom_process.

What are your suggestions for implementing your code into a Brainstorm Process?
Thank you so much!

Yes, this code could be embedded in a process easily:

If you work to extend the configuration options of this process, please open a pull request to update the code on this repository.

This works very well Francois - thank you very much.
I was in the process of developing a process myself - so thank you for doing this.

One small comment though, currently, my window is set to a time resolution of 61.1 and amplitude resolution of 7.5758 (these have to be these specific values for my work). This is shown by the figure below.

Unfortunately, once you input the same values into the process (shown below), it just rounds them up or down to the nearest integer. How would I edit the script so that it does not round things up or down?

Increase the "precision" for options xres and yres.

Example with 4 digits after the comma:
sProcess.options.xres.Value = {400, 'pixels/second', 4};

Reference documentation:

Thanks for that Francois. I've made the below adjustment as per your suggestion.

Unfortunately, even with the change, I can only specify integer numbers.
I can write the numbers with decimals but as soon as I go to the next box it reverts back to the original integer number and correspondingly, the output screen captures are therefore incorrect as well.

Any ideas on how to fix this?

And finally, how would you modify the already existing script so that I can specify the two time points for the process to save the screenshots? I.e. save the screenshots from 600 to 1800 seconds.

You have previously saved values for these options, which include the precision.
In the pipeline editor window, menu "Pipeline" (last button in the toolbar) > Reset options.

Ah of course - thank you very much.

How would you modify the already existing script so that I can specify two time points for the process to save the screenshots? I.e. save the screenshots from 600 to 1800 seconds.

Instead of looping over all the windows, use only 2 time points...
If you are not familiar with Matlab programming, then you may want to start with some basic training.

Thanks for the suggestion Francois.
I am using the process_average_time.m as a basis to modify this process_screencapture_ts so that it uses 2 time points.

I've added the following two sections from the process_average_time.m to the appropriate places in process_screencapture_ts.


And currently, it looks like the below.

Interestingly, whenever I run the process I get the following error report.
Initially, I thought this was due to the fact that the field name "TimeVector" was never referred to in the code before this point. However, I realised that this is not the reason after referring back to process_average_time.m. Therefore something else could be contributing to this error.

What do you think?

The field TimeVector doesn't exist in variable sInputs:

By the way, sInputs is a an array so even if the field existed, sInputs.TimeVector would return multiple values and would crash in this context.

To get started with Brainstorm scripting, see this tutorial: