|
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 |
0 commit comments