%classify DS into DSL and DSM
%2021 Dino Dvorak, New York University, neuraldino@gmail.com
%Dentate spikes and external control of hippocampal function,
%Cell Reports, Volume 36, Issue 5, 2021,109497,ISSN 2211-1247,
%https://doi.org/10.1016/j.celrep.2021.109497

clear; close all; clc;

%directories
drCSD = 'DS_CSD/';
drDS = 'DS/';
drOUT = 'DS_TYPE12/';

%input LFP file (channels x samples)
fnEEG = '2016_09_30_PP12_334_PRETRAIN_BASE_eeg.mat';

%sink regions identified from CSD
sinks = [100,119,175,192];

wLenSearch = 8; %window to search around in CSD profile for minima

%load data
load([drCSD fnEEG]);
load([drDS fnEEG]);

%labelsFeat = {'sample','promZ','peakZ','peakRaw','promZbefore','promZafter',... %1..6
%    'peakZbefore','peakZafter','minZbefore','minZafter',...%7..10
%    'wDG','wBA','w50','tB','tA','tminB','tminA','acc','speed','max10_30before','max10_30after'}; %11..21

%get features
s = feat(:,1); %sample
wDG = feat(:,11); %width

dB = feat(:,3) - feat(:,9); %distance to min before
dA = feat(:,3) - feat(:,10); %distance to min before

dB = log(dB); %log (gamma)
dA = log(dA); %log (gamma)

mx = mean(dB);
sx = std(dB);

dB = (dB-mx)/sx; %zscore
dA = (dA-mx)/sx;

%criteria for DS
k = dB > 0.75 & dA > 0.75 & wDG > 5 & wDG < 25;

%select samples
samplesDS = s(k);

if length(samplesDS) ~= size(resultsCSD,2)
    disp('N DS ~= N CSD');
    return;
end

matches = zeros(4,size(resultsCSD,2));

for i = 1:size(resultsCSD,2)

    x = resultsCSD(:,i)';

    %sources
    %kMax = find((x > [x(1) x(1:(end-1))]) & (x >= [x(2:end) x(end)])); 
    %kMax = kMax(kMax > 1 & kMax < 200);
    %xMax = x(kMax);

    %sinks
    kMin = find((x < [x(1) x(1:(end-1))]) & (x <= [x(2:end) x(end)]));
    kMin = kMin(kMin > 1 & kMin < 200);
    %xMin = x(kMin);

    %sinks (from top to bottom)
    for sI = 1:4

        s = sinks(sI);
        if isnan(s); continue; end;

        st = s-wLenSearch;
        ed = s+wLenSearch;
        if st < 1; st = 1; end;
        if ed > 200; ed = 200; end

        k = sum(kMin > st & kMin < ed);
        if k == 0; continue; end;

        matches(sI,i) = 1;
    end
end

%DSL from superior blade
kType1sup = matches(1,:)==1 & matches(2,:)==0;
%DSM from superior blade
kType2sup = matches(1,:)==0 & matches(2,:)==1;

%DSM from inferior blade
kType2inf = matches(4,:)==0 & matches(3,:)==1;
%DSL from inferior blade
kType1inf = matches(4,:)==1 & matches(3,:)==0;

figure('Position',[100 100 1200 300])
subplot(1,2,1);
imagesc(resultsCSD(:,kType1sup),[-30000 30000])
title('DSL');
text(1000,100,'<- outer mol.layer of superior DG blade (DSL)')
text(1000,148,'<- hilus')
text(1000,192,'<- outer mol.layer of inferior DG blade (DSL)')


subplot(1,2,2);
imagesc(resultsCSD(:,kType2sup),[-30000 30000])
title('DSM');
text(1000,119,'<- middle mol.layer of superior DG blade (DSM)')
text(1000,148,'<- hilus')
text(1000,175,'<- middle mol.layer of inferior DG blade (DSM)')


save([drOUT fnEEG],'kType1sup','kType1inf','kType2sup','kType2inf','samplesDS','matches');
   




