ModulesExtraction.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. %Starting For a Combined Matrix containing the average coordinates for each animals and relative postures
  2. %Extract Behavioral Modules by clustering accross different animals
  3. %CRITICAL! you'll need to assign a number to the variable nBM. This way you
  4. %can chose te number of BM to identify among all the posture extracted per
  5. %every single mouse. The choice of BM might result in an iterative process
  6. %where the user decides the number that better describes each subject. It
  7. %is reccomended to start with a number to N+/-1 respect to the lower number of
  8. %postures identified in one single subject
  9. %EXAMPLE: if in your cohort the lowest number of posture observed is 10
  10. %(in one or more subject), chose something between 9 and 11 to avoid data
  11. %distorsion where some subjects are overrepresented in one BM and other are not
  12. %present.
  13. % TODO needs rewriting
  14. nBMStr = inputdlg("Choose the number of BM you want.");
  15. nBM = str2num(nBMStr{1});
  16. [pathdir, subjdir, ExpClusters] = CombineData(17);
  17. Clusters=ExpClusters(:,17);
  18. % Run the clustering
  19. Modules=kmeans(ExpClusters(:,2:16),nBM);
  20. PrevModules=[ExpClusters,Modules];
  21. N=max(Modules);
  22. ModulesSummary=1:18;
  23. for Cluster2Explore=1:N;
  24. num=length(PrevModules);
  25. ClusterIndex=0;
  26. for ind=1:num;
  27. if PrevModules(ind,18)==Cluster2Explore;
  28. ClusterIndex=(ClusterIndex+1);
  29. Module(ClusterIndex,1:18)=PrevModules(ind,1:18);
  30. end
  31. end
  32. ClusterIndex=(ClusterIndex+1);
  33. ModulesSummary=[ModulesSummary;Module];
  34. clear Module
  35. end
  36. ModulesSummary(1,:)=[];
  37. PostureModulesAssigned=[ModulesSummary(:,1),ModulesSummary(:,17),ModulesSummary(:,18)];
  38. summaryPath = fullfile(subjdir, 'ModulesSummary.csv');
  39. posturePath = fullfile(subjdir, 'ModulesAssigned.csv');
  40. writematrix(ModulesSummary, summaryPath);
  41. writematrix(PostureModulesAssigned, posturePath);
  42. function [pathdir, subjdir, CombinedFile]=CombineData(column)
  43. % TODO rewrite docstring
  44. %input (required); number of colums of the table to combine (all table MUST
  45. %be same size)
  46. % output: pathdir and combined mean dir
  47. pathdir = uigetdir();
  48. subjdir = fullfile(pathdir, '..');
  49. FindMeans = dir(pathdir);
  50. disp(column);
  51. disp(subjdir);
  52. CombinedFile=zeros(1,column);
  53. for ind=1:length(FindMeans);
  54. file = FindMeans(ind);
  55. if ~file.isdir
  56. ThisPose=readmatrix(fullfile(pathdir, file.name));
  57. disp(size(CombinedFile))
  58. disp(size(ThisPose))
  59. CombinedFile=vertcat(CombinedFile,ThisPose);
  60. end
  61. end
  62. CombinedFile(1:1,:)=[];
  63. end