nets_examples_md.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. %%% FSLNets - simple network matrix estimation and applications
  2. %%% FMRIB Analysis Group
  3. %%% Copyright (C) 2012-2014 University of Oxford
  4. %%% See documentation at www.fmrib.ox.ac.uk/fsl
  5. clear;
  6. %%% change the following paths according to your local setup
  7. addpath /Users/pallastn/Desktop/Test/SCA/FSLNets % wherever you've put this package
  8. addpath /Users/pallastn/Desktop/Test/SCA/L1precision % L1precision toolbox
  9. addpath /Users/pallastn/Desktop/Test/SCA/pwling % pairwise causality toolbox
  10. ts_dir = 'group6';
  11. data_prefix = 'SP';
  12. seed_prefix = 'Seed_ROIs';
  13. pathName = '/Users/pallastn/Desktop/Test/SCA/ICA/stat';
  14. pathOfData = fullfile(pathName,ts_dir);
  15. %%% extract timeseries from data with seeds
  16. %nets_fslmeants_md(pathOfData,data_prefix,seed_prefix);
  17. %%% load timeseries data from the dual regression output directory
  18. % ts=nets_load(ts_dir,3,1);
  19. %%% arg2 is the TR (in seconds)
  20. %%% arg3 controls variance normalisation: 0=none, 1=normalise whole subject stddev, 2=normalise each separate timeseries from each subject
  21. % ts_spectra=nets_spectra(ts); % have a look at mean timeseries spectra
  22. ts=nets_load_md(pathOfData,'MasksTCs.',1.42,1);
  23. %ts_spectra=nets_spectra(ts); % have a look at mean spectra after this cleanup
  24. %%% create various kinds of network matrices and optionally convert correlations to z-stats.
  25. %%% here's various examples - you might only generate/use one of these.
  26. %%% the output has one row per subject; within each row, the net matrix is unwrapped into 1D.
  27. %%% ts: structure containing node information including all timeseries
  28. %%% the r2z transformation estimates an empirical correction for autocorrelation in the data.
  29. %%% r2z correction: set to 1 to use the r to z correction factor; set to 0 to leave netmats as z-score
  30. % netmats0=nets_netmats(ts,0,'cov'); % covariance (with variances on diagonal)
  31. % netmats0a=nets_netmats(ts,0,'amp'); % amplitudes only - no correlations (just the diagonal)
  32. % netmats1=nets_netmats(ts,1,'corr'); % full correlation (normalised covariances)
  33. % netmats2=nets_netmats(ts,1,'icov'); % partial correlation
  34. % netmats3=nets_netmats(ts,1,'icov',10); % L1-regularised partial, with lambda=10
  35. % netmats5=nets_netmats(ts,1,'ridgep'); % Ridge Regression partial, with rho=0.1
  36. % netmats11=nets_netmats(ts,0,'pwling'); % Hyvarinen's pairwise causality measure
  37. % netmats1=nets_netmats_md(ts,0,0,'corr');
  38. netmats1=nets_netmats_md(ts,1,0,'corr');
  39. % netmats1=nets_netmats_md(ts,1,1,'corr');
  40. % netmats2=nets_netmats_md(ts,1,0,'icov');
  41. % netmats5=nets_netmats_md(ts,1,0,'ridgep',1);
  42. %nets_savemat_md(pathOfData,ts_dir,netmats1,'netmats');
  43. %figure;
  44. %Mnet1 = reshape(netmats1(1,:),[sqrt(length(netmats1)) sqrt(length(netmats1))]);
  45. % imagesc(Mnet1,[0.0 1.0])
  46. % % imagesc(testMat,[0.0 1.0]);
  47. % colormap(jet);
  48. % colorbar; grid off;
  49. % return
  50. %%% view of consistency of netmats across subjects; returns t-test Z values as a network matrix
  51. %%% second argument (0 or 1) determines whether to display the Z matrix and a consistency scatter plot
  52. %%% third argument (optional) groups runs together; e.g. setting this to 4 means each group of 4 runs were from the same subject
  53. [Znet1,Mnet1]=nets_groupmean(netmats1,1); % test whichever netmat you're interested in; returns Z values from one-group t-test and group-mean netmat
  54. % [Znet5,Mnet5]=nets_groupmean(netmats5,1); % test whichever netmat you're interested in; returns Z values from one-group t-test and group-mean netmat
  55. %[Znet1,Mnet1]=nets_groupmean_md(netmats1,0);
  56. %
  57. %
  58. % nets_savemat_md(ts_dir,Znet1,'Znet');
  59. %nets_savemat_md(pathOfData,Mnet1,'Mnet');
  60. %
  61. %
  62. %
  63. %t = load('/Volumes/AG_Aswendt_Share/Scratch/Asw_fMRI2AllenBrain_Data/annotations.mat');
  64. %labels = t.annotations;
  65. % labels={'lM1/M2','lS1','lS2','lVC','lAC','lEntC','lCg','lPrL','lRSG/RSD','lMO','lGP','lCPu','lTh','lHyTh','lHp','lAmy','rM1/M2','rS1','rS2','rVC','rAC','rEntC','rCg','rPrL','rRSG/RSD','rMO','rGP','rCPu','rTh','rHyTh','rHp','rAmy'...
  66. % 'lM1/M2','lS1','lS2','lVC','lAC','lEntC','lCg','lPrL','lRSG/RSD','lMO','lGP','lCPu','lTh','lHyTh','lHp','lAmy','rM1/M2','rS1','rS2','rVC','rAC','rEntC','rCg','rPrL','rRSG/RSD','rMO','rGP','rCPu','rTh','rHyTh','rHp','rAmy','rVC'};
  67. % %
  68. %plotmat_values([pathOfData '/' ts_dir '_Mnet'], [pathName '/images/' ts_dir '_Mnet'],[0.0 1.0],0,labels,12);
  69. %plotmat_2in1([pathOfData '/' ts_dir '_Mnet'],[pathOfData '/' ts_dir '_Mnet'],['images/' ts_dir_2 '_Mnet_' ts_dir_1 '_Mnet'],[0.0 1.8],labels,10);
  70. %%% view hierarchical clustering of nodes
  71. %%% arg1 is shown below the diagonal (and drives the clustering/hierarchy); arg2 is shown above diagonal
  72. %nets_hierarchy(Znet1,Znet5,ts.DD,group_maps);
  73. %%% view interactive netmat web-based display
  74. % nets_netweb(Znet1,Znet5,ts.DD,group_maps,'netweb');
  75. %%% cross-subject GLM, with inference in randomise (assuming you already have the GLM design.mat and design.con files).
  76. %%% arg4 determines whether to view the corrected-p-values, with non-significant entries removed above the diagonal.
  77. % [p_uncorrected,p_corrected]=nets_glm(netmats1,'design.mat','design.con',1); % returns matrices of 1-p
  78. %%% OR - GLM, but with pre-masking that tests only the connections that are strong on average across all subjects.
  79. %%% change the "8" to a different tstat threshold to make this sparser or less sparse.
  80. % netmats=netmats3; [grotH,grotP,grotCI,grotSTATS]=ttest(netmats); netmats(:,abs(grotSTATS.tstat)<8)=0;
  81. % [p_uncorrected,p_corrected]=nets_glm(netmats,'design.mat','design.con',1);
  82. %design='Designs/nestin/design_n.mat';
  83. %contrast='Designs/nestin/design_n.con';
  84. %[p_uncorrected3,p_corrected3]=nets_glm(netmats3,design,contrast,1);
  85. %%% view 6 most significant edges from this GLM
  86. % nets_edgepics(ts,group_maps,Znet1,reshape(p_corrected(1,:),ts.Nnodes,ts.Nnodes),6);
  87. %%% simple cross-subject multivariate discriminant analyses, for just two-group cases.
  88. %%% arg1 is whichever netmats you want to test.
  89. %%% arg2 is the size of first group of subjects; set to 0 if you have two groups with paired subjects.
  90. %%% arg3 determines which LDA method to use (help nets_lda to see list of options)
  91. % [lda_percentages]=nets_lda(netmats3,36,1)
  92. %[lda_percentages]=nets_lda(netmats3,10,2) % Linear Discriminant Analysis (LDA)
  93. %[lda_percentages]=nets_lda(netmats3,10,8) % Support Vector Machines (SVM) classifier
  94. %%% create boxplots for the two groups for a network-matrix-element of interest (e.g., selected from GLM output)
  95. %%% arg3 = matrix row number, i.e. the first component of interest (from the DD list)
  96. %%% arg4 = matrix column number, i.e. the second component of interest (from the DD list)
  97. %%% arg5 = size of the first group (set to -1 for paired groups)
  98. % nets_boxplots(ts,netmats3,1,7,36);
  99. % print('-depsc',sprintf('boxplot-%d-%d.eps',IC1,IC2)); % example syntax for printing to file
  100. %nets_boxplots(ts,netmats3,3,6,2);