example_trial_histogram.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. clc; clear; close all;
  2. % Note that this histogram could easily be plotted with information that is
  3. % available from the meta data alone. However, this script is supposed to
  4. % demonstrate how to easily loop through the individual files and therefore
  5. % takes an approach that is slightly more time consuming.
  6. % This script takes a few seconds. On a Late 2013 iMac with a 3.4 GHz
  7. % Quad-Core Intel Core i5, running Matlab 2018b it takes around 7s.
  8. set(0, 'DefaultLineLineWidth', 2)
  9. set(0,'defaultAxesFontSize', 18)
  10. %% specify path
  11. codeDir = fileparts(mfilename('fullpath'));
  12. mainDir = codeDir(1:find(codeDir==filesep,1,'last'));
  13. dataDir = [mainDir,'data',filesep];
  14. % add all folders with code and data to the path
  15. addpath(genpath(mainDir));
  16. param2read = {'/event_value', '/event_time'};
  17. events2read = {'TRIAL_start', 'TRIAL_end'};
  18. sun_color = [254,94,105]/255;
  19. igg_color = [7, 182, 75]/255;
  20. edg_color = [0, 169, 255]/255;
  21. % read in meta data
  22. meta_data = readtable([dataDir,'meta_data.txt']);
  23. monkeys = cell2mat(meta_data.monkey);
  24. % Prepare Figure
  25. figure('Color',[1,1,1],'Units','centimeters','Position',[1, 1, 56, 24]);
  26. experiments = {'MSTm', 'MSTt', 'MSTn'};
  27. for exp = 1 : length(experiments)
  28. % the short way would be
  29. % trial_total = eval(['meta_data.Exp_',experiments{exp},'_tt']);
  30. % but we actually want to loop through all files and load them, maybe
  31. % because we don't trust the information in the meta data
  32. trial_total = [];
  33. for rec = 1:height(meta_data) % loop through recording sessions
  34. % construct the filename
  35. filename = ['amm-',experiments{exp},'-',meta_data.monkey{rec},...
  36. '-',strip(meta_data.session_number{rec},'both','"'),'-',...
  37. meta_data.daily_count{rec},'-task.h5'];
  38. try
  39. file_info = h5info(filename, '/event_value');
  40. % extract values and times of each event
  41. event_value = h5_extract(filename, param2read, events2read);
  42. % We perform a sanity check whether the TRIAL numbers are
  43. % equal (this is actually done in the technical validation
  44. % script, but repeated here for illustrative purposes)
  45. if (max(event_value.TRIAL_start) == max(event_value.TRIAL_end)) & (max(event_value.TRIAL_start) == length(event_value.TRIAL_start))
  46. trial_total = cat(1,trial_total,max(event_value.TRIAL_start));
  47. else
  48. error('Different trial numbers for TRIAL_start and TRIAL_end or length of TRIAL_start does not equal max');
  49. end
  50. catch
  51. fprintf("File %s does not seem to exist.\n", filename);
  52. trial_total = cat(1,trial_total,0);
  53. end
  54. end
  55. %% plot a distribution of the trial counts across recording sessions
  56. subplot(1,4,exp)
  57. box('off')
  58. sun_data = trial_total(sum(monkeys=='sun',2)==3);
  59. igg_data = trial_total(sum(monkeys=='igg',2)==3);
  60. edg_data = trial_total(sum(monkeys=='edg',2)==3);
  61. [~,edges] = histcounts(trial_total);
  62. Nsun = histcounts(sun_data,edges);
  63. Nigg = histcounts(igg_data,edges);
  64. Nedg = histcounts(edg_data,edges);
  65. ctrs = (edges(1:end-1)+edges(2:end))/2;
  66. b = bar(ctrs, [ Nsun', Nigg' Nedg'],1,'stacked');
  67. b(1).FaceColor = sun_color;
  68. b(2).FaceColor = igg_color;
  69. b(3).FaceColor = edg_color;
  70. set(gca,'XTick',ctrs(1:2:length(ctrs)));
  71. %histogram(trial_total,'BinWidth',75);
  72. xlabel('Number of trials');
  73. if exp==1
  74. ylabel('Count of recording sessions');
  75. end
  76. title(experiments{exp});
  77. %xlim([0,1400]);
  78. end
  79. % add legend
  80. sp4 = subplot(1,4,4);
  81. sp4.Visible = 'off';
  82. hold on
  83. LH(1) = plot(nan, nan, '*', 'color', sun_color);
  84. L{1} = 'sun';
  85. LH(2) = plot(nan, nan, '*', 'color', igg_color);
  86. L{2} = 'igg';
  87. LH(3) = plot(nan, nan, '*', 'color', edg_color);
  88. L{3} = 'edg';
  89. legend(LH, L, 'FontSize', 20); legend('box', 'off')
  90. legend('Location', 'west')