-
Notifications
You must be signed in to change notification settings - Fork 710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
load discontinuous Neuralynx data as epochs #1155
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,17 +157,42 @@ | |
end | ||
end % if ftype_ncs | ||
|
||
% take into account the pause/stop feature + epoch data if discontinuous recording | ||
ncs = read_neuralynx_ncs(fname{1}); | ||
idxPauses = find(ncs.NumValidSamp < recordsize); | ||
nValidSamples = ncs.NumValidSamp(idxPauses); | ||
nPauses = length(idxPauses); | ||
isPause = false(nPauses, 1); | ||
for i = 1:nPauses | ||
if idxPauses(i) == ncs.NRecords | ||
isPause(i) = true; | ||
else | ||
timeGap = double(diff(ncs.TimeStamp(idxPauses(i):idxPauses(i)+1))); | ||
if timeGap > 3 * mode(diff(ncs.TimeStamp)) % threshold to be tuned | ||
isPause(i) = true; | ||
end | ||
end | ||
end | ||
idxPauses = idxPauses(isPause); | ||
nValidSamples = nValidSamples(isPause); | ||
nTrials = length(idxPauses); | ||
trl = zeros(nTrials, 3); | ||
trl(:, 2) = ((idxPauses-1)*recordsize + nValidSamples)'; | ||
trl(1, 1) = 1; | ||
trl(2:end, 1) = (idxPauses(1:end-1)*recordsize+1)'; | ||
|
||
% construct the header that applies to all channels combined | ||
hdr.nChans = length(label); | ||
hdr.label = label; | ||
hdr.filename = fname; | ||
hdr.nTrials = 1; % it is continuous | ||
hdr.nTrials = nTrials; | ||
hdr.Fs = SamplingFrequency(1); | ||
hdr.nSamplesPre = 0; % it is continuous | ||
|
||
hdr.nSamplesPre = 0; | ||
hdr.trl = trl; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you cannot add fields to the hdr structure. |
||
|
||
if ~isempty(ftype_ncs) | ||
% these elements are only relevant for continuously sampled channels | ||
hdr.nSamples = NRecords(1) * 512; | ||
hdr.nSamples = NRecords(1) * recordsize; | ||
hdr.FirstTimeStamp = FirstTimeStamp(1); | ||
hdr.LastTimeStamp = LastTimeStamp(1); | ||
hdr.TimeStampPerSample = TimeStampPerSample(1); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -415,11 +415,15 @@ | |
trl(1,2) = hdr.nSamples*hdr.nTrials; | ||
trl(1,3) = -hdr.nSamplesPre; | ||
else | ||
trl = zeros(hdr.nTrials, 3); | ||
for i=1:hdr.nTrials | ||
trl(i,1) = (i-1)*hdr.nSamples + 1; | ||
trl(i,2) = (i )*hdr.nSamples ; | ||
trl(i,3) = -hdr.nSamplesPre; | ||
if isfield(hdr, 'trl') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you cannot suddenly add a field to the output of ft_read_header and start using that here. That breaks the API, which is shared with EEGLAB, SPM and other software. See http://www.fieldtriptoolbox.org/development/module/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok good to know. Thanks to your suggestion I created ft_trialfun_neuralynx.m, and besides the fact that I now have to add a few lines before my call to ft_preprocessing, this is indeed way cleaner! |
||
trl = hdr.trl; | ||
else | ||
trl = zeros(hdr.nTrials, 3); | ||
for i=1:hdr.nTrials | ||
trl(i,1) = (i-1)*hdr.nSamples+1; | ||
trl(i,2) = (i)*hdr.nSamples; | ||
trl(i,3) = -hdr.nSamplesPre; | ||
end | ||
end | ||
end | ||
cfg.trl = trl; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
function trl = ft_trialfun_neuralynx(cfg) | ||
|
||
% FT_TRIALFUN_NEURALYNX outputs trials for Neuralynx datasets. | ||
% | ||
% Use as | ||
% cfg = []; | ||
% cfg.dataset = 'directory_with_ncs_files'; | ||
% cfg.trialfun = 'ft_trialfun_neuralynx'; | ||
% cfg = ft_definetrial(cfg); | ||
% data = ft_preprocessing(cfg); | ||
% | ||
% A Neuralynx dataset consists of a directory containing as many .ncs files | ||
% as electrodes. The option cfg.dataset should refer to this directory. | ||
% | ||
% Neuralynx data is saved as a series of 512-sample blocks (or "records"). | ||
% When user presses pause or stop buttons, the recording stops at sample i | ||
% from the current record, and the remaining samples (i+1 to 512) are a | ||
% copy of samples i+1 to 512 of the previous record. These copied samples | ||
% are marked as invalid in ncs.NumValidSamples, and have to be discarded as | ||
% they are not genuine samples and as they introduce redundancies. | ||
% | ||
% Contact ludovic.bellier@berkeley.edu for any questions. | ||
|
||
|
||
gapThreshold = 3; % in number of records (e.g., at fs=8kHz, 3*512 samples represents 192 ms) | ||
recordsize = 512; | ||
|
||
dirname = cfg.dataset; | ||
if ~any(strcmp(dirname(end), {'/', '\'})) | ||
dirname = [dirname filesep]; | ||
end | ||
|
||
ls = dir([dirname '*ncs']); | ||
fname = cellfun(@(x) fullfile(dirname, x), {ls(:).name}, 'un', 0); | ||
|
||
ncs = read_neuralynx_ncs(fname{1}); | ||
idxPauses = find(ncs.NumValidSamp < recordsize); | ||
nValidSamples = ncs.NumValidSamp(idxPauses); | ||
nPauses = length(idxPauses); | ||
isPause = false(nPauses, 1); | ||
for i = 1:nPauses | ||
if idxPauses(i) == ncs.NRecords | ||
isPause(i) = true; | ||
else | ||
timeGap = double(diff(ncs.TimeStamp(idxPauses(i):idxPauses(i)+1))); | ||
if timeGap > gapThreshold * mode(diff(ncs.TimeStamp)) | ||
isPause(i) = true; | ||
end | ||
end | ||
end | ||
idxPauses = idxPauses(isPause); | ||
nValidSamples = nValidSamples(isPause); | ||
nTrials = length(idxPauses); | ||
trl = zeros(nTrials, 3); | ||
trl(:, 2) = ((idxPauses-1)*recordsize + nValidSamples)'; | ||
trl(1, 1) = 1; | ||
trl(2:end, 1) = (idxPauses(1:end-1)*recordsize+1)'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is fine if all trials have the same length.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please note that hdr.nSamples*hdr.nTrials should correspond to the total number of samples in the file.