-
Notifications
You must be signed in to change notification settings - Fork 2
/
IndividualPhotoTagPlots.m
164 lines (145 loc) · 7.19 KB
/
IndividualPhotoTagPlots.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
fileName='SpVi16_0403_WR_4850_LS1Hz2ms100mW_nopp' % _Ch7'% 'SpVi12_1107_WR_Texture_LS500mH_24Ch_nopp' %_Ch6
channelNum=10; %11 %24;
%% 'SpVi12_133_2Hz2ms_7mW_nopp'
%'SpVi12_1206_WR_LS_500mHz_2ms_2_nopp'
% 'SpVi12_1107_WR_Texture_LS500mH_24Ch_nopp'
%'SpVi12_1107_WR_Texture_LS500mH_24Ch_nopp'
%'SpVi12_133_2Hz2ms_7mW_nopp'
%'SpVi12_1103_WR_LS_500mHz2ms70m_nopp'
%'SpVi12_1107_WR_MS_LS500mHz2ms6_nopp';
%'SpVi12_1107_WR_MS_LS500mHz2ms6_24Ch_nopp';
%'SpVi12_1024_KX_MLStim_26Ch_nopp'; %'039v_0925_2Hz20ms_20mW_28Ch_nopp';
% '039v_0927_2Hz20ms_20mW_28Ch_nopp'; % 'SpVi12_133_2Hz2ms_10mW_nopp';
% SpVi12_133_2Hz2ms_10mW_nopp_Ch %SpVi12_133_2Hz2ms_10mW_nopp_Ch
%% From SpikeVisualizationGUI export
spikeData=load([fileName '_Ch' num2str(channelNum) '.mat'],'waveForms','spikeTimes','unitsIdx','samplingRate','selectedUnits');
load([fileName '_Ch' num2str(channelNum) '.mat'],'TTLs');
load([fileName '_Ch' num2str(channelNum) '.mat'], 'traceExcerpt');
traceData=load([fileName '_Ch' num2str(channelNum) '.mat'], 'allTraces','traceInfo');
%% From JRClust csv export
% dirListing=dir; dirName=cd;
% infoFileName=dirListing(~cellfun('isempty',cellfun(@(x) strfind(x,'_info.'),...
% {dirListing.name},'UniformOutput',false))).name;
% recInfo=load(fullfile(dirName,infoFileName),'rec_info');recInfo=recInfo.rec_info;
%
% JRclustData=load([fileName '_JR.csv']);
% spikeData.selectedUnits=[2]; % 3 4];
% unitIdx=JRclustData(:,2)==2; % | JRclustData(:,2)==3 | JRclustData(:,2)==4;
% spikeData.unitsIdx=int8(JRclustData(unitIdx,2));
% spikeData.spikeTimes=uint32(JRclustData(unitIdx,1)*recInfo.samplingRate);
%
% traces = memmapfile(fullfile(dirName,[fileName '.dat']),'Format','int16');
% waveForms=cell(recInfo.numRecChan,1);
% for chNum=1:recInfo.numRecChan
% waveForms{chNum,1}=ExtractChunks(traces.Data(chNum:recInfo.numRecChan:max(size(traces.Data))),...
% JRclustData(JRclustData(JRclustData(:,3)==chNum,2)==spikeData.selectedUnits,1)*recInfo.samplingRate,...
% 50,'tshifted'); %'tzero' 'tmiddle' 'tshifted'
% end
%
% spikeData.waveForms=int16(cell2mat(waveForms(~cellfun('isempty',waveForms)))');
% %
% % spikes=LoadSpikeData([fileName '_JR.csv'],[],recInfo.numRecChan,recInfo.samplingRate,recInfo.bitResolution);
% %
% % spikeData.selectedUnits=2;
% % units=cellfun(@(chUnits) chUnits(chUnits==spikeData.selectedUnits),spikes.Units,'UniformOutput',false);
% % spikeTimes=cellfun(@(chUnits,chSpkTimes) chSpkTimes(chUnits==spikeData.selectedUnits),...
% % spikes.Units,spikes.SpikeTimes,'UniformOutput',false);
% % waveForms=cellfun(@(chUnits,chWf) chWf(:,chUnits==spikeData.selectedUnits),spikes.Units,...
% % spikes.Waveforms,'UniformOutput',false);
% % spikeTimes=cell2mat(spikeTimes(~cellfun('isempty',units)));
% % waveForms=cell2mat(waveForms(~cellfun('isempty',units))');
% % units=cell2mat(units(~cellfun('isempty',units)));
%% get spike times and convert to binary array
for clusNum=1:size(spikeData.selectedUnits,1)
%% convert to 1 millisecond bins and plot excerpt
binSize=1;
numBin=ceil((max(spikeData.spikeTimes(spikeData.unitsIdx==spikeData.selectedUnits(clusNum)))+1)/...
(spikeData.samplingRate/1000)/binSize);
[spikeCount,spikeTime]=histcounts(double(spikeData.spikeTimes(spikeData.unitsIdx==spikeData.selectedUnits(clusNum)))/...
double(spikeData.samplingRate/1000), numBin);
% foo=spikeData.spikeTimes(spikeData.unitsIdx==spikeData.selectedUnits(clusNum))/30;
% figure; bar(spikeTime(1:6000),spikeCount(1:6000),'hist')
%% spike density function
spikeArray = zeros(1,ceil(max(spikeTime))+1);
spikeArray(ceil(spikeTime(1:end-1)))=spikeCount;
% sigma=1;
% convSpikeTime = [zeros(1,sigma*3) fullgauss_filtconv(spikeArray,sigma,0)].*1000;
% hold on
% plot([zeros(1,sigma*3) convSpikeTime zeros(1,sigma*3)])
% plot( convSpikeTime(1:6000-sigma*3))
%% create rasters aligned to TTL
%define parameters
preAlignWindow=20;
postAlignWindow=259;
TTLtimes=uint32(TTLs.TTLtimes)/(TTLs.samplingRate/1000);
raster=nan(numel(TTLs.TTLtimes),preAlignWindow+postAlignWindow+1);
for trialNum=1:numel(TTLs.TTLtimes)
try
raster(trialNum,:)=spikeArray(...
TTLtimes(trialNum)-preAlignWindow:...
TTLtimes(trialNum)+postAlignWindow);
%smoothed:
% spikeRasters(trialNum,:)=convSpikeTime(...
% TTLtimes(trialNum)-preAlignWindow:...
% TTLtimes(trialNum)+postAlignWindow);
catch
continue
end
end
spikeRasters{clusNum}=raster(~isnan(sum(raster,2)),:);
% figure; imagesc(raster)
end
pulseDur=mode(TTLs.end-TTLs.start);
%% Figures
for cellNum=1:size(spikeData.selectedUnits,1)
% keep one cell
% cellNum=2;
figure('Position',[296 149 1504 761],'name',...
[fileName ' Ch' num2str(channelNum) ' Unit' num2str(spikeData.selectedUnits(cellNum))] );
% waveforms
subplot(3,3,[1,4]); hold on
delay=5;
OptoWaveforms(spikeData,TTLtimes,spikeData.selectedUnits(cellNum),delay,gca)
% rasters
subplot(3,3,[2,5]);
OptoRasters(spikeRasters(cellNum),preAlignWindow,pulseDur,gca);
% title(['Channel ' num2str(channelNum) ', Neuron ' num2str(spikeData.selectedUnits(cellNum))],'FontName','Cambria');
% SDF
subplot(3,3,[3,6])
OptoSDF(spikeRasters(cellNum),preAlignWindow,pulseDur,gca)
% % ISI
% subplot(3,3,4); hold on
% OptoISI(spikeData,TTLtimes,spikeData.selectedUnits(cellNum),gca)
%
% %ACG
% subplot(3,3,7); hold on
% OptoACG(spikeData,TTLtimes,spikeData.selectedUnits(cellNum),gca)
% raw trace
subplot(3,3,7:9); hold on
msConv=double(spikeData.samplingRate/1000);
excerptTTLtimes=double(TTLtimes(TTLtimes>(traceExcerpt.location-traceExcerpt.excerptSize)/msConv &...
TTLtimes<(traceExcerpt.location+traceExcerpt.excerptSize)/msConv)-...
(traceExcerpt.location-traceExcerpt.excerptSize)/msConv)*msConv;
if ~isempty(excerptTTLtimes)
% excerptTTLtimes=excerptTTLtimes(end); %if wants to keep only one pulse
else % check further out in the trace
traceExcerpt.location=TTLtimes(1)*msConv;
winIdxStart=(traceExcerpt.location-traceExcerpt.excerptSize)*traceData.traceInfo.numChan+1;
% mod(winIdxStart,traceData.traceInfo.numChan)
winSize=2; %default 1 pulse
winIdxEnd=winIdxStart+(winSize*2*traceExcerpt.excerptSize*traceData.traceInfo.numChan);
excerptWindow=winIdxStart:winIdxEnd-1;
% size(excerptWindow,2)>(2*traceExcerpt.excerptSize*traceData.traceInfo.numChan)
traceExcerpt.data=traceData.allTraces.Data(excerptWindow);
traceExcerpt.data=reshape(traceExcerpt.data,[traceData.traceInfo.numChan traceExcerpt.excerptSize*2*winSize]);
preprocOption={'CAR','all'};
traceExcerpt.data=PreProcData(traceExcerpt.data,30000,preprocOption);
% figure; plot(dataExcerpt(11,:))
traceExcerpt.data=traceExcerpt.data(channelNum,:);
excerptTTLtimes=double(TTLtimes(TTLtimes>(traceExcerpt.location-traceExcerpt.excerptSize)/msConv &...
TTLtimes<(traceExcerpt.location+traceExcerpt.excerptSize*winSize)/msConv)-...
(traceExcerpt.location-traceExcerpt.excerptSize)/msConv)*msConv;
traceExcerpt.spkTimes{cellNum}=NaN;
end %plot anyway
OptoRawTrace(traceExcerpt,traceExcerpt.spkTimes(cellNum),msConv,excerptTTLtimes,'',gca)
end