Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions shared/io/ProcessConfig2.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function [cfg_out] = ProcessConfig2(cfg_default,cfg_in)
%% PROCESSCONFIG2
% Processes the cfg file and the cfg_in function defaults as ProcessConfig does but as a
% function.
%
% INPUTS:
% cfg_default: default parameters stored as a struct within the containing function
% cfg_in: input parameters from the input cfg file
%
% OUTPUT:
% Returns a new cfg file containing the default parameters and any updated cfg
% parameters given the input file.
%
% youkitan 2014-11-04
%
%% Set output cfg to default cfg

cfg_out = cfg_default;

%% Process input cfg parameters into workspace
% NOTE - The two processes (replace and add) are separated only for clarity
% and ease of debugging. It can easily be compressed to half the amount of code (and
% therefore time).

if ~isempty(cfg_in)

% If there are default parameters,replace them with cfg_in input
if ~isempty(cfg_default)
default_F = fieldnames(cfg_default);
numF = length(default_F);

for i = 1:numF
idF = default_F{i};
if isfield(cfg_in,idF)
cfg_out.(idF) = cfg_in.(idF);
end %set cfg_out fields
end %iterate default cfg fields
end

% Add input parameters
new_F = fieldnames(cfg_in);
numF = length(new_F);

for i = 1:numF
inF = new_F{i};
if ~isfield(cfg_default,inF)
cfg_out.(inF) = cfg_in.(inF);
end %set cfg_out fields
end %iterate extra cfg fields

end
73 changes: 73 additions & 0 deletions shared/io/datatypes/iv/AddNActiveCellsIV.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
function iv_in = AddNActiveCellsIV(cfg_in,iv_in,S)
% function iv_out = AddNActiveCellsIV(cfg_in,iv_in,S)
%
% INPUT:
% cfg_in - input cfg parameters
% iv_in - input iv
% S - spikes
%
% OUTPUT:
% iv_out - iv with nActiveCells field added
%
% CFG OPTIONS:
%
% cfg_def.label = 'nActiveCells'; What to call the field that
% contains this data.
% cfg_def.dt = 0.001; % bin size for binning spikes
%
% mvdm 2015-02-01 initial version
% note: uses binning method for speed, i.e. is not exact

% parse cfg parameters
cfg_def = [];
cfg_def.label = 'nActiveCells';
cfg_def.dt = 0.005;
cfg_def.verbose = 1;

mfun = mfilename;
cfg = ProcessConfig(cfg_def,cfg_in,mfun);


% check inputs
if ~CheckIV(iv_in)
error('Interval data must have been made with the iv constructor')
end

if isempty(iv_in.tstart)
error('Interval data is empty')
end

if cfg.verbose; disp([mfun,': Adding the number of active units during each interval']); end

% make matrix of spike counts (nCells x nTimeBins)
cfg_temp.dt = cfg.dt;
cfg_temp.verbose = 0;
Q = MakeQfromS(cfg_temp,S);

% get number of active cells for each
for iIV = length(iv_in.tstart):-1:1

Qr = restrict(Q,iv_in.tstart(iIV),iv_in.tend(iIV));

if ~isempty(Qr.tvec)
spk_counts = sum(Qr.data,2);
nC(iIV) = sum(spk_counts > 0);
else % no spikes in this iv
nC(iIV) = 0;
end

end

if ~isfield(iv_in,'usr')
iv_in.usr = [];
end

if cfg.verbose && isfield(iv_in.usr,cfg.label)
disp(['WARNING in ',mfun,': iv data already includes usr.',cfg.label,', overwriting...'])
end
iv_in.usr.(cfg.label) = nC;


% housekeeping
iv_in.cfg.history.mfun = cat(1,iv_in.cfg.history.mfun,mfun);
iv_in.cfg.history.cfg = cat(1,iv_in.cfg.history.cfg,{cfg});
61 changes: 61 additions & 0 deletions shared/io/datatypes/iv/AddTSDtoIV.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
function iv = AddTSDtoIV(cfg_in,iv,tsd_in)
% function iv_out = AddTSDtoIV(cfg_in,iv_in,tsd_in)
%
% add usr field to iv based on tsd
%
% INPUTS:
%
% iv1: interval data to be selected from based on..
% iv2: interval data to be used to select from iv1
%
% CFG OPTIONS:
% cfg.method = 'max'; 'min', 'mean'
% cfg.target = []; % which label in tsd to use
% cfg.label = []; % what to call this in iv, i.e. usr.label
%
% OUTPUTS:
%
% iv_out: output interval data
%
% MvdM 2014-06-24

