% Analyzing unit stability for one of Misi's sessions
clear all
basepath = '/Volumes/Samsung_T5/GlobusDataFolder/MS22/Peter_MS22_180628_120341_concat';
cd(basepath)

% Loading session info
session = loadSession(basepath);

% Loading spikes
spikes = loadSpikes('basepath',basepath);

% Pulling waveforms
filtFreq = 100:100:1000;
waveform_metrics = {};
for i = 1:numel(filtFreq)
    spikes = getWaveformsFromDat(spikes,session,'nPull',1000,'saveFig', false,'filtFreq',[filtFreq(i),8000],'wfWin_sec',0.006,'wfWinKeep',0.0012);
    waveform_metrics{i} = calc_waveform_metrics(spikes,session.extracellular.sr);
end
cell_metrics = loadCellMetrics('session',session);
pyramidal_cells = find(ismember(cell_metrics.putativeCellType,'Pyramidal Cell'));
interneurons = find(ismember(cell_metrics.putativeCellType,'Narrow Interneuron'));
colors = hsv(numel(filtFreq))*0.8;

bins = 0.15:0.05:1.2;
for i = 1:numel(filtFreq)
    figure(1)
%     plot(waveform_metrics{i}.troughtoPeak,waveform_metrics{i}.ab_ratio,'.','color',colors(i,:)), hold on
    subplot(2,1,1), title('Pyramidal Cells'), hold on
    temp1 = histcounts(waveform_metrics{i}.troughtoPeak(pyramidal_cells),bins);
    plot(bins(1:end-1),temp1,'color',colors(i,:)), hold on
    subplot(2,1,2), title('Narrow Interneurons'), hold on
    temp2 = histcounts(waveform_metrics{i}.troughtoPeak(interneurons),bins);
    plot(bins(1:end-1),temp2,'color',colors(i,:)), hold on
    figure(2)
    subplot(3,4,i), title(['Filter: ' num2str(filtFreq(i))]), hold on
    plot(bins(1:end-1),temp1,'color','b'), hold on
    plot(bins(1:end-1),temp2,'color','r'), hold on
end
figure(2)
legend({'Pyramidal Cells','Narrow Interneurons'})

figure(1)
legend(num2str(filtFreq'))
