get_data¶
get_data asks AnyWave for data: the channels you want, on the time selections you want, with or without filters.
The same options exist in MATLAB and in Python.
out = anywave('get_data', cfg);
out = anywave.get_data(cfg)
cfg is optional. Without it you get every channel of the current montage, on the whole file:
out = anywave('get_data'); % MATLAB
out = anywave.get_data() # Python
The result¶
MATLAB¶
out is a struct array, one element per time selection. There is only one element, unless you use
split_data.
| field | content |
|---|---|
data |
single matrix, one row per channel, one column per sample |
channels |
struct array (1 x number of channels) describing the rows of data, see below |
position |
start of the selection in seconds (the start of the first chunk when chunks are appended) |
duration |
duration requested in seconds (the sum of the chunks when they are appended) |
out = anywave('get_data');
size(out.data) % [3 2500]: 3 channels, 2500 samples
names = {out.channels.name} % {'Fp1', 'Cz', 'O1'}
cz = out.data(strcmp(names, 'Cz'), :); % samples of channel Cz
Every element of out.channels has these fields:
| field | content |
|---|---|
name |
label of the channel |
ref |
label of the reference channel for a bipolar channel, empty otherwise |
type |
EEG, SEEG, MEG, ECG... |
sr |
sampling rate in Hz |
hp, lp, notch |
filters applied to the data in Hz (hp and lp are -1, notch is 0 when there is no filter) |
unit |
unit of the values: µV for EEG and sEEG, pT for MEG |
Note
If no channel matches your request, out is an empty array: test it with isempty(out).
If the selection starts after the end of the file, out.data is empty.
duration is the duration you asked for, even if the file ends before: use size(out.data, 2) / out.channels(1).sr
to know how many seconds you really got.
Python¶
out is a list of dict, one per time selection (a single one unless you use split_data).
| key | content |
|---|---|
'data' |
numpy array of float32, shape (number of channels, number of samples) |
'channels' |
list of channel objects with the attributes name, ref, type, sr, hp, lp, notch, unit |
'position', 'duration' |
same meaning as in MATLAB |
out = anywave.get_data()
data = out[0]['data'] # shape (3, 2500)
names = [c.name for c in out[0]['channels']] # ['Fp1', 'Cz', 'O1']
An empty list means that no channel matches the request.
Options¶
Every option is a field of cfg (a struct in MATLAB, a dict in Python).
| option | value | effect |
|---|---|---|
channels_source |
'montage' (default), 'raw', 'selection' |
which channels are used, see Selecting channels |
labels |
list of labels | only these channels |
types |
list of types | only the channels of these types |
skip_bad_channels |
true (default) or false |
leave out the channels marked as bad |
start, duration |
seconds | one time selection (default: the whole file) |
data_chunks |
[start1 duration1 start2 duration2 ...] |
several time selections at once |
split_data |
true or false (default) |
one result per chunk instead of one result with the chunks appended |
use_markers |
list of marker labels | only the data covered by these markers |
skip_markers |
list of marker labels | all the data except the parts covered by these markers |
marker_file |
path of a .mrk file | markers read from this file instead of the current one |
filters |
[hp lp] or [hp lp notch] |
filters applied to the data, see Filtering |
raw_data |
true or false (default) |
no filter at all |
downsampling_factor |
integer | divides the sampling rate by this factor |
data_path |
path of a data file | reads this file instead of the one open in AnyWave |
Warning
A list with a single number must be a cell array in MATLAB: jsonencode sends cfg.values = 10 as a plain number,
not as a list. Lists of labels are safe with one element too, cfg.labels = 'Cz' and cfg.labels = {'Cz'} both work.
Selecting channels¶
Without any option¶
The channels are chosen by the flags written in the desc.txt file of your plugin:
input_flags |
GUI mode | command line mode |
|---|---|---|
getcurrentmontage |
channels of the current montage | channels of the current montage |
getasrecordedchannels |
raw channels (the montage is skipped) | raw channels (the montage is skipped) |
modifiers_flags |
GUI mode | command line mode |
|---|---|---|
acceptchannelselection |
the selected channels if there are some, otherwise the current montage | not applicable |
Without any flag in desc.txt, the current montage is used.
With channels_source¶
This overrides the flags of desc.txt. The value is a string.
cfg = [];
cfg.channels_source = 'montage'; % channels of the current montage (default)
cfg.channels_source = 'selection'; % channels selected in AnyWave (GUI mode); the montage if nothing is selected
cfg.channels_source = 'raw'; % channels stored in the file
out = anywave('get_data', cfg);
cfg = {'channels_source': 'raw'} # a string, not a list
out = anywave.get_data(cfg)
The raw channels are the physical channels of the data file. The montage and the virtual channels (ICA components...) are skipped. They are not necessarily in the order of the file.
Pick channels by label¶
cfg = [];
cfg.labels = {'Fp1', 'Cz'};
out = anywave('get_data', cfg); % rows in the order of the labels
out = anywave.get_data({'labels': ['Fp1', 'Cz']})
- The rows follow the order of
labels. - A label that does not exist is ignored. If none exists the result is empty.
- Labels are looked for in the channels of the montage. Bipolar channels (
'A1-A2') must exist in the montage. Do not combinelabelswithchannels_source = 'raw'.
Pick channels by type¶
cfg = [];
cfg.types = {'EEG', 'ECG'};
out = anywave('get_data', cfg);
out = anywave.get_data({'types': ['EEG', 'ECG']})
Channels marked as bad¶
Channels marked as bad in AnyWave are left out. To get them too:
cfg = [];
cfg.skip_bad_channels = false;
out = anywave('get_data', cfg);
Selecting time¶
One time selection¶
cfg = [];
cfg.start = 1; % seconds from the beginning of the file
cfg.duration = 10; % seconds
out = anywave('get_data', cfg);
out = anywave.get_data({'start': 1, 'duration': 10})
With start only, you get the data from start to the end of the file.
Several chunks appended¶
data_chunks is a list of start, duration pairs. The chunks are appended in the same matrix.
cfg = [];
cfg.data_chunks = [1 10 2 4]; % 10 s from 1 s, then 4 s from 2 s
out = anywave('get_data', cfg); % (10 + 4) * sampling rate samples, if the file is long enough
out = anywave.get_data({'data_chunks': [1, 10, 2, 4]})
An odd number of values raises the error data_chunks must contain even number of elements.
Get one result per chunk: split_data¶
Add split_data to get one element per chunk. This is what you want to process every chunk on its own.
cfg = [];
cfg.data_chunks = [1 2 5 1];
cfg.split_data = true;
out = anywave('get_data', cfg);
% out(1).data is the first chunk, out(2).data the second one.
% out(1).position is 1, out(2).position is 5.
for k = 1:numel(out)
fprintf('chunk %d: %d samples\n', k, size(out(k).data, 2));
end
out = anywave.get_data({'data_chunks': [1, 2, 5, 1], 'split_data': True})
for k, chunk in enumerate(out):
print(k, chunk['data'].shape, chunk['position'])
Data marked by markers¶
use_markers gives the data covered by the markers with these labels (their position and duration).
cfg = [];
cfg.use_markers = {'section2', 'h2'};
out = anywave('get_data', cfg); % the chunks are appended
cfg.split_data = true;
out = anywave('get_data', cfg); % one element per marker
out = anywave.get_data({'use_markers': ['section2', 'h2'], 'split_data': True})
Warning
If none of the markers exists, AnyWave falls back to start and duration, which means the whole file by default.
Check the markers with get_markers when this matters.
All the data but the artifacts¶
skip_markers removes the parts covered by the markers with these labels:
cfg = [];
cfg.skip_markers = {'artefact', 'bad'};
out = anywave('get_data', cfg);
out = anywave.get_data({'skip_markers': ['artefact', 'bad']})
Marked data without the artifacts¶
Both options can be combined: the data of use_markers minus the parts of skip_markers.
cfg = [];
cfg.use_markers = {'section2', 'h2'};
cfg.skip_markers = {'artefact', 'bad'};
out = anywave('get_data', cfg);
The markers come from the current file. marker_file reads them from another .mrk file.
Filtering¶
By default AnyWave applies to the data the filters currently set on the channels (the hp, lp and notch fields of
out.channels tell what was applied; -1 means no filter). Three ways to change that:
cfg = [];
cfg.filters = [1 40]; % 1 Hz high pass, 40 Hz low pass
cfg.filters = [1 40 50]; % + 50 Hz notch
cfg.filters = [0 0 50]; % 50 Hz notch only (0: not used)
cfg.raw_data = true; % no filter at all, even the ones of AnyWave
out = anywave('get_data', cfg);
out = anywave.get_data({'filters': [1, 40]})
out = anywave.get_data({'raw_data': True})
filtersneeds at least two values ([hp lp]), the third one (notch) is optional.raw_data = truewins over everything:filtersis ignored when both are given.- change_sig_prop changes the filters of channels in AnyWave: a following
get_datareturns data filtered with the new settings.
Downsampling¶
cfg = [];
cfg.downsampling_factor = 2; % 250 Hz -> 125 Hz
out = anywave('get_data', cfg); % half of the samples
sr = out.channels(1).sr / cfg.downsampling_factor; % 125 Hz
Note
out.channels(i).sr still shows the sampling rate of the file. The real sampling rate of the returned data is
sr / downsampling_factor.
Read another file¶
data_path reads a data file without changing the file open in AnyWave. It also works when no file is open.
cfg = [];
cfg.data_path = '/data/subject1/recording.edf';
cfg.raw_data = true;
out = anywave('get_data', cfg);
The file is opened for this request only and closed again when the answer is sent. The returned channels, sampling rate
and duration are those of that file, not those of the file open in AnyWave. The other options (labels, types,
start, duration, data_chunks...) apply to it as usual:
cfg = [];
cfg.data_path = '/data/subject1/recording.edf';
cfg.labels = {'A2'};
cfg.start = 1;
cfg.duration = 2;
out = anywave('get_data', cfg);
The file open in AnyWave, its montage and its channel properties are not touched: a following anywave('get_data')
without data_path still returns the data of the open file. A file AnyWave cannot open raises a regular MATLAB error
(ERROR: Unable to open file <path>.), the plugin can go on with try / catch.
Errors¶
| message | cause |
|---|---|
get_data: bad arguments. Expected struct. |
cfg is not a struct (for instance cfg = [] with no field set) |
data_chunks must contain even number of elements. |
data_chunks has an odd number of values |
ERROR: no file open in AnyWave... |
no file is open in AnyWave and data_path is not set |
ERROR: Unable to open file <path>. |
data_path points to a file that does not exist or that AnyWave cannot read |
Error while sending data to AnyWave. |
AnyWave is not running any more (see the anywave() function) |
An empty cfg (struct()) is not accepted: call anywave('get_data') without argument instead.