PosMapOrdered.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. function [ idx, pos_map_ordered, pos_map, pos_map_trial, pos_map_trial_norm, pos_map_norm_min, pos_map_norm_max]...
  2. = PosMap_ordered( tcs, deconv, behavior, varargin )
  3. % position firing maps are calculated for the entire session
  4. % then reshape to position bins x trials
  5. % check PlotPosMapTcs.m
  6. % default
  7. nBins = 100;
  8. nSmooth = 3;
  9. n_lowpass = 0.3;
  10. n_medf = 10;
  11. use_deconv = false;
  12. use_lowpass = true;
  13. unit_list = 1:size(deconv,2);
  14. % parse input parameters
  15. % Parse parameter list
  16. for i = 1:2:length(varargin),
  17. if ~ischar(varargin{i}),
  18. error(['Parameter ' num2str(i+2) ' is not a property']);
  19. end
  20. switch(lower(varargin{i}))
  21. case 'nsmooth'
  22. nSmooth = varargin{i+1};
  23. if ~isscalar(nSmooth) || length(nSmooth) > 2,
  24. error('Incorrect value for property ''nSmooth''');
  25. end
  26. case 'unit_list',
  27. unit_list = varargin{i+1};
  28. if ~isvector(unit_list),
  29. error('Incorrect value for property ''unit_list''');
  30. end
  31. case 'use_deconv',
  32. use_deconv = varargin{i+1};
  33. if ~islogical(use_deconv) || length(use_deconv) > 2,
  34. error('Incorrect value for property ''use_deconv''');
  35. end
  36. case 'use_lowpass'
  37. use_lowpass = varargin{i+1};
  38. if ~islogical(use_lowpass)
  39. error('Incorrect value for property ''use_lowpass''');
  40. end
  41. otherwise,
  42. error(['Unknown property ''' num2str(varargin{i}) '''']);
  43. end
  44. end
  45. nUnit = length(unit_list);
  46. trial = behavior.trial;
  47. nTrial = max(trial);
  48. nBins_alltrial = [nBins*nTrial 1];
  49. ts_beh = behavior.ts;
  50. pos_cum = behavior.pos_cum;
  51. ok = trial > 0;
  52. ts_beh = ts_beh(ok);
  53. pos_cum = pos_cum(ok);
  54. pos_cum = (pos_cum-pos_cum(1))/(pos_cum(end)-pos_cum(1));
  55. if isrow(ts_beh)
  56. ts_beh = ts_beh';
  57. end
  58. if isrow(pos_cum)
  59. pos_cum = pos_cum';
  60. end
  61. ts_pos_beh = [ts_beh, pos_cum./max(pos_cum)];
  62. ts_tcs = (tcs.tt)';
  63. ok = ts_tcs >= ts_beh(1) & ts_tcs <= ts_beh(end);
  64. ts_tcs = ts_tcs(ok);
  65. if isrow(ts_tcs)
  66. ts_tcs = ts_tcs';
  67. end
  68. if use_lowpass
  69. ratio = lowpass(tcs.ratio, n_lowpass);
  70. else
  71. ratio = medfilt1(double(tcs.ratio), n_medf);
  72. end
  73. pos_map = zeros(nUnit, nBins(1));
  74. pos_map_norm_min = zeros(nUnit, 1);
  75. pos_map_norm_max = zeros(nUnit, 1);
  76. pos_map_ordered = zeros(nUnit, nBins(1));
  77. pos_map_trial = zeros(nTrial, nBins, nUnit);
  78. pos_map_trial_norm = zeros(nTrial, nBins, nUnit);
  79. unit_order = [];
  80. if ~use_deconv
  81. ratio = ratio(ok, :);
  82. y = ratio;
  83. else
  84. deconv = deconv(ok, :);
  85. y = deconv;
  86. end
  87. for iunit = 1:nUnit
  88. id_unit = unit_list(iunit);
  89. y_this = y(:, id_unit);
  90. map = Map(ts_pos_beh, [ts_tcs, y_this], 'smooth', nSmooth, 'nBins', nBins_alltrial);
  91. pos_map_tmp = reshape(map.z, nBins, nTrial);
  92. pos_map_tmp = pos_map_tmp';
  93. pos_map_trial(:,:,iunit) = pos_map_tmp;
  94. [max_val, ~] = max(pos_map_tmp, [], 2);
  95. [min_val, ~] = min(pos_map_tmp, [], 2);
  96. norm_val = max_val - min_val;
  97. ok = norm_val > 0.1; % silent trials, normalized by 1 (i.e. 0/1 = 0)
  98. pos_map_tmp(~ok, :) = 0;
  99. norm_val(~ok) = 1;
  100. pos_map_tmp = bsxfun(@minus, pos_map_tmp, min_val);
  101. pos_map_trial_norm(:,:,iunit) = bsxfun(@rdivide, pos_map_tmp, norm_val);
  102. a = mean(pos_map_tmp,1);
  103. pos_map(iunit, :) = a;
  104. pos_map(iunit, :) = (a-min(a))/(max(a)-min(a));
  105. pos_map_norm_min(iunit, :) = min(a);
  106. pos_map_norm_max(iunit, :) = max(a);
  107. [~, idx] = max(pos_map(iunit, :));
  108. unit_order = cat(1, unit_order, [id_unit, idx]);
  109. end
  110. [~, idx] = sort(unit_order(:, 2));
  111. for iunit = 1:nUnit
  112. i = idx(iunit);
  113. pos_map_ordered(iunit, :) = pos_map(i, :);
  114. end
  115. end