a_BehaviorInstumentalTrainingAnalysis.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. %% Import data from excel
  2. clear;
  3. [~,~,PAInstTrainingData] = xlsread('PAInstTrainingDataALL.xlsx');
  4. %% Assign data to corresponding variables within a structure
  5. VarNames = PAInstTrainingData(1,:);
  6. Data = PAInstTrainingData(2:end,:);
  7. PAInst = struct();
  8. for i=1:42
  9. PAInst.(VarNames{i}) = Data(1:end,(i));
  10. end
  11. PAInst.CSPlusOnset = Data(1:end,43:103);
  12. PAInst.CSMinOnset = Data(1:end,104:163);
  13. PAInst.PETimestamps = Data(1:end,225:942);
  14. %% Convert rat numbers from strings into numbers and append to a separate field
  15. for i = 1:length(PAInst.Rat)
  16. PAInst(:).RatNum(i,1) = str2num(PAInst.Rat{i}(3:5));
  17. end
  18. %% Sort matrix by rat
  19. [sortedRat,index] = sort([PAInst(:).Rat]);
  20. fields = fieldnames(PAInst);
  21. for i = 1:length(fields)
  22. PAInst.(fields{i}) = PAInst.(fields{i})(index,:);
  23. end
  24. %% Number the start dates for each rat and display any errors
  25. Rat = PAInst.Rat(1);
  26. k = 1;
  27. for i=1:length(PAInst.StartDate)
  28. if strcmp(PAInst.Rat(i), Rat)
  29. PAInst.Day(i,:) = k;
  30. k=k+1;
  31. if i < length(PAInst.StartDate)
  32. if PAInst.StartDate{i} == PAInst.StartDate{i+1}
  33. fprintf('repeated date %d with rat %s\n', PAInst.StartDate{i}, PAInst.Rat{i});
  34. end
  35. end
  36. else
  37. k = 1;
  38. Rat = PAInst.Rat(i);
  39. PAInst.Day(i,:) = k;
  40. k=k+1;
  41. end
  42. end
  43. %% Determine stages of training
  44. % look at durations
  45. % if duration = 60 ==> stage 1
  46. % if duration = 30 ==> stage 2
  47. % if duration = 20 ==> stage 3
  48. % if duration = 10 and numtrials <= 60 ==> stage 4
  49. % if duration = 10 and numtrials > 60 ==> stage 5
  50. % if duration = anything else ==> stage NaN, print rat and date in which this occurs
  51. for i=1:length(PAInst.Duration)
  52. if PAInst.Duration{i} >= 60
  53. PAInst.Stage{i,:} = 1;
  54. elseif (PAInst.Duration{i} == 30 || PAInst.Duration{i} == 40)
  55. PAInst.Stage{i,:} = 2;
  56. elseif PAInst.Duration{i} == 20
  57. PAInst.Stage{i,:} = 3;
  58. elseif PAInst.Duration{i} == 10 & PAInst.CSMinOnset{i,1}==0
  59. PAInst.Stage{i,:} = 4;
  60. elseif PAInst.Duration{i} == 10 & PAInst.CSMinOnset{i,1}>0
  61. PAInst.Stage{i,:} = 5;
  62. else
  63. PAInst.Stage{i,:} = NaN;
  64. fprintf('stage is NaN for rat %s on date %d\n', PAInst.Rat{i}, PAInst.StartDate{i});
  65. end
  66. end
  67. %% Setting NS values to 0
  68. for i=1:length(PAInst.CSMinRatio)
  69. if PAInst.Stage{i} < 5
  70. PAInst.CSMinRatio{i} = NaN;
  71. end
  72. end
  73. %% Calculate trial-by-trial PE latencies for CSPlus and CSMin
  74. % Convert necessary cells to matrices, setting all strings to NaN
  75. for y=1:length(PAInst.CSPlusOnset)
  76. for x=1:size(PAInst.CSPlusOnset,2)
  77. if ischar(PAInst.CSPlusOnset{y,x})
  78. PAInst.CSPlusOnset{y,x} = NaN;
  79. end
  80. end
  81. end
  82. for y=1:length(PAInst.CSMinOnset)
  83. for x=1:size(PAInst.CSMinOnset,2)
  84. if ischar(PAInst.CSMinOnset{y,x})
  85. PAInst.CSMinOnset{y,x} = NaN;
  86. end
  87. end
  88. end
  89. for y=1:size(PAInst.PETimestamps,1)
  90. for x=1:size(PAInst.PETimestamps,2)
  91. if ischar(PAInst.PETimestamps{y,x})
  92. PAInst.PETimestamps{y,x} = NaN;
  93. end
  94. end
  95. end
  96. PAInst.CSPlusOnset = cell2mat(PAInst.CSPlusOnset);
  97. PAInst.CSMinOnset = cell2mat(PAInst.CSMinOnset);
  98. PAInst.PETimestamps = cell2mat(PAInst.PETimestamps);
  99. % Calculate CSPlus absolute and cue period latencies
  100. % Conditions:
  101. % if next csplus > 0 but less than pe time, lat = nan
  102. % if next csmin > 0 but less than pe time, lat =nan
  103. for y=1:length(PAInst.CSPlusOnset)
  104. for x=1:size(PAInst.CSPlusOnset,2)
  105. nextPlus = find(PAInst.CSPlusOnset(y,:) > PAInst.CSPlusOnset(y,x), 1);
  106. nextMin = find(PAInst.CSMinOnset(y,:) > PAInst.CSPlusOnset(y,x), 1);
  107. firstPE = find(PAInst.PETimestamps(y,:) > PAInst.CSPlusOnset(y,x), 1);
  108. PAInst.CSPlusAbsLat{y,x} = PAInst.PETimestamps(y,firstPE) - PAInst.CSPlusOnset(y,x);
  109. if PAInst.CSPlusAbsLat{y,x} < PAInst.Duration{y,1}
  110. PAInst.CSPlusCPLat{y,x} = PAInst.CSPlusAbsLat{y,x};
  111. else
  112. PAInst.CSPlusCPLat{y,x} = NaN;
  113. end
  114. if PAInst.CSMinOnset(y,nextMin) > 0 & PAInst.CSMinOnset(y,nextMin) < PAInst.PETimestamps(y,firstPE)
  115. PAInst.CSPlusAbsLat{y,x} = NaN;
  116. PAInst.CSPlusCPLat{y,x} = NaN;
  117. elseif PAInst.CSPlusOnset(y,nextPlus) > 0 & PAInst.CSPlusOnset(y,nextPlus) < PAInst.PETimestamps(y,firstPE)
  118. PAInst.CSPlusAbsLat{y,x} = NaN;
  119. PAInst.CSPlusCPLat{y,x} = NaN;
  120. elseif isnan(PAInst.CSPlusOnset(y,x))
  121. PAInst.CSPlusAbsLat{y,x} = NaN;
  122. PAInst.CSPlusCPLat{y,x} = NaN;
  123. elseif PAInst.CSPlusOnset(y,x) == 0
  124. PAInst.CSPlusAbsLat{y,x} = NaN;
  125. PAInst.CSPlusCPLat{y,x} = NaN;
  126. elseif isempty(firstPE)
  127. PAInst.CSPlusAbsLat{y,x} = NaN;
  128. PAInst.CSPlusCPLat{y,x} = NaN;
  129. end
  130. end
  131. end
  132. % Calculate CSPlus absolute and cue period latency means
  133. PAInst.CSPlusAbsLat = cell2mat(PAInst.CSPlusAbsLat);
  134. PAInst.CSPlusCPLat = cell2mat(PAInst.CSPlusCPLat);
  135. PAInst.CSPlusAbsLatMean = nanmean(PAInst.CSPlusAbsLat,2);
  136. PAInst.CSPlusCPLatMean = nanmean(PAInst.CSPlusCPLat,2);
  137. % Calculate CSMin absolute and cue period latencies
  138. % Conditions:
  139. % if next csplus > 0 but less than pe time, lat = nan
  140. % if next csmin > 0 but less than pe time, lat =nan
  141. for y=1:length(PAInst.CSMinOnset)
  142. for x=1:size(PAInst.CSMinOnset,2)
  143. nextPlus = find(PAInst.CSPlusOnset(y,:) > PAInst.CSMinOnset(y,x), 1);
  144. nextMin = find(PAInst.CSMinOnset(y,:) > PAInst.CSMinOnset(y,x), 1);
  145. firstPE = find(PAInst.PETimestamps(y,:) > PAInst.CSMinOnset(y,x), 1);
  146. PAInst.CSMinAbsLat{y,x} = PAInst.PETimestamps(y,firstPE) - PAInst.CSMinOnset(y,x);
  147. if PAInst.CSMinAbsLat{y,x} < PAInst.Duration{y,1}
  148. PAInst.CSMinCPLat{y,x} = PAInst.CSMinAbsLat{y,x};
  149. else
  150. PAInst.CSMinCPLat{y,x} = NaN;
  151. end
  152. if PAInst.CSMinOnset(y,nextMin) > 0 & PAInst.CSMinOnset(y,nextMin) < PAInst.PETimestamps(y,firstPE)
  153. PAInst.CSMinAbsLat{y,x} = NaN;
  154. PAInst.CSMinCPLat{y,x} = NaN;
  155. elseif PAInst.CSPlusOnset(y,nextPlus) > 0 & PAInst.CSPlusOnset(y,nextPlus) < PAInst.PETimestamps(y,firstPE)
  156. PAInst.CSMinAbsLat{y,x} = NaN;
  157. PAInst.CSMinCPLat{y,x} = NaN;
  158. elseif isnan(PAInst.CSMinOnset(y,x))
  159. PAInst.CSMinAbsLat{y,x} = NaN;
  160. PAInst.CSMinCPLat{y,x} = NaN;
  161. elseif PAInst.CSMinOnset(y,x) == 0
  162. PAInst.CSMinAbsLat{y,x} = NaN;
  163. PAInst.CSMinCPLat{y,x} = NaN;
  164. elseif isempty(firstPE)
  165. PAInst.CSMinAbsLat{y,x} = NaN;
  166. PAInst.CSMinCPLat{y,x} = NaN;
  167. end
  168. end
  169. end
  170. % Calculate CSMin absolute and cue period latency means
  171. PAInst.CSMinAbsLat = cell2mat(PAInst.CSMinAbsLat);
  172. PAInst.CSMinCPLat = cell2mat(PAInst.CSMinCPLat);
  173. PAInst.CSMinAbsLatMean = nanmean(PAInst.CSMinAbsLat,2);
  174. PAInst.CSMinCPLatMean = nanmean(PAInst.CSMinCPLat,2);
  175. %% Determine when rats reach criteria
  176. % FOR EACH RAT:
  177. % criteria = 0 if cs+ratio < .7 or cs-ratio > .3
  178. % if cs+ratio > .7 and cs-ratio < .3, then
  179. % for the first time this happens, criteria = 0
  180. % for every other time this happens and even if they don't meet criteria in later dates (don't need to check cs ratios), criteria = 1
  181. % if new rat, start over
  182. k = 1;
  183. for i=1:length(PAInst.Stage)
  184. if PAInst.Stage{i} < 5
  185. criteria_met = false;
  186. PAInst.Criteria{i,1} = 0;
  187. elseif isnan(PAInst.Stage{i})
  188. criteria_met = false;
  189. PAInst.Criteria{i,1} = NaN;
  190. elseif criteria_met == true % this condition is set from criteria being met
  191. PAInst.Criteria{i,1} = 1;
  192. elseif PAInst.Stage{i,1} == 5
  193. if PAInst.CSPlusRatio{i} >= 0.7 & PAInst.CSMinRatio{i} <= 0.3 % correct criteria
  194. PAInst.Criteria{i,1} = 0;
  195. PAInst.CriteriaDay(k,1) = PAInst.Day(i,1);
  196. PAInst.CriteriaDayRat(k,1) = PAInst.RatNum(i,1);
  197. PAInst.CriteriaDayCSPlusRatio(k,1) = PAInst.CSPlusRatio(i,1);
  198. PAInst.CriteriaDayCSMinRatio(k,1) = PAInst.CSMinRatio(i,1);
  199. PAInst.CriteriaDayCSPlusLat(k,1) = PAInst.CSPlusCPLatMean(i,1);
  200. PAInst.CriteriaDayCSMinLat(k,1) = PAInst.CSMinCPLatMean(i,1);
  201. k = k+1;
  202. criteria_met = true; % false switched to true
  203. else
  204. PAInst.Criteria{i,1} = 0;
  205. criteria_met = false;
  206. end
  207. end
  208. end
  209. PAInst.Criteria = cell2mat(PAInst.Criteria);
  210. PAInst.CSPlusRatio = cell2mat(PAInst.CSPlusRatio);
  211. PAInst.CSMinRatio = cell2mat(PAInst.CSMinRatio);
  212. %% Calculate averages of latency means on days where criteria = 0 during cue period (group data)
  213. % To find averages of CS+ and CS- Cue Period Latency Means by day
  214. RatSelection=ismember(PAInst.RatNum,[105 107 139 151]);
  215. for i=1:max(PAInst.Day(PAInst.Criteria == 0))
  216. PAInst.AverageCSPlusCPLatMean(i) = nanmean(PAInst.CSPlusCPLatMean(PAInst.Criteria == 0 & PAInst.Day == i & RatSelection));
  217. PAInst.Error_AverageCSPlusCPLatMean(i) = nanste(PAInst.CSPlusCPLatMean(PAInst.Criteria == 0 & PAInst.Day == i & RatSelection), 1);
  218. end
  219. for i=1:max(PAInst.Day(PAInst.Criteria == 0))
  220. PAInst.AverageCSMinCPLatMean(i) = nanmean(PAInst.CSMinCPLatMean(PAInst.Criteria == 0 & PAInst.Day == i & RatSelection));
  221. PAInst.Error_AverageCSMinCPLatMean(i) = nanste(PAInst.CSMinCPLatMean(PAInst.Criteria == 0 & PAInst.Day == i & RatSelection), 1);
  222. end
  223. % To find averages of CS+ and CS- Ratios by day
  224. for i=1:max(PAInst.Day(PAInst.Criteria == 0))
  225. PAInst.AverageCSPlusRatio(i) = nanmean(PAInst.CSPlusRatio(PAInst.Criteria == 0 & PAInst.Day == i & RatSelection));
  226. PAInst.Error_AverageCSPlusRatio(i) = nanste(PAInst.CSPlusRatio(PAInst.Criteria == 0 & PAInst.Day == i & RatSelection), 1);
  227. end
  228. for i=1:max(PAInst.Day(PAInst.Criteria == 0))
  229. PAInst.AverageCSMinRatio(i) = nanmean(PAInst.CSMinRatio(PAInst.Criteria == 0 & PAInst.Day == i & RatSelection));
  230. PAInst.Error_AverageCSMinRatio(i) = nanste(PAInst.CSMinRatio(PAInst.Criteria == 0 & PAInst.Day == i & RatSelection), 1);
  231. end
  232. %% Save file
  233. filename = 'InstTrainingAnalysis.mat';
  234. save(filename);