Skip to content

Commit 77c63b8

Browse files
committed
2 parents ee2f158 + 28cce4a commit 77c63b8

40 files changed

+2516
-1128
lines changed
Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
function behaviorDataDiamondBySess = calcDiamondMetricsBySess(sessdata, trialdata, params, dirs, index, animalID, makenewfiles);
2-
%this function calculates session metrics using the raw and trial data
3-
%input:
4-
% sessdata - raw behavior structure, output of loadRawDiamondVirmenFile
5-
% trialdata - behavior broken up into trials, output of calcDiamondMetricsByTrial
6-
% dirs - directory structure with all the file path info
7-
% sessindex - single animal index in format [animal# date session# genotype]
8-
% animalID - animal identifier, ie 'S','F'
9-
10-
filename = [dirs.savedatadir 'behaviorDataDiamondSess_' animalID num2str(index(1)) '_' num2str(index(2)) '_' num2str(index(3))];
11-
disp(['Calculating session metrics for ' animalID num2str(index(1)) ' ' num2str(index(2)) ' ' num2str(index(3))]);
12-
13-
if ~exist(filename) || makenewfiles
14-
%% concatenate trial position, velocity, and viewAngle data
15-
concatTrialData = concatTrialBehavior(trialdata,sessdata.params.trainingtype)
16-
fnamesBehavior = fieldnames(concatTrialData);
17-
for fieldIdx = 1:length(fnamesBehavior)
18-
behaviorDataDiamondBySess.(fnamesBehavior{fieldIdx}) = concatTrialData.(fnamesBehavior{fieldIdx});
19-
end
20-
21-
%% concatenate trial outputs
22-
concatTrialResults = concatTrialOutcomes(trialdata,sessdata.params.trainingtype);
23-
fnamesOutcomes = fieldnames(concatTrialResults);
24-
for fieldIdx = 1:length(fnamesOutcomes)
25-
behaviorDataDiamondBySess.(fnamesOutcomes{fieldIdx}) = concatTrialResults.(fnamesOutcomes{fieldIdx});
26-
end
27-
28-
%% get percent correct
29-
sessPerformance = calcSessionPerformance(behaviorDataDiamondBySess,trialdata,sessdata.params.trainingtype);
30-
fnamesPerformance = fieldnames(sessPerformance);
31-
for fieldIdx = 1:length(fnamesPerformance)
32-
behaviorDataDiamondBySess.(fnamesPerformance{fieldIdx}) = sessPerformance.(fnamesPerformance{fieldIdx});
33-
end
34-
35-
%% get training session type
36-
behaviorDataDiamondBySess.trainingtype = sessdata.params.trainingtype;
37-
38-
%% save filename
39-
save(filename, 'behaviorDataDiamondBySess');
40-
41-
else
42-
load(filename);
43-
end
1+
function behaviorDataDiamondBySess = calcDiamondMetricsBySess(sessdata, trialdata, params, dirs, index, animalID, makenewfiles);
2+
%this function calculates session metrics using the raw and trial data
3+
%input:
4+
% sessdata - raw behavior structure, output of loadRawDiamondVirmenFile
5+
% trialdata - behavior broken up into trials, output of calcDiamondMetricsByTrial
6+
% dirs - directory structure with all the file path info
7+
% sessindex - single animal index in format [animal# date session# genotype]
8+
% animalID - animal identifier, ie 'S','F'
9+
10+
filename = [dirs.savedatadir 'behaviorDataDiamondSess_' animalID num2str(index(1)) '_' num2str(index(2)) '_' num2str(index(3))];
11+
disp(['Calculating session metrics for ' animalID num2str(index(1)) ' ' num2str(index(2)) ' ' num2str(index(3))]);
12+
13+
if ~exist(filename) || makenewfiles
14+
%% concatenate trial position, velocity, and viewAngle data
15+
concatTrialData = concatTrialBehavior(trialdata,sessdata.params.trainingtype);
16+
fnamesBehavior = fieldnames(concatTrialData);
17+
for fieldIdx = 1:length(fnamesBehavior)
18+
behaviorDataDiamondBySess.(fnamesBehavior{fieldIdx}) = concatTrialData.(fnamesBehavior{fieldIdx});
19+
end
20+
21+
%% concatenate trial outputs
22+
concatTrialResults = concatTrialOutcomes(trialdata,sessdata.params.trainingtype);
23+
fnamesOutcomes = fieldnames(concatTrialResults);
24+
for fieldIdx = 1:length(fnamesOutcomes)
25+
behaviorDataDiamondBySess.(fnamesOutcomes{fieldIdx}) = concatTrialResults.(fnamesOutcomes{fieldIdx});
26+
end
27+
28+
%% get percent correct
29+
sessPerformance = calcSessionPerformance(behaviorDataDiamondBySess,trialdata,sessdata.params.trainingtype, index);
30+
fnamesPerformance = fieldnames(sessPerformance);
31+
for fieldIdx = 1:length(fnamesPerformance)
32+
behaviorDataDiamondBySess.(fnamesPerformance{fieldIdx}) = sessPerformance.(fnamesPerformance{fieldIdx});
33+
end
34+
35+
%% get training session type
36+
behaviorDataDiamondBySess.trainingtype = sessdata.params.trainingtype;
37+
38+
%% save filename
39+
save(filename, 'behaviorDataDiamondBySess');
40+
41+
else
42+
load(filename);
43+
end
Lines changed: 96 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,96 @@
1-
function behaviorDataDiamondByTrial = calcDiamondMetricsByTrial(sessdata, params, dirs, index, animalID, makenewfiles)
2-
%this function splits up diamond track behavioral data into individual trials
3-
4-
%input:
5-
% sessdata - raw behavior structure, output of loadRawDiamondVirmenFile
6-
% dirs - directory structure with all the file path info
7-
% sessindex - single animal index in format [animal# date session# genotype]
8-
% animalID - animal identifier, ie 'S','F'
9-
10-
% it perfoms the following actions in this order
11-
% gets trial start/ends based on reward/punishment phase or large position changes
12-
% creates a new data structure where each trial is a cell in a cell array
13-
% gets trialdur
14-
% resamples vectors so constant time sampling rate (corrects stepfunction vectors to remain step functions as well)
15-
% gets incorrect/correct, left/right, north/south trial info
16-
17-
18-
filename = [dirs.savedatadir 'behaviorDataDiamondTrial_' animalID num2str(index(1)) '_' num2str(index(2)) '_' num2str(index(3))];
19-
disp(['Calculating trial metrics for ' animalID num2str(index(1)) ' ' num2str(index(2)) ' ' num2str(index(3))]);
20-
21-
if ~exist(filename) || makenewfiles
22-
%% get trial times (why didn't I use numTrials? idk but I think there was a reason...)
23-
trialStarts = [1; find(diff(ismember(sessdata.currentPhase,[3 4])) == -1) + 1]; %looks for when reward or punishment phase happened and finds the end
24-
trialEnds = [find(diff(ismember(sessdata.currentPhase,[3 4])) == -1); length(sessdata.currentPhase)];
25-
if ~ismember(sessdata.currentPhase,[3,4]) %failed trial or linear track, in this case look for large changes in position
26-
trialStarts = [1; find(abs(diff(sessdata.positionY)) > 10) + 1]; %10 is arbitraty number but seems to work for catching the teleportation events
27-
trialEnds = [find(abs(diff(sessdata.positionY)) > 10); length(sessdata.positionY)];
28-
end
29-
30-
%% make new trial data structure
31-
fnames = fieldnames(sessdata);
32-
fields = fnames(4:end); %get rid of the trackname, params, session info fields that weren't matrices
33-
for trialIdx = 1:size(trialStarts,1)
34-
for fieldIdx = 1:size(fields,1)
35-
trialInds = [trialStarts(trialIdx) trialEnds(trialIdx)];
36-
if ~isnan(sessdata.(fields{fieldIdx}))
37-
behaviorDataDiamondByTrial{trialIdx}.([fields{fieldIdx} 'Raw']) = sessdata.(fields{fieldIdx})(trialInds(1):trialInds(2));
38-
else
39-
behaviorDataDiamondByTrial{trialIdx}.([fields{fieldIdx} 'Raw']) = nan(size(behaviorDataDiamondByTrial{trialIdx}.timeRaw));
40-
end
41-
end
42-
end
43-
44-
%% get trial duration
45-
for trialIdx = 1:size(trialStarts,1)
46-
timeVect = behaviorDataDiamondByTrial{trialIdx}.timeRaw;
47-
behaviorDataDiamondByTrial{trialIdx}.trialdurRaw = timeVect(end)-timeVect(1); %duration in sec
48-
end
49-
50-
%% resample trial data so constant time sampling rate
51-
for trialIdx = 1:size(trialStarts,1)
52-
resampledVects = resampleDiamondMazeTrials(behaviorDataDiamondByTrial{trialIdx},params,sessdata.params.trainingtype);
53-
fnames2add = fieldnames(resampledVects);
54-
for fieldIdx = 1:size(fnames2add,1)
55-
behaviorDataDiamondByTrial{trialIdx}.(fnames2add{fieldIdx}) = resampledVects.(fnames2add{fieldIdx});
56-
end
57-
end
58-
59-
%% get incorrect/correct trial %0 = incorrect, 1 = correct, -1 = failed
60-
for trialIdx = 1:size(trialStarts,1)
61-
behaviorDataDiamondByTrial{trialIdx}.outcome = getTrialOutcomes(sessdata, behaviorDataDiamondByTrial{trialIdx});
62-
end
63-
64-
%% get north or south trial
65-
for trialIdx = 1:size(trialStarts,1)
66-
[startLoc choiceLoc] = getTrialStartLoc(behaviorDataDiamondByTrial{trialIdx});
67-
behaviorDataDiamondByTrial{trialIdx}.trialStartLoc = startLoc;
68-
behaviorDataDiamondByTrial{trialIdx}.trialChoiceLoc = choiceLoc;
69-
end
70-
71-
%% get right or left trial
72-
for trialIdx = 1:size(trialStarts,1)
73-
%note the first correct zone value will be correct side for encoding only (would switch for choice)
74-
if behaviorDataDiamondByTrial{trialIdx}.correctZone(1) == 2
75-
behaviorDataDiamondByTrial{trialIdx}.trialCorrectEncLoc = 'east';
76-
behaviorDataDiamondByTrial{trialIdx}.trialCorrectChoiceLoc = 'west';
77-
elseif behaviorDataDiamondByTrial{trialIdx}.correctZone(1) == 1
78-
behaviorDataDiamondByTrial{trialIdx}.trialCorrectEncLoc = 'west';
79-
behaviorDataDiamondByTrial{trialIdx}.trialCorrectChoiceLoc = 'east';
80-
else
81-
behaviorDataDiamondByTrial{trialIdx}.trialCorrectEncLoc = 'nan';
82-
behaviorDataDiamondByTrial{trialIdx}.trialCorrectChoiceLoc = 'nan';
83-
end
84-
end
85-
86-
%% save file
87-
save(filename, 'behaviorDataDiamondByTrial');
88-
89-
else
90-
load(filename);
91-
end
1+
function behaviorDataDiamondByTrial = calcDiamondMetricsByTrial(sessdata, params, dirs, index, animalID, makenewfiles)
2+
%this function splits up diamond track behavioral data into individual trials
3+
4+
%input:
5+
% sessdata - raw behavior structure, output of loadRawDiamondVirmenFile
6+
% dirs - directory structure with all the file path info
7+
% sessindex - single animal index in format [animal# date session# genotype]
8+
% animalID - animal identifier, ie 'S','F'
9+
10+
% it perfoms the following actions in this order
11+
% gets trial start/ends based on reward/punishment phase or large position changes
12+
% creates a new data structure where each trial is a cell in a cell array
13+
% gets trialdur
14+
% resamples vectors so constant time sampling rate (corrects stepfunction vectors to remain step functions as well)
15+
% gets incorrect/correct, left/right, north/south trial info
16+
17+
18+
filename = [dirs.savedatadir 'behaviorDataDiamondTrial_' animalID num2str(index(1)) '_' num2str(index(2)) '_' num2str(index(3))];
19+
disp(['Calculating trial metrics for ' animalID num2str(index(1)) ' ' num2str(index(2)) ' ' num2str(index(3))]);
20+
21+
if ~exist(filename) || makenewfiles
22+
%% get trial times (why didn't I use numTrials? idk but I think there was a reason...)
23+
trialStarts = [1; find(diff(ismember(sessdata.currentPhase,[3 4])) == -1) + 1 + 1]; %looks for when reward or punishment phase happened and finds the end pluse one sample
24+
trialEnds = [find(diff(ismember(sessdata.currentPhase,[3 4])) == -1); length(sessdata.currentPhase)];
25+
if ~ismember(sessdata.currentPhase,[3,4]) %failed trial or linear track, in this case look for large changes in position
26+
trialStarts = [1; find(abs(diff(sessdata.positionY)) > 10) + 1]; %10 is arbitraty number but seems to work for catching the teleportation events
27+
trialEnds = [find(abs(diff(sessdata.positionY)) > 10); length(sessdata.positionY)];
28+
if sum(diff(trialStarts) == 1) %finds teleportation events that took 2 samples
29+
sample2fix = find(diff(trialStarts) == 1);
30+
trialStarts(sample2fix) = []; trialEnds(sample2fix) = [];
31+
end
32+
33+
end
34+
35+
%% make new trial data structure
36+
fnames = fieldnames(sessdata);
37+
fields = fnames(4:end); %get rid of the trackname, params, session info fields that weren't matrices
38+
for trialIdx = 1:size(trialStarts,1)
39+
for fieldIdx = 1:size(fields,1)
40+
trialInds = [trialStarts(trialIdx) trialEnds(trialIdx)];
41+
if ~isnan(sessdata.(fields{fieldIdx}))
42+
behaviorDataDiamondByTrial{trialIdx}.([fields{fieldIdx} 'Raw']) = sessdata.(fields{fieldIdx})(trialInds(1):trialInds(2));
43+
else
44+
behaviorDataDiamondByTrial{trialIdx}.([fields{fieldIdx} 'Raw']) = nan(size(behaviorDataDiamondByTrial{trialIdx}.timeRaw));
45+
end
46+
end
47+
end
48+
49+
%% get trial duration
50+
for trialIdx = 1:size(trialStarts,1)
51+
timeVect = behaviorDataDiamondByTrial{trialIdx}.timeRaw;
52+
behaviorDataDiamondByTrial{trialIdx}.trialdurRaw = timeVect(end)-timeVect(1); %duration in sec
53+
end
54+
55+
%% resample trial data so constant time sampling rate
56+
for trialIdx = 1:size(trialStarts,1)
57+
resampledVects = resampleDiamondMazeTrials(behaviorDataDiamondByTrial{trialIdx},params,sessdata.params.trainingtype);
58+
fnames2add = fieldnames(resampledVects);
59+
for fieldIdx = 1:size(fnames2add,1)
60+
behaviorDataDiamondByTrial{trialIdx}.(fnames2add{fieldIdx}) = resampledVects.(fnames2add{fieldIdx});
61+
end
62+
end
63+
64+
%% get incorrect/correct trial %0 = incorrect, 1 = correct, -1 = failed
65+
for trialIdx = 1:size(trialStarts,1)
66+
behaviorDataDiamondByTrial{trialIdx}.outcome = getTrialOutcomes(sessdata, behaviorDataDiamondByTrial{trialIdx});
67+
end
68+
69+
%% get north or south trial
70+
for trialIdx = 1:size(trialStarts,1)
71+
[startLoc choiceLoc] = getTrialStartLoc(behaviorDataDiamondByTrial{trialIdx});
72+
behaviorDataDiamondByTrial{trialIdx}.trialStartLoc = startLoc;
73+
behaviorDataDiamondByTrial{trialIdx}.trialChoiceLoc = choiceLoc;
74+
end
75+
76+
%% get right or left trial
77+
for trialIdx = 1:size(trialStarts,1)
78+
%note the first correct zone value will be correct side for encoding only (would switch for choice)
79+
if behaviorDataDiamondByTrial{trialIdx}.correctZone(1) == 2
80+
behaviorDataDiamondByTrial{trialIdx}.trialCorrectEncLoc = 'east';
81+
behaviorDataDiamondByTrial{trialIdx}.trialCorrectChoiceLoc = 'west';
82+
elseif behaviorDataDiamondByTrial{trialIdx}.correctZone(1) == 1
83+
behaviorDataDiamondByTrial{trialIdx}.trialCorrectEncLoc = 'west';
84+
behaviorDataDiamondByTrial{trialIdx}.trialCorrectChoiceLoc = 'east';
85+
else
86+
behaviorDataDiamondByTrial{trialIdx}.trialCorrectEncLoc = 'nan';
87+
behaviorDataDiamondByTrial{trialIdx}.trialCorrectChoiceLoc = 'nan';
88+
end
89+
end
90+
91+
%% save file
92+
save(filename, 'behaviorDataDiamondByTrial');
93+
94+
else
95+
load(filename);
96+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function output = calcHistByPosition(trackDataClean, metric);
2+
3+
for trialIdx = 1:size(trackDataClean.time,1)
4+
%get position histograms
5+
[n edg binsUsedX] = histcounts(trackDataClean.positionXClean{trialIdx},trackDataClean.posXbins);
6+
[n edg binsUsedY] = histcounts(trackDataClean.positionYClean{trialIdx},trackDataClean.posYbins);
7+
uniquebinsX = unique(binsUsedX); uniquebinsY = unique(binsUsedY);
8+
output.posXbins = trackDataClean.posXbins;
9+
output.posYbins = trackDataClean.posYbins;
10+
11+
%initialize data structures
12+
output.histX(trialIdx,:) = nan(size(trackDataClean.posXbins));
13+
output.histY(trialIdx,:) = nan(size(trackDataClean.posYbins));
14+
for binIdx = 1:size(trackDataClean.posXbins,2)
15+
output.allvalsX{trialIdx}{binIdx} = nan;
16+
end
17+
for binIdx = 1:size(trackDataClean.posYbins,2)
18+
output.allvalsY{trialIdx}{binIdx} = nan;
19+
end
20+
21+
%loop through x bins to get histograms by X position
22+
for binIdx = 1:size(uniquebinsX,2)
23+
bins2avg = find(binsUsedX == uniquebinsX(binIdx));
24+
metricXAvg = nanmean(trackDataClean.([metric 'Clean']){trialIdx}(bins2avg));
25+
output.histX(trialIdx,uniquebinsX(binIdx)) = metricXAvg;
26+
output.allvalsX{trialIdx}{uniquebinsX(binIdx)} = trackDataClean.([metric 'Clean']){trialIdx}(bins2avg);
27+
end
28+
29+
%loop through y bins to get histograms by X position
30+
for binIdx = 1:size(uniquebinsY,2)
31+
bins2avg = find(binsUsedY == uniquebinsY(binIdx));
32+
metricYAvg = nanmean(trackDataClean.([metric 'Clean']){trialIdx}(bins2avg));
33+
output.histY(trialIdx,uniquebinsY(binIdx)) = metricYAvg;
34+
output.allvalsY{trialIdx}{uniquebinsY(binIdx)} = trackDataClean.([metric 'Clean']){trialIdx}(bins2avg);
35+
end
36+
end
37+
end

0 commit comments

Comments
 (0)