MIWithinBetweenBandsAllSubjectsAllTimesAllConditions2.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. %This is a script estimating time-delayed phase MI for a series of trials and provides an average
  2. %MI table for all trials of each condition, each timepoint, and each subject. The
  3. %folder is expected to have a sub-directory with the files needed for the
  4. %script to run called 'FilesNeeded', another one in which the output data will be saved called 'MusicPlast_WithinBetweenMI',
  5. %the script it self, and the timeseries to be analyzed.
  6. %Theoretically once the folder is the current directory of matlab, you may
  7. %just run it with no further input needed. Just hit the green button and
  8. %wait.... a lot!
  9. %Add necessary toolbox GCMI to the path!! it is supposed to be in a sub-folder of the current directory called FilesNeeded
  10. % addpath('./FilesNeeded/GCMI');
  11. % create a list with all the files to be processed
  12. filelist=dir('*_Hilbert.mat');
  13. % Get lists of subjects, times and conditions to classify the files
  14. filedetails = getdetails(filelist); %This calls a function placed at the end of the script that generates a table with the files' subject names, condition1, condition2.
  15. %Load lists of files characteristics to be used for classifying the files
  16. load('./FilesNeeded/Subjects.mat'); % A file (string array) with the subject names. It is supposed to be in a sub-folder of the current directory called FilesNeeded
  17. load('./FilesNeeded/Times.mat'); % A file (string array) with the times [Variable1]. It is supposed to be in a sub-folder of the current directory called FilesNeeded
  18. load('./FilesNeeded/Conditions.mat'); % A file (string array) with the conditions [Variable2]. It is supposed to be in a sub-folder of the current directory called FilesNeeded
  19. %Define the saving folder
  20. savingFolder = './MusicPlast_WithinBetween_MI/'; % change accordingly
  21. %Select files on the basis of subject
  22. for s=1:length(Subjects)
  23. subjectName = Subjects(s);
  24. %Select files on the basis of condition 1
  25. for t=1:length(Times)
  26. TimeName = Times(t);
  27. %Select files on the basis of condition 2
  28. for c=1:length(Conditions)
  29. conditionName = Conditions(c);
  30. %Select all files satisfying condition1 condition2 subject selection
  31. selectedfiles = filedetails.Filename(filedetails.Subject == subjectName & filedetails.Condition == conditionName & filedetails.Time == TimeName);
  32. %Select each of the files, one by one...
  33. for f=1%:length(selectedfiles)
  34. WorkFile = importdata(selectedfiles(f));
  35. %Start MI estimation
  36. WorkVariable = WorkFile.TF; % Name of the variable to work with
  37. %Define parameters.
  38. numSamples = 4001; %Number of samples in your trial
  39. minDelay = 16; %Minimum delay for MI
  40. maxDelay = 16; %Max delay for MI
  41. numberOfFreqBands = 4; %How many bands are you interested in?
  42. numSources = 360; %Number of sournces in your atlas
  43. %Caclulate initial tables based on the parameters defined above
  44. numSamplesMinusMinDelay = numSamples-minDelay;
  45. mi=zeros(1,numSamples);
  46. miAtlasSingleSource=zeros(1,numSources);
  47. miAtlasAllSources=zeros(numSources,numSources);
  48. miAtlasAllPhaseBands=zeros((numSources*numberOfFreqBands),(numSources*numberOfFreqBands));
  49. atlasLength=[1:numSources:(numSources*(numberOfFreqBands+1))];atlasLength(numberOfFreqBands+1)=(numSources*(numberOfFreqBands));
  50. %Estimate Within and Between Bands Time delayed Mutual Info Of Phase
  51. for pOne=1:numberOfFreqBands%for all phase bands
  52. for pTwo=1:numberOfFreqBands
  53. for k=1:numSources%for all sources as 1st part of comparison
  54. for i=1:numSources%for all sources as 2nd part of comparison
  55. %search mutual information of phase
  56. for d=minDelay:maxDelay%minDelay:numSamplesMinusMinDelay %search across all delays (from minDelay+)// or Choose 16ms based on https://doi.org/10.1016/j.neuroimage.2013.11.047
  57. mi(1,d)=mi_gg((copnorm(((circshift((WorkVariable(k,:,pOne)),d,2))'))), (copnorm(((WorkVariable(i,:,pTwo)))')));
  58. % mi(1,1:minDelay)=0; mi(1,numSamplesMinusMinDelay:numSamples)=0; %zero out 1st and last
  59. miMax=max(mi);%take the max of all delays
  60. end
  61. miAtlasSingleSource(1,i)=miMax;
  62. end
  63. miAtlasAllSources(k,:)=miAtlasSingleSource;
  64. miAtlasAllSourcesNorm = normalize(miAtlasAllSources);
  65. end
  66. miAtlasAllPhaseBands(atlasLength(pOne):((atlasLength(pOne)+(numSources-1))),atlasLength(pTwo):((atlasLength(pTwo)+(numSources-1))))=miAtlasAllSourcesNorm;
  67. end
  68. end
  69. %Create the outcome table for this file (trial)
  70. trialHypernetwork = miAtlasAllPhaseBands;
  71. % End MI estimation - Add each trial to a subject matrix.
  72. SubjectTimeConditionTrialMatrix=zeros((numSources*numberOfFreqBands),(numSources*numberOfFreqBands),f);
  73. SubjectTimeConditionTrialMatrix(:,:,f)= trialHypernetwork;
  74. end
  75. % Average MI files of each trial to create one hypernetwork
  76. SubjectTimeConditionMatrix= mean(SubjectTimeConditionTrialMatrix,3);
  77. %Save output file for subect-condition1-condition2
  78. combinedStr = strcat(subjectName,'_',TimeName,'_',conditionName); %replace name!!!
  79. combinedStr2 = strcat(savingFolder,combinedStr);
  80. save(combinedStr2,'SubjectTimeConditionMatrix');
  81. end
  82. end
  83. end
  84. % Function to be used for finding the correct files. It creates a table
  85. % that has the characteristics of the file names [subject names, condition1, condition2, complete file name]
  86. function filedetails = getdetails(filelist) %filelist: a structure array generated in the begining of the script
  87. filenames = vertcat(filelist.name); %convert the list of filename into a char array
  88. %temporary variables to create the table. Using string to make comparison easier
  89. Subject = string(filenames(:, 1:5)); % Subject names
  90. Time = string(filenames(:, 7:10)); % Post or Prex
  91. Condition = string(filenames(:, 12:17)); % Standx / AudDev / AvDevx / VisDev
  92. Filename = string({filelist.name}'); %complete file name
  93. %create the table
  94. filedetails = table(Subject, Time, Condition, Filename);
  95. end