spm_dcm_graph.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. function spm_dcm_graph(xY,A)
  2. % Region and anatomical graph display
  3. % FORMAT spm_dcm_graph(xY,[A])
  4. % xY - cell of region structures (see spm_regions) (fMRI)
  5. % or ECD locations xY.Lpos and xY.Sname (EEG)
  6. % A - connections of weighted directed graph
  7. %__________________________________________________________________________
  8. % Copyright (C) 2010-2014 Wellcome Trust Centre for Neuroimaging
  9. % Karl Friston
  10. % $Id: spm_dcm_graph.m 6012 2014-05-22 18:21:41Z guillaume $
  11. % get dimensions, locations and names
  12. %--------------------------------------------------------------------------
  13. try
  14. % fMRI
  15. %----------------------------------------------------------------------
  16. m = size(xY,2);
  17. L = [];
  18. for i = 1:m
  19. L = [L xY(i).xyz];
  20. name{i} = xY(i).name(1:min(end,3));
  21. end
  22. catch
  23. % EEG
  24. %----------------------------------------------------------------------
  25. L = xY.Lpos;
  26. name = xY.Sname;
  27. end
  28. % display parameters
  29. %--------------------------------------------------------------------------
  30. col = {'b','g','r','c','m','y','k','w'};
  31. m = size(L,2);
  32. %-Render graph in anatomical space
  33. %==========================================================================
  34. ax = subplot(2,1,1); cla(ax);
  35. set(ax,'position',[0 .5 1 .5])
  36. options.query = [];
  37. options.hfig = ancestor(ax,'figure');
  38. options.ParentAxes = ax;
  39. options.markersize = 32;
  40. options.meshsurf = fullfile(spm('Dir'),'canonical','iskull_2562.surf.gii');
  41. spm_eeg_displayECD(L(:,1),[],0,[],options);
  42. options.meshsurf = fullfile(spm('Dir'),'canonical','cortex_8196.surf.gii');
  43. h = spm_eeg_displayECD(L,[],8,name,options);
  44. set(h.handles.ht,'FontWeight','bold')
  45. set(h.handles.mesh,'FaceAlpha',1/16);
  46. % return if no connectivity
  47. %--------------------------------------------------------------------------
  48. if nargin < 2, return, end
  49. % check for cell array connections (EEG)
  50. %--------------------------------------------------------------------------
  51. if iscell(A)
  52. W = 0;
  53. C = 0;
  54. for i = 1:length(A)
  55. C = C + abs(exp(A{i}));
  56. W = W + max(C,C');
  57. end
  58. A = C;
  59. elseif isnumeric(A)
  60. W = max(abs(A),abs(A'));
  61. else
  62. W = [];
  63. end
  64. % Connections - if weights (W) are defined
  65. %--------------------------------------------------------------------------
  66. if numel(W)
  67. W = W - diag(diag(W));
  68. W = 3*W/max(W(:));
  69. W = W.*(W > 1/128);
  70. for i = 1:length(A)
  71. for j = (i + 1):length(A)
  72. if W(i,j)
  73. % associate colour with the strongest influence
  74. %----------------------------------------------------------
  75. if abs(A(i,j)) > abs(A(j,i)), c = j; else c = i; end
  76. k = rem(c - 1,length(col)) + 1;
  77. line(L(1,[i j]),L(2,[i j]),L(3,[i j]),'Color',col{k},...
  78. 'LineStyle','-',...
  79. 'LineWidth',W(i,j));
  80. end
  81. end
  82. end
  83. end
  84. %-Render graph in functional space (with the locations U)
  85. %==========================================================================
  86. if isstruct(A)
  87. P.A = zeros(m,m);
  88. P = spm_dcm_fmri_graph_gen([],A,P);
  89. W = P.A;
  90. i = 1:min(size(A.x,1),3);
  91. U = zeros(3,m);
  92. U(i,:) = A.x(i,:);
  93. A = W;
  94. W = sign(W);
  95. else
  96. % Multidimensional scaling (with the Weighted Graph Laplacian)
  97. %----------------------------------------------------------------------
  98. D = diag(sum(W));
  99. G = D - W;
  100. [U,V] = eig(full(spm_pinv(G)));
  101. U = U*sqrt(V);
  102. [V,i] = sort(-diag(V));
  103. U = U(:,i(1:3))';
  104. end
  105. % Procrustean transform
  106. %----------------------------------------------------------------------
  107. U = spm_detrend(U')';
  108. U = real(U*40/max(abs(U(:))));
  109. ax = subplot(2,1,2); cla(ax);
  110. set(ax,'position',[0 0 1 .5])
  111. options.ParentAxes = ax;
  112. if m > 8; i = 8; else i = 16; end
  113. g = spm_eeg_displayECD(U,[],i,name,options);
  114. delete(g.handles.mesh)
  115. delete(findobj(get(gcf,'Children'),'Type','uicontrol'))
  116. for i = 1:m
  117. set(g.handles.ht(i),'FontWeight','bold')
  118. end
  119. % Connections
  120. %--------------------------------------------------------------------------
  121. for i = 1:m
  122. for j = (i + 1):m
  123. % associate colour with the strongest influence
  124. %------------------------------------------------------------------
  125. if abs(A(i,j)) > abs(A(j,i)), c = j; else c = i; end
  126. k = rem(c - 1,length(col)) + 1;
  127. if W(i,j) > 0
  128. line(U(1,[i j]),U(2,[i j]),U(3,[i j]),'Color',col{k},...
  129. 'LineStyle','-',...
  130. 'LineWidth', W(i,j));
  131. elseif W(i,j) < 0
  132. line(U(1,[i j]),U(2,[i j]),U(3,[i j]),'Color',col{k},...
  133. 'LineStyle','-.',...
  134. 'LineWidth',-W(i,j));
  135. end
  136. end
  137. end