cfg_def.method = 'max';

cfg = ProcessConfig(cfg_def,cfg_in); % should take whatever is in cfg_in and put it into cfg!
mfun = mfilename;

% check for well formed input
nData = size(tsd_in.data,1);
if nData == 1
data_in = tsd_in.data;
else
if isempty(cfg.target)
error('Multiple data dimensions present, must specify cfg.target.');
else
data_in = getd(tsd_in,cfg.target);
end
end

% find indices for iv
tstart_idx = nearest_idx3(iv.tstart,tsd_in.tvec);
tend_idx = nearest_idx3(iv.tend,tsd_in.tvec);

% collect data
data_temp = nan(size(tstart_idx));

switch cfg.method
case 'max'
for iI = 1:length(data_temp)
data_temp(iI) = max(data_in(tstart_idx(iI):tend_idx(iI)));
end
case 'mean'
for iI = 1:length(data_temp)
data_temp(iI) = mean(data_in(tstart_idx(iI):tend_idx(iI)));
end
end

iv.usr.(cfg.label) = data_temp;

% housekeeping
iv.cfg.history.mfun = cat(1,iv.cfg.history.mfun,mfun);
iv.cfg.history.cfg = cat(1,iv.cfg.history.cfg,{cfg});
50 changes: 50 additions & 0 deletions shared/io/datatypes/iv/CheckIV.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
function pass_flag = CheckIV(iv_in,varargin)
%% CHECKIV Check IV for datatype violations
% pass_flag = CheckIV(iv_in,varargin) verifies that input is iv and is well formed.
%
% INPUTS:
% iv_in: iv to be checked
% varargins:
% callername: name of invoking function (for more informative warning messages)
%
% OUTPUTS:
% pass_flag: 1 if all checks pass, 0 if otherwise
%
% Checks performed:
% - .tstart and .tend fields must exist (FAIL)
% - are either .tstart or .tend empty? (WARNING)
% - both .tstart and .tend must be column vectors (FAIL)
% - are start-end pairs unidirectional? (WARNING)
%
% see also iv, CheckTS, CheckTSD, CheckTC
%
% MvdM 2014-11-12
% aacarey edit Sept 2015, additional checks
% youkitan edit Sept 2016, additional checks
% youkitan edit Dec 2016, reformat help, add function name to output

pass_flag = 1;

in_mfun = '';
if ~isempty(varargin) && ischar(varargin{1})
in_mfun = [' in ',varargin{1}];
end

if isstruct(iv_in)
if ~isfield(iv_in,'tstart') || ~isfield(iv_in,'tend')
pass_flag = 0;
fprintf('FAIL%s by CheckIV: input iv must contain tstart and tend fields.\n',in_mfun);
elseif isempty(iv_in.tstart) || isempty(iv_in.tend)
fprintf('WARNING%s by CheckIV: input iv is empty.\n',in_mfun);
elseif ~iscolumn(iv_in.tstart) || ~iscolumn(iv_in.tend)
pass_flag = 0;
fprintf('FAIL%s by CheckIV: tstart and tend must be column vectors.\n',in_mfun);
elseif any(iv_in.tstart > iv_in.tend)
fprintf('WARNING%s by CheckIV: start-end pairs not unidirectional.\n',in_mfun);
end
else
pass_flag = 0;
fprintf('FAIL%s by CheckIV: input must be an iv data type.\n',in_mfun);
end


62 changes: 62 additions & 0 deletions shared/io/datatypes/iv/DifferenceIV.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
function iv_out = DifferenceIV(cfg_in,iv1,iv2)
% function iv_out = DifferenceIV(cfg,iv1,iv2)
%
% keep only those iv1's that DO NOT include a piece of iv2
%
% iv1 ____________ _____ ______ ____________ ______
% iv2 ________ _____ _______ ______
%
% iv_out ______ ______
%
% INPUTS:
%
% iv1: interval data to be selected from based on..
% iv2: interval data to be used to select from iv1
%
% CFG OPTIONS:
% cfg.verbose = 1; If 1, displays text in command window; if 0, doesn't.
%
% OUTPUTS:
%
% iv_out: output interval data
%
% MvdM 2014-06-28

