Main_Load_NIX_Data.m 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. %% Close all figures, clear variables and command window
  2. close all
  3. clear
  4. clc
  5. %% Directory of the repository and the NIX library
  6. strMainPath = 'Human_Amygdala_MUA_sEEG_FearVideo'; % 'human_amygdala_mua_seeg_fearvideo'
  7. strNIXLibraryPath = 'nix-mx_Win64_1.4.1'; % or 'nix_mx_macOS_1.4.2_Matlab2020a'
  8. %% Add necessary folders to the MATLAB path
  9. addpath([strMainPath,filesep,'code_MATLAB',filesep])
  10. addpath(genpath(strNIXLibraryPath))
  11. warning('strMainPath should be the full path of the folder for the repository Human_Amygdala_MUA_sEEG_FearVideo (or human_amygdala_mua_seeg_fearvideo)')
  12. warning('strNIXLibraryPath should be the full path of the NIX library ''nix-mx_Win64_1.4.1''')
  13. %% NIX data files
  14. strNIXFileNames = dir([strMainPath,filesep,'data_NIX',filesep,'*.h5']);
  15. strNIXFileNames = {strNIXFileNames.name}';
  16. assert(~isempty(strNIXFileNames),'strMainPath should be the full path of the folder Human_Amygdala_MUA_sEEG_FearVideo')
  17. %% Select NIX file to open
  18. nFile = 1;
  19. %% Open NIX file
  20. strFilePath = [strMainPath,filesep,'data_NIX',filesep,strNIXFileNames{nFile}];
  21. try
  22. f = nix.File(strFilePath,nix.FileMode.ReadOnly);
  23. catch s
  24. if(strcmpi(s.message,'Undefined variable "nix" or class "nix.FileMode.ReadOnly".'))
  25. error('strNIXLibraryPath should be the full path of the NIX library ''nix-mx_Win64_1.4.1''')
  26. else
  27. error(s.message)
  28. end
  29. end
  30. %% Read metadata
  31. % Display names of all sections
  32. cellfun(@(x) disp(x.name),f.sections)
  33. %% General information
  34. sectionGeneral = f.openSection('General');
  35. % Properties
  36. cellfun(@(x) disp(x.name),sectionGeneral.properties)
  37. % Institution
  38. fprintf(['Institution: ',sectionGeneral.openProperty('Institution').values{1}.value,'\n'])
  39. % Recording location
  40. fprintf(['Recording location: ',sectionGeneral.openProperty('Recording location').values{1}.value,'\n'])
  41. % Sections
  42. cellfun(@(x) disp(x.name),sectionGeneral.sections)
  43. % Publications
  44. sectionPublications = sectionGeneral.openSection('Related publications');
  45. cellfun(@(x) disp(x.name),sectionPublications.properties)
  46. fprintf(['Publication name: ',sectionPublications.openProperty('Publication name').values{1}.value,'\n'])
  47. fprintf(['Publication DOI: ',sectionPublications.openProperty('Publication DOI').values{1}.value,'\n'])
  48. % Recording setup
  49. sectionRecordingSetup = sectionGeneral.openSection('Recording setup');
  50. cellfun(@(x) disp(x.name),sectionRecordingSetup.properties)
  51. fprintf(['Recording setup iEEG: ',sectionRecordingSetup.openProperty('Recording setup iEEG').values{1}.value,'\n'])
  52. %% Task information
  53. sectionTask = f.openSection('Task');
  54. % Properties
  55. cellfun(@(x) disp(x.name),sectionTask.properties)
  56. % Task characteristics
  57. fprintf(['Task name: ',sectionTask.openProperty('Task name').values{1}.value,'\n'])
  58. fprintf(['Task description: ',sectionTask.openProperty('Task description').values{1}.value,'\n'])
  59. fprintf(['Task URL: ',sectionTask.openProperty('Task URL').values{1}.value,'\n']) % TODO
  60. %% Subject information
  61. sectionSubject = f.openSection('Subject');
  62. % Properties
  63. cellfun(@(x) disp(x.name),sectionSubject.properties)
  64. % Subject characteristics
  65. fprintf(['Age: ',num2str(sectionSubject.openProperty('Age').values{1}.value),'\n'])
  66. fprintf(['Gender: ',sectionSubject.openProperty('Gender').values{1}.value,'\n']) % Sex?
  67. fprintf(['Pathology: ',sectionSubject.openProperty('Pathology').values{1}.value,'\n'])
  68. fprintf(['Depth electrodes: ',sectionSubject.openProperty('Depth electrodes').values{1}.value,'\n'])
  69. fprintf(['Electrodes in seizure onset zone (SOZ): ',sectionSubject.openProperty('Electrodes in seizure onset zone (SOZ)').values{1}.value,'\n'])
  70. %% Session information
  71. sectionSession = f.openSection('Session');
  72. % Properties
  73. cellfun(@(x) disp(x.name),sectionSession.properties)
  74. % Session characteristics
  75. fprintf(['Number of trials: ',num2str(sectionSession.openProperty('Number of trials').values{1}.value),'\n'])
  76. fprintf(['Trial duration: ',num2str(sectionSession.openProperty('Trial duration').values{1}.value),' ',sectionSession.openProperty('Trial duration').unit,'\n'])
  77. % Sections
  78. cellfun(@(x) disp(x.name),sectionSession.sections)
  79. %% Trial information
  80. sectionTrialProperties = sectionSession.openSection('Trial properties');
  81. % Sections
  82. cellfun(@(x) disp(x.name),sectionTrialProperties.sections)
  83. % Each section is for a single trial
  84. % Select trial
  85. nTrial = 1;
  86. sectionSingleTrial = sectionTrialProperties.sections{nTrial};
  87. %% Single trial
  88. % Properties
  89. cellfun(@(x) disp(x.name),sectionSingleTrial.properties)
  90. for nProperty = 1:length(sectionSingleTrial.properties)
  91. switch sectionSingleTrial.properties{nProperty}.datatype
  92. case {'double','logical'}
  93. fprintf([sectionSingleTrial.properties{nProperty}.name,': ',num2str(sectionSingleTrial.properties{nProperty}.values{1}.value),'\n'])
  94. otherwise
  95. fprintf([sectionSingleTrial.properties{nProperty}.name,': ',sectionSingleTrial.properties{nProperty}.values{1}.value,'\n'])
  96. end
  97. end
  98. %% Blocks
  99. cellfun(@(x) disp(x.name),f.blocks)
  100. block = f.blocks{1};
  101. %% Groups of data arrays, tags, multitags
  102. groups = block.groups;
  103. cellfun(@(x) disp(x.name),block.groups)
  104. %% Select trial
  105. nTrial = 1;
  106. %% Trial events
  107. group_TrialEvents_iEEG = block.openGroup('Trial events single tags iEEG');
  108. %% Trial events for a single trial
  109. indTrialTags_iEEG = contains(cellfun(@(x) x.name,group_TrialEvents_iEEG.tags,'UniformOutput',0),['Trial_',num2str(nTrial,'%.2d')]);
  110. TrialEvents_iEEG = group_TrialEvents_iEEG.tags(indTrialTags_iEEG);
  111. cellfun(@(x) disp(x.name),TrialEvents_iEEG)
  112. %% Time of a single event
  113. nSingleTag = 1; % Gives the single tag for the trial condition
  114. fprintf(['Event name: ',TrialEvents_iEEG{nSingleTag}.name,'\n'])
  115. fprintf([sprintf('Time w.r.t. fixation: %.1f %s',TrialEvents_iEEG{nSingleTag}.position(2),TrialEvents_iEEG{nSingleTag}.units{1}),'\n'])
  116. fprintf([sprintf('Duration: %.1f %s',TrialEvents_iEEG{nSingleTag}.extent(2),TrialEvents_iEEG{nSingleTag}.units{1}),'\n'])
  117. %% iEEG data
  118. group_iEEG = block.openGroup('iEEG data');
  119. % Data array
  120. dataArray_iEEG = group_iEEG.dataArrays{nTrial};
  121. % Electrode labels
  122. striEEGLabels = dataArray_iEEG.dimensions{1}.labels;
  123. % Time axis
  124. if(length(dataArray_iEEG.dataExtent)==2)
  125. tiEEG = (0:(double(dataArray_iEEG.dataExtent(2))-1))*dataArray_iEEG.dimensions{2}.samplingInterval+dataArray_iEEG.dimensions{2}.offset;
  126. else
  127. tiEEG = (0:(double(dataArray_iEEG.dataExtent(1))-1))*dataArray_iEEG.dimensions{2}.samplingInterval+dataArray_iEEG.dimensions{2}.offset;
  128. end
  129. % Read data
  130. data_iEEG = dataArray_iEEG.readAllData;
  131. % Plot trial data
  132. figure
  133. plot(tiEEG,data_iEEG)
  134. title(['iEEG data - Trial ',num2str(nTrial)])
  135. xlabel(['Time (',dataArray_iEEG.dimensions{2}.unit,')'])
  136. ylabel(['Voltage (',dataArray_iEEG.unit,')'])
  137. xlim([tiEEG(1),tiEEG(end)])
  138. legend(striEEGLabels)
  139. %% Electrode information
  140. groupiEEGElecrodes = block.openGroup('iEEG electrode information');
  141. %% Sources of the iEEG data
  142. for nSource = 1:dataArray_iEEG.sourceCount
  143. sourceUnit = dataArray_iEEG.sources{nSource};
  144. % Electrode label
  145. sourceUnit.sources{1}.name
  146. % Anatomical location
  147. sourceUnit.sources{2}.name
  148. % Inside/outside SOZ
  149. sourceUnit.sources{3}.name
  150. nElectrode = find(strcmpi(cellfun(@(x) x.name,groupiEEGElecrodes.sources,'UniformOutput',0),sourceUnit.name));
  151. % MNI coordinates of the electrode
  152. groupiEEGElecrodes.multiTags{1}.retrieveFeatureData(nElectrode,'iEEG_Electrode_MNI_Coordinates')
  153. end
  154. %% Units
  155. group_SpikeTimesMultitags = block.openGroup('Spike times multitags');
  156. if(group_SpikeTimesMultitags.multiTagCount==0)
  157. warning('This subject does not have any units')
  158. else
  159. %% Select unit and trial
  160. nUnit = 1;
  161. nTrial = 5;
  162. % MultiTag for the selected unit and trial
  163. indUnitSpikeTimes = contains(cellfun(@(x) x.name,group_SpikeTimesMultitags.multiTags,'UniformOutput',0),['Unit_',num2str(nUnit),'_'])&...
  164. contains(cellfun(@(x) x.name,group_SpikeTimesMultitags.multiTags,'UniformOutput',0),['Trial_',num2str(nTrial,'%.2d')]);
  165. multiTag_SpikeTimes = group_SpikeTimesMultitags.multiTags{indUnitSpikeTimes};
  166. %% Spike times
  167. dataArray_SpikeTimes = multiTag_SpikeTimes.openPositions;
  168. % Read data
  169. SpikeTimes = dataArray_SpikeTimes.readAllData;
  170. figure
  171. stem(dataArray_SpikeTimes.readAllData,ones(length(SpikeTimes),1))
  172. title(['Spike times - Unit ',num2str(nUnit),' - Trial ',num2str(nTrial)])
  173. xlabel(['Time (',dataArray_SpikeTimes.dimensions{1}.unit,')'])
  174. %% Waveform for the unit
  175. dataArray_Waveform = multiTag_SpikeTimes.features{1}.openData;
  176. % Time axis
  177. tWaveform = (0:(double(dataArray_Waveform.dataExtent(2))-1))*dataArray_Waveform.dimensions{2}.samplingInterval+dataArray_Waveform.dimensions{2}.offset;
  178. % Read data
  179. waveform = dataArray_Waveform.readAllData;
  180. % Plot
  181. figure
  182. plot(tWaveform,waveform(1,:),'b')
  183. hold on
  184. plot(tWaveform,waveform(1,:)+waveform(2,:),'b--')
  185. plot(tWaveform,waveform(1,:)-waveform(2,:),'b--')
  186. title(['Waveform - Unit ',num2str(nUnit)])
  187. xlabel(['Time (',dataArray_Waveform.dimensions{2}.unit,')'])
  188. ylabel(['Voltage (',dataArray_Waveform.unit,')'])
  189. legend({'Mean','Std'})
  190. %% Source of the unit
  191. sourceUnit = dataArray_SpikeTimes.sources{1};
  192. % Electrode label
  193. sourceUnit.sources{1}.name
  194. % Anatomical location
  195. sourceUnit.sources{2}.name
  196. % Inside/outside SOZ
  197. sourceUnit.sources{3}.name
  198. %% Use electrode map to get properties of the macroelectrode the unit is on
  199. nElectrode = find(strcmpi(cellfun(@(x) x.name,groupiEEGElecrodes.sources,'UniformOutput',0),sourceUnit.name));
  200. % MNI coordinates of the electrode
  201. groupiEEGElecrodes.multiTags{1}.retrieveFeatureData(nElectrode,'iEEG_Electrode_MNI_Coordinates')
  202. end