usingNeuroAnalyzerimportCairoMakie# Quarto sets a default figure size (672 × 480 px), which overrides the sizes of# NeuroAnalyzer figures: remove itdelete!(CairoMakie.Makie.CURRENT_DEFAULT_THEME, :resolution)eeg =import_edf("files/eeg/eeg.edf")
This tutorial shows the basic operations on NEURO objects: copying, notes, channels, signal segments and epochs, and creating objects from scratch (for markers, see Markers). The structure of the object is described in NEURO objects.
Most editing functions have two variants: delete_channel() returns a modified copy and leaves the input unchanged, while delete_channel!() (a mutator, with !) modifies the object in place:
# new object, eeg is unchangedeeg_new =delete_channel(eeg; ch ="ECG")# eeg is modifieddelete_channel!(eeg; ch ="ECG")
Mutators save memory, which matters for long or high-density recordings.
To copy an object, use deepcopy():
eeg_copy =deepcopy(eeg)
Tip:eeg_copy = eeg does not copy the object: both names refer to the same object, so changes made through one of them (e.g. with a mutator) are visible through the other.
An object is removed from memory by removing all references to it, e.g. eeg_copy = nothing; the memory is freed by the garbage collector.
Processing history
Each operation is recorded, with its parameters, in the processing history:
data type: eeg
sampling rate: 256 Hz
channels: 24
epochs: 1
signal length: 308480 samples (1205.0 s)
epoch length: 308480 samples (1205.0 s)
data size: (24, 308480, 1)
header(eeg) and info(eeg) print a summary of the object, and NeuroAnalyzer.describe(eeg) basic statistics of each channel.
Channels
Selecting channels
Channels are selected by label, vector of labels, type, "all", "bad" or regular expression (see the Glossary). get_channel() returns the channel numbers of a selection, or the labels of channels of a given type:
All per-channel information (header fields, locations, bad channel flags, MEG sensor geometry, …) is updated. NIRS signal channels are deleted together with their optodes, using delete_optode():
extract_channel() returns the data of one channel (channel × samples × epochs), extract_data() the data of several channels and epochs, optionally with the time points:
s =extract_channel(eeg; ch ="Cz")println("extract_channel: ", size(s))d =extract_data(eeg; ch = ["Fp1", "Fp2"])println("extract_data: ", size(d))d, t =extract_data(eeg; ch ="Fp1", time =true)println("time points: ", t[1:3], " …")
add_channel() appends new channels (data: channels × samples × epochs), with their labels, types and units, e.g. the difference of two channels:
d =extract_channel(eeg; ch ="Fp1") .-extract_channel(eeg; ch ="F7")e=add_channel(eeg; data = d, label ="Fp1-F7", type="eeg", unit ="μV")println(labels(e)[end-2:end])
["EOG2", "ECG", "Fp1-F7"]
Channels computed from formulas are created more conveniently with virtual channels.
Adding a signal to channels
add_signal() adds a signal vector (of the length of one epoch) to the selected channels in all epochs, e.g. to simulate an artifact or a known signal and test whether a processing pipeline detects or removes it:
# 10 Hz sine wave of 10 μV added to O1 and O2tt = eeg.epoch_timee=add_signal(eeg; ch = ["O1", "O2"], s =10.*sin.(2π *10.* tt))d =extract_channel(e; ch ="O1") .-extract_channel(eeg; ch ="O1")println("added to O1: ", round.(extrema(d); digits =2), " μV")
added to O1: (-10.0, 10.0) μV
Signal Segments
Trimming and cropping
trim() removes a segment (start and end in seconds); with keep = true, only the segment is kept. crop() is the same as trim(…; keep = true):
original: 1205.0 s
trim (10, 20): 1194.99609375 s
trim (10, 20), keep: 10.00390625 s
crop (10, 20): 10.00390625 s
trim() and crop() work on continuous objects only; in epoched objects, whole epochs are removed with delete_epoch() (see below).
Joining
join!() appends the signal of another object (with the same channels and sampling rate) to the first one:
part1 =crop(eeg; seg = (0, 10))part2 =crop(eeg; seg = (60, 70))join!(part1, part2)println("joined: ", dur(part1))
joined: 20.0078125 s
Reflecting
reflect() extends the signal at both ends with its mirror image (1234 → 432112344321), and chop!() removes the extension. This reduces edge artifacts of filters and other operations applied to short signals:
r =reflect(eeg; n =sr(eeg))println("reflected: ", dur(r))chop!(r; n =sr(eeg))println("chopped: ", dur(r), ", identical to the original: ", r.data == eeg.data)
reflected: 1207.0 s
chopped: 1205.0 s, identical to the original: true
Epochs
Epoching
Epoching divides a continuous recording into segments (epochs); the data change from channels × samples (1 epoch) to channels × samples × epochs. Epochs can have a fixed length:
or be time-locked to events (markers): marker selects the marker value, offset the time before the event and ep_len the epoch length (in seconds). A face perception experiment, epochs from 200 ms before to 800 ms after the s71 stimuli:
The epoch time axis starts at \(-0.2\) s, so that 0 is the stimulus onset.
Tip: Filtering, ICA and other operations sensitive to the signal length should be applied to the continuous signal, before epoching.
The shorter the epoch, the coarser the frequency resolution of its spectrum (\(\Delta f = 1 / T\), e.g. 0.5 Hz for 2-second and 0.1 Hz for 10-second epochs).
Epoch time axis and sub-epochs
epoch_ts() shifts the epoch time axis (e.g. to set the event onset to 0 for fixed-length epochs) and subepoch() keeps a part of each epoch:
data type: erp, epochs: 101 (average + 100 epochs)
Markers
Markers (events) are stored in obj.markers; viewing, adding, editing and deleting markers, creating markers from an event channel and their timing are described in Markers.
Creating Objects
A NEURO object can be created from data generated or imported by other means. create_object() creates an empty object of the given data type, and create_data!() adds the data (channels × samples × epochs), the sampling rate and the type of the channels, with automatic labels:
Alternatively, add_channel!() adds channels with chosen labels, types and units to an empty object; the time axes are then created with create_time!():