cfg_def.verbose = 1;
mfun = mfilename;

cfg = ProcessConfig(cfg_def,cfg_in,mfun); % should take whatever is in cfg_in and put it into cfg!

if ~CheckIV(iv1) || ~CheckIV(iv2); error('Inputs must be iv data type'); end

keep = ones(length(iv1.tstart),1);
for iI = 1:length(iv1.tstart)

% first, check if any start or end is within this interval
temp1 = find(iv2.tstart >= iv1.tstart(iI) & iv2.tstart <= iv1.tend(iI));
temp2 = find(iv2.tend >= iv1.tstart(iI) & iv2.tend <= iv1.tend(iI));

if ~isempty(temp1) | ~isempty(temp2)
keep(iI) = 0;
continue;
end

% check if interval is enveloped by anything
for iJ = 1:length(iv2.tstart)
if iv2.tstart(iJ) < iv1.tstart(iI) & iv2.tend(iJ) > iv1.tend(iI)
keep(iI) = 0;
break;
end
end

end

iv_out = iv1;
iv_out.tstart = iv_out.tstart(logical(keep));
iv_out.tend = iv_out.tend(logical(keep));

if cfg.verbose; fprintf('%s: %d intervals in, %d intervals out\n',mfun,length(iv1.tstart) + length(iv2.tstart),length(iv_out.tstart)); end

% housekeeping
iv1.cfg.history.mfun = cat(1,iv1.cfg.history.mfun,mfun);
iv1.cfg.history.cfg = cat(1,iv1.cfg.history.cfg,{cfg});
8 changes: 8 additions & 0 deletions shared/io/datatypes/iv/IVcenters.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function c = IVcenters(iv_in)
% returns nIV x 1 vector with center times of iv_in

if ~CheckIV(iv_in)
return;
end

c = nanmean(cat(2,iv_in.tstart,iv_in.tend),2);
51 changes: 51 additions & 0 deletions shared/io/datatypes/iv/IntersectIV.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function iv1 = IntersectIV(cfg_in,iv1,iv2)
% function iv = IntersectIV(cfg,iv1,iv2)
%
% keep only those iv1's that include a piece of iv2
%
% INPUTS:
%
% iv1: interval data to be selected from based on..
% iv2: interval data to be used to select from iv1
%
% CFG OPTIONS:
%
% OUTPUTS:
%
% iv_out: output interval data
%
% MvdM 2014-06-24

cfg_def = [];

mfun = mfilename;
cfg = ProcessConfig(cfg_def,cfg_in,mfun); % should take whatever is in cfg_in and put it into cfg!

keep = zeros(length(iv1.tstart),1);
for iI = 1:length(iv1.tstart)

% first, check if any start or end is within this interval
temp1 = find(iv2.tstart > iv1.tstart(iI) & iv2.tstart < iv1.tend(iI));
temp2 = find(iv2.tend > iv1.tstart(iI) & iv2.tend < iv1.tend(iI));

if ~isempty(temp1) || ~isempty(temp2)
keep(iI) = 1;
continue;
end

% check if interval is enveloped by anything
for iJ = 1:length(iv2.tstart)
if iv2.tstart(iJ) < iv1.tstart(iI) && iv2.tend(iJ) > iv1.tend(iI)
keep(iI) = 1;
break;
end
end

end

iv1.tstart = iv1.tstart(logical(keep));
iv1.tend = iv1.tend(logical(keep));

% housekeeping
iv1.cfg.history.mfun = cat(1,iv1.cfg.history.mfun,mfun);
iv1.cfg.history.cfg = cat(1,iv1.cfg.history.cfg,{cfg});
11 changes: 11 additions & 0 deletions shared/io/datatypes/iv/InvertIV.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function iv_out = InvertIV(iv_in,t0,t1)
% function iv_out = InvertIV(iv_in,t0,t1)
%
% "flips" included intervals to non-included, and vice versa
%
% MvdM 2016-04-27

iv_out = iv;
iv_out.tstart = cat(1,t0,iv_in.tend);
iv_out.tend = cat(1,iv_in.tstart,t1);

Loading