abmFiringRateMap.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. function [occMat spkMat rawMat skaggsrateMat] = abmFiringRateMap(tmazeMat, finAlzPosMat, imROW, imCOL, thisSCALE, samplingRate)
  2. %Generate firing rate map using Skaggs' adaptive binning methods
  3. %[occMat spkMat rawMat skaggsrateMat] = abmFiringRateMap(tmazeMat, finAlzPosMat, imROW, imCOL, thisSCALE, samplingRate)
  4. %
  5. %Input format
  6. % tmazeMat [n x 3 matrix] timestamp x-coord y-coord
  7. % finAlzPosMat [n x 3 matrix] timestamp x-coord y-coord
  8. % imROW [1 x 1] number of rows of ratemap
  9. % imCOL [1 x 1] number of columns of ratemap
  10. % thisSCALE [1 x 1] if you want to use original resolution, it should be 1. If you want to scale-down the resolusion by 10, it should be 10.
  11. % samplingRate [1 x 1] sampling rate for behavioral traces. The current settings use 30 Hz.
  12. %
  13. %Output format
  14. % occMat [imROW x imCOL matrix] raw occupancy map
  15. % spkMat [imROW x imCOL matrix] raw spike position map
  16. % rawMat [imROW x imCOL matrix; spkMat ./ (occMat .* samplingRate)] raw firing rate map
  17. % skaggsrateMat [imROW x imCOL matrix] Skaggs' ABM firing rate map
  18. %
  19. %Originally from VB codes which Inah Lee has.
  20. %Translate VB codes to matlab was done by [Jangjin Kim, July-13-2008]
  21. %Verification was done
  22. %reviced by SB 4/29/2013 : add 'to avoid error'
  23. %revised by JSW 4/4/2019 : add empty tmazeMat error
  24. %to avoid error
  25. tmazeMatSize = size(tmazeMat);
  26. if tmazeMatSize(1) == 0 && tmazeMatSize(2) == 0
  27. tmazeMat = zeros(0,3);
  28. end
  29. %pINDEX
  30. pXCOORD = 2; %x coordinates [from Neuralynx]
  31. pYCOORD = 3; %y coordinates [from Neuralynx]
  32. alphaSET = .0001;
  33. occMat = nan(imROW, imCOL);
  34. spkMat = zeros(imROW, imCOL);
  35. rawMat = zeros(imROW, imCOL);
  36. skaggsrateMat = zeros(imROW, imCOL);
  37. for rowRUN = 1:1:imROW
  38. for colRUN = 1:1:imCOL
  39. if ~isempty(tmazeMat) %revised by JSW 4/4/2019 : add empty tmazeMat error
  40. numspk = size(tmazeMat(tmazeMat(:, pXCOORD) ./ thisSCALE > (colRUN - 1) & tmazeMat(:, pXCOORD) ./ thisSCALE <= colRUN & ...
  41. tmazeMat(:, pYCOORD) ./ thisSCALE > (rowRUN - 1) & tmazeMat(:, pYCOORD) ./ thisSCALE <= rowRUN, :));
  42. if numspk(1, 1) == 0
  43. spkMat(rowRUN, colRUN) = 0;
  44. else
  45. spkMat(rowRUN, colRUN) = numspk(1, 1);
  46. end %numspk(1, 1) == 0
  47. else
  48. spkMat(rowRUN, colRUN) = 0;
  49. end
  50. if ~isempty(finAlzPosMat)
  51. numocc = size(finAlzPosMat(finAlzPosMat(:, pXCOORD) ./ thisSCALE > (colRUN - 1) & finAlzPosMat(:, pXCOORD) ./ thisSCALE <= colRUN & ...
  52. finAlzPosMat(:, pYCOORD) ./ thisSCALE > (rowRUN - 1) & finAlzPosMat(:, pYCOORD) ./ thisSCALE <= rowRUN, :));
  53. if numocc(1, 1) == 0
  54. occMat(rowRUN, colRUN) = nan;
  55. else
  56. occMat(rowRUN, colRUN) = numocc(1, 1);
  57. end %numocc(1, 1) == 0
  58. else
  59. occMat(rowRUN, colRUN) = nan;
  60. end
  61. end %colRUN = 1:1:imCOL
  62. end %rowRUN = 1:1:imROW
  63. rawMat = spkMat ./ occMat .* samplingRate;
  64. if sum(sum(spkMat)) > 0
  65. nanIND = find(isnan(occMat));
  66. occMat(isnan(occMat)) = 0;
  67. i = -20:20; j = -20:19;
  68. Ti = zeros(28, 400);
  69. Tj = zeros(28, 400);
  70. for colRUN = 1:1:imCOL
  71. for rowRUN = 1:1:imROW
  72. if occMat(rowRUN, colRUN) > 0
  73. %Equivalent to BuildRTable
  74. Nr = zeros(1, 400);
  75. for ii = 1:1:41
  76. for jj = 1:1:40
  77. %r(1, (ii - 1) * 41 + jj) = i(1, ii)^2 + j(1, jj)^2;
  78. %r(ii, jj) = i(1, ii)^2 + j(1, jj)^2;
  79. r = i(1, ii)^2 + j(1, jj)^2 + 1;
  80. if r <= 400
  81. N = Nr(r);
  82. Ti(N + 1, r) = i(1, ii);
  83. Tj(N + 1, r) = j(1, jj);
  84. Nr(r) = Nr(r) + 1;
  85. end %r <= 400
  86. end %jj = 1:1:40
  87. end %ii = 1:1:41
  88. nSPK = spkMat(rowRUN, colRUN);
  89. nOCC = occMat(rowRUN, colRUN);
  90. lambda = nSPK / nOCC * samplingRate;
  91. for colRUN2 = 1:1:imCOL %i
  92. for rowRUN2 = 1:1:imROW %j
  93. if occMat(rowRUN2, colRUN2) == 0
  94. skaggsrateMat(rowRUN2, colRUN2) = nan;
  95. else
  96. EnoughPoints = false;
  97. occCount = 1; %Assume there's at least 1 spk and 1 occ
  98. spkCount = 1;
  99. r = 0;
  100. while (r <= 200 & EnoughPoints == false) % r <= 200 original
  101. for h = 1:1:Nr(r + 1)
  102. ii = (colRUN2 - 1) + Ti(h, r + 1);
  103. jj = (rowRUN2 - 1) + Tj(h, r + 1); %-1 to compensate VB code
  104. if (ii >= 0 & ii < 64) & (jj >= 0 & jj < 48)
  105. occCount = occCount + occMat(jj + 1, ii + 1);
  106. spkCount = spkCount + spkMat(jj + 1, ii + 1);
  107. end %(ii >= 0 & ii < 64) & (jj >= 0 & jj < 48)
  108. end %h = 1:1:Nr(r + 1)
  109. if alphaSET^2 * occCount^2 * r * spkCount > 1
  110. EnoughPoints = true;
  111. end %alphaSET^2 * occCount^2 * r * spkCount > 1
  112. r = r + 1;
  113. end %(r <= 200 & EnoughPoints == false)
  114. if occCount < samplingRate / 2
  115. skaggsrateMat(rowRUN2, colRUN2) = 0;
  116. else
  117. skaggsrateMat(rowRUN2, colRUN2) = spkCount / occCount * samplingRate;
  118. end %occCount < samplingRate / 2
  119. end %occMat(colRUN2, rowRUN2) == 0
  120. if occMat(rowRUN2, colRUN2) ~= 0 & isnan(skaggsrateMat(rowRUN2, colRUN2))
  121. rowRUN2 = rowRUN2;
  122. end %occMat(rowRUN2, colRUN2) ~= 0 & isnan(skaggsrateMat(rowRUN2, colRUN2))
  123. if rowRUN2 == 16 & colRUN2 == 39 %Add 1 to compensate VB indices
  124. rowRUN2 = rowRUN2;
  125. end %rowRUN2 == 16 & colRUN2 == 39
  126. end %rowRUN2 = 1:1:imROW
  127. end %colRUN2 = 1:1:imCOL
  128. end %occMat(rowRUN, colRUN) > 0
  129. end %rowRUN = 1:1:imROW
  130. end %colRUN = 1:1:imCOL
  131. occMat(nanIND) = nan;
  132. else
  133. skaggsrateMat = rawMat;
  134. end %sum(sum(spkMat)) > 0