-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsaveData.m
More file actions
163 lines (133 loc) · 5.73 KB
/
Copy pathsaveData.m
File metadata and controls
163 lines (133 loc) · 5.73 KB
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
% We save the following information:
% For LFPs
% 1. Average time-frequency power spectra for each condition (averaged over trials)
% 2. Average power as a function of time in each trial in the frequency ranges specified by methodParams.frequencyRangeList
% For Spikes:
% 1. Number of spikes in timesteps defined by methodParams.winStep, for each trial
function saveData(subjectName,expDate,protocolName,protocolType,folderSourceString,folderSaveString,methodName,methodParams)
gridType='Microelectrode';
% Get Useful Folders
folderDate = fullfile(folderSourceString,'data',subjectName,gridType,expDate);
folderName = fullfile(folderDate,protocolName);
folderExtract = fullfile(folderName,'extractedData');
folderSegment = fullfile(folderName,'segmentedData');
folderLFP = fullfile(folderSegment,'LFP');
folderSpikes = fullfile(folderSegment,'Spikes');
% Get electrodes on which analysis will be done
goodElectrodes = getGoodElectrodes(subjectName);
%Bad Trials Load
if exist(fullfile(folderSegment,'badTrials.mat'), 'file')
x=load(fullfile(folderSegment,'badTrials.mat'));
badTrials = x.badTrials;
else
disp('Bad trials file not found...');
badTrials = [];
end
% timeVals
x = load(fullfile(folderLFP,'lfpInfo.mat'));
timeVals = x.timeVals;
Fs = round(1/(timeVals(2)-timeVals(1)));
methodParams.Fs = Fs;
% parameterCombinations
parameterCombinations = load(fullfile(folderExtract,'parameterCombinations.mat'));
fBands = methodParams.frequencyRangeList;
numFrequencyBands = length(fBands);
% Make Folders
folderSaveEnergy = [protocolType methodName];
folderSaveSpikes = [protocolType 'Spikes'];
folderSaveDataEnergy = fullfile(folderSaveString,folderSaveEnergy,subjectName,expDate,protocolName);
folderSaveDataSpikes = fullfile(folderSaveString,folderSaveSpikes,subjectName,expDate,protocolName);
makeDirectory(folderSaveDataEnergy);
makeDirectory(folderSaveDataSpikes);
if strcmp(protocolType,'FlickeringGratings') % Counterphasing gratings dataset
conditionVals = parameterCombinations.tValsUnique;
numCombinations = length(conditionVals);
goodPosList = cell(1,numCombinations);
aPos=1;ePos=1;rPos=1;oPos=5;cPos=7;fPos=1;
for i=1:numCombinations
goodPosList{i} = setdiff(parameterCombinations.parameterCombinations{aPos,ePos,rPos,fPos,oPos,cPos,i},badTrials);
end
elseif strcmp(protocolType,'NaturalImages') % Natural Images dataset
conditionVals = parameterCombinations.fValsUnique;
numCombinations = length(conditionVals);
goodPosList = cell(1,numCombinations);
aPos=1;ePos=1;rPos=1;oPos=1;cPos=1;tPos=1;
for i=1:numCombinations
goodPosList{i} = setdiff(parameterCombinations.parameterCombinations{aPos,ePos,rPos,i,oPos,cPos,tPos},badTrials);
end
end
for i=1:size(goodElectrodes,2)
elec = goodElectrodes(i);
disp(['Electrode: ' num2str(elec)]);
% Get Analog Data
clear analogData; clear analogInfo;
if exist(fullfile(folderLFP ,['elec' num2str(elec) '.mat']), 'file')
analogData = load(fullfile(folderLFP ,['elec' num2str(elec) '.mat']));
else
analogData = [];
end
if ~isempty(analogData)
saveDataFileName = fullfile(folderSaveDataEnergy,['elec' num2str(elec) '_c' num2str(cPos)]);
meanEnergyTF = cell(1,numCombinations); % Mean power
energyVsT = cell(numCombinations,numFrequencyBands); % EnergyVsTime
for j=1:numCombinations
goodPos = goodPosList{j};
[S,tSpec,fSpec] = getEnergyData(analogData.analogData(goodPos,:),methodName,methodParams);
tSpec = tSpec + timeVals(1);
meanEnergyTF{j} = squeeze(mean(S,3));
for k=1:numFrequencyBands
fRange = fBands{k};
fid1 = find(fSpec> fRange(1), 1); fid2 = find(fSpec <= fRange(2), 1, 'last');
energyVsT{j,k} = squeeze(mean(S(:,fid1:fid2,:),2))';
end
end
save(saveDataFileName,'meanEnergyTF','energyVsT','tSpec','fSpec','conditionVals','fBands');
end
% Get Spike Data
clear spikeData; clear spikeInfo;
if exist(fullfile(folderSpikes,['elec' num2str(elec) '_SID0.mat']), 'file')
spikeData = load(fullfile(folderSpikes ,['elec' num2str(elec) '_SID0.mat']));
else
spikeData = [];
end
if ~isempty(spikeData)
saveDataFileName = fullfile(folderSaveDataSpikes,['elec' num2str(elec) '_c' num2str(cPos)]);
sRaster = cell(1,numCombinations);
for j=1:numCombinations
goodPos = goodPosList{j};
xRaster = spikeData.spikeData(:,goodPos);
[sRaster{j},tRaster] = getSpikeRaster(xRaster,[tSpec(1)-methodParams.winStep/2 tSpec(end)+methodParams.winStep/2],methodParams.winStep);
end
save(saveDataFileName,'sRaster','tRaster','conditionVals');
end
end
end
function[S,t,f] = getEnergyData(X,methodName,methodParams)
if strcmp(methodName,'MultiTaper')
% MT parameters
params.tapers = methodParams.tapers;
params.pad = -1;
params.Fs = methodParams.Fs;
params.fpass = [0 250];
params.trialave = 0;
movingwin = [methodParams.winSize methodParams.winStep];
[S,t,f] = mtspecgramc(X',movingwin,params);
end
end
function[S,tvec] = getSpikeRaster(spikeData, T, dt)
% input spikeData as a cell array of spike times
% T = [T1, T2] = the end points of the big interval
% dt = size of time bins
eps = 0.000001; % tolerance for comparisons
S = zeros(size(spikeData,2), round(diff(T)/dt)); % numTrials x numTimeBins
tvec = zeros(round(diff(T)/dt),1);
t1 = T(1); t2 = T(1) + dt;
ctr = 0;
while abs(t1 - T(2)) > eps
ctr = ctr + 1;
S(:, ctr) = getSpikeCounts(spikeData, [t1,t2])';
tvec(ctr) = 0.5*(t1+t2);
t1 = t2;
t2 = t2 + dt;
end
end