b_BehaviorPavlovianTrainingAnalysis.m 9.3 KB

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