analysis.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/bin/sh
  2. CAT_DIR=$1
  3. WORK_DIR=$2
  4. echo "data folder in ${CAT_DIR}"
  5. echo "working directory is ${WORK_DIR}"
  6. chmod -R 777 ${WORK_DIR}
  7. cd ${WORK_DIR}
  8. touch ${WORK_DIR}/subjs_p1.txt
  9. touch ${WORK_DIR}/subjs_p2.txt
  10. touch ${WORK_DIR}/affines.txt
  11. for subject in $(find $CAT_DIR/ -maxdepth 5 -mindepth 1 -name "p1*T1w.nii" -printf '%P\n'); do
  12. p1_path=${CAT_DIR}/$(dirname $subject)
  13. echo "subject ${p1_path}"
  14. # gunzip -d --force ${p1_path}/p1*T1w.nii.gz
  15. # gunzip -d --force ${p1_path}/p2*T1w.nii.gz
  16. if [ "$?" -ne 0 ]; then
  17. echo "${subject} failed to copy T1 in node tmp \n"
  18. fi
  19. echo \'${p1_path}/$(find ${p1_path}/ -name "p1sub*_T1w.nii" -printf '%P\n')\' >> ${WORK_DIR}/subjs_p1.txt
  20. echo \'${p1_path}/$(find ${p1_path}/ -name "p2sub*_T1w.nii" -printf '%P\n')\' >> ${WORK_DIR}/subjs_p2.txt
  21. echo \'${p1_path}/$(find ${p1_path}/ -name "t_*T1w_affine_reorient.mat" -printf '%P\n')\' >> ${WORK_DIR}/affines.txt
  22. done
  23. echo "creating batch files"
  24. #make .m file
  25. touch ${WORK_DIR}/main_batch.txt
  26. echo -n "for i=1:1:size(subjs)
  27. clear matlabbatch
  28. matlabbatch{1}.spm.util.reorient.prefix = 'r_';
  29. matlabbatch{2}.spm.spatial.smooth.data(1) = cfg_dep('Reorient Images: Reoriented Images', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','files'));
  30. matlabbatch{2}.spm.spatial.smooth.fwhm = [4 4 4];
  31. matlabbatch{2}.spm.spatial.smooth.dtype = 0;
  32. matlabbatch{2}.spm.spatial.smooth.im = 0;
  33. matlabbatch{2}.spm.spatial.smooth.prefix = 's4';
  34. matlabbatch{3}.spm.spatial.coreg.write.ref = {'/extras/mask_8mm_binarized.nii,1'};
  35. matlabbatch{3}.spm.spatial.coreg.write.source(1) = cfg_dep('Smooth: Smoothed Images', substruct('.','val', '{}',{2}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','files'));
  36. matlabbatch{3}.spm.spatial.coreg.write.roptions.interp = 1;
  37. matlabbatch{3}.spm.spatial.coreg.write.roptions.wrap = [0 0 0];
  38. matlabbatch{3}.spm.spatial.coreg.write.roptions.mask = 0;
  39. matlabbatch{3}.spm.spatial.coreg.write.roptions.prefix = 'r8';
  40. matlabbatch{4}.spm.spatial.coreg.write.ref = {'/extras/mask_4mm_binarized.nii,1'};
  41. matlabbatch{4}.spm.spatial.coreg.write.source(1) = cfg_dep('Smooth: Smoothed Images', substruct('.','val', '{}',{2}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','files'));
  42. matlabbatch{4}.spm.spatial.coreg.write.roptions.interp = 1;
  43. matlabbatch{4}.spm.spatial.coreg.write.roptions.wrap = [0 0 0];
  44. matlabbatch{4}.spm.spatial.coreg.write.roptions.mask = 0;
  45. matlabbatch{4}.spm.spatial.coreg.write.roptions.prefix = 'r4';
  46. matlabbatch{5}.spm.spatial.smooth.data(1) = cfg_dep('Reorient Images: Reoriented Images', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','files'));
  47. matlabbatch{5}.spm.spatial.smooth.fwhm = [8 8 8];
  48. matlabbatch{5}.spm.spatial.smooth.dtype = 0;
  49. matlabbatch{5}.spm.spatial.smooth.im = 0;
  50. matlabbatch{5}.spm.spatial.smooth.prefix = 's8';
  51. matlabbatch{6}.spm.spatial.coreg.write.ref = {'/extras/mask_8mm_binarized.nii,1'};
  52. matlabbatch{6}.spm.spatial.coreg.write.source(1) = cfg_dep('Smooth: Smoothed Images', substruct('.','val', '{}',{5}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','files'));
  53. matlabbatch{6}.spm.spatial.coreg.write.roptions.interp = 1;
  54. matlabbatch{6}.spm.spatial.coreg.write.roptions.wrap = [0 0 0];
  55. matlabbatch{6}.spm.spatial.coreg.write.roptions.mask = 0;
  56. matlabbatch{6}.spm.spatial.coreg.write.roptions.prefix = 'r8';
  57. matlabbatch{7}.spm.spatial.coreg.write.ref = {'/extras/mask_4mm_binarized.nii,1'};
  58. matlabbatch{7}.spm.spatial.coreg.write.source(1) = cfg_dep('Smooth: Smoothed Images', substruct('.','val', '{}',{5}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','files'));
  59. matlabbatch{7}.spm.spatial.coreg.write.roptions.interp = 1;
  60. matlabbatch{7}.spm.spatial.coreg.write.roptions.wrap = [0 0 0];
  61. matlabbatch{7}.spm.spatial.coreg.write.roptions.mask = 0;
  62. matlabbatch{7}.spm.spatial.coreg.write.roptions.prefix = 'r4';
  63. matlabbatch{1}.spm.util.reorient.srcfiles = subjs(i);
  64. matlabbatch{1}.spm.util.reorient.transform.transF = affines(i);
  65. spm_jobman('run', matlabbatch);
  66. end" >> ${WORK_DIR}/main_batch.txt
  67. touch ${WORK_DIR}/p1_proc_batch.m
  68. echo -n 'subjs={' >> ${WORK_DIR}/p1_proc_batch.m
  69. cat ${WORK_DIR}/subjs_p1.txt >> ${WORK_DIR}/p1_proc_batch.m
  70. echo '};' >> ${WORK_DIR}/p1_proc_batch.m
  71. echo -n 'affines={' >> ${WORK_DIR}/p1_proc_batch.m
  72. cat ${WORK_DIR}/affines.txt >> ${WORK_DIR}/p1_proc_batch.m
  73. echo '};' >> ${WORK_DIR}/p1_proc_batch.m
  74. echo "writing from ${WORK_DIR}/main_batch.txt to ${WORK_DIR}/p1_proc_batch.m"
  75. cat ${WORK_DIR}/main_batch.txt >> ${WORK_DIR}/p1_proc_batch.m
  76. touch ${WORK_DIR}/p2_proc_batch.m
  77. echo -n 'subjs={' >> ${WORK_DIR}/p2_proc_batch.m
  78. cat ${WORK_DIR}/subjs_p2.txt >> ${WORK_DIR}/p2_proc_batch.m
  79. echo '}' >> ${WORK_DIR}/p2_proc_batch.m
  80. echo -n 'affines={' >> ${WORK_DIR}/p2_proc_batch.m
  81. cat ${WORK_DIR}/affines.txt >> ${WORK_DIR}/p2_proc_batch.m
  82. echo '};' >> ${WORK_DIR}/p2_proc_batch.m
  83. echo "writing from ${WORK_DIR}/main_batch.txt to ${WORK_DIR}/p2_proc_batch.m"
  84. cat ${WORK_DIR}/main_batch.txt >> ${WORK_DIR}/p2_proc_batch.m
  85. # run spm-octave
  86. chmod 777 ${WORK_DIR}*
  87. spm12 batch ${WORK_DIR}/p1_proc_batch.m
  88. spm12 batch ${WORK_DIR}/p2_proc_batch.m
  89. echo "finished resampling and smoothing"
  90. echo "starting features extraction"
  91. python /codes/save_feat.py ${CAT_DIR}
  92. echo "features are saved in subject's folder"
  93. python /codes/predict_age_stacking.py ${CAT_DIR}
  94. echo "age prediction is saved in subject's folder"