KTUEAImpedanceFile.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. classdef KTUEAImpedanceFile
  2. % Plots a graphical representation of the impedance data collected by
  3. % Cerebus and saved in a text file.
  4. %
  5. % Usage:
  6. %
  7. % plotImpedances(mapfile)
  8. %
  9. % WHERE:
  10. %
  11. % mapfile: Is the mapfile structure obtained with mapfileLoader
  12. % function that maps the electrodes to channel numbers. If
  13. % mapfile is not provided then a default map will be used.
  14. %
  15. % To see a list of methods type methods(plotImpedances).
  16. %
  17. % Kian Torab
  18. % ktorab@blackrockmicro.com
  19. % Blackrock Microsystems
  20. % Version 1.1.0
  21. %
  22. % Ideas borrowed from Sergey Stavisky, The BrainGate Project
  23. properties (Hidden, SetAccess = private, GetAccess = private)
  24. chanCount = 96;
  25. impedanceDataValues = NaN(1, 96);
  26. Mapfile
  27. fileHandle
  28. pathHandle
  29. end
  30. methods(Hidden)
  31. function obj = KTUEAImpedanceFile(Mapfile)
  32. if ~exist('Mapfile', 'var')
  33. obj.Mapfile = KTUEAMapFile;
  34. else
  35. obj.Mapfile = Mapfile;
  36. end
  37. if ~obj.Mapfile.isValid; return; end;
  38. if exist('getFile.m', 'file') == 2
  39. [obj.fileHandle obj.pathHandle] = getFile('*.txt', 'Open an impedance file...');
  40. else
  41. [obj.fileHandle obj.pathHandle] = uigetfile('*.txt', 'Open an impedance file...');
  42. end
  43. if ~obj.fileHandle; disp('No file was selected.'); return; end;
  44. impedanceDataCell = importdata([obj.pathHandle obj.fileHandle], ' ', 200);
  45. impedanceDataCell(1:9) = [];
  46. impedanceDataCell(97:end) = [];
  47. impedanceDataCellParsed = regexp(impedanceDataCell, '\t', 'split');
  48. for i = 1:size(impedanceDataCellParsed, 1)
  49. impedanceSingleCell = impedanceDataCellParsed{i,:}(2);
  50. impedanceSingleText = impedanceSingleCell{:};
  51. obj.impedanceDataValues(i) = str2double(impedanceSingleText(1:end-4));
  52. end
  53. end
  54. end
  55. methods
  56. function FilePath = PathName(obj)
  57. FilePath = obj.pathHandle;
  58. end
  59. function FileName = Filename(obj)
  60. FileName = obj.fileHandle;
  61. end
  62. function validFlag = isValid(obj)
  63. if any(isnan(obj.getChannelImpedances))
  64. validFlag = 0;
  65. else
  66. validFlag = 1;
  67. end
  68. end
  69. function impedanceDataValues = getImpedances(obj)
  70. impedanceDataValues = obj.impedanceDataValues;
  71. end
  72. function impedanceDataValue = getChannelImpedance(obj, chanNum)
  73. if ~exist('chanNum', 'var')
  74. disp('Channel number is a required argument.');
  75. return;
  76. end
  77. impedanceDataValue = obj.impedanceDataValues(chanNum);
  78. end
  79. function analyzedStat = getImpedanceMean(obj)
  80. analyzedStat = mean(obj.impedanceDataValues);
  81. end
  82. function analyzedStat = getImpedanceMedian(obj)
  83. analyzedStat = median(obj.impedanceDataValues);
  84. end
  85. function analyzedStat = getImpedanceRange(obj)
  86. analyzedStat = obj.getImpedanceMax-obj.getImpedanceMin;
  87. end
  88. function analyzedStat = getImpedanceSTD(obj)
  89. analyzedStat = std(obj.impedanceDataValues);
  90. end
  91. function analyzedStat = getImpedance975Perc(obj)
  92. sortedImpedances = sort(obj.impedanceDataValues, 'ascend');
  93. analyzedStat = sortedImpedances(round(obj.chanCount*0.975));
  94. end
  95. function analyzedStat = getImpedance750Perc(obj)
  96. sortedImpedances = sort(obj.impedanceDataValues, 'ascend');
  97. analyzedStat = sortedImpedances(round(obj.chanCount*0.75));
  98. end
  99. function analyzedStat = getImpedance250Perc(obj)
  100. sortedImpedances = sort(obj.impedanceDataValues, 'ascend');
  101. analyzedStat = sortedImpedances(round(obj.chanCount*0.25));
  102. end
  103. function analyzedStat = getImpedance25Perc(obj)
  104. sortedImpedances = sort(obj.impedanceDataValues, 'ascend');
  105. analyzedStat = sortedImpedances(round(obj.chanCount*0.025));
  106. end
  107. function analyzedStat = getImpedanceMin(obj)
  108. analyzedStat = min(obj.impedanceDataValues);
  109. end
  110. function analyzedStat = getImpedanceMax(obj)
  111. analyzedStat = max(obj.impedanceDataValues);
  112. end
  113. function plotStatistics(obj)
  114. figure('Name', 'Array Statistics');
  115. title('Array Impedance Values Statistics');
  116. set(gcf, 'Position', [657 540 288 287]);
  117. axis off;
  118. text(0,0.9, 'Mean'); text(0.6,0.9, num2str(obj.getImpedanceMean, '%5.1f'));
  119. text(0,0.8, 'Median'); text(0.6,0.8, num2str(obj.getImpedanceMean, '%5.1f'));
  120. text(0,0.7, 'Range'); text(0.6,0.7, num2str(obj.getImpedanceRange, '%5.1f'));
  121. text(0,0.6, 'STD'); text(0.6,0.6, num2str(obj.getImpedanceSTD, '%5.1f'));
  122. text(0,0.5, '97.5th Percentile'); text(0.6,0.5, num2str(obj.getImpedance975Perc, '%5.1f'));
  123. text(0,0.4, '75th Percentile'); text(0.6,0.4, num2str(obj.getImpedance750Perc, '%5.1f'));
  124. text(0,0.3, '25th Percentile'); text(0.6,0.3, num2str(obj.getImpedance250Perc, '%5.1f'));
  125. text(0,0.2, '2.5th Percentile'); text(0.6,0.2, num2str(obj.getImpedance25Perc, '%5.1f'));
  126. text(0,0.1, 'Min'); text(0.6,0.1, num2str(obj.getImpedanceMin, '%5.1f'));
  127. text(0,0.0, 'Max'); text(0.6,0.0, num2str(obj.getImpedanceMax, '%5.1f'));
  128. end
  129. function plotHistogram(obj, binCount, maxRange)
  130. if ~exist('binCount', 'var')
  131. binCount = 20;
  132. end
  133. if ~exist('maxRange', 'var')
  134. if max(obj.impedanceDataValues) > 10000
  135. maxRange = 10000;
  136. else
  137. maxRange = max(obj.impedanceDataValues);
  138. end
  139. end
  140. hist(obj.impedanceDataValues, min(obj.impedanceDataValues):...
  141. maxRange/binCount:...
  142. maxRange,1);
  143. title('Impedance Values Histogram');
  144. xlabel('Impedance Values (kOhm)');
  145. ylabel('Impedance Occurances');
  146. end
  147. function plotBoxPlot(obj, boxStyle)
  148. if ~exist('boxStyle', 'var');
  149. boxStyle = 'traditional';
  150. end
  151. sortedImpedances = sort(obj.impedanceDataValues);
  152. boxplot(obj.impedanceDataValues);
  153. title(['Box Plot (' boxStyle ')']);
  154. ylabel('Impedance Values (kOhm)');
  155. set(gca, 'xTickLabel', ' ');
  156. xlim([0.9, 1.1]);
  157. set(gcf, 'Position', [632 356 235 398]);
  158. end
  159. function plotImpedances(obj, yelThreshold, redThreshold)
  160. if ~exist('yelThreshold', 'var')
  161. yelThreshold = 100;
  162. end
  163. if ~exist('redThreshold', 'var')
  164. redThreshold = 800;
  165. end
  166. plotFigure = KTFigure;
  167. plotFigure.EnlargeFigure;
  168. plotFigure.MakeBackgroundWhite;
  169. hold on;
  170. for channelIDX = 1:obj.chanCount
  171. if obj.impedanceDataValues(channelIDX) > redThreshold
  172. spColor = [1,0,0];
  173. fColor = [1,1,1];
  174. elseif obj.impedanceDataValues(channelIDX) < redThreshold && obj.impedanceDataValues(channelIDX) > yelThreshold
  175. spColor = [0,0,0];
  176. fColor = [1,1,1];
  177. elseif obj.impedanceDataValues(channelIDX) < yelThreshold
  178. spColor = [1,1,0];
  179. fColor = [0,0,0];
  180. end
  181. obj.Mapfile.GenerateChannelSubplot(channelIDX);
  182. obj.Mapfile.GenerateChannelSubplotNames(channelIDX, fColor);
  183. text(0.05, 0.7, [num2str(obj.impedanceDataValues(channelIDX)) ' kOhm'], 'Color', fColor, 'FontSize', 10, 'FontWeight', 'bold');
  184. axis on;
  185. obj.Mapfile.setAxisBackgroundColor(spColor);
  186. obj.Mapfile.setAxesColor(spColor);
  187. end
  188. hold off;
  189. plotFigure.ShowFigure;
  190. end
  191. function plotImpedancesColorBackground(obj)
  192. for channelIDX = 1:obj.chanCount
  193. obj.Mapfile.GenerateChannelSubplot(channelIDX);
  194. axis on;
  195. obj.Mapfile.setAxisBackgroundColor([0,obj.impedanceDataValues(channelIDX)/max(max(obj.impedanceDataValues)),0]);
  196. end
  197. end
  198. function plotImpedancesComparison(obj)
  199. changeThreshold = 0.2;
  200. yelThreshold = 100;
  201. secImpedanceFile = KTUEAImpedanceFile(obj.Mapfile);
  202. secImpedanceValues = secImpedanceFile.getChannelImpedances;
  203. plotFigure = KTFigure;
  204. plotFigure.EnlargeFigure;
  205. plotFigure.MakeBackgroundWhite;
  206. hold on;
  207. for channelIDX = 1:obj.chanCount
  208. percentChange = (obj.impedanceDataValues(channelIDX) - secImpedanceValues(channelIDX))/obj.impedanceDataValues(channelIDX);
  209. if abs(percentChange) > changeThreshold && percentChange > 0
  210. % Lime Green -- If impedance is increased by more
  211. % than threshold.
  212. if abs(percentChange) > 2 * changeThreshold
  213. spColor = [34,139,34] ./255;
  214. fColor = [1,1,1];
  215. else
  216. spColor = [154,205,50] ./255;
  217. fColor = [0,0,0];
  218. end
  219. elseif abs(percentChange) > changeThreshold && percentChange < 0
  220. % Chocolate orange -- If impedance is dropped by more
  221. % than threshold.
  222. if abs(percentChange) > 2 * changeThreshold
  223. spColor = [210,105,30] ./255;
  224. fColor = [1,1,1];
  225. else
  226. spColor = [245,222,179] ./255;
  227. fColor = [0,0,0];
  228. end
  229. else
  230. % Honeydew -- If threshold change is less than
  231. % threshold.
  232. spColor = [240,255,240] ./255;
  233. fColor = [0,0,0];
  234. end
  235. obj.Mapfile.GenerateChannelSubplot(channelIDX);
  236. patchHandle = patch([0.5, 0.5, 0, 0], [1, 0, 0, 1], spColor);
  237. obj.Mapfile.GenerateChannelSubplotNames(channelIDX, fColor);
  238. text(0.05, 0.7, ['1: ' num2str(obj.impedanceDataValues(channelIDX)) ' kOhm'], 'Color', fColor, 'FontSize', 10, 'FontWeight', 'bold');
  239. text(0.05, 0.5, ['2: ' num2str(secImpedanceValues(channelIDX)) ' kOhm'], 'Color', fColor, 'FontSize', 10, 'FontWeight', 'bold');
  240. end
  241. hold off;
  242. plotFigure.ShowFigure;
  243. end
  244. end
  245. end