= Tutorial 12: Artifact detection = ''Authors: Francois Tadel, Elizabeth Bock, Sylvain Baillet'' The previous tutorial illustrated how to remove noise patterns occurring continuously and at specific frequencies. However, most of the events that contaminate the MEG/EEG recordings are not permanent, span over a large frequency range or overlap with the frequencies of the brain signals of interest. Frequency filters are not appropriate to correct for eye movements, breathing movements, heartbeats or other muscle activity. For getting rid of reproducible artifacts, one popular approach is the Signal-Space Projection (SSP). This method is based on the spatial decomposition of the MEG/EEG recordings for a selection of time samples during which the artifact is present. Therefore we need to identify when each type of artifact is occurring in the recordings. This tutorial shows how to detect automatically some well defined artifacts: the blinks and the heartbeats. <> == Observation == Let's start by observing the type of contamination the blinks and heartbeats cause to the MEG recordings. * '''Run #01''': Double-click on the link to show the MEG sensors. * '''Configuration''': Page of '''3 seconds''', view in columns, selection of the "'''CTF LT'''" sensors (the left-temporal sensors will be a good example to show at the same time the two types of artifacts). * '''EOG''': Right-click on the link > EOG > Display time series. Two channels are classified as EOG: * '''VEOG''': Vertical electrooculogram (two electrodes placed below and above one eye) * '''HEOG''': Horizontal electrooculogram (two electrodes placed on the temples of the subject) * On those traces, there is not much happening for most of the recordings except for a few bumps. This subject is sitting very still and not blinking much. We can expect MEG recordings of a very good quality. * '''ECG''': Right-click on the link > ECG > Display time series.<
>The electrocardiogram was recorded with a bipolar montage of electrodes across the chest. You can recognize the typical shape of the electric activity of the heart (P, QRS and T waves). * '''Find a blink''': Scroll through the recordings using the '''F3''' shortcut until you see a large blink. * Remember you can change the amplitude scale with many shortcuts (eg. right-click + move). * To keep the scale fixed between two pages: Uncheck the button [AS] (auto-scale). * For instance, you can observe a nice blink at 20.8s (red cursor in the screen capture below). * On the same page, you should be able to observe the contamination due to a few heartbeats, corresponding to the peaks of the ECG signal (eg. 19.8s, shown as a blue selection below). * The additional data channels (ECG and EOG) contain precious information that we can use for the automatic detection of the blinks and heartbeats. We strongly recommend that you always record these signals during your own experiments, it helps a lot with the data pre-processing. <
><
> {{attachment:observe.gif||height="271",width="662"}} == Detection: Heartbeats == In the Record tab, select the menu: '''"Artifacts > Detect heartbeats"'''. * It opens automatically the pipeline editor, with the process "Detect heartbeats" selected. * '''Channel name''': Name of the channel that is used to perform the detection. Select "'''ECG'''". * '''Time window''': Time range that the algorithm should scan for amplitude peaks. Leave the default values to process the entire file, or check the option '''[All file]'''. * '''Event name''': Name of the event group created for saving the detected events. Enter "'''cardiac'''".<
><
> {{attachment:detect_ecg.gif||height="254",width="549"}} * Click on Run. After the process stops, you can see a new event category "'''cardiac'''". The 464 heartbeats for 360s of recordings indicate an average heart rate of 77bpm, everything looks normal. * You can check a few of them, to make sure the "cardiac" markers really indicate the ECG peaks, and that there are not too many peaks that are skipped.<
><
> {{attachment:detect_ecg_done.gif||height="127",width="517"}} == Detection: Blinks == Now do the same thing for the blinks: Menu "'''Artifacts > Detect eye blinks'''". * Channel name: '''VEOG''' * Time window: '''All file''' * Event name: '''Blink''' <
><
> {{attachment:detect_eog.gif||height="257",width="550"}} * Run, then look quickly at the 15 detected blinks (shortcut: Shift+Right arrow). <
><
> {{attachment:detect_eog_done.gif||height="130",width="535"}} == Remove simultaneous blinks/heartbeats == We will use these event markers as the input to our SSP cleaning method. This technique works well if each artifact is defined as precisely as possible, and as independently as possible from the other artifacts. This means that we should try to avoid having two different artifacts marked at the same time. If the two artifacts happen simulateneously, the SSP projectors calculated for the blink may contain some of the heartbeat and vice versa. When trying to remove the second artifact, we might not be able to isolate it clearly anymore. Because the heart beats every second or so, there is a high chance that when the subject blinks there is a heartbeat not too far away in the recordings. Therefore a significant number of the blinks will be contaminated with heartbeats. To isolate correctly these two common artifacts, we recommend the following procedure (this will be explained again in the next tutorial): * Remove the markers "cardiac" that are occurring during a blink. * Compute the cardiac SSP (with no eye movements). * Compute the blink SSP (with no heartbeats, because they've already been taken care of). In the Record tab, select the menu "'''Artifacts > Remove ''''''simultaneous'''". Set the options: * Remove events named: "'''cardiac'''" * When too close to events: "'''blink'''" * Minimum delay between events: '''250ms''' <
><
> {{attachment:detect_simult.gif||height="257",width="550"}} After executing this process, the number of "cardiac" events goes from '''464''' to '''455'''. The deleted heartbeats were all less than 250ms away from a blink. == Run #02: Running from a script == Let's perform the same detection operations on '''Run #02''', using this time the '''Process1''' box. * Close everything with the [X] button at the top-right corner of the Brainstorm window. * Select the run '''AEF #02''' in the Process1 box, then select the following processes: * '''Events > Detect heartbeats:''' Select channel '''ECG''', check "All file", event name "cardiac". * '''Events > Detect eye blinks:''' Select channel '''VEOG''', check "All file", event name "blink". * '''Events > Remove simultaneous''': Remove "'''cardiac'''", too close to "'''blink'''", delay '''250ms'''.<
><
> {{attachment:detect_script.gif||height="286",width="506"}} * Open the Run#02 recordings (MEG+EOG+ECG) and verify that the detection worked as expected. You should get '''472 cardiac''' events and '''19 blink''' events. <> == Custom detection == Those two processes "Detect heartbeats" and "Detect eye blinks" are in reality shortcuts for a generic process "'''Detect custom events'''". This process can be used for detecting any kind of event based on the signal power in a specific frequency band. We are not going to use it here, but you may have to use it if the standard parameters do not work well, or for detecting other types of events. * The signal to analyze is read from the continuous file (options "Channel name" and "Time window"). * '''Frequency band''': The signal is filtered in a frequency band where the artifact is easy to detect. For EOG: 1.5-15Hz ; for ECG: 10-40Hz. * '''Threshold''': An event of interest is detected if the absolute value of the filtered signal value goes over a given number of times the standard deviation. For EOG: 2xStd, for ECG: 4xStd * '''Minimum duration between two events''': If the filtered signal crosses the threshold several times in relation with the same artifact (like it would be the case for muscular activity recordings on an EMG channel), we don't want to trigger several events but just one at the beginning of the activity. This parameter would indicate the algorithm to take only the maximum value over the given time window; it also prevents from detecting other events immediately after a successful detection. For the ECG, this value is set to 500ms, because it is very unlikely that the heart rate of the subject goes over 120 beats per minute. * '''Ignore the noisy segments''': If this option is selected, the detection is not performed on the segments that are much noisier than the rest of the recordings. * '''Enable classification''': If this option is selected, the events are classified by shape in different categories, based on correlation measure. In the end, only the categories that have more than 5 occurrences are kept, all the other successful detections are ignored. <
><
> {{attachment:detect_custom.gif||height="435",width="313"}} <> == In case of failure == If the signals are not as clean as in this sample dataset, the automatic detection of the heartbeats and blinks may fail with the standard parameters. You may have to use the process "Detect custom events" and adjust some parameters. For instance: * If nothing is detected: decrease the amplitude threshold, or try to adjust the frequency band. * If too many events are detected: increase the amplitude threshold or the minimum duration between two events. * If too many categories of events are generated, and you have a very little number of events in the end: disable the classification. * To find the optimal frequency band for an artifact, you can open the recordings and play with the online band-pass filters in the Filter tab. Keep the band that shows the highest amplitude peaks. If you cannot get your artifacts to be detected automatically, you can browse through the recordings and mark all the artifacts manually, as explained in the tutorial [[Tutorials/EventMarkers|Event markers]]. == Additional documentation == * Related tutorial: [[Tutorials/MovementDetect|Detect subject movements]] <> <>