├── README.md ├── how_to_use.txt ├── lib ├── NIfTI_20140122 │ ├── affine.m │ ├── bipolar.m │ ├── bresenham_line3d.m │ ├── clip_nii.m │ ├── collapse_nii_scan.m │ ├── examples.txt │ ├── expand_nii_scan.m │ ├── extra_nii_hdr.m │ ├── flip_lr.m │ ├── get_nii_frame.m │ ├── license.txt │ ├── load_nii.m │ ├── load_nii_ext.m │ ├── load_nii_hdr.m │ ├── load_nii_img.m │ ├── load_untouch0_nii_hdr.m │ ├── load_untouch_header_only.m │ ├── load_untouch_nii.m │ ├── load_untouch_nii_hdr.m │ ├── load_untouch_nii_img.m │ ├── make_ana.m │ ├── make_nii.m │ ├── mat_into_hdr.m │ ├── pad_nii.m │ ├── reslice_nii.m │ ├── rri_file_menu.m │ ├── rri_orient.m │ ├── rri_orient_ui.m │ ├── rri_select_file.m │ ├── rri_xhair.m │ ├── rri_zoom_menu.m │ ├── save_nii.m │ ├── save_nii_ext.m │ ├── save_nii_hdr.m │ ├── save_untouch0_nii_hdr.m │ ├── save_untouch_header_only.m │ ├── save_untouch_nii.m │ ├── save_untouch_nii_hdr.m │ ├── save_untouch_slice.m │ ├── unxform_nii.m │ ├── verify_nii_ext.m │ ├── view_nii.m │ ├── view_nii_menu.m │ └── xform_nii.m ├── Ncut_9 │ ├── ICgraph.m │ ├── NcutImage.m │ ├── README.txt │ ├── X2distances.m │ ├── a_times_b_cmplx.cpp │ ├── affinityic.cpp │ ├── affinityic.mexw64 │ ├── build_scene.m │ ├── cimgnbmap.cpp │ ├── cimgnbmap.mexw64 │ ├── compileDir_simple.m │ ├── computeEdges.m │ ├── computeW.m │ ├── compute_relation.m │ ├── compute_relation2.m │ ├── demoNcutClustering.m │ ├── demoNcutImage.m │ ├── discretisation.m │ ├── discretisationEigenVectorData.m │ ├── doog1.m │ ├── doog2.m │ ├── eigs_new.m │ ├── fft_filt_2.m │ ├── gaussian.m │ ├── imread_ncut.m │ ├── make_filterbank_even2.m │ ├── make_filterbank_odd2.m │ ├── mex_w_times_x_symmetric.cpp │ ├── mex_w_times_x_symmetric.mexw64 │ ├── ncut.m │ ├── ncutW.m │ ├── quadedgep.m │ ├── showmask.m │ ├── sparsifyc.cpp │ ├── sparsifyc.mexw64 │ ├── spmtimesd.cpp │ └── spmtimesd.mexw64 ├── cifti │ ├── ciftiopen.m │ ├── ciftisave.m │ └── ciftisavereset.m ├── freesurfer │ ├── matlab │ │ ├── ComputeGeodesicProjection.m │ │ ├── MRIeuler2Mdc.m │ │ ├── MRIextractImage.m │ │ ├── MRIfspec.m │ │ ├── MRIisBHDR.m │ │ ├── MRIisMGH.m │ │ ├── MRImdc2euler.m │ │ ├── MRIread.m │ │ ├── MRIseg2labelxyz.m │ │ ├── MRIsegReg.m │ │ ├── MRIvol2vol.m │ │ ├── MRIvote.m │ │ ├── MRIwrite.m │ │ ├── MakeGeodesicOuterROI.m │ │ ├── PropagateGeodesic.m │ │ ├── ReadSiemensPhysio.m │ │ ├── SearchProjectionOnPial.m │ │ ├── angles2rotmat.m │ │ ├── barsegstats.m │ │ ├── bmm_mcvect.m │ │ ├── bmm_mcvhist.m │ │ ├── bmmcost.m │ │ ├── bmmroc.m │ │ ├── cc_cut_afd.m │ │ ├── cc_cut_dir_afd.m │ │ ├── cc_cut_table.m │ │ ├── combine_labels.m │ │ ├── compute_lgi.m │ │ ├── convert_fieldsign.m │ │ ├── convert_unwarp_resample.m │ │ ├── cortical_labeling_afd_txt.m │ │ ├── cortical_labeling_dir_afd_txt.m │ │ ├── cortical_labeling_table.m │ │ ├── createMeshFacesOfVertex.m │ │ ├── dice_labels.m │ │ ├── dijk.m │ │ ├── dtifa.m │ │ ├── dtimatrix.m │ │ ├── find_corresponding_center_FSformat.m │ │ ├── fisher_twoclass.m │ │ ├── fread3.m │ │ ├── freesurfer_fread3.m │ │ ├── freesurfer_read_surf.m │ │ ├── fwrite3.m │ │ ├── getFaceArea.m │ │ ├── getFacesArea.m │ │ ├── getMeshArea.m │ │ ├── getOrthogonalVector.m │ │ ├── getVerticesAndFacesInSphere.m │ │ ├── isInGeodesicROI.m │ │ ├── isVertexInRadius.m │ │ ├── isdicomfile.m │ │ ├── juelichmat2mat.m │ │ ├── labelic.m │ │ ├── llbmm.m │ │ ├── lme │ │ │ ├── Qdec │ │ │ │ ├── Qdec2num.m │ │ │ │ ├── fReadQdec.m │ │ │ │ ├── fWriteQdec.m │ │ │ │ ├── findQdecVar.m │ │ │ │ ├── getQdecVarData.m │ │ │ │ ├── getQdecVars.m │ │ │ │ ├── num2Qdec.m │ │ │ │ └── rmQdecCol.m │ │ │ ├── fs_read_Y.m │ │ │ ├── fs_read_label.m │ │ │ ├── fs_read_surf.m │ │ │ ├── fs_write_Y.m │ │ │ ├── fs_write_fstats.m │ │ │ ├── geodesic │ │ │ │ ├── create_flat_triangular_mesh.m │ │ │ │ ├── create_hedgehog_mesh.m │ │ │ │ ├── create_subdivision_pattern.m │ │ │ │ ├── example1.m │ │ │ │ ├── example2.m │ │ │ │ ├── example3.m │ │ │ │ ├── example4.m │ │ │ │ ├── example5.m │ │ │ │ ├── extract_coordinates_from_path.m │ │ │ │ ├── geodesic_convert_surface_points.m │ │ │ │ ├── geodesic_create_surface_point.m │ │ │ │ ├── geodesic_delete.m │ │ │ │ ├── geodesic_distance_and_source.m │ │ │ │ ├── geodesic_license.txt │ │ │ │ ├── geodesic_matlab_api.h │ │ │ │ ├── geodesic_new_algorithm.m │ │ │ │ ├── geodesic_new_mesh.m │ │ │ │ ├── geodesic_propagate.m │ │ │ │ ├── geodesic_readme.txt │ │ │ │ ├── geodesic_src.tar.gz │ │ │ │ └── geodesic_trace_back.m │ │ │ ├── mass_univariate │ │ │ │ ├── AdjMtx.m │ │ │ │ ├── lme_FSfit.m │ │ │ │ ├── lme_RgFSfit.m │ │ │ │ ├── lme_mass_F.m │ │ │ │ ├── lme_mass_FDR.m │ │ │ │ ├── lme_mass_FDR2.m │ │ │ │ ├── lme_mass_LR.m │ │ │ │ ├── lme_mass_RgDist.m │ │ │ │ ├── lme_mass_RgEI.m │ │ │ │ ├── lme_mass_RgEI1.m │ │ │ │ ├── lme_mass_RgFSfit.m │ │ │ │ ├── lme_mass_RgGradient.m │ │ │ │ ├── lme_mass_RgGradient1.m │ │ │ │ ├── lme_mass_RgGrow.m │ │ │ │ ├── lme_mass_RgMean.m │ │ │ │ ├── lme_mass_fit.m │ │ │ │ ├── lme_mass_fit_EMinit.m │ │ │ │ ├── lme_mass_fit_Rgw.m │ │ │ │ ├── lme_mass_fit_init.m │ │ │ │ ├── lme_mass_fit_vw.m │ │ │ │ ├── parfor_progress.m │ │ │ │ └── sphCoord.m │ │ │ ├── sortData.m │ │ │ ├── univariate │ │ │ │ ├── lme_EI.m │ │ │ │ ├── lme_F.m │ │ │ │ ├── lme_Gradient.m │ │ │ │ ├── lme_Hessian.m │ │ │ │ ├── lme_LR.m │ │ │ │ ├── lme_fit_EM.m │ │ │ │ ├── lme_fit_EMinit.m │ │ │ │ ├── lme_fit_FS.m │ │ │ │ ├── lme_fit_FSinit.m │ │ │ │ ├── lme_fit_NR.m │ │ │ │ ├── lme_fit_init.m │ │ │ │ ├── lme_lowessPlot.m │ │ │ │ ├── lme_plannedPower.m │ │ │ │ ├── lme_plannedSampleSize.m │ │ │ │ ├── lme_realizedPower.m │ │ │ │ ├── lme_timePlot.m │ │ │ │ └── vec.m │ │ │ └── usage.txt │ │ ├── load_analyze.m │ │ ├── load_analyze_hdr.m │ │ ├── load_cor.m │ │ ├── load_csd.m │ │ ├── load_dicom_fl.m │ │ ├── load_dicom_series.m │ │ ├── load_gca.m │ │ ├── load_ima.m │ │ ├── load_mgh.m │ │ ├── load_mgh2.m │ │ ├── load_nifti.m │ │ ├── load_nifti_hdr.m │ │ ├── load_segstats.m │ │ ├── lta_read.m │ │ ├── make_outer_surface.m │ │ ├── make_roi_paths.m │ │ ├── mesh_adjacency.m │ │ ├── mesh_vertex_nearest.m │ │ ├── mksubfov.m │ │ ├── mri_cdf2p.m │ │ ├── mri_kurtosis.m │ │ ├── mri_surfrft_jlbr.m │ │ ├── mri_zcdf.m │ │ ├── mris_display.m │ │ ├── peakfinder.m │ │ ├── pons_cut_afd.m │ │ ├── pons_cut_dir_afd.m │ │ ├── pons_cut_table.m │ │ ├── pred2path.m │ │ ├── randb.m │ │ ├── read_ROIlabel.m │ │ ├── read_all.m │ │ ├── read_annotation.m │ │ ├── read_asc.m │ │ ├── read_ascii_curv.m │ │ ├── read_cor.m │ │ ├── read_csf_patch.m │ │ ├── read_curv.m │ │ ├── read_eccen_patch.m │ │ ├── read_freq_patch.m │ │ ├── read_fscolorlut.m │ │ ├── read_genesis_image.m │ │ ├── read_label.m │ │ ├── read_label_old.m │ │ ├── read_moviebyu.m │ │ ├── read_normals.m │ │ ├── read_patch.m │ │ ├── read_siemens_header.m │ │ ├── read_siemens_image.m │ │ ├── read_smooth_eccen.m │ │ ├── read_surf.m │ │ ├── read_type.m │ │ ├── read_vf.m │ │ ├── read_wfile.m │ │ ├── readrec.m │ │ ├── redo_lgi.m │ │ ├── remove_spaces.m │ │ ├── reorganize_verticeslist.m │ │ ├── ribbon_afd.m │ │ ├── ribbon_dir_afd.m │ │ ├── ribbon_table.m │ │ ├── rotmat.m │ │ ├── rotmat2angles.m │ │ ├── sampleSize.m │ │ ├── save_cor.m │ │ ├── save_mgh.m │ │ ├── save_mgh2.m │ │ ├── save_nifti.m │ │ ├── ssbloch.m │ │ ├── ssblochgrad.m │ │ ├── stringunique.m │ │ ├── strlen.m │ │ ├── subcortical_labeling_afd.m │ │ ├── subcortical_labeling_dir_afd.m │ │ ├── subcortical_labeling_table.m │ │ ├── surf_registration_afd.m │ │ ├── surf_registration_stats.m │ │ ├── surf_registration_table.m │ │ ├── talairaching_afd.m │ │ ├── talairaching_dir_afd.m │ │ ├── talairaching_stats.m │ │ ├── talairaching_table.m │ │ ├── transVertexToNormalAxisBase.m │ │ ├── unwarp_init_globals.m │ │ ├── unwarp_resample.m │ │ ├── unwarp_scanners_table.m │ │ ├── vox2rasToQform.m │ │ ├── vox2ras_0to1.m │ │ ├── vox2ras_1to0.m │ │ ├── vox2ras_dfmeas.m │ │ ├── vox2ras_ksolve.m │ │ ├── vox2ras_rsolve.m │ │ ├── vox2ras_rsolveAA.m │ │ ├── vox2ras_tkreg.m │ │ ├── wm_seg_afd.m │ │ ├── wm_seg_dir_afd.m │ │ ├── wm_seg_table.m │ │ ├── write_analyze_hdr.m │ │ ├── write_annotation.m │ │ ├── write_ascii_curv.m │ │ ├── write_curv.m │ │ ├── write_label.m │ │ ├── write_lgi.m │ │ ├── write_path.m │ │ ├── write_surf.m │ │ ├── write_wfile.m │ │ └── xfm_read.m │ └── subjects │ │ ├── fsaverage4 │ │ ├── label │ │ │ ├── lh.Medial_wall.label │ │ │ └── rh.Medial_wall.label │ │ └── surf │ │ │ ├── lh.pial │ │ │ └── rh.pial │ │ └── fsaverage5 │ │ ├── label │ │ ├── lh.Medial_wall.label │ │ └── rh.Medial_wall.label │ │ └── surf │ │ ├── lh.pial │ │ └── rh.pial └── gifti-1.6 │ ├── @gifti │ ├── Contents.m │ ├── display.m │ ├── export.m │ ├── fieldnames.m │ ├── gifti.m │ ├── isfield.m │ ├── plot.m │ ├── private │ │ ├── Makefile │ │ ├── Makefile.var │ │ ├── base64decode.m │ │ ├── base64encode.m │ │ ├── getdict.m │ │ ├── isintent.m │ │ ├── miniz.c │ │ ├── mvtk_write.m │ │ ├── read_freesurfer_file.m │ │ ├── read_gifti_file_standalone.m │ │ ├── xml_parser.m │ │ ├── zstream.c │ │ └── zstream.m │ ├── save.m │ ├── saveas.m │ ├── struct.m │ ├── subsasgn.m │ └── subsref.m │ ├── GPL │ ├── LICENCE │ └── README ├── scripts ├── s0_script_CreatePrepData.m ├── s1_script_FuncInit_surf_fs.m ├── s1_script_FuncInit_surf_hcp.m ├── s1_script_FuncInit_vol.m ├── s2_script_SelRobustInit_vol.m ├── s3_script_FuncMvnmfL21p1_func_surf_fs.m ├── s3_script_FuncMvnmfL21p1_func_surf_hcp.m ├── s3_script_FuncMvnmfL21p1_func_vol.m ├── s4_script_SaveResImgIdv_vol.m └── s4_script_SaveResImg_vol.m └── src ├── backNMF_u.m ├── backNMF_v.m ├── constructW_surf.m ├── constructW_vol.m ├── createPrepData.m ├── dataPrepro.m ├── deployFuncInit_mat.m ├── deployFuncInit_surf_fs.m ├── deployFuncInit_surf_hcp.m ├── deployFuncInit_vol.m ├── deployFuncMvnmfL21p1_func_mat.m ├── deployFuncMvnmfL21p1_func_surf_fs.m ├── deployFuncMvnmfL21p1_func_surf_hcp.m ├── deployFuncMvnmfL21p1_func_vol.m ├── dispSM_func.m ├── func_initialization_woLoadSrc.m ├── func_mMvNMF4fmri_l21p1_ard_woSrcLoad.m ├── func_saveVolRes2Nii.m ├── func_saveVolRes2Nii_idv.m ├── getFsSurf.m ├── getHcpSurf.m ├── mMultiNMF_l21p1_ard.m ├── mMultiNMF_l21p1_ard_parfor.m ├── mNMF_sp.m ├── mNMF_sp_v.m ├── prepareFuncData_fs_func.m ├── prepareFuncData_hcp_func.m ├── prepareFuncData_mat_func.m ├── prepareFuncData_vol_func.m └── selRobustInit.m /lib/NIfTI_20140122/expand_nii_scan.m: -------------------------------------------------------------------------------- 1 | % Expand a multiple-scan NIFTI file into multiple single-scan NIFTI files 2 | % 3 | % Usage: expand_nii_scan(multi_scan_filename, [img_idx], [path_to_save]) 4 | % 5 | % NIFTI data format can be found on: http://nifti.nimh.nih.gov 6 | % 7 | % - Jimmy Shen (jimmy@rotman-baycrest.on.ca) 8 | % 9 | function expand_nii_scan(filename, img_idx, newpath) 10 | 11 | v = version; 12 | 13 | % Check file extension. If .gz, unpack it into temp folder 14 | % 15 | if length(filename) > 2 & strcmp(filename(end-2:end), '.gz') 16 | 17 | if ~strcmp(filename(end-6:end), '.img.gz') & ... 18 | ~strcmp(filename(end-6:end), '.hdr.gz') & ... 19 | ~strcmp(filename(end-6:end), '.nii.gz') 20 | 21 | error('Please check filename.'); 22 | end 23 | 24 | if str2num(v(1:3)) < 7.1 | ~usejava('jvm') 25 | error('Please use MATLAB 7.1 (with java) and above, or run gunzip outside MATLAB.'); 26 | else 27 | gzFile = 1; 28 | end 29 | end 30 | 31 | if ~exist('newpath','var') | isempty(newpath), newpath = pwd; end 32 | if ~exist('img_idx','var') | isempty(img_idx), img_idx = 1:get_nii_frame(filename); end 33 | 34 | for i=img_idx 35 | nii_i = load_untouch_nii(filename, i); 36 | 37 | fn = [nii_i.fileprefix '_' sprintf('%04d',i)]; 38 | pnfn = fullfile(newpath, fn); 39 | 40 | if exist('gzFile', 'var') 41 | pnfn = [pnfn '.nii.gz']; 42 | end 43 | 44 | save_untouch_nii(nii_i, pnfn); 45 | end 46 | 47 | return; % expand_nii_scan 48 | 49 | -------------------------------------------------------------------------------- /lib/NIfTI_20140122/license.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014, Jimmy Shen 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are 6 | met: 7 | 8 | * Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | * Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in 12 | the documentation and/or other materials provided with the distribution 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 18 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 | POSSIBILITY OF SUCH DAMAGE. 25 | -------------------------------------------------------------------------------- /lib/NIfTI_20140122/rri_zoom_menu.m: -------------------------------------------------------------------------------- 1 | % Imbed a zoom menu to any figure. 2 | % 3 | % Usage: rri_zoom_menu(fig); 4 | % 5 | 6 | % - Jimmy Shen (jimmy@rotman-baycrest.on.ca) 7 | % 8 | %-------------------------------------------------------------------- 9 | function menu_hdl = rri_zoom_menu(fig) 10 | 11 | if isnumeric(fig) 12 | menu_hdl = uimenu('Parent',fig, ... 13 | 'Label','Zoom on', ... 14 | 'Userdata', 1, ... 15 | 'Callback','rri_zoom_menu(''zoom'');'); 16 | 17 | return; 18 | end 19 | 20 | zoom_on_state = get(gcbo,'Userdata'); 21 | 22 | if (zoom_on_state == 1) 23 | zoom on; 24 | set(gcbo,'Userdata',0,'Label','Zoom off'); 25 | set(gcbf,'pointer','crosshair'); 26 | else 27 | zoom off; 28 | set(gcbo,'Userdata',1,'Label','Zoom on'); 29 | set(gcbf,'pointer','arrow'); 30 | end 31 | 32 | return % rri_zoom_menu 33 | 34 | -------------------------------------------------------------------------------- /lib/NIfTI_20140122/save_nii_ext.m: -------------------------------------------------------------------------------- 1 | % Save NIFTI header extension. 2 | % 3 | % Usage: save_nii_ext(ext, fid) 4 | % 5 | % ext - struct with NIFTI header extension fields. 6 | % 7 | % NIFTI data format can be found on: http://nifti.nimh.nih.gov 8 | % 9 | % - Jimmy Shen (jimmy@rotman-baycrest.on.ca) 10 | % 11 | function save_nii_ext(ext, fid) 12 | 13 | if ~exist('ext','var') | ~exist('fid','var') 14 | error('Usage: save_nii_ext(ext, fid)'); 15 | end 16 | 17 | if ~isfield(ext,'extension') | ~isfield(ext,'section') | ~isfield(ext,'num_ext') 18 | error('Wrong header extension'); 19 | end 20 | 21 | write_ext(ext, fid); 22 | 23 | return; % save_nii_ext 24 | 25 | 26 | %--------------------------------------------------------------------- 27 | function write_ext(ext, fid) 28 | 29 | fwrite(fid, ext.extension, 'uchar'); 30 | 31 | for i=1:ext.num_ext 32 | fwrite(fid, ext.section(i).esize, 'int32'); 33 | fwrite(fid, ext.section(i).ecode, 'int32'); 34 | fwrite(fid, ext.section(i).edata, 'uchar'); 35 | end 36 | 37 | return; % write_ext 38 | 39 | -------------------------------------------------------------------------------- /lib/NIfTI_20140122/unxform_nii.m: -------------------------------------------------------------------------------- 1 | % Undo the flipping and rotations performed by xform_nii; spit back only 2 | % the raw img data block. Initial cut will only deal with 3D volumes 3 | % strongly assume we have called xform_nii to write down the steps used 4 | % in xform_nii. 5 | % 6 | % Usage: a = load_nii('original_name'); 7 | % manipulate a.img to make array b; 8 | % 9 | % if you use unxform_nii to un-tranform the image (img) data 10 | % block, then nii.original.hdr is the corresponding header. 11 | % 12 | % nii.original.img = unxform_nii(a, b); 13 | % save_nii(nii.original,'newname'); 14 | % 15 | % Where, 'newname' is created with data in the same space as the 16 | % original_name data 17 | % 18 | % - Jeff Gunter, 26-JUN-06 19 | % 20 | function outblock = unxform_nii(nii, inblock) 21 | 22 | if isempty(nii.hdr.hist.rot_orient) 23 | outblock=inblock; 24 | else 25 | [dummy unrotate_orient] = sort(nii.hdr.hist.rot_orient); 26 | outblock = permute(inblock, unrotate_orient); 27 | end 28 | 29 | if ~isempty(nii.hdr.hist.flip_orient) 30 | flip_orient = nii.hdr.hist.flip_orient(unrotate_orient); 31 | 32 | for i = 1:3 33 | if flip_orient(i) 34 | outblock = flipdim(outblock, i); 35 | end 36 | end 37 | end; 38 | 39 | return; 40 | 41 | -------------------------------------------------------------------------------- /lib/NIfTI_20140122/verify_nii_ext.m: -------------------------------------------------------------------------------- 1 | % Verify NIFTI header extension to make sure that each extension section 2 | % must be an integer multiple of 16 byte long that includes the first 8 3 | % bytes of esize and ecode. If the length of extension section is not the 4 | % above mentioned case, edata should be padded with all 0. 5 | % 6 | % Usage: [ext, esize_total] = verify_nii_ext(ext) 7 | % 8 | % ext - Structure of NIFTI header extension, which includes num_ext, 9 | % and all the extended header sections in the header extension. 10 | % Each extended header section will have its esize, ecode, and 11 | % edata, where edata can be plain text, xml, or any raw data 12 | % that was saved in the extended header section. 13 | % 14 | % esize_total - Sum of all esize variable in all header sections. 15 | % 16 | % NIFTI data format can be found on: http://nifti.nimh.nih.gov 17 | % 18 | % - Jimmy Shen (jimmy@rotman-baycrest.on.ca) 19 | % 20 | function [ext, esize_total] = verify_nii_ext(ext) 21 | 22 | if ~isfield(ext, 'section') 23 | error('Incorrect NIFTI header extension structure.'); 24 | elseif ~isfield(ext, 'num_ext') 25 | ext.num_ext = length(ext.section); 26 | elseif ~isfield(ext, 'extension') 27 | ext.extension = [1 0 0 0]; 28 | end 29 | 30 | esize_total = 0; 31 | 32 | for i=1:ext.num_ext 33 | if ~isfield(ext.section(i), 'ecode') | ~isfield(ext.section(i), 'edata') 34 | error('Incorrect NIFTI header extension structure.'); 35 | end 36 | 37 | ext.section(i).esize = ceil((length(ext.section(i).edata)+8)/16)*16; 38 | ext.section(i).edata = ... 39 | [ext.section(i).edata ... 40 | zeros(1,ext.section(i).esize-length(ext.section(i).edata)-8)]; 41 | esize_total = esize_total + ext.section(i).esize; 42 | end 43 | 44 | return % verify_nii_ext 45 | 46 | -------------------------------------------------------------------------------- /lib/Ncut_9/ICgraph.m: -------------------------------------------------------------------------------- 1 | function [W,imageEdges] = ICgraph(I,dataW,dataEdgemap); 2 | % [W,imageEdges] = ICgraph(I,dataW,dataEdgemap); 3 | % Input: 4 | % I = gray-level image 5 | % optional parameters: 6 | % dataW.sampleRadius=10; 7 | % dataW.sample_rate=0.3; 8 | % dataW.edgeVariance = 0.1; 9 | % 10 | % dataEdgemap.parametres=[4,3, 21,3];%[number of filter orientations, number of scales, filter size, elongation] 11 | % dataEdgemap.threshold=0.02; 12 | % 13 | % Output: 14 | % W: npixels x npixels similarity matrix based on Intervening Contours 15 | % imageEdges: image showing edges extracted in the image 16 | % 17 | % Timothee Cour, Stella Yu, Jianbo Shi, 2004. 18 | 19 | 20 | 21 | [p,q] = size(I); 22 | 23 | if (nargin< 2) | isempty(dataW), 24 | dataW.sampleRadius=10; 25 | dataW.sample_rate=0.3; 26 | dataW.edgeVariance = 0.1; 27 | end 28 | 29 | if (nargin<3) | isempty(dataEdgemap), 30 | dataEdgemap.parametres=[4,3, 21,3];%[number of filter orientations, number of scales, filter size, elongation] 31 | dataEdgemap.threshold=0.02; 32 | end 33 | 34 | 35 | edgemap = computeEdges(I,dataEdgemap.parametres,dataEdgemap.threshold); 36 | imageEdges = edgemap.imageEdges; 37 | W = computeW(I,dataW,edgemap.emag,edgemap.ephase); 38 | -------------------------------------------------------------------------------- /lib/Ncut_9/NcutImage.m: -------------------------------------------------------------------------------- 1 | function [SegLabel,NcutDiscrete,NcutEigenvectors,NcutEigenvalues,W,imageEdges]= NcutImage(I,nbSegments); 2 | % [SegLabel,NcutDiscrete,NcutEigenvectors,NcutEigenvalues,W,imageEdges]= NcutImage(I); 3 | % Input: I = brightness image 4 | % nbSegments = number of segmentation desired 5 | % Output: SegLable = label map of the segmented image 6 | % NcutDiscrete = Discretized Ncut vectors 7 | % 8 | % Timothee Cour, Stella Yu, Jianbo Shi, 2004. 9 | 10 | 11 | 12 | if nargin <2, 13 | nbSegments = 10; 14 | end 15 | 16 | [W,imageEdges] = ICgraph(I); 17 | 18 | [NcutDiscrete,NcutEigenvectors,NcutEigenvalues] = ncutW(W,nbSegments); 19 | 20 | %% generate segmentation label map 21 | [nr,nc,nb] = size(I); 22 | 23 | SegLabel = zeros(nr,nc); 24 | for j=1:size(NcutDiscrete,2), 25 | SegLabel = SegLabel + j*reshape(NcutDiscrete(:,j),nr,nc); 26 | end -------------------------------------------------------------------------------- /lib/Ncut_9/X2distances.m: -------------------------------------------------------------------------------- 1 | function distances = X2distances(X,Sigma); 2 | %Timothee Cour, 2004 3 | [n,k] = size(X); 4 | if nargin >= 2 5 | X = X*inv(sqrtm(Sigma)); 6 | end 7 | temp = sum(X.*X,2); 8 | temp = repmat(temp,1,n); 9 | distances = -2*X*X' + temp + temp'; -------------------------------------------------------------------------------- /lib/Ncut_9/affinityic.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hmlicas/Collaborative_Brain_Decomposition/6f3ad613d065fe592979e1f105065ed22472e999/lib/Ncut_9/affinityic.mexw64 -------------------------------------------------------------------------------- /lib/Ncut_9/cimgnbmap.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hmlicas/Collaborative_Brain_Decomposition/6f3ad613d065fe592979e1f105065ed22472e999/lib/Ncut_9/cimgnbmap.mexw64 -------------------------------------------------------------------------------- /lib/Ncut_9/compileDir_simple.m: -------------------------------------------------------------------------------- 1 | function compileDir_simple(Cdir); 2 | if nargin<1 3 | Cdir=pwd; 4 | end 5 | 6 | files = dir(fullfile(Cdir,'*.cpp')); 7 | 8 | oldDir=pwd; 9 | cd(Cdir); 10 | for j=1:length(files) 11 | try 12 | % cm = sprintf('mex %s',files(j).name); 13 | cm = sprintf('mex -largeArrayDims %s',files(j).name); 14 | disp(cm); 15 | eval(cm); 16 | catch 17 | disp(lasterr); 18 | disp('IGNORE if the file is a C++ file which is not a mex file (ie without a mexFunction inside)'); 19 | end 20 | end 21 | 22 | cd(oldDir); -------------------------------------------------------------------------------- /lib/Ncut_9/computeEdges.m: -------------------------------------------------------------------------------- 1 | function edgemap = computeEdges(imageX,parametres,threshold) 2 | % edgemap = computeEdges(imageX,parametres,threshold) 3 | % 4 | % computes the edge in imageX with parameters parametres and threshold 5 | % Timothee Cour, Stella Yu, Jianbo Shi, 2004. 6 | 7 | [ex,ey,egx,egy,eg_par,eg_th,emag,ephase , g ] = quadedgep(imageX,parametres,threshold); 8 | % example : [ex,ey,egx,egy,eg_par,eg_th,emag,ephase] = quadedgep(imageX,[4,3,30,3],0.05); 9 | 10 | % [emagTrie,eindex] = sort(emag); 11 | 12 | %edges3 = sparse(floor(ex),floor(ey),(egx.^2+egy.^2).^(1/2),size(imageX,2),size(imageX,1))'; 13 | 14 | try 15 | edges2 = emag .* edge(imageX,'canny') ; 16 | %edges2 = emag .* edge(imageX,'sobel') ; 17 | catch 18 | edges2 = 0 * emag; 19 | end 20 | 21 | edges2 = edges2 .* (edges2 > threshold); 22 | egx1 = g(:,:,1); 23 | egy1 = g(:,:,2); 24 | eindex = find(edges2); 25 | [ey,ex,values] = find(edges2); 26 | 27 | egx = egx1(eindex); 28 | egy = egy1(eindex); 29 | 30 | edgemap.eindex = eindex; 31 | edgemap.values = values; 32 | edgemap.x = ex; 33 | edgemap.y = ey; 34 | edgemap.gx = egx; 35 | edgemap.gy = egy; 36 | edgemap.emag = emag; 37 | edgemap.ephase = ephase; 38 | edgemap.imageEdges = edges2; 39 | -------------------------------------------------------------------------------- /lib/Ncut_9/computeW.m: -------------------------------------------------------------------------------- 1 | function W = computeW(imageX,dataW,emag,ephase) 2 | % W = computeW(imageX,dataW,emag,ephase) 3 | % Timothee Cour, Stella Yu, Jianbo Shi, 2004. 4 | [p,q] = size(imageX); 5 | 6 | [w_i,w_j] = cimgnbmap([p,q],dataW.sampleRadius,dataW.sample_rate); 7 | 8 | W = affinityic(emag,ephase,w_i,w_j,max(emag(:)) * dataW.edgeVariance); 9 | W = W/max(W(:)); 10 | -------------------------------------------------------------------------------- /lib/Ncut_9/compute_relation.m: -------------------------------------------------------------------------------- 1 | function [W,distances] = compute_relation(data,scale_sig,order) 2 | % 3 | % [W,distances] = compute_relation(data,scale_sig) 4 | % Input: data= Feature_dimension x Num_data 5 | % ouput: W = pair-wise data similarity matrix 6 | % Dist = pair-wise Euclidean distance 7 | % 8 | % 9 | % Jianbo Shi, 1997 10 | 11 | 12 | distances = zeros(length(data),length(data)); 13 | for j = 1:length(data), 14 | distances(j,:) = (sqrt((data(1,:)-data(1,j)).^2 +... 15 | (data(2,:)-data(2,j)).^2)); 16 | end 17 | 18 | % distances = X2distances(data'); 19 | 20 | if (~exist('scale_sig')), 21 | scale_sig = 0.05*max(distances(:)); 22 | end 23 | 24 | if (~exist('order')), 25 | order = 2; 26 | end 27 | 28 | tmp = (distances/scale_sig).^order; 29 | 30 | W = exp(-tmp); 31 | 32 | -------------------------------------------------------------------------------- /lib/Ncut_9/compute_relation2.m: -------------------------------------------------------------------------------- 1 | function [A,B] = compute_relation2(data,scale_sig,order) 2 | % 3 | % [W,Dist] = compute_relation(data,scale_sig) 4 | % Input: data= Feature_dimension x Num_data 5 | % ouput: W = pair-wise data similarity matrix 6 | % Dist = pair-wise Euclidean distance 7 | % 8 | % 9 | % Jianbo Shi, 1997 10 | 11 | if (~exist('order')), 12 | order = 2; 13 | % order = 1; 14 | end 15 | 16 | n = size(data,2); 17 | 18 | B = zeros(n); 19 | for j = 1:n 20 | B(:,j) = (sqrt((data(1,:)-data(1,j)).^2 +... 21 | (data(2,:)-data(2,j)).^2))'; 22 | end 23 | 24 | if (~exist('scale_sig')), 25 | scale_sig = 0.05*max(B(:)); 26 | % scale_sig = 0.04*max(B(:)); 27 | end 28 | 29 | % kNN = 5; 30 | % B2 = B; 31 | % for j=1:n 32 | % [ignore,ind] = sort(B(:,j)); 33 | % B2(ind(kNN+1:end),j) = Inf; 34 | % B2(ind(1:kNN),j) = B(ind(1:kNN),j) / max(B(ind(1:kNN),j)) * 0.5; 35 | % end 36 | % scale_sig = 1; 37 | % B = min(B2,B2');%(B+B')/2; 38 | 39 | tmp = (B/scale_sig).^order; 40 | 41 | A = exp(-tmp); 42 | 43 | 44 | -------------------------------------------------------------------------------- /lib/Ncut_9/demoNcutClustering.m: -------------------------------------------------------------------------------- 1 | function demoNcutClustering; 2 | % demoNcutClustering 3 | % 4 | % demo for NcutClustering 5 | % also initialize matlab paths to subfolders 6 | % Timothee Cour, Stella Yu, Jianbo Shi, 2004. 7 | 8 | disp('Ncut Clustering demo'); 9 | 10 | %% make up a point data set 11 | caseid = 2;%3 12 | [data,size_cluster] = build_scene(caseid); 13 | figure(1);clf; 14 | plot(data(1,:),data(2,:),'ks', 'MarkerFaceColor','k','MarkerSize',5); axis image; hold on; 15 | 16 | disp('This is the input data points to be clustered, press Enter to continue...'); 17 | pause; 18 | 19 | disp('Compute clustering...'); 20 | 21 | % compute similarity matrix 22 | [W,Dist] = compute_relation(data); 23 | 24 | % clustering graph in 25 | nbCluster = 4; 26 | tic; 27 | [NcutDiscrete,NcutEigenvectors,NcutEigenvalues] = ncutW(W,nbCluster); 28 | disp(['The computation took ' num2str(toc) ' seconds']); 29 | figure(3); 30 | plot(NcutEigenvectors); 31 | 32 | % display clustering result 33 | cluster_color = ['rgbmyc']; 34 | figure(2);clf; 35 | for j=1:nbCluster, 36 | id = find(NcutDiscrete(:,j)); 37 | plot(data(1,id),data(2,id),[cluster_color(j),'s'], 'MarkerFaceColor',cluster_color(j),'MarkerSize',5); hold on; 38 | end 39 | hold off; axis image; 40 | disp('This is the clustering result'); 41 | disp('The demo is finished.'); 42 | -------------------------------------------------------------------------------- /lib/Ncut_9/demoNcutImage.m: -------------------------------------------------------------------------------- 1 | function demoNcutImage; 2 | % demoNcutImage 3 | % 4 | % demo for NcutImage 5 | % also initialize matlab paths to subfolders 6 | % Timothee Cour, Stella Yu, Jianbo Shi, 2004. 7 | 8 | disp('Ncut Image Segmentation demo'); 9 | 10 | %% read image, change color image to brightness image, resize to 160x160 11 | I = imread_ncut('jpg_images/3.jpg',160,160); 12 | 13 | %% display the image 14 | figure(1);clf; imagesc(I);colormap(gray);axis off; 15 | disp('This is the input image to segment, press Enter to continue...'); 16 | pause; 17 | 18 | %% compute the edges imageEdges, the similarity matrix W based on 19 | %% Intervening Contours, the Ncut eigenvectors and discrete segmentation 20 | nbSegments = 5; 21 | disp('computing Ncut eigenvectors ...'); 22 | tic; 23 | [SegLabel,NcutDiscrete,NcutEigenvectors,NcutEigenvalues,W,imageEdges]= NcutImage(I,nbSegments); 24 | disp(['The computation took ' num2str(toc) ' seconds on the ' num2str(size(I,1)) 'x' num2str(size(I,2)) ' image']); 25 | 26 | 27 | %% display the edges 28 | figure(2);clf; imagesc(imageEdges); axis off 29 | disp('This is the edges computed, press Enter to continue...'); 30 | pause; 31 | 32 | %% display the segmentation 33 | figure(3);clf 34 | bw = edge(SegLabel,0.01); 35 | J1=showmask(I,imdilate(bw,ones(2,2))); imagesc(J1);axis off 36 | disp('This is the segmentation, press Enter to continue...'); 37 | pause; 38 | 39 | %% display Ncut eigenvectors 40 | figure(4);clf;set(gcf,'Position',[100,500,200*(nbSegments+1),200]); 41 | [nr,nc,nb] = size(I); 42 | for i=1:nbSegments 43 | subplot(1,nbSegments,i); 44 | imagesc(reshape(NcutEigenvectors(:,i) , nr,nc));axis('image');axis off; 45 | end 46 | disp('This is the Ncut eigenvectors...'); 47 | disp('The demo is finished.'); 48 | 49 | -------------------------------------------------------------------------------- /lib/Ncut_9/discretisation.m: -------------------------------------------------------------------------------- 1 | function [EigenvectorsDiscrete,EigenVectors]=discretisation(EigenVectors) 2 | % 3 | % EigenvectorsDiscrete=discretisation(EigenVectors) 4 | % 5 | % Input: EigenVectors = continuous Ncut vector, size = ndata x nbEigenvectors 6 | % Output EigenvectorsDiscrete = discrete Ncut vector, size = ndata x nbEigenvectors 7 | % 8 | % Timothee Cour, Stella Yu, Jianbo Shi, 2004 9 | 10 | [n,k]=size(EigenVectors); 11 | 12 | vm = sqrt(sum(EigenVectors.*EigenVectors,2)); 13 | EigenVectors = EigenVectors./repmat(vm,1,k); 14 | 15 | R=zeros(k); 16 | R(:,1)=EigenVectors(1+round(rand(1)*(n-1)),:)'; 17 | c=zeros(n,1); 18 | for j=2:k 19 | c=c+abs(EigenVectors*R(:,j-1)); 20 | [minimum,i]=min(c); 21 | R(:,j)=EigenVectors(i,:)'; 22 | end 23 | 24 | lastObjectiveValue=0; 25 | exitLoop=0; 26 | nbIterationsDiscretisation = 0; 27 | nbIterationsDiscretisationMax = 20;%voir 28 | while exitLoop== 0 29 | nbIterationsDiscretisation = nbIterationsDiscretisation + 1 ; 30 | EigenvectorsDiscrete = discretisationEigenVectorData(EigenVectors*R); 31 | [U,S,V] = svd(EigenvectorsDiscrete'*EigenVectors,0); 32 | NcutValue=2*(n-trace(S)); 33 | 34 | if abs(NcutValue-lastObjectiveValue) < eps | nbIterationsDiscretisation > nbIterationsDiscretisationMax 35 | exitLoop=1; 36 | else 37 | lastObjectiveValue = NcutValue; 38 | R=V*U'; 39 | end 40 | end -------------------------------------------------------------------------------- /lib/Ncut_9/discretisationEigenVectorData.m: -------------------------------------------------------------------------------- 1 | function Y = discretisationEigenVectorData(EigenVector) 2 | % Y = discretisationEigenVectorData(EigenVector) 3 | % 4 | % discretizes previously rotated eigenvectors in discretisation 5 | % Timothee Cour, Stella Yu, Jianbo Shi, 2004 6 | 7 | [n,k]=size(EigenVector); 8 | 9 | 10 | [Maximum,J]=max(EigenVector'); 11 | 12 | Y=sparse(1:n,J',1,n,k); 13 | -------------------------------------------------------------------------------- /lib/Ncut_9/doog1.m: -------------------------------------------------------------------------------- 1 | function H=doog1(sig,r,th,N); 2 | % H=doog1(sig,r,th,N); 3 | % Serge Belongie 4 | 5 | no_pts=N; % no. of points in x,y grid 6 | 7 | [x,y]=meshgrid(-(N/2)+1/2:(N/2)-1/2,-(N/2)+1/2:(N/2)-1/2); 8 | 9 | phi=pi*th/180; 10 | sigy=sig; 11 | sigx=r*sig; 12 | R=[cos(phi) -sin(phi); sin(phi) cos(phi)]; 13 | C=R*diag([sigx,sigy])*R'; 14 | 15 | X=[x(:) y(:)]; 16 | 17 | Gb=gaussian(X,[0 0]',C); 18 | Gb=reshape(Gb,N,N); 19 | 20 | m=R*[0 sig]'; 21 | 22 | a=1; 23 | b=-1; 24 | 25 | % make odd-symmetric filter 26 | Ga=gaussian(X,m/2,C); 27 | Ga=reshape(Ga,N,N); 28 | Gb=rot90(Ga,2); 29 | H=a*Ga+b*Gb; 30 | 31 | -------------------------------------------------------------------------------- /lib/Ncut_9/doog2.m: -------------------------------------------------------------------------------- 1 | function G=doog2(sig,r,th,N); 2 | % G=doog2(sig,r,th,N); 3 | % Make difference of offset gaussians kernel 4 | % theta is in degrees 5 | % (see Malik & Perona, J. Opt. Soc. Amer., 1990) 6 | % 7 | % Example: 8 | % >> imagesc(doog2(1,12,0,64,1)) 9 | % >> colormap(gray) 10 | % 11 | % Serge Belongie 12 | 13 | 14 | no_pts=N; % no. of points in x,y grid 15 | 16 | [x,y]=meshgrid(-(N/2)+1/2:(N/2)-1/2,-(N/2)+1/2:(N/2)-1/2); 17 | 18 | phi=pi*th/180; 19 | sigy=sig; 20 | sigx=r*sig; 21 | R=[cos(phi) -sin(phi); sin(phi) cos(phi)]; 22 | C=R*diag([sigx,sigy])*R'; 23 | 24 | X=[x(:) y(:)]; 25 | 26 | Gb=gaussian(X,[0 0]',C); 27 | Gb=reshape(Gb,N,N); 28 | 29 | m=R*[0 sig]'; 30 | Ga=gaussian(X,m,C); 31 | Ga=reshape(Ga,N,N); 32 | Gc=rot90(Ga,2); 33 | 34 | a=-1; 35 | b=2; 36 | c=-1; 37 | 38 | G = a*Ga + b*Gb + c*Gc; 39 | 40 | -------------------------------------------------------------------------------- /lib/Ncut_9/fft_filt_2.m: -------------------------------------------------------------------------------- 1 | function FI=fft_filt_2(V,FB,sf); 2 | % FI=fft_filt_2(V,FB,sf); 3 | % fft-based filtering 4 | % requires image to be called "V" 5 | % and filters to be in FB 6 | % sf is the subsampling factor 7 | % 8 | % FI is the result 9 | % Jianbo Shi, 1997 10 | 11 | [M1,M2,N3]=size(FB); 12 | % prepare FFT of image for filtering 13 | [N1,N2]=size(V); 14 | I=zeros(size(V)+[M1-1 M2-1]); 15 | I(1:N1,1:N2)=V; 16 | N1s=length(1:sf:N1); 17 | N2s=length(1:sf:N2); 18 | IF=fft2(I); 19 | FI=zeros(N1s,N2s,N3); 20 | 21 | % apply filters 22 | for n=1:N3; 23 | f=rot90(FB(:,:,n),2); 24 | fF=fft2(f,N1+M1-1,N2+M2-1); 25 | IfF=IF.*fF; 26 | If=real(ifft2(IfF)); 27 | If=If(ceil((M1+1)/2):ceil((M1+1)/2)+N1-1,ceil((M2+1)/2):ceil((M2+1)/2)+N2-1); 28 | FI(:,:,n)=If(1:sf:N1,1:sf:N2); 29 | end 30 | 31 | -------------------------------------------------------------------------------- /lib/Ncut_9/gaussian.m: -------------------------------------------------------------------------------- 1 | function p=gaussian(x,m,C); 2 | % p=gaussian(x,m,C); 3 | % 4 | % Evaluate the multi-variate density with mean vector m and covariance 5 | % matrix C for the input vector x. 6 | % 7 | % p=gaussian(X,m,C); 8 | % 9 | % Vectorized version: Here X is a matrix of column vectors, and p is 10 | % a vector of probabilities for each vector. 11 | % Jianbo Shi, 1997 12 | d=length(m); 13 | 14 | if size(x,1)~=d 15 | x=x'; 16 | end 17 | N=size(x,2); 18 | 19 | detC = det(C); 20 | if rcond(C)1), 13 | I =double(rgb2gray(I)); 14 | else 15 | I = double(I); 16 | end 17 | 18 | I = imresize(I,[nr, nc],'bicubic'); 19 | -------------------------------------------------------------------------------- /lib/Ncut_9/make_filterbank_even2.m: -------------------------------------------------------------------------------- 1 | function FB = make_filterbank(num_ori,filter_scales,wsz,enlong) 2 | % 3 | % F = make_filterbank(num_ori,num_scale,wsz) 4 | % Jianbo Shi, 1997 5 | 6 | 7 | if nargin<4, 8 | enlong = 3; 9 | end 10 | 11 | enlong = 2*enlong; 12 | 13 | % definine filterbank 14 | %num_ori=6; 15 | %num_scale=3; 16 | 17 | num_scale = length(filter_scales); 18 | 19 | M1=wsz; % size in pixels 20 | M2=M1; 21 | 22 | ori_incr=180/num_ori; 23 | ori_offset=ori_incr/2; % helps with equalizing quantiz. error across filter set 24 | 25 | FB=zeros(M1,M2,num_ori,num_scale); 26 | 27 | % elongated filter set 28 | counter = 1; 29 | 30 | for m=1:num_scale 31 | for n=1:num_ori 32 | % r=12 here is equivalent to Malik's r=3; 33 | f=doog2(filter_scales(m),enlong,ori_offset+(n-1)*ori_incr,M1); 34 | FB(:,:,n,m)=f; 35 | end 36 | end 37 | 38 | FB=reshape(FB,M1,M2,num_scale*num_ori); 39 | total_num_filt=size(FB,3); 40 | 41 | for j=1:total_num_filt, 42 | F = FB(:,:,j); 43 | a = sum(sum(abs(F))); 44 | FB(:,:,j) = FB(:,:,j)/a; 45 | end 46 | 47 | -------------------------------------------------------------------------------- /lib/Ncut_9/make_filterbank_odd2.m: -------------------------------------------------------------------------------- 1 | function FB = make_filterbank(num_ori,filter_scales,wsz,enlong) 2 | % 3 | % F = make_filterbank(num_ori,num_scale,wsz) 4 | % Jianbo Shi, 1997 5 | 6 | if nargin<4, 7 | enlong = 3; 8 | end 9 | 10 | enlong = enlong*2; 11 | 12 | % definine filterbank 13 | %num_ori=6; 14 | %num_scale=3; 15 | 16 | num_scale = length(filter_scales); 17 | 18 | M1=wsz; % size in pixels 19 | M2=M1; 20 | 21 | ori_incr=180/num_ori; 22 | ori_offset=ori_incr/2; % helps with equalizing quantiz. error across filter set 23 | 24 | FB=zeros(M1,M2,num_ori,num_scale); 25 | 26 | 27 | % elongated filter set 28 | counter = 1; 29 | 30 | for m=1:num_scale 31 | for n=1:num_ori 32 | % r=12 here is equivalent to Malik's r=3; 33 | f=doog1(filter_scales(m),enlong,ori_offset+(n-1)*ori_incr,M1); 34 | FB(:,:,n,m)=f; 35 | end 36 | end 37 | 38 | FB=reshape(FB,M1,M2,num_scale*num_ori); 39 | total_num_filt=size(FB,3); 40 | 41 | for j=1:total_num_filt, 42 | F = FB(:,:,j); 43 | a = sum(sum(abs(F))); 44 | FB(:,:,j) = FB(:,:,j)/a; 45 | end 46 | 47 | -------------------------------------------------------------------------------- /lib/Ncut_9/mex_w_times_x_symmetric.cpp: -------------------------------------------------------------------------------- 1 | /*================================================================ 2 | * mex_w_times_x_symmetric.c = used by ncuthard2.m in eigensolver. 3 | * 4 | * Examples: 5 | * mex_w_times_x_c_symmetric(x,triu(A)) = A*x; 6 | * A is sparse and symmetric, but x is full 7 | * 8 | * Timothee Cour, Oct 12, 2003. 9 | 10 | % test sequence: 11 | m=100; 12 | n=50; 13 | x=rand(n,1); 14 | A=sprand(m,n,0.01); 15 | 16 | y2 = mex_w_times_x_c_symmetric(x,triu(A)); 17 | y1=A*x; 18 | max(abs(y1-y2)) 19 | *=================================================================*/ 20 | 21 | # include "math.h" 22 | # include "mex.h" 23 | # include "a_times_b_cmplx.cpp" 24 | /*# include "a_times_b.c"*/ 25 | 26 | 27 | void mexFunction( 28 | int nargout, 29 | mxArray *out[], 30 | int nargin, 31 | const mxArray *in[] 32 | ) 33 | { 34 | int np, nc; 35 | mwIndex*ir, *jc; 36 | double *x, *y, *pr; 37 | 38 | if (nargin < 2) {//voir 39 | mexErrMsgTxt("Four input arguments required !"); 40 | } 41 | if (nargout>1) { 42 | mexErrMsgTxt("Too many output arguments."); 43 | } 44 | 45 | x = mxGetPr(in[0]); 46 | pr = mxGetPr(in[1]); 47 | ir = mxGetIr(in[1]); 48 | jc = mxGetJc(in[1]); 49 | 50 | np = mxGetM(in[1]); 51 | nc = mxGetN(in[1]); 52 | 53 | out[0] = mxCreateDoubleMatrix(np,1,mxREAL); 54 | y = mxGetPr(out[0]); 55 | 56 | CSRsymm_VecMult_CAB_double(np,nc,pr,ir,jc,x,y); 57 | } 58 | -------------------------------------------------------------------------------- /lib/Ncut_9/mex_w_times_x_symmetric.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hmlicas/Collaborative_Brain_Decomposition/6f3ad613d065fe592979e1f105065ed22472e999/lib/Ncut_9/mex_w_times_x_symmetric.mexw64 -------------------------------------------------------------------------------- /lib/Ncut_9/ncutW.m: -------------------------------------------------------------------------------- 1 | function [NcutDiscrete,NcutEigenvectors,NcutEigenvalues] = ncutW(W,nbcluster); 2 | % [NcutDiscrete,NcutEigenvectors,NcutEigenvalues] = ncutW(W,nbcluster); 3 | % 4 | % Calls ncut to compute NcutEigenvectors and NcutEigenvalues of W with nbcluster clusters 5 | % Then calls discretisation to discretize the NcutEigenvectors into NcutDiscrete 6 | % Timothee Cour, Stella Yu, Jianbo Shi, 2004 7 | 8 | % compute continuous Ncut eigenvectors 9 | [NcutEigenvectors,NcutEigenvalues] = ncut(W,nbcluster); 10 | 11 | % compute discretize Ncut vectors 12 | [NcutDiscrete,NcutEigenvectors] =discretisation(NcutEigenvectors); 13 | 14 | 15 | NcutDiscrete = full(NcutDiscrete); -------------------------------------------------------------------------------- /lib/Ncut_9/showmask.m: -------------------------------------------------------------------------------- 1 | function RGB=showmask(V,M,display_flag); 2 | % showmask(V,M); 3 | % 4 | % M is a nonneg. mask 5 | % Jianbo Shi, 1997 6 | 7 | V=V-min(V(:)); 8 | V=V/max(V(:)); 9 | V=.25+0.75*V; %brighten things up a bit 10 | 11 | M=M-min(M(:)); 12 | M=M/max(M(:)); 13 | 14 | H=0.0+zeros(size(V)); 15 | S=M; 16 | RGB=hsv2rgb(H,S,V); 17 | 18 | %if nargin>2 19 | image(RGB) 20 | axis('image') 21 | %end 22 | -------------------------------------------------------------------------------- /lib/Ncut_9/sparsifyc.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hmlicas/Collaborative_Brain_Decomposition/6f3ad613d065fe592979e1f105065ed22472e999/lib/Ncut_9/sparsifyc.mexw64 -------------------------------------------------------------------------------- /lib/Ncut_9/spmtimesd.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hmlicas/Collaborative_Brain_Decomposition/6f3ad613d065fe592979e1f105065ed22472e999/lib/Ncut_9/spmtimesd.mexw64 -------------------------------------------------------------------------------- /lib/cifti/ciftiopen.m: -------------------------------------------------------------------------------- 1 | function [ cifti ] = ciftiopen(filename,wbcommand,verbose) 2 | 3 | % CIFTI = CIFTIOPEN(FILENAME,WBCOMMAND,VERBOSE) 4 | % 5 | % Open a CIFTI file by converting to GIFTI external binary first and then 6 | % using the GIFTI toolbox 7 | % 8 | % FILENAME is string containing file name to open 9 | % WBCOMMAND is string containing the Workbench command. 10 | % (Necessary for the intermediate step of conversion to gifti. 11 | % Matlab must be able to find this when it executes a 'system' command). 12 | % VERBOSE (optional; default is off): Set to 1 for more verbose output. 13 | 14 | % MPH: Modified to use a temporary file location suitable 15 | % for that OS; added an explicit verbose flag; switched to 16 | % using matlabs 'delete' command for file removal and 'system' 17 | % command for executing 'wbcommand'. 18 | 19 | % Default is VERBOSE=0 (OFF) 20 | if (nargin < 3) 21 | verbose = 0; 22 | end 23 | 24 | % Use Matlab 'tempname' to return a temporary file name and location appropriate for this system 25 | tmpfile = tempname; 26 | 27 | tstart=tic; 28 | 29 | % Do conversion and loading 30 | system([wbcommand ' -cifti-convert -to-gifti-ext ' filename ' ' tmpfile '.gii']); 31 | cifti = gifti([tmpfile '.gii']); 32 | 33 | if (verbose) 34 | fprintf(1,'%s: Elapsed time is %.2f seconds\n',filename,toc(tstart)); 35 | end 36 | 37 | % Clean-up 38 | delete([tmpfile '.gii'],[tmpfile '.gii.data']); 39 | -------------------------------------------------------------------------------- /lib/cifti/ciftisave.m: -------------------------------------------------------------------------------- 1 | function [ output_args ] = ciftisave(cifti,filename,wbcommand,verbose) 2 | 3 | % CIFTISAVE(CIFTI,FILENAME,WBCOMMAND,VERBOSE) 4 | % 5 | % Save a CIFTI file as a GIFTI external binary and then convert it to CIFTI 6 | % 7 | % CIFTI is the cifti-formatted file in the Matlab workspace 8 | % FILENAME is the string containing the file name to save as 9 | % WBCOMMAND is string containing the Workbench command. 10 | % (Necessary for the intermediate step of conversion to gifti. 11 | % Matlab must be able to find this when it executes a 'system' command). 12 | % VERBOSE (optional; default is off): Set to 1 for more verbose output. 13 | 14 | % Default is VERBOSE=0 (OFF) 15 | if (nargin < 4) 16 | verbose = 0; 17 | end 18 | 19 | tstart=tic; 20 | 21 | % Do work 22 | % Note that 'save' is an "overloaded" function for objects 23 | % of class 'gifti', and as such its behavior is defined by the 24 | % 'gifti' class implementation 25 | save(cifti,[filename '.gii'],'ExternalFileBinary') 26 | system([wbcommand ' -cifti-convert -from-gifti-ext ' filename '.gii ' filename]); 27 | 28 | if (verbose) 29 | fprintf(1,'%s: Elapsed time is %.2f seconds\n',filename,toc(tstart)); 30 | end 31 | 32 | % Clean-up 33 | delete([filename '.gii'],[filename '.dat']); 34 | -------------------------------------------------------------------------------- /lib/cifti/ciftisavereset.m: -------------------------------------------------------------------------------- 1 | function [ output_args ] = ciftisavereset(cifti,filename,caret7command) 2 | %Save a CIFTI file as a GIFTI external binary and then convert it to CIFTI 3 | 4 | tic 5 | save(cifti,[filename '.gii'],'ExternalFileBinary') 6 | toc 7 | 8 | %unix(['/media/1TB/matlabsharedcode/ciftiunclean.sh ' filename '.gii ' filename '_.gii']); 9 | 10 | %unix(['mv ' filename '_.gii ' filename '.gii']); 11 | strlength=length(filename); 12 | if strcmp('.dscalar.nii',filename(strlength-11:strlength)) 13 | flag=' -reset-scalars'; 14 | elseif strcmp('.dtseries.nii',filename(strlength-12:strlength)) 15 | flag=' -reset-timepoints 1 0'; 16 | else 17 | flag=''; 18 | end 19 | 20 | tic 21 | unix([caret7command ' -cifti-convert -from-gifti-ext ' filename '.gii ' filename ' ' flag]); 22 | toc 23 | 24 | unix([' /bin/rm ' filename '.gii ' filename '.dat ']); 25 | 26 | end 27 | 28 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/MRIeuler2Mdc.m: -------------------------------------------------------------------------------- 1 | function Mdc = MRIeuler2Mdc(eulerangles) 2 | % Mdc = MRIeuler2Mdc(eulerangles) 3 | % 4 | % Returns the matrix of direction cosines given the Euler angles. 5 | % Not sure if this works. 6 | % 7 | 8 | 9 | % 10 | % MRIeuler2Mdc.m 11 | % 12 | % Original Author: Doug Greve 13 | % CVS Revision Info: 14 | % $Author: nicks $ 15 | % $Date: 2011/03/02 00:04:12 $ 16 | % $Revision: 1.3 $ 17 | % 18 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 19 | % 20 | % Terms and conditions for use, reproduction, distribution and contribution 21 | % are found in the 'FreeSurfer Software License Agreement' contained 22 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 23 | % 24 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 25 | % 26 | % Reporting: freesurfer@nmr.mgh.harvard.edu 27 | % 28 | 29 | 30 | Mdc = []; 31 | if(nargin ~= 1) 32 | fprintf('Mdc = MRIeuler2Mdc(eulerangles)\n'); 33 | return; 34 | end 35 | 36 | theta = eulerangles(1); 37 | phi = eulerangles(2); 38 | psi = eulerangles(3); 39 | 40 | Mdc(1,1) = cos(phi)*cos(theta)*cos(psi) - sin(phi)*sin(psi); 41 | Mdc(2,1) = sin(phi)*cos(psi) + cos(phi)*cos(theta)*sin(psi); 42 | Mdc(3,1) = -cos(phi)*sin(theta); 43 | 44 | Mdc(1,2) = -sin(phi)*cos(theta)*cos(psi) - cos(phi)*sin(psi); 45 | Mdc(2,2) = -sin(phi)*cos(theta)*sin(psi) + cos(phi)*cos(psi); 46 | Mdc(3,2) = sin(theta)*sin(phi); 47 | 48 | Mdc(3,1) = sin(theta)*cos(psi); 49 | Mdc(3,2) = sin(theta)*sin(psi); 50 | Mdc(3,3) = cos(theta); 51 | 52 | return; 53 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/MRIisBHDR.m: -------------------------------------------------------------------------------- 1 | function [isBHDR, fstem] = MRIisBHDR(fspec) 2 | % [isBHDR fstem] = MRIisBHDR(fspec) 3 | % 4 | % Determines whether the given file spec is an BHDR 5 | % file based soley on its extension. BHDR is a bshort or 6 | % bfloat. 7 | % 8 | % Returns non-zero if fspec is the name of an BHDR file, 9 | % Returns 1 if it has a .bhdr extension. 10 | % Returns 0 otherwise. 11 | % 12 | 13 | 14 | % 15 | % MRIisBHDR.m 16 | % 17 | % Original Author: Doug Greve 18 | % CVS Revision Info: 19 | % $Author: nicks $ 20 | % $Date: 2011/03/02 00:04:12 $ 21 | % $Revision: 1.3 $ 22 | % 23 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 24 | % 25 | % Terms and conditions for use, reproduction, distribution and contribution 26 | % are found in the 'FreeSurfer Software License Agreement' contained 27 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 28 | % 29 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 30 | % 31 | % Reporting: freesurfer@nmr.mgh.harvard.edu 32 | % 33 | 34 | 35 | fstem = []; 36 | isBHDR = []; 37 | if(nargin ~= 1) 38 | fprintf('[isBHDR fstem] = MRIisBHDR(fspec)\n'); 39 | return; 40 | end 41 | 42 | isBHDR = 0; 43 | if(length(fspec) < 6) return; end 44 | 45 | ext = fspec(end-4:end); 46 | 47 | if(strcmp(ext,'.bhdr')) 48 | isBHDR = 1; 49 | fstem = fspec(1:end-5); 50 | end 51 | 52 | return; 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/MRIisMGH.m: -------------------------------------------------------------------------------- 1 | function isMGH = MRIisMGH(fspec) 2 | % isMGH = MRIisMGH(fspec) 3 | % 4 | % Determines whether the given file spec is an MGH/MGZ 5 | % file based soley on its extension 6 | % 7 | % Returns non-zero if fspec is the name of an MGH file, 8 | % Returns 1 if it has a .mgh extension. 9 | % Returns 2 if it has a .mgz extension. 10 | % Returns 0 otherwise. 11 | % 12 | 13 | 14 | % 15 | % MRIisMGH.m 16 | % 17 | % Original Author: Doug Greve 18 | % CVS Revision Info: 19 | % $Author: nicks $ 20 | % $Date: 2011/03/02 00:04:12 $ 21 | % $Revision: 1.3 $ 22 | % 23 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 24 | % 25 | % Terms and conditions for use, reproduction, distribution and contribution 26 | % are found in the 'FreeSurfer Software License Agreement' contained 27 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 28 | % 29 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 30 | % 31 | % Reporting: freesurfer@nmr.mgh.harvard.edu 32 | % 33 | 34 | 35 | isMGH = []; 36 | if(nargin ~= 1) 37 | fprintf('isMGH = MRIisMGH(fspec)\n'); 38 | return; 39 | end 40 | 41 | isMGH = 0; 42 | if(length(fspec) < 5) return; end 43 | 44 | ext = fspec(end-3:end); 45 | 46 | if(strcmp(ext,'.mgh')) isMGH = 1; end 47 | if(strcmp(ext,'.mgz')) isMGH = 2; end 48 | 49 | return; 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/MRImdc2euler.m: -------------------------------------------------------------------------------- 1 | function eulerangles = MRImdc2euler(Mdc) 2 | % eulerangles = MRImdc2euler(Mdc) 3 | % 4 | % Solves for the Euler angles given the 3x3 matrix of direction cosines. 5 | % This code does not work in all cases. 6 | % 7 | 8 | 9 | % 10 | % MRImdc2euler.m 11 | % 12 | % Original Author: Doug Greve 13 | % CVS Revision Info: 14 | % $Author: nicks $ 15 | % $Date: 2011/03/02 00:04:12 $ 16 | % $Revision: 1.4 $ 17 | % 18 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 19 | % 20 | % Terms and conditions for use, reproduction, distribution and contribution 21 | % are found in the 'FreeSurfer Software License Agreement' contained 22 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 23 | % 24 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 25 | % 26 | % Reporting: freesurfer@nmr.mgh.harvard.edu 27 | % 28 | 29 | 30 | eulerangles = []; 31 | if(nargin ~= 1) 32 | fprintf('eulerangles = MRImdc2euler(Mdc)\n'); 33 | return; 34 | end 35 | 36 | theta = acos(Mdc(3,3)); 37 | phi = asin(Mdc(3,2)/(sin(theta)+eps)); 38 | psi = asin(Mdc(2,3)/(sin(theta)+eps)); 39 | 40 | eulerangles = [theta phi psi]; 41 | 42 | return; 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/MRIseg2labelxyz.m: -------------------------------------------------------------------------------- 1 | function xyzlab = MRIseg2labelxyz(segmri,segid) 2 | % xyzlab = MRIseg2labelxyz(segmri,) 3 | % 4 | % Computes the xyz for use in a label of the voxels in 5 | % the segmentation volume. 6 | % 7 | % segmri - MRI struct. eg, segmri = MRIread('segvol'); 8 | % segid - id to find in the seg (default is 1) 9 | % 10 | % xyzlab - xyz in segvol's RAS space (defined by segmri.vox2ras0) 11 | % 12 | 13 | 14 | % 15 | % MRIseg2labelxyz.m 16 | % 17 | % Original Author: Doug Greve 18 | % CVS Revision Info: 19 | % $Author: nicks $ 20 | % $Date: 2011/03/02 00:04:12 $ 21 | % $Revision: 1.3 $ 22 | % 23 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 24 | % 25 | % Terms and conditions for use, reproduction, distribution and contribution 26 | % are found in the 'FreeSurfer Software License Agreement' contained 27 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 28 | % 29 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 30 | % 31 | % Reporting: freesurfer@nmr.mgh.harvard.edu 32 | % 33 | 34 | 35 | xyzlab = []; 36 | if(nargin < 2 | nargin > 3) 37 | fprintf('xyzlab = MRIseg2labelxyz(segmri,segid)\n'); 38 | return; 39 | end 40 | 41 | if(~exist('segid','var')) segid = 1; end 42 | 43 | % Get list of voxels with seg id 44 | indlab = find(segmri.vol == segid); 45 | nlab = length(indlab); 46 | 47 | % Convert indices to row, col, slice 48 | [r c s] = ind2sub(segmri.volsize,indlab); 49 | crs = [c r s]' - 1 ; % 0-based 50 | crs = [crs; ones(1,nlab)]; 51 | 52 | % Convert row, col, slice to XYZ 53 | xyz1 = segmri.vox2ras0 * crs; 54 | xyz = xyz1(1:3,:)'; 55 | 56 | return; 57 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/MRIvote.m: -------------------------------------------------------------------------------- 1 | function [v nocc] = MRIvote(vol) 2 | % [v nocc] = MRIvote(vol) 3 | % 4 | % For each voxel, selects most freqeuntly occurring value across 5 | % frames. nocc is the number of times it occurs. 6 | % See also mri2.c::MRIvote() 7 | % 8 | % $Id: MRIvote.m,v 1.2 2011/03/02 00:04:12 nicks Exp $ 9 | 10 | % 11 | % MRIvote.m 12 | % 13 | % Original Author: Doug Greve 14 | % CVS Revision Info: 15 | % $Author: nicks $ 16 | % $Date: 2011/03/02 00:04:12 $ 17 | % $Revision: 1.2 $ 18 | % 19 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 20 | % 21 | % Terms and conditions for use, reproduction, distribution and contribution 22 | % are found in the 'FreeSurfer Software License Agreement' contained 23 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 24 | % 25 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 26 | % 27 | % Reporting: freesurfer@nmr.mgh.harvard.edu 28 | % 29 | 30 | volsize = size(vol); 31 | 32 | mat = fast_vol2mat(vol); 33 | [nf nv] = size(mat); 34 | u = unique(mat); 35 | nu = length(u); 36 | 37 | vu = zeros(nu,nv); 38 | for nthu = 1:nu 39 | vu(nthu,:) = sum(mat==u(nthu)); 40 | end 41 | 42 | [nocc uind] = max(vu); 43 | v = u(uind)'; 44 | 45 | v = fast_mat2vol(v,volsize); 46 | nocc = fast_mat2vol(nocc,volsize); 47 | 48 | return; 49 | 50 | 51 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/MakeGeodesicOuterROI.m: -------------------------------------------------------------------------------- 1 | function ListFacesInHullROI = MakeGeodesicOuterROI (mesh_outer, iV, facesInROI); 2 | 3 | FacesByLines=[]; 4 | FacesByLines=mesh_outer.faces(facesInROI,:); 5 | 6 | [index,value]=find(FacesByLines==iV); 7 | index=index'; 8 | t=FacesByLines(index,:); 9 | 10 | more off 11 | l=unique(t(:)); 12 | l2=l; 13 | oldsize=1; 14 | while size(l,1)>oldsize 15 | for r=1:size(l2,1) 16 | [index2,value2]=find(FacesByLines==l2(r)); 17 | index2=index2'; 18 | index=[index index2]; 19 | t2=FacesByLines(index2,:); 20 | t=[t; t2]; 21 | end 22 | t=unique(t,'rows'); 23 | oldsize=size(l); 24 | oldl=l; 25 | l=unique(t(:)); 26 | l2=setdiff(l,oldl); 27 | indexListHull=unique(index); 28 | end 29 | 30 | ListFacesInHullROI=t; 31 | 32 | if size(facesInROI,2) ~= size(indexListHull,2) 33 | disp([' non geodesic regions removed from the patch ']) 34 | end 35 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/PropagateGeodesic.m: -------------------------------------------------------------------------------- 1 | function [ListVerticesInPialROI,ListFacesInPialROI] = PropagateGeodesic(mesh_total,m,iV,wholepath) 2 | 3 | 4 | 5 | [centerIndexMT,centerValuesMT]=mesh_vertex_nearest(mesh_total.vertices,m.vertices(iV,:)); 6 | index=mesh_total.facesOfVertex(centerIndexMT).faceList; 7 | clear centerIndexMT; 8 | clear centerValuesMT; 9 | 10 | t=mesh_total.faces(index,:); 11 | 12 | % pour trouver la liste des faces qui contiennent le vertex le plus proche, 13 | % ensuite il faut enlever successivement toutes les lignes qui contiennent 14 | % au moins un des vertex 15 | 16 | more off 17 | l=unique(t(:)); 18 | l2=l; 19 | oldsize=1; 20 | while size(l,1)>oldsize 21 | fprintf('.') 22 | index2=[]; 23 | for r = l2' 24 | index2 = [index2, mesh_total.facesOfVertex(r).faceList]; 25 | end 26 | index2 = unique (index2); 27 | for u = 1: size(index2,2) 28 | if isInGeodesicROI(mesh_total,wholepath,index2(u)) 29 | t2=mesh_total.faces(index2(u),:); 30 | t=[t; t2]; 31 | t=unique(t,'rows'); 32 | %else 33 | %index2(u) 34 | %fprintf('one vertex not added .. ') 35 | end 36 | end 37 | oldsize=size(l); 38 | oldl=l; 39 | l=unique(t(:)); 40 | l2=setdiff(l,oldl); 41 | %size(l2) 42 | l=[oldl ; l2]; 43 | end 44 | 45 | disp('region delimited!') 46 | 47 | ListVerticesInPialROI=unique(l); 48 | 49 | ListFacesInPialROI = t; 50 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/SearchProjectionOnPial.m: -------------------------------------------------------------------------------- 1 | function [verticeslist]=SearchProjectionOnPial(mesh_total,mesh_outer,perim, step) 2 | 3 | % step is typically set between 5 and 10. Default is 7, increasing it will 4 | % limit redundancies in the resulting path file. 5 | 6 | verticeslist=[]; 7 | for t=1:step:size(perim,2) 8 | [nearestIndexMT,nearestValuesMT]=mesh_vertex_nearest(mesh_total.vertices,mesh_outer.vertices(perim(t),:)); 9 | verticeslist= [verticeslist nearestIndexMT]; 10 | end 11 | verticeslist=unique(verticeslist); 12 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/angles2rotmat.m: -------------------------------------------------------------------------------- 1 | function [R Rx Ry Rz] = angles2rotmat(angles) 2 | % [R Rx Ry Rz] = angles2rotmat(angles) 3 | % 4 | % Convert 3 euler angles into a rotation matrix 5 | % 6 | % angles is a 3x1 vector in radians 7 | % angles(1) - pitch - rotation about x or LR (gamma) 8 | % angles(2) - yaw - rotation about y or AP (beta) 9 | % angles(3) - roll - rotation about z or SI (alpha) 10 | % R = Rz*Ry*Rx; 11 | % 12 | % See also: rotmat2angles 13 | % Ref: Craig, Intro to Robotics 14 | % 15 | % $Id: angles2rotmat.m,v 1.4 2011/03/02 00:04:12 nicks Exp $ 16 | 17 | % 18 | % angles2rotmat.m 19 | % 20 | % Original Author: Doug Greve 21 | % CVS Revision Info: 22 | % $Author: nicks $ 23 | % $Date: 2011/03/02 00:04:12 $ 24 | % $Revision: 1.4 $ 25 | % 26 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 27 | % 28 | % Terms and conditions for use, reproduction, distribution and contribution 29 | % are found in the 'FreeSurfer Software License Agreement' contained 30 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 31 | % 32 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 33 | % 34 | % Reporting: freesurfer@nmr.mgh.harvard.edu 35 | % 36 | 37 | R = []; 38 | Rx = []; 39 | Ry = []; 40 | Rz = []; 41 | if(nargin ~= 1) 42 | fprintf('R = angles2rotmat(angles)\n'); 43 | return; 44 | end 45 | 46 | gamma = angles(1); 47 | beta = angles(2); 48 | alpha = angles(3); 49 | 50 | Rx = zeros(3,3); 51 | Rx(1,1) = +1; 52 | Rx(2,2) = +cos(gamma); 53 | Rx(2,3) = -sin(gamma); 54 | Rx(3,2) = +sin(gamma); 55 | Rx(3,3) = +cos(gamma); 56 | 57 | Ry = zeros(3,3); 58 | Ry(1,1) = +cos(beta); 59 | Ry(1,3) = +sin(beta); 60 | Ry(2,2) = +1; 61 | Ry(3,1) = -sin(beta); 62 | Ry(3,3) = +cos(beta); 63 | 64 | Rz = zeros(3,3); 65 | Rz(1,1) = +cos(alpha); 66 | Rz(1,2) = -sin(alpha); 67 | Rz(2,1) = +sin(alpha); 68 | Rz(2,2) = +cos(alpha); 69 | Rz(3,3) = +1; 70 | 71 | R = Rz*Ry*Rx; 72 | 73 | return; 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/barsegstats.m: -------------------------------------------------------------------------------- 1 | function [hbar, htxt] = barsegstats(segnames,segstats) 2 | % [hbar htxt] = barsegstats(segnames,segstats) 3 | % 4 | % [segnames segindex segstats] = load_segstats('aseg.stats','bert'); 5 | % [hbar htxt] = barsegstats(segnames,segstats(:,3)) 6 | % 7 | % 8 | 9 | 10 | % 11 | % barsegstats.m 12 | % 13 | % Original Author: Doug Greve 14 | % CVS Revision Info: 15 | % $Author: nicks $ 16 | % $Date: 2011/03/02 00:04:12 $ 17 | % $Revision: 1.4 $ 18 | % 19 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 20 | % 21 | % Terms and conditions for use, reproduction, distribution and contribution 22 | % are found in the 'FreeSurfer Software License Agreement' contained 23 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 24 | % 25 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 26 | % 27 | % Reporting: freesurfer@nmr.mgh.harvard.edu 28 | % 29 | 30 | 31 | mn = min(segstats(:)); 32 | mn = 0; 33 | mx = max(segstats(:)); 34 | d = mx-mn; 35 | a = mn+.02*d; 36 | 37 | nseg = size(segnames,1); 38 | x = 1:nseg; 39 | hbar = bar(segstats,'r'); 40 | for n = 1:nseg 41 | htxt(n) = text(n+.25,a,segnames(n,:)); 42 | set(htxt(n),'rotation',90); 43 | set(htxt(n),'fontsize',4) 44 | end 45 | 46 | 47 | return; 48 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/bmmcost.m: -------------------------------------------------------------------------------- 1 | function cost = bmmcost(params,Ndet) 2 | % cost = bmmcost(params,Ndet) 3 | % 4 | % cost (log-likelihood) function for binomial mixture model 5 | % 6 | % params = [pA pI lambda] 7 | % cost = -llbmm(Ndet,pA,pI,lambda) 8 | % 9 | % optparams = fminsearch('bmmcost',initparams,[],Ndet); 10 | % 11 | % Init is important because there is a symmetry between the 12 | % parameters, ie, you get the same cost if you swap pA and pI and use 13 | % 1-lambda. 14 | % 15 | 16 | 17 | % 18 | % bmmcost.m 19 | % 20 | % Original Author: Doug Greve 21 | % CVS Revision Info: 22 | % $Author: nicks $ 23 | % $Date: 2011/03/02 00:04:12 $ 24 | % $Revision: 1.3 $ 25 | % 26 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 27 | % 28 | % Terms and conditions for use, reproduction, distribution and contribution 29 | % are found in the 'FreeSurfer Software License Agreement' contained 30 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 31 | % 32 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 33 | % 34 | % Reporting: freesurfer@nmr.mgh.harvard.edu 35 | % 36 | 37 | 38 | pA = params(1); 39 | pI = params(2); 40 | lambda = params(3); 41 | 42 | % Check for out-of-bounds 43 | if(pA < eps | (1-pA) < eps | ... 44 | pI < eps | (1-pI) < eps | ... 45 | lambda < eps | (1-lambda) < eps) 46 | cost = 10e10; 47 | return; 48 | end 49 | 50 | % Use negative because it is a minimization 51 | cost = -llbmm(Ndet,pA,pI,lambda); 52 | 53 | return 54 | 55 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/cc_cut_table.m: -------------------------------------------------------------------------------- 1 | 2 | % 3 | % cc_cut_table.m 4 | % 5 | % Original Author: Laurence Wastiaux 6 | % CVS Revision Info: 7 | % $Author: nicks $ 8 | % $Date: 2011/03/02 00:04:12 $ 9 | % $Revision: 1.3 $ 10 | % 11 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 12 | % 13 | % Terms and conditions for use, reproduction, distribution and contribution 14 | % are found in the 'FreeSurfer Software License Agreement' contained 15 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 16 | % 17 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 18 | % 19 | % Reporting: freesurfer@nmr.mgh.harvard.edu 20 | % 21 | 22 | 23 | 24 | [Dr, Dl, I]=cc_cut_dir_afd('/space/neo/2/recon/buckner', 0.01) 25 | % load('/space/okapi/3/data/laurence/ADF/cutting_planes/Dice_cc_lrh_lta.mat') %loads Dr Dl 26 | % load('/space/okapi/3/data/laurence/ADF/cutting_planes/Subj_nb_cc.mat') %loads I 27 | files=dir('/space/neo/2/recon/buckner'); 28 | fp=fopen('~/cc_lh_table.txt', 'w'); 29 | fprintf(fp,'# FSAFD VerticalCuttingPlaneCheck 1\n# date 20050516\n# $Id: cc_cut_table.m,v 1.3 2011/03/02 00:04:12 nicks Exp $\n# Buckner data set\n# hemi lh\n# ncols 1\n# nrows %d\n# label_col 1 DiceCoefficient\n', length(Dl)); 30 | for i=1:length(Dl) 31 | nb=I(i); 32 | fprintf(fp, '# label_row %d %s\n', i, char(files(nb).name)); 33 | end 34 | for j=1:length(Dl) 35 | fprintf(fp, '%f\n', Dl(j)); 36 | end 37 | fclose(fp) 38 | fp2=fopen('~/cc_rh_table.txt', 'w'); 39 | fprintf(fp2,'# FSAFD VerticalCuttingPlaneCheck 1\n# date 20050516\n# $Id: cc_cut_table.m,v 1.3 2011/03/02 00:04:12 nicks Exp $\n# Buckner data set\n# hemi rh\n# ncols 1\n# nrows %d\n# label_col 1 DiceCoefficient\n', length(Dr)); 40 | for i=1:length(Dr) 41 | nb=I(i); 42 | fprintf(fp2, '# label_row %d %s\n', i, char(files(nb).name)); 43 | end 44 | for j=1:length(Dr) 45 | fprintf(fp2, '%f\n', Dr(j)); 46 | end 47 | fclose(fp2) 48 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/combine_labels.m: -------------------------------------------------------------------------------- 1 | function lboth = combine_labels(l1, l2) 2 | % lboth = combine_labels(l1, l2) 3 | 4 | ind1 = l1(:,1) ; 5 | ind2 = l2(:,1); 6 | 7 | 8 | same = 0 ; 9 | same1 = zeros(size(ind1)) ; 10 | same2 = zeros(size(ind2)) ; 11 | 12 | for i=1:length(ind1) 13 | for j=1:length(ind2) 14 | if (ind1(i) == ind2(j)) 15 | same = same+1 ; 16 | same1(i) = j ; 17 | same2(j) = i ; 18 | end 19 | end 20 | end 21 | 22 | 23 | for i=1:length(ind1) 24 | if (same1(i) == 0) 25 | lboth(i,:) = l1(i,:) ; 26 | else 27 | lboth(i,:) = l1(i,:) ; 28 | lboth(i,5) = l1(i,5) + l2(same1(i),5) ; 29 | end 30 | end 31 | 32 | ind = i ; 33 | for i=1:length(ind2) 34 | if (same2(i) == 0) 35 | lboth(ind,:) = l2(i,:) ; 36 | ind = ind+1 ; 37 | end 38 | end 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/convert_fieldsign.m: -------------------------------------------------------------------------------- 1 | function err = convert_fieldsign(infname,outfname) 2 | % err = convert_fieldsign(infname,outfname) 3 | % 4 | % Loads a field sign (infname) as saved by the void write_fieldsign() 5 | % in tksurfer and saves in the given output format. This allows the 6 | % field sign map to be loaded as an overlay in tksurfer. This can 7 | % also be used to convert a field sign mask. 8 | % 9 | % Eg: 10 | % In matlab: 11 | % convert_fieldsign('fieldsign-lh','lh.fieldsign.mgh'); 12 | % convert_fieldsign('fieldsignmask-lh','lh.fieldsignmask.mgh'); 13 | % In the unix shell: 14 | % mri_mask lh.fieldsign.mgh lh.fieldsignmask.mgh lh.fieldsign-masked.mgh 15 | % tksurfer subjectname lh inflated \ 16 | % -overlay lh.fieldsign-masked.mgh -fthresh 0.5 17 | % 18 | % $Id: convert_fieldsign.m,v 1.1 2008/05/13 22:22:37 greve Exp $ 19 | 20 | err = 1; 21 | fp = fopen(infname,'r','ieee-be'); 22 | if(fp == -1) 23 | fprintf('ERROR: could not open %s\n',fname); 24 | return; 25 | end 26 | 27 | vnum = fread(fp,1,'int32'); 28 | %fprintf('vnum = %d\n',vnum); 29 | 30 | fs = fread(fp,inf,'float32'); 31 | fclose(fp); 32 | 33 | if(length(fs) ~= vnum) 34 | fprintf('ERROR: number of data points (%d) does not equal vnum (%d)\n',... 35 | length(fs),vnum); 36 | return; 37 | end 38 | 39 | mri.nframes = vnum; 40 | mri.vol = fs(:)'; 41 | mri.vox2ras0 = eye(4); 42 | mri.volsize = [1 vnum 1]; 43 | mri.volres = [1 1 1]; 44 | mri.tr = 0; 45 | mri.flip_angle = 0; 46 | mri.te = 0; 47 | mri.ti = 0; 48 | mri.xsize = mri.volres(1); 49 | mri.ysize = mri.volres(2); 50 | mri.zsize = mri.volres(3); 51 | MRIwrite(mri,outfname); 52 | 53 | err = 0; 54 | 55 | return -------------------------------------------------------------------------------- /lib/freesurfer/matlab/createMeshFacesOfVertex.m: -------------------------------------------------------------------------------- 1 | function mesh_out = createMeshFacesOfVertex(vertices, faces) 2 | 3 | %% Original Author: Lucas Tamarit, lucas.tamarit@cisa.unige.ch 4 | 5 | mesh_out.vertices = vertices; 6 | mesh_out.faces = faces; 7 | 8 | mesh_out.facesOfVertex = repmat(struct('faceList',[]),size(vertices,1),1); 9 | 10 | for iF = 1:size(faces,1) 11 | if (mod(iF,5000) == 0) 12 | disp(['face ',num2str(iF),' / ',num2str(size(faces,1))]) 13 | end 14 | 15 | for iV = 1:3 16 | mesh_out.facesOfVertex(faces(iF,iV)).faceList = ... 17 | union( mesh_out.facesOfVertex(faces(iF,iV)).faceList, iF); 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/dice_labels.m: -------------------------------------------------------------------------------- 1 | function dice = dice_labels(lname1, lname2) 2 | % d = dice_labels(lname1, lname2) 3 | % 4 | % computes Dice coefficient of the two given label files. 5 | % 6 | 7 | % 8 | % dice_labels.m 9 | % 10 | % Original Author: Nick Schmansky 11 | % CVS Revision Info: 12 | % $Author: nicks $ 13 | % $Date: 2011/03/02 00:04:12 $ 14 | % $Revision: 1.2 $ 15 | % 16 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 17 | % 18 | % Terms and conditions for use, reproduction, distribution and contribution 19 | % are found in the 'FreeSurfer Software License Agreement' contained 20 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 21 | % 22 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 23 | % 24 | % Reporting: freesurfer@nmr.mgh.harvard.edu 25 | % 26 | 27 | label1 = read_label('',lname1); 28 | label2 = read_label('',lname2); 29 | 30 | label1Size = size(label1,1); 31 | label2Size = size(label2,1); 32 | 33 | hits = 0; 34 | for i=1:label1Size 35 | x1 = label1(i,2); 36 | y1 = label1(i,3); 37 | z1 = label1(i,4); 38 | for j=1:label2Size 39 | x2 = label2(j,2); 40 | y2 = label2(j,3); 41 | z2 = label2(j,4); 42 | if ((x1==x2) && (y1==y2) && (z1==z2)) 43 | hits = hits+1; 44 | break; 45 | end 46 | end 47 | end 48 | 49 | dice = (2 * hits) / (label1Size + label2Size); 50 | 51 | 52 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/dtifa.m: -------------------------------------------------------------------------------- 1 | function fa = dtifa(T) 2 | % fa = dtifa(T) 3 | % Compute fractional anisotropy of a 3x3 tensor 4 | % $Id: dtifa.m,v 1.1 2007/03/23 08:16:05 greve Exp $ 5 | 6 | if(nargin ~= 1) 7 | fprintf('fa = dtifa(T)\n'); 8 | return; 9 | end 10 | 11 | e = eig(T); 12 | emn = mean(e); 13 | d = e - emn; 14 | sse = sum(d.^2); 15 | ss = sum(e.^2); 16 | 17 | fa = sqrt(1.5*sse/ss); 18 | 19 | return; 20 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/dtimatrix.m: -------------------------------------------------------------------------------- 1 | function B = dtimatrix(bvalues,bvectors) 2 | % B = dtimatrix(bvalues,bvectors) 3 | % 4 | % Constructs a DTI design matrix from the bvalues and bvectors. 5 | % bvalues is a vector of length N 6 | % bvectors is a matrix either Nx3 or 3xN 7 | % 8 | % B will be N by 7 9 | % The 7th is the mean (all ones) 10 | % The tensor will be constructed using the following regressors 11 | % 1 2 3 12 | % 2 4 5 13 | % 3 5 6 14 | % 15 | % $Id: dtimatrix.m,v 1.2 2011/03/02 00:04:12 nicks Exp $ 16 | 17 | % 18 | % dtimatrix.m 19 | % 20 | % Original Author: Doug Greve 21 | % CVS Revision Info: 22 | % $Author: nicks $ 23 | % $Date: 2011/03/02 00:04:12 $ 24 | % $Revision: 1.2 $ 25 | % 26 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 27 | % 28 | % Terms and conditions for use, reproduction, distribution and contribution 29 | % are found in the 'FreeSurfer Software License Agreement' contained 30 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 31 | % 32 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 33 | % 34 | % Reporting: freesurfer@nmr.mgh.harvard.edu 35 | % 36 | 37 | 38 | if(nargin ~= 2) 39 | fprintf('B = dtimatrix(bvalues,bvectors)\n'); 40 | return; 41 | end 42 | 43 | if(size(bvectors,2) ~= 3) bvectors = bvectors'; end 44 | if(size(bvectors,2) ~= 3) 45 | fprintf('ERROR: bvectors must be Nx3 or 3xN\n'); 46 | return; 47 | end 48 | Nb = size(bvectors,1); 49 | 50 | if(Nb ~= length(bvalues)) 51 | fprintf('ERROR: dimension mismatch between bvectors and bvalues\n'); 52 | return; 53 | end 54 | 55 | bvalues = bvalues(:); 56 | 57 | B = zeros(Nb,7); 58 | B(:,1) = bvalues .* bvectors(:,1).*bvectors(:,1); 59 | B(:,2) = 2 * bvalues .* bvectors(:,1).*bvectors(:,2); 60 | B(:,3) = 2 * bvalues .* bvectors(:,1).*bvectors(:,3); 61 | B(:,4) = bvalues .* bvectors(:,2).*bvectors(:,2); 62 | B(:,5) = 2 * bvalues .* bvectors(:,2).*bvectors(:,3); 63 | B(:,6) = bvalues .* bvectors(:,3).*bvectors(:,3); 64 | B(:,7) = 1; 65 | 66 | return; 67 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/find_corresponding_center_FSformat.m: -------------------------------------------------------------------------------- 1 | function find_corresponding_center_FSformat (pial, outersmoothed, stepsize, outdir, flagfile) 2 | 3 | % Original Author: Marie Schaer 4 | % Date: 2007/11/14 5 | % 6 | % This function takes as an input the pial mesh and its envelope (the outer 7 | % mesh), and is used to isolate a set of corresponding points of both 8 | % meshes. In the % lGI computation process, it is used to define the seed 9 | % point for the obtention of the pial regions of interests (by filling 10 | % until a closed perimeter). 11 | % 12 | % The function uses "mesh_vertex_nearest" and "freesurfer_read_surf" 13 | % from Darren Webers bioelectromagnetism toolbox 14 | % 15 | % Input: 16 | % ?h.pial surface from FreeSurfer 17 | % ?h.outer-pial from make_outer_surface 18 | % 19 | % Output: 20 | % c_* asci files containing the index of the mesh_pial vertex 21 | % corresponding to the vertex number * of the outer mesh, 22 | % and a file named 'center.vertices' which contains selected vertices 23 | 24 | t0 = cputime; 25 | 26 | [mesh_pial.vertices, mesh_pial.faces] = freesurfer_read_surf(pial); 27 | [mesh_outer.vertices, mesh_outer.faces] = freesurfer_read_surf(outersmoothed); 28 | 29 | fidv = fopen([outdir '/' pial '.center.vertices'], 'w') ; 30 | 31 | for iV = 1 :stepsize: length(mesh_outer.vertices) 32 | 33 | centerSeed=mesh_vertex_nearest(mesh_pial.vertices,mesh_outer.vertices(iV,:)); 34 | centerSeed = centerSeed - 1; %FreeSurfer-s vertex are 0-based 35 | p = sprintf ('%06d', iV); 36 | 37 | fid = fopen([outdir '/' pial '.' p '.center'], 'w') ; 38 | fprintf(fid,'%d\n',centerSeed); 39 | fclose(fid) ; 40 | 41 | fprintf(fidv,'%s\n',p); 42 | end 43 | 44 | fclose(fidv) ; 45 | 46 | deltaT = cputime - t0 47 | 48 | % indicate successful completion by deleting flagfile 49 | delete(flagfile); 50 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/fread3.m: -------------------------------------------------------------------------------- 1 | function [retval] = fd3(fid) 2 | % [retval] = fd3(fid) 3 | % read a 3 byte integer out of a file 4 | 5 | 6 | % 7 | % fread3.m 8 | % 9 | % Original Author: Bruce Fischl 10 | % CVS Revision Info: 11 | % $Author: nicks $ 12 | % $Date: 2011/03/02 00:04:12 $ 13 | % $Revision: 1.3 $ 14 | % 15 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 16 | % 17 | % Terms and conditions for use, reproduction, distribution and contribution 18 | % are found in the 'FreeSurfer Software License Agreement' contained 19 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 20 | % 21 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 22 | % 23 | % Reporting: freesurfer@nmr.mgh.harvard.edu 24 | % 25 | 26 | b1 = fread(fid, 1, 'uchar') ; 27 | b2 = fread(fid, 1, 'uchar') ; 28 | b3 = fread(fid, 1, 'uchar') ; 29 | retval = bitshift(b1, 16) + bitshift(b2,8) + b3 ; 30 | 31 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/freesurfer_fread3.m: -------------------------------------------------------------------------------- 1 | function [retval] = freesurfer_fread3(fid) 2 | 3 | % freesurfer_fread3 - read a 3 byte integer out of a file 4 | % 5 | % [retval] = freesurfer_fread3(fid) 6 | % 7 | % see also freesurfer_write3, freesurfer_read_surf, freesurfer_write_surf 8 | % 9 | 10 | b1 = fread(fid, 1, 'uchar') ; 11 | b2 = fread(fid, 1, 'uchar') ; 12 | b3 = fread(fid, 1, 'uchar') ; 13 | retval = bitshift(b1, 16) + bitshift(b2,8) + b3 ; -------------------------------------------------------------------------------- /lib/freesurfer/matlab/fwrite3.m: -------------------------------------------------------------------------------- 1 | function fwrite3(fid, val) 2 | 3 | 4 | % 5 | % fwrite3.m 6 | % 7 | % Original Author: Bruce Fischl 8 | % CVS Revision Info: 9 | % $Author: nicks $ 10 | % $Date: 2011/03/02 00:04:12 $ 11 | % $Revision: 1.3 $ 12 | % 13 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 14 | % 15 | % Terms and conditions for use, reproduction, distribution and contribution 16 | % are found in the 'FreeSurfer Software License Agreement' contained 17 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 18 | % 19 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 20 | % 21 | % Reporting: freesurfer@nmr.mgh.harvard.edu 22 | % 23 | 24 | % write a 3 byte integer out of a file 25 | %fwrite(fid, val, '3*uchar') ; 26 | b1 = bitand(bitshift(val, -16), 255) ; 27 | b2 = bitand(bitshift(val, -8), 255) ; 28 | b3 = bitand(val, 255) ; 29 | fwrite(fid, b1, 'uchar') ; 30 | fwrite(fid, b2, 'uchar') ; 31 | fwrite(fid, b3, 'uchar') ; 32 | 33 | 34 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/getFaceArea.m: -------------------------------------------------------------------------------- 1 | function farea = getFaceArea(mesh_in,ListFaces, face) 2 | 3 | % to measure the area of a single face (given by the index of a face in a list of faces) 4 | 5 | v1 = ListFaces(face,1); 6 | v2 = ListFaces(face,2); 7 | v3 = ListFaces(face,3); 8 | 9 | first_comp = [mesh_in.vertices(v1,2), mesh_in.vertices(v1,3), 1; ... 10 | mesh_in.vertices(v2,2), mesh_in.vertices(v2,3), 1; ... 11 | mesh_in.vertices(v3,2), mesh_in.vertices(v3,3) 1]; 12 | 13 | scnd_comp = [mesh_in.vertices(v1,3), mesh_in.vertices(v1,1), 1; ... 14 | mesh_in.vertices(v2,3), mesh_in.vertices(v2,1), 1; ... 15 | mesh_in.vertices(v3,3), mesh_in.vertices(v3,1), 1]; 16 | 17 | third_comp = [mesh_in.vertices(v1,1), mesh_in.vertices(v1,2), 1; ... 18 | mesh_in.vertices(v2,1), mesh_in.vertices(v2,2), 1; ... 19 | mesh_in.vertices(v3,1), mesh_in.vertices(v3,2), 1]; 20 | 21 | a = det(first_comp)^2; 22 | b = det(scnd_comp)^2; 23 | c = det(third_comp)^2; 24 | 25 | farea = 0.5*(sqrt(a+b+c)); 26 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/getFacesArea.m: -------------------------------------------------------------------------------- 1 | function totalArea = getFacesArea(mesh_in, ListFaces) 2 | 3 | % to measure the area of a ROI given by a list of faces 4 | 5 | totalArea = 0; 6 | 7 | for iF = 1:size(ListFaces,1) 8 | totalArea = totalArea + getFaceArea(mesh_in,ListFaces,iF); 9 | end 10 | 11 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/getMeshArea.m: -------------------------------------------------------------------------------- 1 | function area = getMeshArea (mesh) 2 | 3 | area=0; 4 | for i=1:size(mesh.faces) 5 | p1=mesh.faces(i,1); 6 | p2=mesh.faces(i,2); 7 | p3=mesh.faces(i,3); 8 | first_comp=[mesh.vertices(p1,2) mesh.vertices(p1,3) 1 ; mesh.vertices(p2,2) mesh.vertices(p2,3) 1; mesh.vertices(p3,2) mesh.vertices(p3,3) 1]; 9 | scnd_comp=[mesh.vertices(p1,3) mesh.vertices(p1,1) 1 ; mesh.vertices(p2,3) mesh.vertices(p2,1) 1; mesh.vertices(p3,3) mesh.vertices(p3,1) 1]; 10 | third_comp=[mesh.vertices(p1,1) mesh.vertices(p1,2) 1 ; mesh.vertices(p2,1) mesh.vertices(p2,2) 1; mesh.vertices(p3,1) mesh.vertices(p3,2) 1]; 11 | a=det(first_comp); 12 | a=a^2; 13 | b=det(scnd_comp); 14 | b=b^2; 15 | c=det(third_comp); 16 | c=c^2; 17 | area_i=0.5*(sqrt(a+b+c)); 18 | area=area+area_i; 19 | end 20 | 21 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/getOrthogonalVector.m: -------------------------------------------------------------------------------- 1 | function vo = getOrthogonalVector(v) 2 | 3 | vn = v ./ norm(v); 4 | 5 | vo = [0,0,0]; 6 | while (isequal(vo,[0,0,0])) 7 | vo = rand(1,3)-0.5; 8 | vc = vo*vn'; 9 | vo = vo-(vn*vc); 10 | end 11 | 12 | vo = vo ./ norm(vo); 13 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/getVerticesAndFacesInSphere.m: -------------------------------------------------------------------------------- 1 | function [verticesList, facesList] = getVerticesAndFacesInSphere(mesh_outer, iV, radius) 2 | 3 | % find all the vertices included in a sphere 4 | verticesList = []; 5 | 6 | for vertex = 1:length(mesh_outer.vertices) 7 | if isVertexInRadius(mesh_outer.vertices(vertex,:), mesh_outer.vertices(iV,:), radius) 8 | verticesList = [verticesList,vertex]; 9 | end 10 | end 11 | 12 | % find faces to which those vertices belong 13 | facesList = []; 14 | for vert = verticesList 15 | facesList = [facesList, mesh_outer.facesOfVertex(vert).faceList]; 16 | end 17 | 18 | facesList = unique(facesList); 19 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/isInGeodesicROI.m: -------------------------------------------------------------------------------- 1 | function result = isInGeodesicROI(mesh_total,wholepath,face) 2 | 3 | wholepath=wholepath(2:end); 4 | verticesinfaces=mesh_total.faces(face,:); 5 | b = intersect(verticesinfaces,wholepath); 6 | 7 | resultat = size(b,2) < 1; 8 | result = resultat(1); 9 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/isVertexInRadius.m: -------------------------------------------------------------------------------- 1 | function result = isVertexInRadius(vertex, origin, radius) 2 | 3 | vt = vertex - origin; 4 | 5 | result = ( ((vt(1)^2 + vt(2)^2) <= (radius^2)) & ... 6 | ((vt(2)^2 + vt(3)^2) <= (radius^2)) & ... 7 | ((vt(1)^2 + vt(3)^2) <= (radius^2)) ); 8 | 9 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/isdicomfile.m: -------------------------------------------------------------------------------- 1 | function [res, dcminfo] = isdicomfile(fname) 2 | % [res, dcminfo] = isdicomfile(fname) 3 | % 4 | % Determines whether the given file is dicom by 5 | % trying to read the dicom info. If this fails, 6 | % then res=0 and dcminfo=[]. If successful, then 7 | % res=1, and dcminfo is the result of matlabs 8 | % dicominfo. 9 | % 10 | 11 | 12 | % 13 | % isdicomfile.m 14 | % 15 | % Original Author: Doug Greve 16 | % CVS Revision Info: 17 | % $Author: nicks $ 18 | % $Date: 2011/03/02 00:04:12 $ 19 | % $Revision: 1.3 $ 20 | % 21 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 22 | % 23 | % Terms and conditions for use, reproduction, distribution and contribution 24 | % are found in the 'FreeSurfer Software License Agreement' contained 25 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 26 | % 27 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 28 | % 29 | % Reporting: freesurfer@nmr.mgh.harvard.edu 30 | % 31 | 32 | 33 | try 34 | dcminfo = dicominfo(fname); 35 | res = 1; 36 | catch 37 | dcminfo = []; 38 | res = 0; 39 | end 40 | 41 | return; 42 | 43 | %%%%%%% This was Anders original code, does not always work %%%%%%%% 44 | fid = fopen(fname,'r'); 45 | if fid < 0 46 | res = 0; 47 | else 48 | stat = fseek(fid,128,'bof'); % move to DICM string 49 | tmp = char(fread(fid,4,'uchar')');%' 50 | res = strcmp(tmp,'DICM'); 51 | fclose(fid); 52 | end 53 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/juelichmat2mat.m: -------------------------------------------------------------------------------- 1 | function err = juelichmat2mat(matfile) 2 | % err = juelichmat2mat(matfile) 3 | % $Id: juelichmat2mat.m,v 1.1 2007/08/16 02:10:12 greve Exp $ 4 | 5 | err = 1; 6 | if(nargin ~= 1) 7 | printf('err = juelichmat2mat(matfile)'); 8 | return; 9 | end 10 | 11 | if(~exist(matfile,'file')) 12 | printf('ERROR: cannot find %s\n',matfile); 13 | return; 14 | end 15 | 16 | bakfile = sprintf('%s.bak',matfile); 17 | if(exist(bakfile,'file')) 18 | printf('ERROR: backup file already exists for %s\n',matfile); 19 | return; 20 | end 21 | 22 | a = load(matfile); 23 | if(~isfield(a,'M')) 24 | printf('ERROR: %s does not have an M variable\n',matfile); 25 | return; 26 | end 27 | M = a.M; 28 | 29 | cmd = sprintf('cp %s %s',matfile,bakfile); 30 | s = unix(cmd); 31 | if(s ~= 0) 32 | printf('ERROR: when executing %s\n',cmd); 33 | return 34 | end 35 | 36 | save(matfile,'M','-v4'); 37 | 38 | err = 0; 39 | return; 40 | 41 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lme/Qdec/Qdec2num.m: -------------------------------------------------------------------------------- 1 | function M = Qdec2num(Qdec) 2 | % M = Qdec2num(Qdec) 3 | % 4 | % Attempts to convert the two dimensional cell string array Qdec to numeric 5 | % matrix M. 6 | % 7 | % Input 8 | % Qdec: Two dimensional cell string array of Qdec data (eg. read with 9 | % fReadQdec). 10 | % 11 | % Output 12 | % M: Numeric matrix (without the name of the variables). 13 | % 14 | % $Revision: 1.1.2.2 $ $Date: 2013/02/23 21:08:09 $ 15 | % Original Author: Jorge Luis Bernal Rusiel 16 | % CVS Revision Info: 17 | % $Author: nicks $ 18 | % $Date: 2013/02/23 21:08:09 $ 19 | % $Revision: 1.1.2.2 $ 20 | % 21 | Qdec = Qdec(2:end,:); 22 | M = str2double(Qdec); 23 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lme/Qdec/fReadQdec.m: -------------------------------------------------------------------------------- 1 | function Qdec = fReadQdec(fname) 2 | % Qdec = fReadQdec(fname) 3 | % 4 | % Reads a Freesurfer's Qdec table file into cell string array Qdec. 5 | % 6 | % Input 7 | % fname: The name of a Qdec table data file. 8 | % 9 | % Output 10 | % Qdec: Two dimensional cell string array with the data. 11 | % 12 | % $Revision: 1.1.2.2 $ $Date: 2013/02/23 21:08:09 $ 13 | % Original Author: Jorge Luis Bernal Rusiel 14 | % CVS Revision Info: 15 | % $Author: nicks $ 16 | % $Date: 2013/02/23 21:08:09 $ 17 | % $Revision: 1.1.2.2 $ 18 | % 19 | fid = fopen(fname); 20 | tline = fgetl(fid); 21 | Qdec = []; 22 | i = 1; 23 | while ischar(tline) 24 | j = 1; 25 | [str,remain] = strtok(tline, ' '); 26 | while ~isempty(str) 27 | Qdec{i,j} = str; 28 | j = j + 1; 29 | [str,remain] = strtok(remain, ' '); 30 | end 31 | i = i + 1; 32 | tline = fgetl(fid); 33 | end 34 | fclose(fid); 35 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lme/Qdec/fWriteQdec.m: -------------------------------------------------------------------------------- 1 | function fWriteQdec(fname,Qdec) 2 | % fWriteQdec(fname,Qdec) 3 | % 4 | % Writes cell string array Qdec to a Freesurfer's Qdec table data file. 5 | % 6 | % Input 7 | % fname: The name of a Qdec table data file. 8 | % Qdec: Two dimensional cell string array with the data. 9 | % 10 | % $Revision: 1.1.2.2 $ $Date: 2013/02/23 21:08:09 $ 11 | % Original Author: Jorge Luis Bernal Rusiel 12 | % CVS Revision Info: 13 | % $Author: nicks $ 14 | % $Date: 2013/02/23 21:08:09 $ 15 | % $Revision: 1.1.2.2 $ 16 | % 17 | if nargin < 2 18 | error('Too few inputs'); 19 | end; 20 | szQ = size(Qdec); 21 | fid = fopen (fname,'w'); 22 | if fid < 0 23 | error('Do you have write permissions for %s?', pwd); 24 | end 25 | if ~strcmp(Qdec{1,1},'fsid') 26 | warning('Dat{1,1} is not ''fsid'' will change it '); 27 | Qdec{1,1} = 'fsid'; 28 | end; 29 | for i=1:szQ(1) 30 | j = 1; 31 | while j < szQ(2) 32 | fprintf(fid, [char(Qdec{i,j}) ' ']); 33 | j = j + 1; 34 | end; 35 | fprintf(fid, [char(Qdec{i,j}) '\n']); 36 | end; 37 | fclose (fid); 38 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lme/Qdec/findQdecVar.m: -------------------------------------------------------------------------------- 1 | function col = findQdecVar(Qdec,name) 2 | % col = findQdecVar(Qdec,name) 3 | % 4 | % Gives the column of a variable in a Freesurfer's Qdec table that was read 5 | % into the two dimensional cell string array Qdec. Returns empty if the 6 | % variable is not in the Qdec table. 7 | % 8 | % Input 9 | % Qdec: Two dimensional cell string array of Qdec data (eg. read with 10 | % fReadQdec). 11 | % name: A string with the name of the variable. 12 | % 13 | % Output 14 | % col: Column of the variable in the cell string array Qdec. 15 | % 16 | % $Revision: 1.1.2.2 $ $Date: 2013/02/23 21:08:10 $ 17 | % Original Author: Jorge Luis Bernal Rusiel 18 | % CVS Revision Info: 19 | % $Author: nicks $ 20 | % $Date: 2013/02/23 21:08:10 $ 21 | % $Revision: 1.1.2.2 $ 22 | % 23 | if nargin < 2 24 | error('Too few inputs'); 25 | end; 26 | col = find(strcmp(getQdecVars(Qdec),name)); -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lme/Qdec/getQdecVarData.m: -------------------------------------------------------------------------------- 1 | function Qdec2 = getQdecVarData(Qdec1,Vars) 2 | % Qdec2 = getQdecVarData(Qdec1,Vars) 3 | % 4 | % Returns the data for the variables in Qdec table Qdec1 that are named in 5 | % Vars. 6 | % 7 | % Input 8 | % Qdec1: Two dimensional cell string array of Qdec data (eg. read with 9 | % fReadQdec). 10 | % Vars: One dimensional cell string array. 11 | % 12 | % Output 13 | % Qdec2: Two dimensional cell string array of Qdec data. 14 | % 15 | % $Revision: 1.1.2.2 $ $Date: 2013/02/23 21:08:10 $ 16 | % Original Author: Jorge Luis Bernal Rusiel 17 | % CVS Revision Info: 18 | % $Author: nicks $ 19 | % $Date: 2013/02/23 21:08:10 $ 20 | % $Revision: 1.1.2.2 $ 21 | % 22 | if nargin < 2 23 | error('Too few inputs'); 24 | end; 25 | Qdec2 = {}; 26 | for i=1:length(Vars) 27 | col = findQdecVar(Qdec1,Vars{i}); 28 | if ~isempty(col) 29 | Qdec2 = [Qdec2 Qdec1(:,col)]; 30 | else 31 | error(['The variable ''' Vars{i} ''' is not in the Qdec table']); 32 | end; 33 | end; -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lme/Qdec/getQdecVars.m: -------------------------------------------------------------------------------- 1 | function Vars = getQdecVars(Qdec) 2 | % Vars = getQdecVars(Qdec) 3 | % 4 | % Returns a one dimensional cell string array with the name of the variables 5 | % in Qdec. 6 | % 7 | % Input 8 | % Qdec: Two dimensional cell string array of Qdec data. 9 | % 10 | % Output 11 | % Vars: One dimensional cell string array. 12 | % 13 | % $Revision: 1.1.2.2 $ $Date: 2013/02/23 21:08:10 $ 14 | % Original Author: Jorge Luis Bernal Rusiel 15 | % CVS Revision Info: 16 | % $Author: nicks $ 17 | % $Date: 2013/02/23 21:08:10 $ 18 | % $Revision: 1.1.2.2 $ 19 | % 20 | Vars = Qdec(1,:)'; -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lme/Qdec/num2Qdec.m: -------------------------------------------------------------------------------- 1 | function Qdec = num2Qdec(M,Vars) 2 | % Qdec = num2Qdec(M,Vars) 3 | % 4 | % Converts numeric matrix M to two dimensional cell string array Qdec. 5 | % 6 | % Input 7 | % M: Numeric matrix. 8 | % Vars: One dimensional cell string array with the name of the Qdec 9 | % variables corresponding to the columns of M. 10 | % 11 | % Output 12 | % Qdec: Two dimensional cell string array of Qdec data . 13 | % 14 | % $Revision: 1.1.2.2 $ $Date: 2013/02/23 21:08:10 $ 15 | % Original Author: Jorge Luis Bernal Rusiel 16 | % CVS Revision Info: 17 | % $Author: nicks $ 18 | % $Date: 2013/02/23 21:08:10 $ 19 | % $Revision: 1.1.2.2 $ 20 | % 21 | if nargin < 2 22 | error('Too few inputs'); 23 | end; 24 | szM = size(M); 25 | Qdec = cell(szM(1)+1,szM(2)); 26 | Qdec(1,1:end) = Vars; 27 | for i=2:szM(1)+1 28 | for j=1:szM(2) 29 | Qdec{i,j} = num2str(M(i-1,j)); 30 | end; 31 | end; -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lme/Qdec/rmQdecCol.m: -------------------------------------------------------------------------------- 1 | function Qdec2 = rmQdecCol(Qdec1,col) 2 | % Qdec2 = rmQdecCol(Qdec1,col) 3 | % 4 | % Removes the specify column from cell string array Qdec1. 5 | % 6 | % Input 7 | % Qdec1: Two dimensional cell string array of Qdec data (eg. read with 8 | % fReadQdec). 9 | % col: Column to be removed. 10 | % 11 | % Output 12 | % Qdec2: Two dimensional cell string array of Qdec data. 13 | % 14 | % $Revision: 1.1.2.2 $ $Date: 2013/02/23 21:08:10 $ 15 | % Original Author: Jorge Luis Bernal Rusiel 16 | % CVS Revision Info: 17 | % $Author: nicks $ 18 | % $Date: 2013/02/23 21:08:10 $ 19 | % $Revision: 1.1.2.2 $ 20 | % 21 | if nargin < 2 22 | error('Too few inputs'); 23 | end; 24 | Qdec2 = [Qdec1(:,1:col-1) Qdec1(:,col+1:end)]; 25 | 26 | 27 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lme/fs_read_Y.m: -------------------------------------------------------------------------------- 1 | function [Y,mri] = fs_read_Y(Y_fname) 2 | % [Y,mri] = fs_read_Y(Y_fname) 3 | % 4 | % Reads Freesurfer's .mgh or .mgz data files (eg. cortical thickness data 5 | % files generated with mris_preproc). 6 | % 7 | % Input 8 | % Y_fname: A Freesurfer's cortical data file. 9 | % 10 | % Output 11 | % Y: Data matrix (nmxnv, nm total # of maps, nv #vertices). 12 | % mri: Mri structure. 13 | % 14 | % $Revision: 1.1.2.2 $ $Date: 2013/02/23 21:08:09 $ 15 | % Original Author: Jorge Luis Bernal Rusiel 16 | % CVS Revision Info: 17 | % $Author: nicks $ 18 | % $Date: 2013/02/23 21:08:09 $ 19 | % $Revision: 1.1.2.2 $ 20 | % 21 | 22 | if nargin < 1 23 | error('Too few inputs'); 24 | end; 25 | mri = struct('M',[],'mr_parms',[],'volsz',[]); 26 | [Y,M,mr_parms,volsz] = load_mgh(Y_fname); 27 | if (isempty(M)) 28 | error(['Can not load ' Y_fname ' as an mgh or mgz file']); 29 | end; 30 | mri.M = M; 31 | mri.mr_parms = mr_parms; 32 | mri.volsz = volsz; 33 | nv = volsz(1)*volsz(2)*volsz(3); 34 | Y = reshape(Y,[nv volsz(4)])'; -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lme/fs_read_label.m: -------------------------------------------------------------------------------- 1 | function [vtxs,nvtxs] = fs_read_label(labelf) 2 | % [vtxs,nvtxs] = fs_read_label(labelf) 3 | % 4 | % Reads the indices of the vertices of a Freesurfer's label. 5 | % 6 | % Input 7 | % labelf: A Freesurfer's label file. 8 | % 9 | % Output 10 | % vtxs: Indices of the vertices (1-based). 11 | % nvtxs: The number of vertices in the label. 12 | % 13 | % $Revision: 1.1.2.2 $ $Date: 2013/02/23 21:08:09 $ 14 | % Original Author: Jorge Luis Bernal Rusiel 15 | % CVS Revision Info: 16 | % $Author: nicks $ 17 | % $Date: 2013/02/23 21:08:09 $ 18 | % $Revision: 1.1.2.2 $ 19 | % 20 | fid = fopen(labelf); 21 | tline = fgetl(fid); 22 | nvtxs = fscanf(fid,'%d',1); 23 | vtxs = uint32(fscanf(fid,'%d %*g %*g %*g %*g',[1 nvtxs])); 24 | vtxs = vtxs + 1; 25 | fclose(fid); 26 | 27 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lme/fs_read_surf.m: -------------------------------------------------------------------------------- 1 | function Surf = fs_read_surf(fname) 2 | % Surf = fs_read_surf(fname) 3 | % 4 | % Reads coordinates and triangles of a Freesurfer's surf (Depends on 5 | % Freesurfer's read_surf function). 6 | % 7 | % Input 8 | % fname: A Freesurfer's surface file. 9 | % 10 | % Output 11 | % Surf: Surface which represent a coordinate system where the analysis is 12 | % to be done. It is a structure with Surf.tri = t x 3 matrix of triangle 13 | % indices, 1-based, t=#triangles and Surf.coord = 3 x nv matrix of 14 | % coordinates, nv=#vertices. 15 | % 16 | % $Revision: 1.1.2.2 $ $Date: 2013/02/23 21:08:09 $ 17 | % Original Author: Jorge Luis Bernal Rusiel 18 | % CVS Revision Info: 19 | % $Author: nicks $ 20 | % $Date: 2013/02/23 21:08:09 $ 21 | % $Revision: 1.1.2.2 $ 22 | % 23 | [coord,faces] = read_surf(fname); 24 | Surf.tri = uint32(faces + 1); 25 | Surf.coord = coord'; -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lme/fs_write_Y.m: -------------------------------------------------------------------------------- 1 | function fs_write_Y(Y,mri,fname) 2 | % fs_write_Y(Y,mri,fname) 3 | % 4 | % Writes volume Y to a Freesurfer's .mgh or .mgz data file. 5 | % 6 | % Input 7 | % Y: Data matrix or vector. 8 | % mri: Mri structure (read with fs_read_Y). 9 | % fname: Output file name. 10 | % 11 | % $Revision: 1.1.2.2 $ $Date: 2013/02/23 21:08:09 $ 12 | % Original Author: Jorge Luis Bernal Rusiel 13 | % CVS Revision Info: 14 | % $Author: nicks $ 15 | % $Date: 2013/02/23 21:08:09 $ 16 | % $Revision: 1.1.2.2 $ 17 | % 18 | if nargin < 3 19 | error('Too few inputs'); 20 | end; 21 | save_mgh(reshape(Y',mri.volsz),fname,mri.M,mri.mr_parms); -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lme/fs_write_fstats.m: -------------------------------------------------------------------------------- 1 | function fs_write_fstats(fstats,mri,fname,data_type) 2 | % fs_write_fstats(fstats,mri,fname,data_type) 3 | % 4 | % Writes F-statistic or p-value or Freesurfer significance maps to Freesurfer's 5 | % .mgh or .mgz data file. This can be useful for visualization and post-processing 6 | % in Freesurfer. 7 | % 8 | % Input 9 | % fstats: Structure obtained from lme_mass_F. 10 | % mri: Mri structure (read with fs_read_Y). 11 | % fname: Output file name. 12 | % data_type: Determines what is going to be written. This input can be one 13 | % of three strings: 'fval' (signed F-statistic map), 'pval' (signed p-value map) 14 | % or 'sig' (Freesurfer significance map -log10(pval).*sgn). 15 | % 16 | % $Revision: 1.1.2.2 $ $Date: 2013/02/23 21:08:09 $ 17 | % Original Author: Jorge Luis Bernal Rusiel 18 | % CVS Revision Info: 19 | % $Author: nicks $ 20 | % $Date: 2013/02/23 21:08:09 $ 21 | % $Revision: 1.1.2.2 $ 22 | % 23 | if nargin < 4 24 | error('Too few inputs'); 25 | end; 26 | mri.volsz(4) = 1; 27 | if strcmpi(data_type,'fval') 28 | fs_write_Y(fstats.F.*fstats.sgn,mri,fname); 29 | elseif strcmpi(data_type,'pval') 30 | pval = fstats.pval.*fstats.sgn; 31 | pval(pval==1) = 1; 32 | fs_write_Y(pval,mri,fname); 33 | elseif strcmpi(data_type,'sig') 34 | fs_write_Y(-log10(fstats.pval).*fstats.sgn,mri,fname); 35 | else 36 | error('Valid strings for data_type are ''fval'' or ''pval'' or ''sig'''); 37 | end; 38 | 39 | 40 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lme/geodesic/create_flat_triangular_mesh.m: -------------------------------------------------------------------------------- 1 | %good mesh to catch possible bugs in geodesic algorithms 2 | %Copyright (c) 2007 Danil Kirsanov 3 | function [p,tri] = create_flat_triangular_mesh(step, smoothness) 4 | 5 | x = -1:step:1; 6 | y = x; 7 | 8 | N_p = length(x)*length(y); %regular grid 9 | p = zeros(N_p,3); 10 | N_t = (length(x)-1)*(length(y)-1)*2; %two triangles per square 11 | tri = zeros(N_t,3); 12 | 13 | n_p = 1; 14 | n_t = 1; 15 | for i=1:length(x); 16 | for j=1:length(y); 17 | p(n_p,:) = [x(i) y(j) 0]; 18 | 19 | if i 1 24 | p(i,:) = p(i,:)/max(abs(p(i,:))); 25 | end 26 | 27 | scale = abs(p(i,1))*waist + (1 - waist); 28 | p(i,2:3) = p(i,2:3)*scale; % add "waist" on x-axis 29 | end; 30 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lme/geodesic/create_subdivision_pattern.m: -------------------------------------------------------------------------------- 1 | %regular subdivision pattern of a triangle 2 | %used in drawing approximate equidistant lines in example5.m 3 | %Copyright (c) 2007 Danil Kirsanov 4 | function [weights,tri] = create_subdivision_pattern(level) %"level" is the number of additional vertices per edge 5 | 6 | step = 1/(level + 1); 7 | N = level + 2; %total number of points per edge 8 | 9 | N_p = (N^2 + N)/2; %number of vertices in the regular grid 10 | N_t = (N-1)^2; %two triangles per square 11 | weights = zeros(N_p,3); 12 | tri = zeros(N_t,3); 13 | 14 | n_p = 1; 15 | n_t = 1; 16 | for i=1:N; 17 | K = N-i+1; 18 | for j=1:K 19 | c = step*[j-1, i-1]; 20 | weights(n_p,:) = [c, 1-sum(c)]; 21 | 22 | if i 0 23 | f = fopen(fname, 'a'); 24 | fprintf(f, [num2str(N) '\n']); 25 | fclose(f); 26 | f = fopen(fname, 'r'); 27 | progress = fscanf(f, '%d'); 28 | fclose(f); 29 | percent = 100*sum(progress(2:end))/progress(1); 30 | perc = sprintf('%3.0f%%', percent); % 4 characters wide, percentage 31 | disp([ 'Aproximate percentage of fitted locations: ' perc]); 32 | else 33 | delete(fname); 34 | disp('Aproximate percentage of fitted locations: 100%'); 35 | end; 36 | end; 37 | 38 | 39 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lme/mass_univariate/sphCoord.m: -------------------------------------------------------------------------------- 1 | function [phi,theta] = sphCoord(Coord3D) 2 | % [phi,theta] = sphCoord(Coord3D) 3 | % 4 | % This function transforms spherical 3D XYZ spherical coordinates to 2D 5 | % (theta,phi) spherical coordinates. 6 | % 7 | % Input 8 | % Coord3D: Data Matrix with the spherical XYZ coordinates at the first 9 | % three columns respectively. 10 | % 11 | % Output 12 | % theta: Latitude 13 | % phi: Longitud. 14 | % 15 | % $Revision: 1.1.2.2 $ $Date: 2013/02/23 21:08:11 $ 16 | % Original Author: Jorge Luis Bernal Rusiel 17 | % CVS Revision Info: 18 | % $Author: nicks $ 19 | % $Date: 2013/02/23 21:08:11 $ 20 | % $Revision: 1.1.2.2 $ 21 | 22 | % [theta,phi,r] = cart2sph(overlayData(:,1),overlayData(:,2),overlayData(:,3)); 23 | % //It was confusion 24 | 25 | X=Coord3D(:,1); Y=Coord3D(:,2); Z=Coord3D(:,3); 26 | phi=atan2(Y,X); 27 | theta=atan2(sqrt(X.^2+Y.^2),Z); 28 | 29 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lme/univariate/lme_lowessPlot.m: -------------------------------------------------------------------------------- 1 | function lme_lowessPlot(time,y,bw,group) 2 | % lme_lowessPlot(time,y,bw,group) 3 | % 4 | % Time plot of the lowess regression curve estimating the trend in the 5 | % mean response over time (Depends on the curve fitting toolbox). 6 | % 7 | % Input 8 | % time: Time covariate (ordered according to time for each subject) . 9 | % y: Ordered data vector (according to time for each subject). 10 | % bw: Fraction of data points to be included in the sliding window of the 11 | % lowess smoothing method (band width, default 0.6). 12 | % group: A covariate indicating the corresponding group for each subject. 13 | % Default ones(length(time),1) (in this case will also plot the sample data 14 | % points). 15 | % 16 | % 17 | % $Revision: 1.1.2.2 $ $Date: 2013/02/23 21:08:11 $ 18 | % Original Author: Jorge Luis Bernal Rusiel 19 | % CVS Revision Info: 20 | % $Author: nicks $ 21 | % $Date: 2013/02/23 21:08:11 $ 22 | % $Revision: 1.1.2.2 $ 23 | % References: W.S.Cleveland, "Robust Locally Weighted Regression and 24 | % Smoothing Scatterplots", _J. of the American Statistical Ass._, Vol 74, 25 | % No. 368 (Dec.,1979), pp. 829-836. 26 | % http://www.math.tau.ac.il/~yekutiel/MA%20seminar/Cleveland%201979.pdf 27 | % 28 | if nargin < 2 29 | error('Too few inputs'); 30 | else 31 | if nargin < 4 32 | group = ones(length(time),1); 33 | if nargin < 3 34 | bw = 0.6; 35 | end; 36 | end; 37 | end; 38 | un = unique(group); 39 | figure('Name','Lowess plot'); 40 | hold on; 41 | colors = {'b','k','g','c','m','r','y'}; 42 | if length(un)==1 43 | plot(time,y,'ok'); 44 | end; 45 | for i=1:length(un) 46 | t = time(group == un(i)); 47 | dat = y(group == un(i)); 48 | [st,ix] = sort(t); 49 | ybw = smooth(st,dat(ix),bw,'rlowess'); 50 | plot(st,ybw,['-' colors{mod(i,7)+1}],'LineWidth',4); 51 | end; 52 | hold off; 53 | 54 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lme/univariate/lme_realizedPower.m: -------------------------------------------------------------------------------- 1 | function pw = lme_realizedPower(stats,C,alpha) 2 | % pw = lme_realizedPower(stats,C,alpha) 3 | % 4 | % Power associated with a contrast C for the actual realization of the 5 | % linear mixed-effects model (Depends on the Statistics toolbox). 6 | % 7 | % Input 8 | % stats: Structure obtained from any of lme_fit_EM, lme_fit_FS or 9 | % lme_fit_NR functions. 10 | % C: Contrast matrix. 11 | % alpha: Significance level of the test. Default 0.05. 12 | % 13 | % Output 14 | % pw: Power of the test. 15 | % 16 | % $Revision: 1.1.2.2 $ $Date: 2013/02/23 21:08:11 $ 17 | % Original Author: Jorge Luis Bernal Rusiel 18 | % CVS Revision Info: 19 | % $Author: nicks $ 20 | % $Date: 2013/02/23 21:08:11 $ 21 | % $Revision: 1.1.2.2 $ 22 | % References: Bernal-Rusiel J.L., Greve D.N., Reuter M., Fischl B., Sabuncu 23 | % M.R., 2012. Statistical Analysis of Longitudinal Neuroimage Data with Linear 24 | % Mixed Effects Models, NeuroImage, doi:10.1016/j.neuroimage.2012.10.065. 25 | % 26 | if nargin < 2 27 | error('Too few inputs'); 28 | elseif nargin < 3 29 | alpha = 0.05; 30 | end; 31 | X = stats.X; 32 | Zcols = stats.Zcols; 33 | ni = stats.ni; 34 | m = length(ni); 35 | q = length(Zcols); 36 | n = sum(ni); 37 | Z = zeros(n,m*q); 38 | posi = 1; 39 | for i=1:m 40 | posf = posi+ni(i)-1; 41 | Z(posi:posf,(i-1)*q+1:i*q) = X(posi:posf,Zcols); 42 | posi = posf+1; 43 | end; 44 | Bhat = stats.Bhat; 45 | CovBhat = stats.CovBhat; 46 | c = rank(C); 47 | ve = n-rank([X,Z]); 48 | nc = Bhat'*C'*(C*CovBhat*C')^-1*C*Bhat; 49 | cv = finv(1-alpha,c,ve); 50 | pw = 1-ncfcdf(cv,c,ve,nc); 51 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lme/univariate/lme_timePlot.m: -------------------------------------------------------------------------------- 1 | function lme_timePlot(time,y,ni,subjs) 2 | % lme_timePlot(time,y,ni,subjs) 3 | % 4 | % Spaghetti time plot of longitudinal data. 5 | % 6 | % Input 7 | % time: Time covariate. 8 | % y: Ordered data vector (according to time for each subject). 9 | % ni: Vector whose entries are the number of repeated measures for each 10 | % subject in the study (ordered according to y). 11 | % subjs: A vector with the indices of a particular subset of subjects. If 12 | % not specify, time series data for all subjects will be ploted. 13 | % 14 | % $Revision: 1.1.2.2 $ $Date: 2013/02/23 21:08:11 $ 15 | % Original Author: Jorge Luis Bernal Rusiel 16 | % CVS Revision Info: 17 | % $Author: nicks $ 18 | % $Date: 2013/02/23 21:08:11 $ 19 | % $Revision: 1.1.2.2 $ 20 | % 21 | if nargin < 3 22 | error('Too few inputs'); 23 | elseif nargin < 4 24 | t = time; 25 | dat = y; 26 | else 27 | n = sum(ni(subjs)); 28 | t = zeros(n,1); 29 | dat = zeros(n,1); 30 | pos1 = 0; 31 | for i=1:length(subjs) 32 | pos2 = sum(ni(1:subjs(i)-1)); 33 | t(pos1+1:pos1+ni(subjs(i))) = time(pos2+1:pos2+ni(subjs(i))); 34 | dat(pos1+1:pos1+ni(subjs(i))) = y(pos2+1:pos2+ni(subjs(i))); 35 | pos1 = pos1+ni(subjs(i)); 36 | end; 37 | ni = ni(subjs); 38 | end; 39 | figure('Name','Time plot'); 40 | hold on; 41 | pos1 = 0; 42 | for i=1:length(ni) 43 | plot(t(pos1+1:pos1+ni(i)),dat(pos1+1:pos1+ni(i)),'-ok'); 44 | pos1 = pos1+ni(i); 45 | end; 46 | hold off; 47 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lme/univariate/vec.m: -------------------------------------------------------------------------------- 1 | function h = vec(H) 2 | % h = vec(H) 3 | % 4 | % Vectorize matrix. 5 | % 6 | % H: Matrix 7 | % 8 | % $Revision: 1.1.2.2 $ $Date: 2013/02/23 21:08:11 $ 9 | % Original Author: Jorge Luis Bernal Rusiel 10 | % CVS Revision Info: 11 | % $Author: nicks $ 12 | % $Date: 2013/02/23 21:08:11 $ 13 | % $Revision: 1.1.2.2 $ 14 | % 15 | 16 | h = H(:); 17 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/lta_read.m: -------------------------------------------------------------------------------- 1 | function M = lta_read(fname) 2 | % M = lta_read(fname) 3 | 4 | 5 | % 6 | % lta_read.m 7 | % 8 | % Original Author: Bruce Fischl 9 | % CVS Revision Info: 10 | % $Author: nicks $ 11 | % $Date: 2011/03/02 00:04:12 $ 12 | % $Revision: 1.3 $ 13 | % 14 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 15 | % 16 | % Terms and conditions for use, reproduction, distribution and contribution 17 | % are found in the 'FreeSurfer Software License Agreement' contained 18 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 19 | % 20 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 21 | % 22 | % Reporting: freesurfer@nmr.mgh.harvard.edu 23 | % 24 | 25 | if (strcmp(fname((length(fname)-2):length(fname)), 'xfm') | ... 26 | strcmp(fname((length(fname)-2):length(fname)), 'XFM')) 27 | M = xfm_read(fname) ; 28 | return ; 29 | end 30 | 31 | 32 | fid = fopen(fname) ; 33 | if (fid < 0) 34 | error(sprintf('could not open file %s', fname)); 35 | end 36 | 37 | tline = fgetl(fid) ; 38 | while ((length(tline) > 0) & (tline(1) == '#')) 39 | tline = fgetl(fid) ; 40 | end 41 | 42 | 43 | tline = fgetl(fid) ; % type 44 | tline = fgetl(fid) ; % nxforms 45 | tline = fgetl(fid) ; % mean 46 | tline = fgetl(fid) ; % sigma 47 | tline = fgetl(fid) ; % dimensions 48 | 49 | M = zeros(4,4) ; 50 | for row=1:4 51 | tline = fgetl(fid) ; % one row of matrix 52 | tmp = sscanf(tline, '%f'); 53 | M(row,:) = tmp'; 54 | end 55 | 56 | fclose(fid) ; 57 | 58 | 59 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/mesh_adjacency.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hmlicas/Collaborative_Brain_Decomposition/6f3ad613d065fe592979e1f105065ed22472e999/lib/freesurfer/matlab/mesh_adjacency.m -------------------------------------------------------------------------------- /lib/freesurfer/matlab/mesh_vertex_nearest.m: -------------------------------------------------------------------------------- 1 | function [nearestIndex,nearestValues] = mesh_vertex_nearest(vertices,points) 2 | 3 | % mesh_vertex_nearest - find nearest vertices to specified points 4 | % 5 | % Usage: [nearestIndex,nearestValues] = mesh_vertex_nearest(vertices,points) 6 | % 7 | % vertices is a Vx3 matrix of 3D Cartesian coordinates. 8 | % points is a Px3 matrix of 3D Cartesian coordinates. These points need not 9 | % be among the vertices, but they are somewhere near to particular points 10 | % in the vertices cloud. The function finds just one of the nearest 11 | % vertices in the cloud for each of these points. 12 | % 13 | % nearestIndex is the indices into vertices nearest to points 14 | % nearestValues is the coordinates for nearestIndex 15 | % 16 | % This function is just a wrapper for dsearchn. 17 | % 18 | 19 | % $Revision: 1.1 $ $Date: 2007/11/16 02:54:47 $ 20 | 21 | % Author: Darren.Weber_at_radiology.ucsf.edu 22 | % Created: 23 May 2004 23 | % 24 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 25 | 26 | %fprintf('...Finding nearest vertices to points.\n'); 27 | 28 | 29 | nearestIndex = dsearchn(vertices,points); 30 | nearestValues = vertices(nearestIndex,:); 31 | 32 | % for p = 1:length(points), 33 | % tmp = repmat(points(p,:),length(vertices),1); 34 | % [minValue, nearestIndex] = min(abs(vertices - tmp)); 35 | % end 36 | % nearVertex = vertices(nearestIndex,:); 37 | 38 | 39 | 40 | 41 | 42 | return 43 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/mri_cdf2p.m: -------------------------------------------------------------------------------- 1 | function p = mri_cdf2p(x,xcdf,cdf) 2 | % p = mri_cdf2p(x,xcdf,cdf) 3 | % 4 | % Prob that x <= X. x can be any size, but xcdf and cdf 5 | % must be vectors of the same length. 6 | % 7 | % Note: when creating xcdf and cdf from ComputePDF, 8 | % make sure to subtract xdelta from xcdf before 9 | % using this function. 10 | % 11 | % $Id: mri_cdf2p.m,v 1.2 2011/03/02 00:04:12 nicks Exp $ 12 | 13 | % 14 | % mri_cdf2p.m 15 | % 16 | % Original Author: Doug Greve 17 | % CVS Revision Info: 18 | % $Author: nicks $ 19 | % $Date: 2011/03/02 00:04:12 $ 20 | % $Revision: 1.2 $ 21 | % 22 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 23 | % 24 | % Terms and conditions for use, reproduction, distribution and contribution 25 | % are found in the 'FreeSurfer Software License Agreement' contained 26 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 27 | % 28 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 29 | % 30 | % Reporting: freesurfer@nmr.mgh.harvard.edu 31 | % 32 | 33 | p = []; 34 | if(nargin ~= 3) 35 | fprintf('p = mri_cdf2p(x,xcdf,cdf)\n'); 36 | return; 37 | end 38 | 39 | xsize = size(x); 40 | x = transpose(x(:)); % row vector 41 | nx = length(x); 42 | 43 | xcdf = xcdf(:); % column vector 44 | nxcdf = length(xcdf); 45 | 46 | dx = repmat(x,[nxcdf 1]) - repmat(xcdf,[1 nx]); 47 | 48 | [dxmin idxmin] = min(abs(dx)); 49 | 50 | p = cdf(idxmin); 51 | p = reshape(p,xsize); 52 | 53 | return; 54 | 55 | 56 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/mri_kurtosis.m: -------------------------------------------------------------------------------- 1 | function g2 = mri_kurtosis(x,unbiasedflag,dim) 2 | % g2 = mri_kurtosis(x,,) 3 | % Kurtosis estimator. 4 | % Biased by default. For unbiased set unbiasedflag=1. 5 | % To make estimator zero mean, subtract 3. 6 | % 7 | % $Id: mri_kurtosis.m,v 1.4 2011/03/02 00:04:12 nicks Exp $ 8 | 9 | % 10 | % mri_kurtosis.m 11 | % 12 | % Original Author: Doug Greve 13 | % CVS Revision Info: 14 | % $Author: nicks $ 15 | % $Date: 2011/03/02 00:04:12 $ 16 | % $Revision: 1.4 $ 17 | % 18 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 19 | % 20 | % Terms and conditions for use, reproduction, distribution and contribution 21 | % are found in the 'FreeSurfer Software License Agreement' contained 22 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 23 | % 24 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 25 | % 26 | % Reporting: freesurfer@nmr.mgh.harvard.edu 27 | % 28 | 29 | 30 | g2 = []; 31 | if(nargin < 1 & nargin > 3) 32 | fprintf('g2 = mri_kurtosis(x,,)\n'); 33 | return; 34 | end 35 | 36 | if(~exist('unbiasedflag','var')) unbiasedflag = []; end 37 | if(isempty(unbiasedflag)) unbiasedflag = 0; end 38 | if(~exist('dim','var')) dim = []; end 39 | if(isempty(dim)) dim = 1; end 40 | 41 | szx = size(x); 42 | n = szx(dim); 43 | 44 | xmean = mean(x,dim); 45 | tmp = ones(1,length(szx)); 46 | tmp(dim) = n; 47 | xmeanRep = repmat(xmean,tmp); 48 | 49 | m4 = n*sum((x - xmeanRep).^4,dim); 50 | m2 = sum((x - xmeanRep).^2,dim); 51 | 52 | if(unbiasedflag) 53 | b1 = (n+1)*(n-1)/((n-2)*(n-3)); 54 | b2 = ((n-1).^2)/((n-2)*(n-3)); 55 | g2 = b1*(m4./(m2.^2)) - 3*b2 + 3; 56 | else 57 | % Biased 58 | g2 = m4./(m2.^2); 59 | end 60 | 61 | %G2 = b1*(m4./(m2.^2)) + 3*b2; 62 | 63 | return; 64 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/mri_zcdf.m: -------------------------------------------------------------------------------- 1 | function p = mri_zcdf(Z) 2 | % p = mri_zcdf(Z) 3 | % Cumulative distribution function for z, ie, 4 | % p = prob that a number drawn from a z distribution is <= Z. 5 | % 6 | % Simply returns: 7 | % p = (1 + erf(Z/sqrt(2)) )/2; 8 | % 9 | % To use this as a z-to-p converter, compute 10 | % p = 1 - mri_zcdf(z); 11 | % 12 | 13 | 14 | % 15 | % mri_zcdf.m 16 | % 17 | % Original Author: Doug Greve 18 | % CVS Revision Info: 19 | % $Author: nicks $ 20 | % $Date: 2011/03/02 00:04:12 $ 21 | % $Revision: 1.3 $ 22 | % 23 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 24 | % 25 | % Terms and conditions for use, reproduction, distribution and contribution 26 | % are found in the 'FreeSurfer Software License Agreement' contained 27 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 28 | % 29 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 30 | % 31 | % Reporting: freesurfer@nmr.mgh.harvard.edu 32 | % 33 | 34 | 35 | p = (1 + erf(Z/sqrt(2)) )/2; 36 | 37 | return; 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/pons_cut_table.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | % 4 | % pons_cut_table.m 5 | % 6 | % Original Author: Laurence Wastiaux 7 | % CVS Revision Info: 8 | % $Author: nicks $ 9 | % $Date: 2011/03/02 00:04:12 $ 10 | % $Revision: 1.3 $ 11 | % 12 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 13 | % 14 | % Terms and conditions for use, reproduction, distribution and contribution 15 | % are found in the 'FreeSurfer Software License Agreement' contained 16 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 17 | % 18 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 19 | % 20 | % Reporting: freesurfer@nmr.mgh.harvard.edu 21 | % 22 | 23 | 24 | 25 | [Dpons, Ipons]=pons_cut_dir_afd('/space/neo/2/recon/buckner',0.01) 26 | % load('/space/okapi/3/data/laurence/ADF/cutting_planes/Dice_pons.mat') %loads Dpons 27 | % load('/space/okapi/3/data/laurence/ADF/cutting_planes/Subj_nb_pons.mat') %loads Ipons 28 | files=dir('/space/neo/2/recon/buckner'); 29 | fp=fopen('~/pons_table.txt', 'w'); 30 | fprintf(fp,'# FSAFD HorizontalCuttingPlaneCheck 1\n# date 20050516\n# $Id: pons_cut_table.m,v 1.3 2011/03/02 00:04:12 nicks Exp $\n# Buckner data set\n# ncols 1\n# nrows %d\n# label_col 1 DiceCoefficient\n', length(Dpons)); 31 | for i=1:length(Dpons) 32 | nb=Ipons(i); 33 | fprintf(fp, '# label_row %d %s\n', i, char(files(nb).name)); 34 | end 35 | for j=1:length(Dpons) 36 | fprintf(fp, '%f\n', Dpons(j)); 37 | end 38 | fclose(fp) 39 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/randb.m: -------------------------------------------------------------------------------- 1 | function b = randb(p,ntrials,szb) 2 | % b = randb(p,ntrials,) 3 | % 4 | % Draw samples from a binomial distribution. 5 | % 6 | % p is the probability of a trial being "successful" 7 | % (eg, probability that a voxel will be declared 8 | % active (whether it is or not)). Scalar. 9 | % ntrials - number of trials. Scalar. 10 | % szb - size of b. Can be a vector. Default is 1. 11 | % 12 | % b will be a number between 0 and ntrials indicating the 13 | % number of "successful" trials. 14 | % 15 | % Example: 16 | % b = randb(p,ntrials,100000); 17 | % x = [0:ntrials]; 18 | % h = hist(b,x); 19 | % fx = binomialpdf(x,ntrials,p); 20 | % plot(x,fx,x,h/sum(h)); 21 | % 22 | 23 | 24 | % 25 | % randb.m 26 | % 27 | % Original Author: Doug Greve 28 | % CVS Revision Info: 29 | % $Author: nicks $ 30 | % $Date: 2011/03/02 00:04:12 $ 31 | % $Revision: 1.3 $ 32 | % 33 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 34 | % 35 | % Terms and conditions for use, reproduction, distribution and contribution 36 | % are found in the 'FreeSurfer Software License Agreement' contained 37 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 38 | % 39 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 40 | % 41 | % Reporting: freesurfer@nmr.mgh.harvard.edu 42 | % 43 | 44 | 45 | b = []; 46 | if(nargin < 2 | nargin > 3) 47 | fprintf('b = randb(p,ntrials,szb)\n'); 48 | return; 49 | end 50 | 51 | if(~exist('szb','var')) szb = []; end 52 | if(isempty(szb)) szb = 1; end 53 | 54 | szb = szb(:)'; 55 | nv = prod(szb); 56 | b = reshape(sum(rand(ntrials,nv), lname) 3 | % 4 | % reads the label file 'lname' from the subject 'sname' 5 | % in the subject's label directory into the vector l 6 | % l will be nvertices-by-5, where each column means: 7 | % (1) vertex number, (2-4) xyz at each vertex, (5) stat 8 | % 9 | % IMPORTANT: the vertex number is 0-based. 10 | % 11 | 12 | 13 | % 14 | % read_label.m 15 | % 16 | % Original Author: Bruce Fischl 17 | % CVS Revision Info: 18 | % $Author: nicks $ 19 | % $Date: 2011/03/02 00:04:12 $ 20 | % $Revision: 1.7 $ 21 | % 22 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 23 | % 24 | % Terms and conditions for use, reproduction, distribution and contribution 25 | % are found in the 'FreeSurfer Software License Agreement' contained 26 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 27 | % 28 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 29 | % 30 | % Reporting: freesurfer@nmr.mgh.harvard.edu 31 | % 32 | 33 | 34 | l = []; 35 | 36 | if(nargin ~= 2) 37 | fprintf('l = read_label(, lname)\n'); 38 | return; 39 | end 40 | 41 | if(~isempty(sname)) 42 | sdir = getenv('SUBJECTS_DIR') ; 43 | fname = sprintf('%s/%s/label/%s.label', sdir, sname, lname) ; 44 | else 45 | ind = findstr(lname, '.label') ; 46 | if (length(ind) > 0) 47 | fname = lname ; 48 | else 49 | fname = sprintf('%s.label', lname); 50 | end 51 | end 52 | 53 | % open it as an ascii file 54 | fid = fopen(fname, 'r') ; 55 | if(fid == -1) 56 | fprintf('ERROR: could not open %s\n',fname); 57 | return; 58 | end 59 | 60 | fgets(fid) ; 61 | if(fid == -1) 62 | fprintf('ERROR: could not open %s\n',fname); 63 | return; 64 | end 65 | 66 | line = fgets(fid) ; 67 | nv = sscanf(line, '%d') ; 68 | l = fscanf(fid, '%d %f %f %f %f\n') ; 69 | l = reshape(l, 5, nv) ; 70 | l = l' ; 71 | 72 | fclose(fid) ; 73 | 74 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/read_label_old.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | % 4 | % read_label_old.m 5 | % 6 | % Original Author: Bruce Fischl 7 | % CVS Revision Info: 8 | % $Author: nicks $ 9 | % $Date: 2011/03/02 00:04:12 $ 10 | % $Revision: 1.3 $ 11 | % 12 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 13 | % 14 | % Terms and conditions for use, reproduction, distribution and contribution 15 | % are found in the 'FreeSurfer Software License Agreement' contained 16 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 17 | % 18 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 19 | % 20 | % Reporting: freesurfer@nmr.mgh.harvard.edu 21 | % 22 | 23 | 24 | 25 | %fname_label='lh-v1l.label'; 26 | fid=fopen(fname_label); 27 | s = fgetl(fid); 28 | s = fgetl(fid); 29 | [num_vert_label] = sscanf(s,'%d'); 30 | vert_num = zeros(num_vert_label,1); 31 | vert_data = zeros(5); 32 | for vert = 1:1:num_vert_label, 33 | s = fgetl(fid); 34 | vert_data = sscanf(s,'%d %f %f %f %f'); 35 | vert_num(vert) = vert_data(1:1); 36 | end; 37 | fclose(fid); 38 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/read_moviebyu.m: -------------------------------------------------------------------------------- 1 | function [S, v, f] = read_moviebyu(fname) 2 | % function [v, f] = read_moviebyu(fname) 3 | % Reads an ascii-file (movie.byu format). Returns a vertices and 4 | % faces matrix, ready to input in patches. 5 | 6 | 7 | % 8 | % read_moviebyu.m 9 | % 10 | % Original Author: Bruce Fischl 11 | % CVS Revision Info: 12 | % $Author: nicks $ 13 | % $Date: 2011/03/02 00:04:12 $ 14 | % $Revision: 1.3 $ 15 | % 16 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 17 | % 18 | % Terms and conditions for use, reproduction, distribution and contribution 19 | % are found in the 'FreeSurfer Software License Agreement' contained 20 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 21 | % 22 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 23 | % 24 | % Reporting: freesurfer@nmr.mgh.harvard.edu 25 | % 26 | 27 | fp = fopen(fname, 'r'); 28 | 29 | % Read in first two lines 30 | S = fscanf(fp, '%d', 4); 31 | fscanf(fp, '%d', 2); 32 | 33 | v = zeros(6, ceil(S(2)/2)); 34 | % Read vertices 35 | v(:, 1:floor(S(2)/2)) = fscanf(fp, '%f ', [6, floor(S(2)/2)]); 36 | 37 | if S(2)/2 ~= floor(S(2)/2) 38 | v(1:3, ceil(S(2)/2)) = fscanf(fp, '%f', 3); 39 | v = reshape(v, [3 S(2)+1])'; 40 | v = v(1:S(2), :); 41 | else 42 | v = reshape(v, [3 S(2)])'; 43 | end 44 | 45 | % Read faces 46 | f = fscanf(fp, '%d', [4 S(3)]); 47 | f(4, :) = -f(4, :); 48 | f = f+1; 49 | f = f'; 50 | 51 | S = S(1:3); 52 | 53 | fclose(fp); 54 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/read_normals.m: -------------------------------------------------------------------------------- 1 | function norm = read_normals (mesh_in, fname) 2 | 3 | vnum = length(mesh_in.vertices); 4 | fnum = length(mesh_in.faces); 5 | tnum = vnum + fnum; 6 | 7 | fid=fopen(fname,'r'); 8 | normals = textscan (fid, '%f %f %f %f',tnum,'headerlines',2); 9 | fclose(fid); 10 | 11 | norm = [normals{1} normals{2} normals{3}]; 12 | 13 | norm = norm(1:vnum,:); -------------------------------------------------------------------------------- /lib/freesurfer/matlab/read_patch.m: -------------------------------------------------------------------------------- 1 | % 2 | % read_patch.m 3 | % 4 | % Original Author: Bruce Fischl 5 | % CVS Revision Info: 6 | % $Author: nicks $ 7 | % $Date: 2013/01/22 20:59:09 $ 8 | % $Revision: 1.3.2.2 $ 9 | % 10 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 11 | % 12 | % Terms and conditions for use, reproduction, distribution and contribution 13 | % are found in the 'FreeSurfer Software License Agreement' contained 14 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 15 | % 16 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 17 | % 18 | % Reporting: freesurfer@nmr.mgh.harvard.edu 19 | % 20 | 21 | 22 | function patch = read_patch(fname) 23 | % function patch = read_patch(fname) 24 | 25 | 26 | fid = fopen(fname,'r'); 27 | if (fid == -1) 28 | error('could not open file %s', fname) ; 29 | end 30 | 31 | ver = fread(fid, 1, 'int', 0, 'b'); 32 | if (ver ~= -1) 33 | error('incorrect version # %d (not -1) found in file',ver) ; 34 | end 35 | 36 | patch.npts = fread(fid, 1, 'int', 0, 'b') ; 37 | 38 | for i=1:patch.npts 39 | ind = fread(fid, 1, 'int', 0, 'b') ; 40 | if (ind < 0) 41 | ind = -ind - 1 ; 42 | else 43 | ind = ind - 1 ; 44 | end 45 | patch.ind(i) = ind ; 46 | patch.x(i) = fread(fid, 1, 'float', 0, 'b') ; 47 | patch.y(i) = fread(fid, 1, 'float', 0, 'b') ; 48 | patch.z(i) = fread(fid, 1, 'float', 0, 'b') ; 49 | patch.vno(i) = ind ; 50 | end 51 | 52 | fclose(fid); 53 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/read_siemens_image.m: -------------------------------------------------------------------------------- 1 | function m = read_siemens_image(fname) 2 | % m = read_siemens(fname) 3 | 4 | 5 | % 6 | % read_siemens_image.m 7 | % 8 | % Original Author: Bruce Fischl 9 | % CVS Revision Info: 10 | % $Author: nicks $ 11 | % $Date: 2011/03/02 00:04:12 $ 12 | % $Revision: 1.3 $ 13 | % 14 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 15 | % 16 | % Terms and conditions for use, reproduction, distribution and contribution 17 | % are found in the 'FreeSurfer Software License Agreement' contained 18 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 19 | % 20 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 21 | % 22 | % Reporting: freesurfer@nmr.mgh.harvard.edu 23 | % 24 | 25 | fid = fopen(fname, 'r', 'b'); 26 | if fid < 0 27 | disp(sprintf('read_siemens_image: error opening file %s', fname)); 28 | return; 29 | end 30 | 31 | fseek(fid, 4994, 'bof'); 32 | rows = fread(fid, 1, 'short'); 33 | cols = fread(fid, 1, 'short'); 34 | 35 | fseek(fid, 6144, 'bof'); 36 | m = fread(fid, [rows cols], 'short'); 37 | 38 | fclose(fid); 39 | 40 | % eof 41 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/read_type.m: -------------------------------------------------------------------------------- 1 | function m = read_type(fname, offset, size, type) 2 | % m = read_type('fname', offset, size, 'type') 3 | 4 | 5 | % 6 | % read_type.m 7 | % 8 | % Original Author: Bruce Fischl 9 | % CVS Revision Info: 10 | % $Author: nicks $ 11 | % $Date: 2011/03/02 00:04:12 $ 12 | % $Revision: 1.3 $ 13 | % 14 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 15 | % 16 | % Terms and conditions for use, reproduction, distribution and contribution 17 | % are found in the 'FreeSurfer Software License Agreement' contained 18 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 19 | % 20 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 21 | % 22 | % Reporting: freesurfer@nmr.mgh.harvard.edu 23 | % 24 | 25 | fid = fopen(fname, 'r', 'b'); 26 | if fid < 0 27 | disp(sprintf('read_type: error opening file %s', fname)); 28 | return; 29 | end 30 | 31 | fseek(fid, offset, 'bof'); 32 | m = fread(fid, size, type); 33 | 34 | fclose(fid); 35 | 36 | % eof 37 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/read_wfile.m: -------------------------------------------------------------------------------- 1 | function [w,v] = read_wfile(fname) 2 | % 3 | % [w,v] = read_wfile(fname) 4 | % reads a vector into a binary 'w' file 5 | % fname - name of file to write to 6 | % w - vector of values to be written 7 | % v - vector of vertex indices (0-based) 8 | % 9 | % See also write_wfile. 10 | % 11 | 12 | 13 | % 14 | % read_wfile.m 15 | % 16 | % Original Author: Bruce Fischl 17 | % CVS Revision Info: 18 | % $Author: nicks $ 19 | % $Date: 2011/03/02 00:04:12 $ 20 | % $Revision: 1.4 $ 21 | % 22 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 23 | % 24 | % Terms and conditions for use, reproduction, distribution and contribution 25 | % are found in the 'FreeSurfer Software License Agreement' contained 26 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 27 | % 28 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 29 | % 30 | % Reporting: freesurfer@nmr.mgh.harvard.edu 31 | % 32 | 33 | 34 | w = []; 35 | v = []; 36 | 37 | if(nargin ~= 1) 38 | fprintf('[w,v] = read_wfile(fname)\n'); 39 | return; 40 | end 41 | 42 | % open it as a big-endian file 43 | fid = fopen(fname, 'rb', 'b') ; 44 | if (fid < 0) 45 | str = sprintf('could not open w file %s.', fname) ; 46 | error(str) ; 47 | end 48 | 49 | fread(fid, 1, 'int16') ; 50 | vnum = fread3(fid) ; 51 | w = zeros(vnum,1) ; 52 | v = zeros(vnum,1) ; 53 | for i=1:vnum 54 | v(i) = fread3(fid) ; % vertex number (0-based) 55 | w(i) = fread(fid, 1, 'float') ; % vertex value 56 | end 57 | 58 | fclose(fid) ; 59 | 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/readrec.m: -------------------------------------------------------------------------------- 1 | function A = readrec(infile,type,resx,resy,slice,offset) 2 | % READREC reads a rectangular slice from a raw infile and 3 | % assigns it to a matrix. 4 | % 5 | % a = READREC('infile','type',resx,resy,slice,offset): 6 | % infile = raw data set name must be 7 | % in single quotes. 8 | % type = data type must also be in 9 | % single quotes and can only 10 | % be: 'short'(2byt),'long'(4byt), 11 | % 'float'(4byt),'double'(8byt), 12 | % 'char'(1byt),'uchar'(1byt), 13 | % 'uint8' (unsigned 8 bit integer data). 14 | % resx = pixels per line 15 | % resy = lines 16 | % slice = slice number desired (assuming the rest of the 17 | % frames are of the same size) 18 | % offset = header offset in increments of the type defined 19 | % 20 | % E.g. 21 | % a = readrec('mammo.raw','uint8',986,987,1,0); 22 | % 23 | 24 | % 25 | % readrec.m 26 | % 27 | % Original Author: Bruce Fischl 28 | % CVS Revision Info: 29 | % $Author: nicks $ 30 | % $Date: 2011/03/02 00:04:12 $ 31 | % $Revision: 1.3 $ 32 | % 33 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 34 | % 35 | % Terms and conditions for use, reproduction, distribution and contribution 36 | % are found in the 'FreeSurfer Software License Agreement' contained 37 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 38 | % 39 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 40 | % 41 | % Reporting: freesurfer@nmr.mgh.harvard.edu 42 | % 43 | 44 | 45 | size = resx*resy; 46 | fid = fopen(infile, 'r'); 47 | dummy = fread(fid, offset, type); 48 | for i =1:(slice) 49 | A = fread(fid, size, type); 50 | end 51 | status = fclose(fid); 52 | 53 | A = reshape(A,resx,resy); 54 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/remove_spaces.m: -------------------------------------------------------------------------------- 1 | function str = remove_spaces(str) 2 | % str = remove_spaces(str) 3 | 4 | for j=length(str):-1:1 5 | if (isspace(str(1,j))) 6 | str(j) = []; 7 | else 8 | break ; 9 | end 10 | end 11 | 12 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/ribbon_table.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | % 4 | % ribbon_table.m 5 | % 6 | % Original Author: Laurence Wastiaux 7 | % CVS Revision Info: 8 | % $Author: nicks $ 9 | % $Date: 2011/03/02 00:04:13 $ 10 | % $Revision: 1.3 $ 11 | % 12 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 13 | % 14 | % Terms and conditions for use, reproduction, distribution and contribution 15 | % are found in the 'FreeSurfer Software License Agreement' contained 16 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 17 | % 18 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 19 | % 20 | % Reporting: freesurfer@nmr.mgh.harvard.edu 21 | % 22 | 23 | 24 | 25 | [D, Isubj]=ribbon_dir_afd('/space/neo/2/recon/buckner', 0.01); 26 | %load('/space/okapi/3/data/laurence/ADF/ribbon/Dice_ribbon.mat') %loads D 27 | %load('/space/okapi/3/data/laurence/ADF/ribbon/Subj_name_ribbon.mat') %loads nf 28 | files=dir('/space/neo/2/recon/buckner'); 29 | fp=fopen('ribbon_table.txt', 'w'); 30 | fprintf(fp,'# FSAFD RibbonPlacementCheck 1\n# date 20050516\n# $Id: ribbon_table.m,v 1.3 2011/03/02 00:04:13 nicks Exp $\n# Buckner data set\n# ncols 1\n# nrows %d\n# label_col 1 DiceCoefficient\n', length(D)); 31 | for i=1:length(D) 32 | %nb=Isubj(i); 33 | %fprintf(fp, '# label_row %d %s\n', i, char(files(nb).name)); 34 | fprintf(fp, '# label_row %d %s\n', i, char(nf(i).name)); 35 | end 36 | for j=1:length(D) 37 | fprintf(fp, '%f\n', D(j)); 38 | end 39 | fclose(fp) 40 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/rotmat.m: -------------------------------------------------------------------------------- 1 | function M = rotmat(deg) 2 | % M = rotmat(deg) 3 | 4 | rads = radians(deg) ; 5 | 6 | M = eye(3) ; 7 | M(1,1) = cos(rads) ; 8 | M(1,2) = sin(rads) ; 9 | M(2,1) = -sin(rads) ; 10 | M(2,2) = cos(rads) ; 11 | 12 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/rotmat2angles.m: -------------------------------------------------------------------------------- 1 | function [angles ambflag]= rotmat2angles(R) 2 | % [angles ambflag]= rotmat2angles(R) 3 | % 4 | % Converts rotation matrix to euler angles (in radians) 5 | % angles is a 3x1 vector in radians 6 | % angles(1) - pitch - rotation about x or LR (gamma) 7 | % angles(2) - yaw - rotation about y or AP (beta) 8 | % angles(3) - roll - rotation about z or SI (alpha) 9 | % 10 | % ambflag=1 indicates some ambiguity (eb, cos(beta)=1) 11 | % There may also be amibuity if the rotation matrix 12 | % was created with angles > pi/2. 13 | % 14 | % See also: angles2rotmat 15 | % Ref: Craig, Intro to Robotics 16 | % 17 | % $Id: rotmat2angles.m,v 1.3 2011/03/02 00:04:13 nicks Exp $ 18 | 19 | % 20 | % rotmat2angles.m 21 | % 22 | % Original Author: Doug Greve 23 | % CVS Revision Info: 24 | % $Author: nicks $ 25 | % $Date: 2011/03/02 00:04:13 $ 26 | % $Revision: 1.3 $ 27 | % 28 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 29 | % 30 | % Terms and conditions for use, reproduction, distribution and contribution 31 | % are found in the 'FreeSurfer Software License Agreement' contained 32 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 33 | % 34 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 35 | % 36 | % Reporting: freesurfer@nmr.mgh.harvard.edu 37 | % 38 | 39 | angles = []; 40 | ambflag = []; 41 | if(nargin ~= 1) 42 | fprintf('angles = rotmat2angles(R)\n'); 43 | return; 44 | end 45 | 46 | r31 = R(3,1); 47 | beta = asin(-r31); 48 | 49 | % This threshold is surprisingly large 50 | if(abs(1-abs(r31)) > 1e-4) 51 | alpha = atan2(R(2,1),R(1,1)); 52 | gamma = atan2(R(3,2),R(3,3)); 53 | ambflag = 0; 54 | else 55 | % beta = +/-pi/2 = +/-90deg -- ambiguous? 56 | alpha = 0; 57 | gamma = atan2(-R(1,2),R(2,2)); 58 | ambflag = 1; 59 | %printf('here\n'); 60 | end 61 | 62 | angles = [gamma beta alpha ]'; 63 | 64 | return; 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/stringunique.m: -------------------------------------------------------------------------------- 1 | function ulist = stringunique(strvlist) 2 | % ulist = stringunique(strvlist) 3 | % 4 | % Returns a unique list of strings found in the vertically 5 | % concatenated string list strvlist. Matches are determined 6 | % AFTER deblanking. 7 | % 8 | % $Id: stringunique.m,v 1.2 2011/03/02 00:04:13 nicks Exp $ 9 | 10 | % 11 | % stringunique 12 | % 13 | % Original Author: Doug Greve 14 | % CVS Revision Info: 15 | % $Author: nicks $ 16 | % $Date: 2011/03/02 00:04:13 $ 17 | % $Revision: 1.2 $ 18 | % 19 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 20 | % 21 | % Terms and conditions for use, reproduction, distribution and contribution 22 | % are found in the 'FreeSurfer Software License Agreement' contained 23 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 24 | % 25 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 26 | % 27 | % Reporting: freesurfer@nmr.mgh.harvard.edu 28 | % 29 | 30 | 31 | ulist = []; 32 | if(nargin ~= 1) 33 | fprintf('ulist = stringunique(strvlist)\n'); 34 | return; 35 | end 36 | 37 | nlist = size(strvlist,1); 38 | 39 | % Go thru each string in the list 40 | for nth = 1:nlist 41 | nthstr = deblank(strvlist(nth,:)); 42 | hit = 0; 43 | % Go thru each string in the unique list 44 | for mth = 1:size(ulist,1) 45 | ustr = deblank(ulist(mth,:)); 46 | if(strcmp(nthstr,ustr)) 47 | % The nth string is already in the unique list 48 | hit = 1; 49 | break; 50 | end 51 | end 52 | if(hit == 0) 53 | % The nth string was not found in the unique list, so add it 54 | ulist = strvcat(ulist,nthstr); 55 | end 56 | end 57 | 58 | return; 59 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/strlen.m: -------------------------------------------------------------------------------- 1 | function len = strlen(str) 2 | % len = strlen(str) 3 | % compute the # of characters in str (ignoring 0s at the end) 4 | 5 | 6 | % 7 | % strlen.m 8 | % 9 | % Original Author: Bruce Fischl 10 | % CVS Revision Info: 11 | % $Author: nicks $ 12 | % $Date: 2011/03/02 00:04:13 $ 13 | % $Revision: 1.3 $ 14 | % 15 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 16 | % 17 | % Terms and conditions for use, reproduction, distribution and contribution 18 | % are found in the 'FreeSurfer Software License Agreement' contained 19 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 20 | % 21 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 22 | % 23 | % Reporting: freesurfer@nmr.mgh.harvard.edu 24 | % 25 | 26 | len = length(str) ; 27 | for i=length(str):-1:1 28 | if (str(i) ~= 0) 29 | break ; 30 | end 31 | 32 | len = len-1; 33 | end 34 | 35 | 36 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/subcortical_labeling_table.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | % 4 | % subcortical_labeling_table.m 5 | % 6 | % Original Author: Laurence Wastiaux 7 | % CVS Revision Info: 8 | % $Author: nicks $ 9 | % $Date: 2011/03/02 00:04:13 $ 10 | % $Revision: 1.4 $ 11 | % 12 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 13 | % 14 | % Terms and conditions for use, reproduction, distribution and contribution 15 | % are found in the 'FreeSurfer Software License Agreement' contained 16 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 17 | % 18 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 19 | % 20 | % Reporting: freesurfer@nmr.mgh.harvard.edu 21 | % 22 | 23 | 24 | 25 | [Dvolume,Ivol]=subcortical_labeling_dir_afd('/space/neo/2/recon/buckner', 0.01) 26 | % load('/space/okapi/3/data/laurence/ADF/subcortical_labeling/Percent_Vol0517.mat') %loads Dvolume 27 | % load('/space/okapi/3/data/laurence/ADF/subcortical_labeling/Subj_nb_Vol0517.mat') %loads Ivol 28 | files=dir('/space/neo/2/recon/buckner'); 29 | fp=fopen('~/subcortlab_table.txt', 'w'); 30 | sz=size(Dvolume); 31 | fprintf(fp,'# FSAFD SubcorticalLabelingCheck 1\n# date 20050516\n# $Id: subcortical_labeling_table.m,v 1.4 2011/03/02 00:04:13 nicks Exp $\n# Buckner data set\n# ncols 20\n# nrows %d\n', sz(1)); 32 | ROI_Labels=[4 43 17 53 10 49 11 50 12 51 13 52 18 54 26 58 14 15 5 44]; 33 | labmapfile=('/space/lyon/1/fsdev/freesurfer_dev'); 34 | labmap=strcat(labmapfile,'/','tkmeditColorsCMA'); 35 | [label name val1 val2 val3 val4]=textread(labmap,'%d %s %d %d %d %d',89); 36 | nn=1; 37 | for u=1:length(ROI_Labels) 38 | fprintf(fp, '# label_col %d %s\n', u, char(name(ROI_Labels(u)+1))); 39 | end 40 | for i=1:sz(1) 41 | if(Dvolume(i,1)>0) 42 | nb=Ivol(i); 43 | fprintf(fp, '# label_row %d %s\n',nn, char(files(nb).name)); 44 | nn=nn+1; 45 | end 46 | end 47 | for j=1:sz(1) 48 | for k=1:20 49 | fprintf(fp,'%f ', Dvolume(j,k)); 50 | end 51 | fprintf(fp, '\n'); 52 | end 53 | fclose(fp) 54 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/surf_registration_table.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | % 4 | % surf_registration_table.m 5 | % 6 | % Original Author: Laurence Wastiaux 7 | % CVS Revision Info: 8 | % $Author: nicks $ 9 | % $Date: 2011/03/02 00:04:13 $ 10 | % $Revision: 1.3 $ 11 | % 12 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 13 | % 14 | % Terms and conditions for use, reproduction, distribution and contribution 15 | % are found in the 'FreeSurfer Software License Agreement' contained 16 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 17 | % 18 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 19 | % 20 | % Reporting: freesurfer@nmr.mgh.harvard.edu 21 | % 22 | 23 | 24 | 25 | [Dsubj, nf, r90, thdemean, M] = surf_registration_stats('/space/neo/2/recon/buckner') 26 | %load('/space/okapi/3/data/laurence/ADF/surf_registration/residual_var90.mat') %loads r90 27 | %load('/space/okapi/3/data/laurence/ADF/surf_registration/Subj_Names.mat') %loads nf 28 | %files=dir('/space/neo/2/recon/buckner'); 29 | fp=fopen('~/surfreg_table.txt', 'w'); 30 | fprintf(fp,'# FSAFD SurfRegistrationCheck 1\n# date 20050516\n# $Id: surf_registration_table.m,v 1.3 2011/03/02 00:04:13 nicks Exp $\n# Buckner data set\n# hemi lh\n# PercentRemovedVar 90\n# ncols 1\n# nrows %d\n# label_col 1 PercentResidualVariance\n', length(r90)); 31 | nn=1; 32 | for i=1:length(r90) 33 | %nb=I(i); 34 | if(r90(i)>0) 35 | fprintf(fp, '# label_row %d %s\n', nn, char(nf(i).name)); 36 | nn=nn+1; 37 | end 38 | 39 | end 40 | for j=1:length(r90) 41 | if(r90(j)>0) 42 | fprintf(fp, '%f\n', r90(j)); 43 | end 44 | end 45 | fclose(fp) 46 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/transVertexToNormalAxisBase.m: -------------------------------------------------------------------------------- 1 | function result = transVertexToNormalAxisBase(mesh_outer, iV, coord) 2 | 3 | % Originally written by Lucas Tamarit, tamarit@cisa.unige.ch 4 | % Adapted to the local GI whole computation on 2007/15/11 5 | % 6 | % create a new referential based on the normal of the outer surface at the 7 | % center of the outer region of interest. In order to give the distance of 8 | % a given point (coord) the main axis of this referential. 9 | 10 | 11 | B.origin = mesh_outer.vertices(iV,:); % set the origin of the referential 12 | n = mesh_outer.vertexNormal(iV,:); % use the normal to the outer smoothed surface at that point 13 | 14 | B.axe = n ./ norm(n); 15 | B.u = getOrthogonalVector(B.axe); 16 | B.v = cross(B.axe,B.u); 17 | B.v = B.v ./ norm(B.v); 18 | 19 | vt = coord - B.origin; 20 | vt = [B.u; B.v; B.axe]*vt'; 21 | 22 | result = vt; 23 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/unwarp_init_globals.m: -------------------------------------------------------------------------------- 1 | function unwarp_init_globals(called_by_script) 2 | 3 | 4 | % Called by convert_unwarp_resample.m and unwarp_scanners_table.m 5 | % This can't be the right way to do this. 6 | 7 | 8 | % 9 | % unwarp_init_globals.m 10 | % 11 | % Original Author: Elizabeth Haley 12 | % CVS Revision Info: 13 | % $Author: nicks $ 14 | % $Date: 2011/03/02 00:04:13 $ 15 | % $Revision: 1.4 $ 16 | % 17 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 18 | % 19 | % Terms and conditions for use, reproduction, distribution and contribution 20 | % are found in the 'FreeSurfer Software License Agreement' contained 21 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 22 | % 23 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 24 | % 25 | % Reporting: freesurfer@nmr.mgh.harvard.edu 26 | % 27 | 28 | 29 | 30 | global GRADWARPPATH; 31 | global TABLE; 32 | global QuitOnError; 33 | 34 | GRADWARPPATH = sprintf('%s/grad_unwarp_tables',getenv('DEV')); 35 | TABLE = sprintf('%s/table.mat',GRADWARPPATH); 36 | if (exist('called_by_script') & ~isempty(called_by_script)) 37 | QuitOnError = called_by_script; 38 | else 39 | QuitOnError = 1; 40 | end 41 | 42 | return; 43 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/vox2ras_0to1.m: -------------------------------------------------------------------------------- 1 | function M1 = vox2ras_0to1(M0) 2 | % M1 = vox2ras_0to1(M0) 3 | % 4 | % Converts a 0-based vox2ras matrix to 1-based, ie, 5 | % Pxyz = M0*[c r s 1]' = M1*[(c+1) (r+1) (s+1) 1]' 6 | % 7 | 8 | 9 | % 10 | % vox2ras_0to1.m 11 | % 12 | % Original Author: Doug Greve 13 | % CVS Revision Info: 14 | % $Author: nicks $ 15 | % $Date: 2011/03/02 00:04:13 $ 16 | % $Revision: 1.3 $ 17 | % 18 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 19 | % 20 | % Terms and conditions for use, reproduction, distribution and contribution 21 | % are found in the 'FreeSurfer Software License Agreement' contained 22 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 23 | % 24 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 25 | % 26 | % Reporting: freesurfer@nmr.mgh.harvard.edu 27 | % 28 | 29 | 30 | M1 = []; 31 | 32 | if(nargin ~= 1) 33 | fprintf('M1 = vox2ras_0to1(M0)\n'); 34 | return; 35 | end 36 | 37 | Q = zeros(4); 38 | Q(1:3,4) = ones(3,1); 39 | 40 | M1 = inv(inv(M0)+Q); 41 | 42 | return; 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/vox2ras_1to0.m: -------------------------------------------------------------------------------- 1 | function M0 = vox2ras_1to0(M1) 2 | % M0 = vox2ras_1to0(M1) 3 | % 4 | % Converts a 1-based vox2ras matrix to 0-based, ie, 5 | % Pxyz = M0*[c r s 1]' = M1*[(c+1) (r+1) (s+1) 1]' 6 | % 7 | % See also: vox2ras_0to1 8 | % 9 | 10 | 11 | % 12 | % vox2ras_1to0.m 13 | % 14 | % Original Author: Doug Greve 15 | % CVS Revision Info: 16 | % $Author: nicks $ 17 | % $Date: 2011/03/02 00:04:13 $ 18 | % $Revision: 1.3 $ 19 | % 20 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 21 | % 22 | % Terms and conditions for use, reproduction, distribution and contribution 23 | % are found in the 'FreeSurfer Software License Agreement' contained 24 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 25 | % 26 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 27 | % 28 | % Reporting: freesurfer@nmr.mgh.harvard.edu 29 | % 30 | 31 | 32 | M0 = []; 33 | 34 | if(nargin ~= 1) 35 | fprintf('M0 = vox2ras_1to0(M1)'); 36 | return; 37 | end 38 | 39 | Q = zeros(4); 40 | Q(1:3,4) = -ones(3,1); 41 | 42 | M0 = inv(inv(M1)+Q); 43 | 44 | return; 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/vox2ras_tkreg.m: -------------------------------------------------------------------------------- 1 | function T = vox2ras_tkreg(voldim, voxres) 2 | % T = vox2ras_tkreg(voldim, voxres) 3 | % voldim = [ncols nrows nslices ...] 4 | % volres = [colres rowres sliceres ...] 5 | % 6 | % Computes the 0-based vox2ras transform of a volume 7 | % compatible with the registration matrix produced 8 | % by tkregister. May work with MINC xfm. 9 | % 10 | 11 | 12 | % 13 | % vox2ras_tkreg.m 14 | % 15 | % Original Author: Doug Greve 16 | % CVS Revision Info: 17 | % $Author: nicks $ 18 | % $Date: 2011/03/02 00:04:13 $ 19 | % $Revision: 1.4 $ 20 | % 21 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 22 | % 23 | % Terms and conditions for use, reproduction, distribution and contribution 24 | % are found in the 'FreeSurfer Software License Agreement' contained 25 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 26 | % 27 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 28 | % 29 | % Reporting: freesurfer@nmr.mgh.harvard.edu 30 | % 31 | 32 | T = []; 33 | 34 | if(nargin ~= 2) 35 | fprintf('T = vox2ras_tkreg(voldim, voxres)\n'); 36 | return; 37 | end 38 | 39 | T = zeros(4,4); 40 | T(4,4) = 1; 41 | 42 | T(1,1) = -voxres(1); 43 | T(1,4) = voxres(1)*voldim(1)/2; 44 | 45 | T(2,3) = voxres(3); 46 | T(2,4) = -voxres(3)*voldim(3)/2; 47 | 48 | 49 | T(3,2) = -voxres(2); 50 | T(3,4) = voxres(2)*voldim(2)/2; 51 | 52 | 53 | return; 54 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/wm_seg_table.m: -------------------------------------------------------------------------------- 1 | [D, pvals, I]=wm_seg_dir_afd('/space/neo/2/recon/buckner', 0.01); 2 | 3 | 4 | % 5 | % wm_seg_table.m 6 | % 7 | % Original Author: Laurence Wastiaux 8 | % CVS Revision Info: 9 | % $Author: nicks $ 10 | % $Date: 2011/03/02 00:04:13 $ 11 | % $Revision: 1.3 $ 12 | % 13 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 14 | % 15 | % Terms and conditions for use, reproduction, distribution and contribution 16 | % are found in the 'FreeSurfer Software License Agreement' contained 17 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 18 | % 19 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 20 | % 21 | % Reporting: freesurfer@nmr.mgh.harvard.edu 22 | % 23 | 24 | 25 | %load('/space/okapi/3/data/laurence/ADF/wm_segmentation/WM_dice_buckner.mat') %loads D 26 | %load('/space/okapi/3/data/laurence/ADF/wm_segmentation/subject_nb.mat') %loads I 27 | %files=dir('/space/neo/2/recon/buckner'); 28 | fp=fopen('~/wm_table.txt', 'w'); 29 | fprintf(fp,'# FSAFD WMsegmentationCheck 1\n# date 20050516\n# $Id: wm_seg_table.m,v 1.3 2011/03/02 00:04:13 nicks Exp $\n# Buckner data set\n# ncols 1\n# nrows %d\n# label_col 1 DiceCoefficient\n', length(D)); 30 | for i=1:length(D) 31 | nb=I(i); 32 | fprintf(fp, '# label_row %d %s\n', i, char(files(nb).name)); 33 | end 34 | for j=1:length(D) 35 | fprintf(fp, '%f\n', D(j)); 36 | end 37 | fclose(fp) 38 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/write_ascii_curv.m: -------------------------------------------------------------------------------- 1 | function [fid] = write_curv(curv, fname) 2 | % 3 | % writes a curvature vector into an ascii file 4 | % 5 | 6 | 7 | % 8 | % write_ascii_curv.m 9 | % 10 | % Original Author: Bruce Fischl 11 | % CVS Revision Info: 12 | % $Author: nicks $ 13 | % $Date: 2011/03/02 00:04:13 $ 14 | % $Revision: 1.3 $ 15 | % 16 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 17 | % 18 | % Terms and conditions for use, reproduction, distribution and contribution 19 | % are found in the 'FreeSurfer Software License Agreement' contained 20 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 21 | % 22 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 23 | % 24 | % Reporting: freesurfer@nmr.mgh.harvard.edu 25 | % 26 | 27 | 28 | fid = fopen(fname, 'w') ; 29 | nvertices = size(curv,1) ; 30 | fprintf(fid, '%d\n', nvertices) ; 31 | for i=1:nvertices 32 | fprintf(fid, '0 0.0 0.0 0.0 %f\n', curv(i,1)) ; 33 | end 34 | fclose(fid) ; 35 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/write_curv.m: -------------------------------------------------------------------------------- 1 | function [curv] = write_curv(fname, curv, fnum) 2 | % [curv] = write_curv(fname, curv, fnum) 3 | % 4 | % writes a curvature vector into a binary file 5 | % fname - name of file to write to 6 | % curv - vector of curvatures 7 | % fnum - # of faces in surface. 8 | % 9 | 10 | 11 | % 12 | % write_curv.m 13 | % 14 | % Original Author: Bruce Fischl 15 | % CVS Revision Info: 16 | % $Author: nicks $ 17 | % $Date: 2011/03/02 00:04:13 $ 18 | % $Revision: 1.3 $ 19 | % 20 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 21 | % 22 | % Terms and conditions for use, reproduction, distribution and contribution 23 | % are found in the 'FreeSurfer Software License Agreement' contained 24 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 25 | % 26 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 27 | % 28 | % Reporting: freesurfer@nmr.mgh.harvard.edu 29 | % 30 | 31 | 32 | % open it as a big-endian file 33 | fid = fopen(fname, 'wb', 'b') ; 34 | vnum = length(curv) ; 35 | NEW_VERSION_MAGIC_NUMBER = 16777215; 36 | fwrite3(fid, NEW_VERSION_MAGIC_NUMBER ) ; 37 | fwrite(fid, vnum,'int32') ; 38 | fwrite(fid, fnum,'int32') ; 39 | fwrite(fid, 1, 'int32'); 40 | fwrite(fid, curv, 'float') ; 41 | fclose(fid) ; 42 | 43 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/write_lgi.m: -------------------------------------------------------------------------------- 1 | function ok = write_lgi (mesh_pial, lgi, outputfile) 2 | % ok = write_lGI (mesh_pial, lgi, outputfile) 3 | % 4 | % 5 | % modified from write_label.m by Marie Schaer 6 | % 7 | % Example: write_lgi('lh.pial',lgi,'lh.lgi.asc') 8 | % 9 | % Original Author: Doug Greve 10 | % CVS Revision Info: 11 | % $Author: nicks $ 12 | % $Date: 2011/03/02 00:04:30 $ 13 | % $Revision: 1.2 $ 14 | % 15 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 16 | % 17 | % Terms and conditions for use, reproduction, distribution and contribution 18 | % are found in the 'FreeSurfer Software License Agreement' contained 19 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 20 | % 21 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 22 | % 23 | % Reporting: freesurfer@nmr.mgh.harvard.edu 24 | % 25 | 26 | ok = 0; 27 | 28 | lxyz=mesh_pial.vertices; 29 | 30 | if(~isempty(lgi) & ~isempty(lxyz)) 31 | npoints1 = length(lgi); 32 | npoints2 = size(lxyz,1); 33 | if(npoints1 ~= npoints2) 34 | fprintf('ERROR: lGI file does not have the same number of vertices than the pial surface.\n'); 35 | return; 36 | end 37 | npoints = npoints1; 38 | elseif(~isempty(lgi)) 39 | npoints = length(lgi); 40 | lxyz = zeros(npoints,3); 41 | elseif(~isempty(lxyz)) 42 | npoints = length(lxyz); 43 | lgi = zeros(npoints,1); 44 | end 45 | 46 | % open as an ascii file 47 | fid = fopen(outputfile, 'w') ; 48 | if(fid == -1) 49 | fprintf('ERROR: could not open %s\n',labelfile); 50 | return; 51 | end 52 | 53 | num = [1:npoints]; 54 | num = num'; 55 | lgi = reshape(lgi,[npoints 1]); 56 | lxyz = reshape(lxyz,[npoints 3]); 57 | 58 | l = [num lxyz lgi]; 59 | fprintf(fid,'%u %f %f %f %f \n',l'); 60 | fprintf(fid,'ENDPATH'); 61 | 62 | fclose(fid) ; 63 | 64 | ok = 1; 65 | 66 | return; 67 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/write_surf.m: -------------------------------------------------------------------------------- 1 | function write_surf(fname, vert, face) 2 | 3 | % write_surf - FreeSurfer I/O function to write a surface file 4 | % 5 | % write_surf(fname, vert, face) 6 | % 7 | % writes a surface triangulation into a binary file 8 | % fname - name of file to write 9 | % vert - Nx3 matrix of vertex coordinates 10 | % face - Mx3 matrix of face triangulation indices 11 | % 12 | % The face matrix here must be matlab compatible 13 | % (no zero indices). It is converted to FreeSurfer 14 | % indices that start at zero. 15 | % 16 | % See also freesurfer_read_surf, freesurfer_write_curv, freesurfer_write_wfile 17 | 18 | if(nargin ~= 3) 19 | fprintf('USAGE: freesurfer_write_surf(fname, vert, face)\n'); 20 | return; 21 | end 22 | 23 | if size(vert,2) ~= 3, 24 | error('vert must be Nx3 matrix'); 25 | end 26 | 27 | if size(face,2) ~= 3, 28 | error('face must be Mx3 matrix'); 29 | end 30 | 31 | fprintf('...subtracting 1 from face indices for FreeSurfer compatibility.\n'); 32 | face = face - 1; 33 | 34 | % open it as a big-endian file 35 | fid = fopen(fname, 'wb', 'b') ; 36 | 37 | TRIANGLE_FILE_MAGIC_NUMBER = 16777214 ; 38 | fwrite3(fid, TRIANGLE_FILE_MAGIC_NUMBER); 39 | 40 | vnum = size(vert,1) ; % number of vertices 41 | fnum = size(face,1) ; % number of faces 42 | 43 | % Ouput a couple of text lines with creation date 44 | fprintf(fid,'created by %s on %s\n\n',getenv('USER'),datestr(now)); % creation date 45 | 46 | fwrite(fid, vnum,'int32'); 47 | fwrite(fid, fnum,'int32'); 48 | 49 | % reshape vert into column array and write 50 | vert = reshape(vert',size(vert,1)*size(vert,2),1); 51 | fwrite(fid, vert,'float32'); 52 | 53 | % reshape face into column array and write 54 | face = reshape(face',size(face,1)*size(face,2),1); 55 | fwrite(fid, face,'int32'); 56 | 57 | fclose(fid) ; 58 | 59 | return 60 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/write_wfile.m: -------------------------------------------------------------------------------- 1 | function err = write_wfile(fname, w, v) 2 | % err = write_wfile(fname, w, ) 3 | % 4 | % writes a vector into a binary 'w' file 5 | % fname - name of file to write to 6 | % w - vector of values to be written 7 | % v - 0-based vertex numbers 8 | % (assumes 0 to N-1 if not present or empty). 9 | % 10 | % See also read_wfile. 11 | % 12 | 13 | 14 | % 15 | % write_wfile.m 16 | % 17 | % Original Author: Doug Greve 18 | % CVS Revision Info: 19 | % $Author: nicks $ 20 | % $Date: 2011/03/02 00:04:13 $ 21 | % $Revision: 1.3 $ 22 | % 23 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 24 | % 25 | % Terms and conditions for use, reproduction, distribution and contribution 26 | % are found in the 'FreeSurfer Software License Agreement' contained 27 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 28 | % 29 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 30 | % 31 | % Reporting: freesurfer@nmr.mgh.harvard.edu 32 | % 33 | 34 | 35 | err = 1; 36 | 37 | if(nargin ~= 2 & nargin ~= 3) 38 | fprintf('USAGE: err = write_wfile(fname, w, ) \n'); 39 | return; 40 | end 41 | 42 | vnum = length(w) ; 43 | 44 | % Handle when v not given or is empty % 45 | if(exist('v') ~= 1) v = []; end 46 | if(isempty(v)) v = [0:vnum-1]; end 47 | 48 | % open it as a big-endian file 49 | fid = fopen(fname, 'wb', 'b') ; 50 | if(fid == -1) 51 | fprintf('ERROR: could not open %s\n',fname); 52 | return; 53 | end 54 | 55 | fwrite(fid, 0, 'int16') ; 56 | fwrite3(fid, vnum) ; 57 | for i=1:vnum 58 | fwrite3(fid, v(i)) ; % vertex number (0-based) 59 | fwrite(fid, w(i), 'float') ; % vertex value 60 | end 61 | 62 | fclose(fid) ; 63 | 64 | err = 0; 65 | 66 | return 67 | -------------------------------------------------------------------------------- /lib/freesurfer/matlab/xfm_read.m: -------------------------------------------------------------------------------- 1 | function M = xfm_read(fname) 2 | % M = xfm_read(fname) 3 | 4 | 5 | % 6 | % xfm_read.m 7 | % 8 | % Original Author: Bruce Fischl 9 | % CVS Revision Info: 10 | % $Author: nicks $ 11 | % $Date: 2011/03/02 00:04:13 $ 12 | % $Revision: 1.4 $ 13 | % 14 | % Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH" 15 | % 16 | % Terms and conditions for use, reproduction, distribution and contribution 17 | % are found in the 'FreeSurfer Software License Agreement' contained 18 | % in the file 'LICENSE' found in the FreeSurfer distribution, and here: 19 | % 20 | % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense 21 | % 22 | % Reporting: freesurfer@nmr.mgh.harvard.edu 23 | % 24 | 25 | 26 | fid = fopen(fname) ; 27 | if (fid < 0) 28 | error(sprintf('could not open file %s', fname)); 29 | end 30 | 31 | tline = fgetl(fid) ; 32 | while ((length(tline) > 0) & (tline(1) == '%')) 33 | tline = fgetl(fid) ; 34 | end 35 | 36 | tok = strtok(tline); 37 | while (strcmp(tok, 'Linear_Transform') ~= 1) 38 | tline = fgetl(fid) ; 39 | tok = strtok(tline); 40 | end 41 | 42 | 43 | M = zeros(4,4) ; M(4,4) = 1; 44 | for row=1:3 45 | tline = fgetl(fid) ; % one row of matrix 46 | tmp = sscanf(tline, '%f'); 47 | M(row,:) = tmp'; 48 | end 49 | 50 | fclose(fid) ; 51 | 52 | 53 | -------------------------------------------------------------------------------- /lib/freesurfer/subjects/fsaverage4/surf/lh.pial: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hmlicas/Collaborative_Brain_Decomposition/6f3ad613d065fe592979e1f105065ed22472e999/lib/freesurfer/subjects/fsaverage4/surf/lh.pial -------------------------------------------------------------------------------- /lib/freesurfer/subjects/fsaverage4/surf/rh.pial: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hmlicas/Collaborative_Brain_Decomposition/6f3ad613d065fe592979e1f105065ed22472e999/lib/freesurfer/subjects/fsaverage4/surf/rh.pial -------------------------------------------------------------------------------- /lib/freesurfer/subjects/fsaverage5/surf/lh.pial: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hmlicas/Collaborative_Brain_Decomposition/6f3ad613d065fe592979e1f105065ed22472e999/lib/freesurfer/subjects/fsaverage5/surf/lh.pial -------------------------------------------------------------------------------- /lib/freesurfer/subjects/fsaverage5/surf/rh.pial: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hmlicas/Collaborative_Brain_Decomposition/6f3ad613d065fe592979e1f105065ed22472e999/lib/freesurfer/subjects/fsaverage5/surf/rh.pial -------------------------------------------------------------------------------- /lib/gifti-1.6/@gifti/Contents.m: -------------------------------------------------------------------------------- 1 | % GIfTI Class for MATLAB 2 | % 3 | % Geometry format under the Neuroimaging Informatics Technology Initiative 4 | % (NIfTI): 5 | % http://www.nitrc.org/projects/gifti/ 6 | % http://nifti.nimh.nih.gov/ 7 | % 8 | % This MATLAB class is part of SPM: 9 | % http://www.fil.ion.ucl.ac.uk/spm/ 10 | % 11 | % It relies on external libraries: 12 | % Base64, by Peter J. Acklam: 13 | % http://home.online.no/~pjacklam/ 14 | % miniz, by Rich Geldreich: 15 | % http://code.google.com/p/miniz/ 16 | % XMLTree, by Guillaume Flandin: 17 | % http://www.artefact.tk/software/matlab/xml/ 18 | %__________________________________________________________________________ 19 | % Copyright (C) 2008-2015 Wellcome Trust Centre for Neuroimaging 20 | 21 | % Guillaume Flandin 22 | % $Id: Contents.m 6404 2015-04-13 14:29:53Z guillaume $ 23 | 24 | % GIfTI file format for MATLAB (The Mathworks, Inc.). 25 | % Copyright (C) 2008-2015 Wellcome Trust Centre for Neuroimaging 26 | % 27 | % This program is free software; you can redistribute it and/or 28 | % modify it under the terms of the GNU General Public License 29 | % as published by the Free Software Foundation; either version 2 30 | % of the License, or any later version. 31 | % 32 | % This program is distributed in the hope that it will be useful, 33 | % but WITHOUT ANY WARRANTY; without even the implied warranty of 34 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 35 | % GNU General Public License for more details. 36 | % 37 | % You should have received a copy of the GNU General Public License 38 | % along with this program; if not, write to the Free Software 39 | % Foundation Inc, 59 Temple Pl. - Suite 330, Boston, MA 02111-1307, USA. -------------------------------------------------------------------------------- /lib/gifti-1.6/@gifti/display.m: -------------------------------------------------------------------------------- 1 | function display(this) 2 | % Display method for GIfTI objects 3 | % FORMAT display(this) 4 | % this - GIfTI object 5 | %__________________________________________________________________________ 6 | % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging 7 | 8 | % Guillaume Flandin 9 | % $Id: display.m 4182 2011-02-01 12:29:09Z guillaume $ 10 | 11 | display_name = inputname(1); 12 | if isempty(display_name) 13 | display_name = 'ans'; 14 | end 15 | 16 | if length(this) == 1 && ~isempty(this.data) 17 | eval([display_name ' = struct(this);']); 18 | eval(['display(' display_name ');']); 19 | else 20 | disp(' ') 21 | disp([display_name ' =']); 22 | disp(' '); 23 | eval([display_name ' = this;']); 24 | eval(['disp(' display_name ');']); 25 | end -------------------------------------------------------------------------------- /lib/gifti-1.6/@gifti/fieldnames.m: -------------------------------------------------------------------------------- 1 | function names = fieldnames(this) 2 | % Fieldnames method for GIfTI objects 3 | % FORMAT names = fieldnames(this) 4 | % this - GIfTI object 5 | % names - field names 6 | %__________________________________________________________________________ 7 | % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging 8 | 9 | % Guillaume Flandin 10 | % $Id: fieldnames.m 6507 2015-07-24 16:48:02Z guillaume $ 11 | 12 | if numel(this) > 1, warning('Only handle scalar objects yet.'); end 13 | 14 | pfn = {'vertices','faces','normals','cdata','mat','labels','indices'}; 15 | 16 | names = unique(pfn(isintent(this,pfn))); 17 | -------------------------------------------------------------------------------- /lib/gifti-1.6/@gifti/isfield.m: -------------------------------------------------------------------------------- 1 | function tf = isfield(this,field) 2 | % Isfield method for GIfTI objects 3 | % FORMAT tf = isfield(this,field) 4 | % this - GIfTI object 5 | % field - string of cell array 6 | % tf - logical array 7 | %__________________________________________________________________________ 8 | % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging 9 | 10 | % Guillaume Flandin 11 | % $Id: isfield.m 6507 2015-07-24 16:48:02Z guillaume $ 12 | 13 | tf = ismember(field, fieldnames(this)); 14 | -------------------------------------------------------------------------------- /lib/gifti-1.6/@gifti/plot.m: -------------------------------------------------------------------------------- 1 | function varargout = plot(varargin) 2 | % plot method for GIfTI objects 3 | %__________________________________________________________________________ 4 | % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging 5 | 6 | % Guillaume Flandin 7 | % $Id: plot.m 5888 2014-02-19 19:54:12Z guillaume $ 8 | 9 | % if ishandle(varargin{1}) 10 | % h = figure(varargin{1}); 11 | % else 12 | % h = figure; 13 | % %axis equal; 14 | % %axis off; 15 | % %camlight; 16 | % %camlight(-80,-10); 17 | % %lighting phong; 18 | % end 19 | % cameramenu; 20 | 21 | 22 | cdata = []; 23 | ax = []; 24 | if nargin == 1 25 | this = varargin{1}; 26 | h = gcf; 27 | else 28 | if ishandle(varargin{1}) 29 | ax = varargin{1}; 30 | h = figure(get(ax,'parent')); 31 | this = varargin{2}; 32 | else 33 | this = varargin{1}; 34 | h = gcf; 35 | cdata = subsref(varargin{2},struct('type','.','subs','cdata')); 36 | end 37 | if nargin > 2 38 | indc = varargin{3}; 39 | else 40 | indc = 1; 41 | end 42 | end 43 | 44 | if isempty(ax), ax = axes('Parent',h); end 45 | axis(ax,'equal'); 46 | axis(ax,'off'); 47 | hp = patch(struct(... 48 | 'vertices', subsref(this,struct('type','.','subs','vertices')),... 49 | 'faces', subsref(this,struct('type','.','subs','faces'))),... 50 | 'FaceColor', 'b',... 51 | 'EdgeColor', 'none',... 52 | 'Parent',ax); 53 | 54 | if ~isempty(cdata) 55 | set(hp,'FaceVertexCData',cdata(:,indc), 'FaceColor','interp') 56 | end 57 | 58 | axes(ax); 59 | camlight; 60 | camlight(-80,-10); 61 | lighting phong; 62 | axes(ax); 63 | cameramenu; 64 | 65 | if nargout 66 | varargout{1} = hp; 67 | end 68 | -------------------------------------------------------------------------------- /lib/gifti-1.6/@gifti/private/Makefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env make -f 2 | # GIfTI Makefile 3 | # 4 | # Copyright (C) 2015 Wellcome Trust Centre for Neuroimaging 5 | # 6 | # $Id: Makefile 6402 2015-04-09 18:11:06Z guillaume $ 7 | 8 | include Makefile.var 9 | 10 | SPMMEX = zstream.$(SUF) 11 | 12 | ifeq (mex,$(SUF)) 13 | export CFLAGS = $(shell $(MEX) -p CFLAGS) -std=c99 14 | else 15 | MEXOPTS += CFLAGS='$$CFLAGS -std=c99' 16 | endif 17 | 18 | all: $(SPMMEX) 19 | @: 20 | 21 | clean: 22 | @: 23 | 24 | distclean: clean 25 | $(DEL) $(SPMMEX) 26 | 27 | install: 28 | @: 29 | 30 | tarball: all 31 | $(TAR) cf spm_mex.tar $(SPMMEX) 32 | 33 | %.$(SUF) : %.c 34 | $(MEX) $< $(MEXEND) 35 | -------------------------------------------------------------------------------- /lib/gifti-1.6/@gifti/private/getdict.m: -------------------------------------------------------------------------------- 1 | function d = getdict 2 | % Dictionary of GIfTI/NIfTI stuff 3 | %__________________________________________________________________________ 4 | % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging 5 | 6 | % Guillaume Flandin 7 | % $Id: getdict.m 4505 2011-09-30 11:45:58Z guillaume $ 8 | 9 | persistent dict; 10 | if ~isempty(dict) 11 | d = dict; 12 | return; 13 | end 14 | 15 | table = {... 16 | 'NIFTI_TYPE_UINT8', 'uint8', '%d', @uint8, 'uint8' 17 | 'NIFTI_TYPE_INT32', 'int32', '%d', @int32, 'int32' 18 | 'NIFTI_TYPE_FLOAT32', 'float32', '%f', @single, 'single' 19 | 'NIFTI_TYPE_FLOAT64', 'float64', '%f', @double, 'double'}; 20 | 21 | for i=1:size(table,1) 22 | dict.(table{i,1}) = cell2struct({table{i,2:end}},... 23 | {'class', 'format', 'conv', 'cast'}, 2); 24 | end 25 | 26 | d = dict; -------------------------------------------------------------------------------- /lib/gifti-1.6/@gifti/private/read_freesurfer_file.m: -------------------------------------------------------------------------------- 1 | function this = read_freesurfer_file(filename) 2 | % Low level reader of FreeSurfer files (ASCII triangle surface file) 3 | % FORMAT this = read_freesurfer_file(filename) 4 | % filename - FreeSurfer file 5 | % 6 | % See http://wideman-one.com/gw/brain/fs/surfacefileformats.htm 7 | %__________________________________________________________________________ 8 | % Copyright (C) 2013 Wellcome Trust Centre for Neuroimaging 9 | 10 | % Guillaume Flandin 11 | % $Id: read_freesurfer_file.m 5322 2013-03-13 15:04:14Z guillaume $ 12 | 13 | 14 | fid = fopen(filename,'rt'); 15 | if fid == -1, error('Cannot open "%s".',filename); end 16 | 17 | fgetl(fid); % #!ascii 18 | N = fscanf(fid,'%d',2); 19 | this.vertices = fscanf(fid,'%f %f %f %d',[4 N(1)])'; 20 | this.faces = fscanf(fid,'%d %d %d %d',[4 N(2)])'; 21 | 22 | fclose(fid); 23 | 24 | this.vertices = this.vertices(:,1:3); 25 | this.faces = this.faces(:,1:3) + 1; 26 | -------------------------------------------------------------------------------- /lib/gifti-1.6/@gifti/private/zstream.m: -------------------------------------------------------------------------------- 1 | function Z = zstream(action,D) 2 | % Compress/decompress stream of bytes using Deflate/Inflate 3 | % FORMAT Z = zstream('C',D) 4 | % D - data stream to compress (converted to uint8 if needed) 5 | % Z - compressed data stream (uint8 vector) 6 | % FORMAT D = zstream('D',Z) 7 | % Z - data stream to decompress (uint8 vector) 8 | % D - decompressed data stream (uint8 vector) 9 | %__________________________________________________________________________ 10 | % 11 | % This C-MEX file relies on: 12 | % * miniz, by Rich Geldreich 13 | % http://code.google.com/p/miniz/ 14 | % Fallback Java implementation is adapted from: 15 | % * dzip/dunzip, by Michael Kleder 16 | % http://www.mathworks.com/matlabcentral/fileexchange/8899 17 | %__________________________________________________________________________ 18 | % Copyright (C) 2015 Wellcome Trust Centre for Neuroimaging 19 | 20 | % Guillaume Flandin 21 | % $Id: zstream.m 6417 2015-04-21 16:03:44Z guillaume $ 22 | 23 | 24 | if exist('OCTAVE_VERSION','builtin') 25 | error('zstream.c not compiled - see Makefile'); 26 | end 27 | 28 | switch upper(action) 29 | case 'C' 30 | D = typecast(D(:),'uint8'); 31 | f = java.io.ByteArrayOutputStream(); 32 | g = java.util.zip.DeflaterOutputStream(f); 33 | g.write(D); 34 | g.close; 35 | Z = typecast(f.toByteArray,'uint8'); 36 | f.close; 37 | 38 | case 'D' 39 | import com.mathworks.mlwidgets.io.InterruptibleStreamCopier 40 | a = java.io.ByteArrayInputStream(D); 41 | b = java.util.zip.InflaterInputStream(a); 42 | isc = InterruptibleStreamCopier.getInterruptibleStreamCopier; 43 | c = java.io.ByteArrayOutputStream; 44 | isc.copyStream(b,c); 45 | Z = c.toByteArray; 46 | 47 | otherwise 48 | error('Unknown action "%s".',action); 49 | end 50 | -------------------------------------------------------------------------------- /lib/gifti-1.6/@gifti/struct.m: -------------------------------------------------------------------------------- 1 | function s = struct(this) 2 | % Struct method for GIfTI objects 3 | % FORMAT s = struct(this) 4 | % this - GIfTI object 5 | % s - a structure containing public fields of the object 6 | %__________________________________________________________________________ 7 | % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging 8 | 9 | % Guillaume Flandin 10 | % $Id: struct.m 6507 2015-07-24 16:48:02Z guillaume $ 11 | 12 | names = fieldnames(this); 13 | values = cell(length(names), length(this(:))); 14 | 15 | for i=1:length(names) 16 | [values{i,:}] = subsref(this(:), substruct('.',names{i})); 17 | end 18 | s = reshape(cell2struct(values,names,1),size(this)); 19 | -------------------------------------------------------------------------------- /lib/gifti-1.6/LICENCE: -------------------------------------------------------------------------------- 1 | GIfTI - a MATLAB GIfTI Library 2 | Copyright (C) 2008-2015 Guillaume Flandin 3 | 4 | This program is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU General Public License 6 | as published by the Free Software Foundation; either version 2 7 | of the License, or any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation Inc, 59 Temple Pl. - Suite 330, Boston, MA 02111-1307, USA. 17 | -------------------------------------------------------------------------------- /scripts/s0_script_CreatePrepData.m: -------------------------------------------------------------------------------- 1 | clc; 2 | clear; 3 | 4 | addpath(genpath('path-to-{CollaborativeBrainDecomposition}')); 5 | 6 | % for volumetric data 7 | maskFile = 'path-to-mask-file/mask.nii.gz'; 8 | maskNii = load_untouch_nii(maskFile); 9 | 10 | gNb = createPrepData('volumetric', maskNii.img, 1); 11 | 12 | % for surface data 13 | surfL = 'path-to-{CollaborativeBrainDecomposition}/lib/freesurfer/subjects/fsaverage5/surf/lh.pial'; 14 | surfR = 'path-to-{CollaborativeBrainDecomposition}/lib/freesurfer/subjects/fsaverage5/surf/rh.pial'; 15 | surfML = 'path-to-{CollaborativeBrainDecomposition}/lib/freesurfer/subjects/fsaverage5/label/lh.Medial_wall.label'; 16 | surfMR = 'path-to-{CollaborativeBrainDecomposition}/lib/freesurfer/subjects/fsaverage5/label/rh.Medial_wall.label'; 17 | 18 | [surfStru, surfMask] = getFsSurf(surfL, surfR, surfML, surfMR); 19 | 20 | gNb = createPrepData('surface', surfStru, 1, surfMask); 21 | 22 | % save gNb into file for later use 23 | prepDataName = 'path-to-output/test_CreatePrepData.mat'; 24 | save(prepDataName, 'gNb'); 25 | -------------------------------------------------------------------------------- /scripts/s1_script_FuncInit_surf_fs.m: -------------------------------------------------------------------------------- 1 | clc; 2 | clear; 3 | 4 | addpath(genpath('path-to-{CollaborativeBrainDecomposition}')); 5 | 6 | sbjListFile = 'path-to-data/fs_sbjLst.txt'; 7 | surfL = 'path-to-{CollaborativeBrainDecomposition}/lib/freesurfer/subjects/fsaverage5/surf/lh.pial'; 8 | surfR = 'path-to-{CollaborativeBrainDecomposition}/lib/freesurfer/subjects/fsaverage5/surf/rh.pial'; 9 | surfML = 'path-to-{CollaborativeBrainDecomposition}/lib/freesurfer/subjects/fsaverage5/label/lh.Medial_wall.label'; 10 | surfMR = 'path-to-{CollaborativeBrainDecomposition}/lib/freesurfer/subjects/fsaverage5/label/rh.Medial_wall.label'; 11 | prepDataFile = 'path-to-output-fs/prepData.mat'; 12 | outDir = 'path-to-output-fs/init'; 13 | spaR = 1; 14 | vxI = 0; 15 | ard = 0; 16 | iterNum = 1000; 17 | K = 17; 18 | tNum = 120; 19 | alpha = 2; 20 | beta = 10; 21 | resId = 'fmri_fs'; 22 | 23 | deployFuncInit_surf_fs(sbjListFile,surfL,surfR,surfML,surfMR,prepDataFile,outDir,spaR,vxI,ard,iterNum,K,tNum,alpha,beta,resId); 24 | -------------------------------------------------------------------------------- /scripts/s1_script_FuncInit_surf_hcp.m: -------------------------------------------------------------------------------- 1 | clc; 2 | clear; 3 | 4 | addpath(genpath('path-to-{CollaborativeBrainDecomposition}')); 5 | 6 | sbjListFile = 'path-to-hcp-data/hcp_sbjLst.txt'; 7 | wbPath = 'path-to-workbench/wb_command.exe'; 8 | surfL = 'path-to-hcp-data/100307/MNINonLinear/fsaverage_LR32k/100307.L.inflated.32k_fs_LR.surf.gii'; 9 | surfR = 'path-to-hcp-data/100307/MNINonLinear/fsaverage_LR32k/100307.R.inflated.32k_fs_LR.surf.gii'; 10 | surfML = 'path-to-hcp-data/100307/MNINonLinear/fsaverage_LR32k/100307.L.atlasroi.32k_fs_LR.shape.gii'; 11 | surfMR = 'path-to-hcp-data/100307/MNINonLinear/fsaverage_LR32k/100307.R.atlasroi.32k_fs_LR.shape.gii'; 12 | prepDataFile = 'path-to-output-hcp/prepData.mat'; 13 | outDir = 'path-to-output-hcp/init'; 14 | spaR = 1; 15 | vxI = 0; 16 | ard = 0; 17 | iterNum = 1000; 18 | K = 17; 19 | tNum = 1200; 20 | alpha = 2; 21 | beta = 10; 22 | resId = 'hcp'; 23 | 24 | deployFuncInit_surf_hcp(sbjListFile,wbPath,surfL,surfR,surfML,surfMR,prepDataFile,outDir,spaR,vxI,ard,iterNum,K,tNum,alpha,beta,resId); %,svFileL,svFileR); 25 | -------------------------------------------------------------------------------- /scripts/s1_script_FuncInit_vol.m: -------------------------------------------------------------------------------- 1 | clc; 2 | clear; 3 | 4 | addpath(genpath('path-to-{CollaborativeBrainDecomposition}')); 5 | 6 | sbjListFile = 'path-to-data/vol_sbjLst.txt'; 7 | maskFile = 'path-to-data/mask.nii.gz'; 8 | prepDataFile = 'path-to-output-vol/prepData.mat'; 9 | outDir = 'path-to-output-vol/init'; 10 | spaR = 1; 11 | vxI = 0; 12 | ard = 1; 13 | iterNum = 1000; 14 | K = 17; 15 | tNum = 118; 16 | alpha = 2; 17 | beta = 10; 18 | resId = 'fmri_vol'; 19 | 20 | deployFuncInit_vol(sbjListFile,maskFile,prepDataFile,outDir,spaR,vxI,ard,iterNum,K,tNum,alpha,beta,resId); 21 | -------------------------------------------------------------------------------- /scripts/s2_script_SelRobustInit_vol.m: -------------------------------------------------------------------------------- 1 | clc; 2 | clear; 3 | 4 | addpath(genpath('path-to-{CollaborativeBrainDecomposition}')); 5 | 6 | 7 | candidateLstFile = 'path-to-output/init_cand_lst.txt'; 8 | outDir = 'path-to-output/robustInit'; 9 | K = 17; 10 | 11 | initV = selRobustInit(candidateLstFile,K,outDir); 12 | -------------------------------------------------------------------------------- /scripts/s3_script_FuncMvnmfL21p1_func_surf_fs.m: -------------------------------------------------------------------------------- 1 | clc; 2 | clear; 3 | 4 | addpath(genpath('path-to-{CollaborativeBrainDecomposition}')); 5 | 6 | sbjListFile = 'path-to-data/fs_sbjLst.txt'; 7 | medialWallFileL = 'path-to-{CollaborativeBrainDecomposition}/lib/freesurfer/subjects/fsaverage5/label/lh.Medial_wall.label'; 8 | medialWallFileR = 'path-to-{CollaborativeBrainDecomposition}/lib/freesurfer/subjects/fsaverage5/label/rh.Medial_wall.label'; 9 | prepDataFile = 'path-to-output-fs/prepData.mat'; 10 | outDir = 'path-to-output-fs/res'; 11 | 12 | resId = 'fmri_fs'; 13 | initName = 'path-to-output/robustInit/init.mat'; 14 | K = 17; 15 | alphaS21 = 2; 16 | alphaL = 10; 17 | vxI = 0; 18 | spaR = 1; 19 | ard = 1; 20 | eta = 1; 21 | iterNum = 30; 22 | calcGrp = 1; 23 | parforOn = 0; 24 | 25 | deployFuncMvnmfL21p1_func_surf_fs(sbjListFile,medialWallFileL,medialWallFileR,prepDataFile,outDir,resId,initName,K,alphaS21,alphaL,vxI,spaR,ard,eta,iterNum,calcGrp,parforOn); 26 | -------------------------------------------------------------------------------- /scripts/s3_script_FuncMvnmfL21p1_func_surf_hcp.m: -------------------------------------------------------------------------------- 1 | clc; 2 | clear; 3 | 4 | addpath(genpath('path-to-{CollaborativeBrainDecomposition}')); 5 | 6 | sbjListFile = 'path-to-hcp-data/hcp_sbjLst.txt'; 7 | wbPath = 'path-to-workbench/wb_command.exe'; 8 | prepDataFile = 'path-to-output-hcp/prepData.mat'; 9 | outDir = 'path-to-output-hcp/res'; 10 | 11 | resId = 'hcp'; 12 | initName = 'path-to-output/robustInit/init.mat'; 13 | K = 17; 14 | alphaS21 = 2; 15 | alphaL = 10; 16 | vxI = 0; 17 | spaR = 1; 18 | ard = 1; 19 | eta = 1; 20 | iterNum = 30; 21 | calcGrp = 1; 22 | parforOn = 0; 23 | 24 | deployFuncMvnmfL21p1_func_surf_hcp(sbjListFile,wbPath,prepDataFile,outDir,resId,initName,K,alphaS21,alphaL,vxI,spaR,ard,eta,iterNum,calcGrp,parforOn); 25 | -------------------------------------------------------------------------------- /scripts/s3_script_FuncMvnmfL21p1_func_vol.m: -------------------------------------------------------------------------------- 1 | clc; 2 | clear; 3 | 4 | addpath(genpath('path-to-{CollaborativeBrainDecomposition}')); 5 | 6 | sbjListFile = 'path-to-data/vol_sbjLst.txt'; 7 | maskFile = 'path-to-data/mask.nii.gz'; 8 | prepDataFile = 'path-to-output-vol/prepData.mat'; 9 | outDir = 'path-to-output-vol/res'; 10 | 11 | resId = 'fmri_vol'; 12 | initName = 'path-to-output/robustInit/init.mat'; 13 | K = 17; 14 | alphaS21 = 2; 15 | alphaL = 10; 16 | vxI = 0; 17 | spaR = 1; 18 | ard = 1; 19 | eta = 1; 20 | iterNum = 30; 21 | calcGrp = 1; 22 | parforOn = 0; 23 | 24 | deployFuncMvnmfL21p1_func_vol(sbjListFile,maskFile,prepDataFile,outDir,resId,initName,K,alphaS21,alphaL,vxI,spaR,ard,eta,iterNum,calcGrp,parforOn); 25 | -------------------------------------------------------------------------------- /scripts/s4_script_SaveResImgIdv_vol.m: -------------------------------------------------------------------------------- 1 | clc; 2 | clear; 3 | 4 | addpath(genpath('path-to-{CollaborativeBrainDecomposition}')); 5 | 6 | resFileName = 'path-to-output-vol/res/final_UV.mat'; 7 | maskName = 'path-to-data/mask.nii.gz'; 8 | outDir = 'path-to-output-vol/fig_idv'; 9 | saveFig = 1; 10 | refNiiName = 'path-to-data/mask.nii.gz'; % reference image 11 | sbjId = 1; 12 | 13 | func_saveVolRes2Nii_idv(resFileName,maskName,sbjId,outDir,saveFig,refNiiName); 14 | -------------------------------------------------------------------------------- /scripts/s4_script_SaveResImg_vol.m: -------------------------------------------------------------------------------- 1 | clc; 2 | clear; 3 | 4 | addpath(genpath('path-to-{CollaborativeBrainDecomposition}')); 5 | 6 | resFileName = 'path-to-output-vol/res/res_cen.mat'; 7 | maskName = 'path-to-data/mask.nii.gz'; 8 | outDir = 'path-to-output-vol/fig'; 9 | saveFig = 1; 10 | refNiiName = 'path-to-data/mask.nii.gz'; % reference image 11 | 12 | func_saveVolRes2Nii(resFileName,maskName,outDir,saveFig,refNiiName); 13 | -------------------------------------------------------------------------------- /src/constructW_vol.m: -------------------------------------------------------------------------------- 1 | function gvol = constructW_vol(maskMat,neiR) 2 | %generate sparse graph for 3D image 3 | % maskMat: a 3D matrix containing the mask of regions of intesest (ROI) 4 | % neiR: the radius of spatial neighborhood 5 | 6 | if nargin<2 7 | neiR = 1; 8 | end 9 | 10 | maskMat = int32(maskMat); 11 | maskSz = size(maskMat); 12 | if length(maskSz)~=3 13 | error(' Only support 3D image now'); 14 | end 15 | 16 | if length(maskSz)~=length(neiR) 17 | neiRvec = repmat(neiR,1,length(maskSz)); 18 | else 19 | neiRvec = neiR; 20 | end 21 | 22 | %assign a label (1 to length(non-zero voxel)) for each non-zero voxel in the mask 23 | labMaskMat = maskMat; 24 | nonZero = maskMat~=0; 25 | eleNum = sum(nonZero(:)); 26 | labMaskMat(maskMat~=0) = 1:eleNum; 27 | 28 | gvol = cell(eleNum,1); 29 | for zi=1:maskSz(3) 30 | for yi=1:maskSz(2) 31 | for xi=1:maskSz(1) 32 | cenLab = labMaskMat(xi,yi,zi); 33 | if cenLab~=0 34 | nstart = max([xi,yi,zi]-neiRvec,[1,1,1]); 35 | nend = min([xi,yi,zi]+neiRvec,maskSz); 36 | for zni=nstart(3):nend(3) 37 | for yni=nstart(2):nend(2) 38 | for xni=nstart(1):nend(1) 39 | neiLab = labMaskMat(xni,yni,zni); 40 | if neiLab~=0 && neiLab>cenLab 41 | gvol{cenLab}(end+1) = neiLab; 42 | gvol{neiLab}(end+1) = cenLab; 43 | end 44 | end 45 | end 46 | end 47 | end 48 | end 49 | end 50 | end 51 | 52 | 53 | -------------------------------------------------------------------------------- /src/createPrepData.m: -------------------------------------------------------------------------------- 1 | function gNb = createPrepData(dataType, maskStru, nb, auxMask) 2 | % create spatial neighborhood for volumetric or surface data 3 | % Input: 4 | % dataType: 'volumetric' or 'surface' 5 | % maskStru: if dataType is 'volumetric', maskStru should be matrix containing a mask image 6 | % if dataType is 'surface', maskStru should be a surface structure including 7 | % maskStru.vx_l: vertices of left hemi-sphere 8 | % maskStru.vx_r: vertices of right hemi-sphere 9 | % maskStru.faces_l: faces of left hemi-sphere 10 | % maskStru.faces_r: faces of right hemi-sphere 11 | % nb: size of spaital neighborhood 12 | % auxMask: (required if dataType is 'surface') 13 | % a structure containing the mask of regions of interest (ROI) for each hemi-sphere, including 14 | % auxMask.l (vector) and auxMask.r (vector) 15 | 16 | if (strcmp(dataType, 'volumetric') && nargin==3) || (strcmp(dataType, 'surface') && nargin==4) 17 | if strcmp(dataType,'volumetric') 18 | gNb = constructW_vol(maskStru,nb); 19 | elseif strcmp(dataType,'surface') 20 | gNb = constructW_surf(maskStru,nb,auxMask); 21 | end 22 | else 23 | error('Data format not supported !'); 24 | end 25 | 26 | -------------------------------------------------------------------------------- /src/deployFuncInit_mat.m: -------------------------------------------------------------------------------- 1 | function deployFuncInit_mat(sbjListFile,maskFile,prepDataFile,outDir,spaR,vxI,ard,iterNum,K,tNum,alpha,beta,resId) 2 | 3 | if nargin~=13 4 | error('number of input should be 13 !'); 5 | end 6 | 7 | if isdeployed 8 | spaR = str2double(spaR); 9 | vxI = str2double(vxI); 10 | ard = str2double(ard); 11 | iterNum = str2double(iterNum); 12 | K = str2double(K); 13 | tNum = str2double(tNum); 14 | alpha = str2double(alpha); 15 | beta = str2double(beta); 16 | end 17 | 18 | load(prepDataFile); % containing gNb 19 | 20 | nmVec = zeros(length(gNb),1); 21 | for gni=1:length(gNb) 22 | nmVec(gni) = length(gNb{gni}); 23 | end 24 | nM = median(nmVec); 25 | 26 | sbjData = prepareFuncData_mat_func(sbjListFile,maskFile); 27 | 28 | numUsed = length(sbjData); 29 | pS = round((alpha*tNum*numUsed)/K); 30 | pL = round((beta*tNum*numUsed)/(K*nM)); 31 | 32 | tic; 33 | func_initialization_woLoadSrc(sbjData,prepDataFile,outDir,resId,numUsed,K,pS,pL,spaR,vxI,ard,iterNum); 34 | toc; 35 | 36 | if isdeployed 37 | exit; 38 | end 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/deployFuncInit_surf_fs.m: -------------------------------------------------------------------------------- 1 | function deployFuncInit_surf_fs(sbjListFile,surfL,surfR,surfML,surfMR,prepDataFile,outDir,spaR,vxI,ard,iterNum,K,tNum,alpha,beta,resId) 2 | 3 | if nargin~=16 4 | error('number of input should be 16 !'); 5 | end 6 | 7 | if isdeployed 8 | spaR = str2double(spaR); 9 | vxI = str2double(vxI); 10 | ard = str2double(ard); 11 | iterNum = str2double(iterNum); 12 | K = str2double(K); 13 | tNum = str2double(tNum); 14 | alpha = str2double(alpha); 15 | beta = str2double(beta); 16 | end 17 | 18 | if ~exist(prepDataFile,'file') 19 | [surfStru, surfMask] = getFsSurf(surfL, surfR, surfML, surfMR); 20 | gNb = constructW_surf(surfStru, spaR, surfMask); 21 | 22 | save(prepDataFile,'gNb','-v7.3'); 23 | else 24 | load(prepDataFile); % containing gNb 25 | end 26 | 27 | nmVec = zeros(length(gNb),1); 28 | for gni=1:length(gNb) 29 | nmVec(gni) = length(gNb{gni}); 30 | end 31 | nM = median(nmVec); 32 | 33 | sbjData = prepareFuncData_fs_func(sbjListFile, surfML, surfMR); 34 | 35 | numUsed = length(sbjData); 36 | pS = round((alpha*tNum*numUsed)/K); 37 | pL = round((beta*tNum*numUsed)/(K*nM)); 38 | 39 | tic; 40 | func_initialization_woLoadSrc(sbjData,prepDataFile,outDir,resId,numUsed,K,pS,pL,spaR,vxI,ard,iterNum); 41 | toc; 42 | 43 | if isdeployed 44 | exit; 45 | end 46 | -------------------------------------------------------------------------------- /src/deployFuncInit_surf_hcp.m: -------------------------------------------------------------------------------- 1 | function deployFuncInit_surf_hcp(sbjListFile,wbPath,surfL,surfR,surfML,surfMR,prepDataFile,outDir,spaR,vxI,ard,iterNum,K,tNum,alpha,beta,resId,svFileL,svFileR) 2 | 3 | if nargin~=17 && nargin~=19 4 | error('number of input should be 13 or 15 !'); 5 | end 6 | 7 | if isdeployed 8 | spaR = str2double(spaR); 9 | vxI = str2double(vxI); 10 | ard = str2double(ard); 11 | iterNum = str2double(iterNum); 12 | K = str2double(K); 13 | tNum = str2double(tNum); 14 | alpha = str2double(alpha); 15 | beta = str2double(beta); 16 | end 17 | 18 | if ~exist(prepDataFile,'file') 19 | [surfStru, surfMask] = getHcpSurf(surfL, surfR, surfML, surfMR); 20 | gNb = constructW_surf(surfStru, spaR, surfMask); 21 | 22 | save(prepDataFile,'gNb','-v7.3'); 23 | else 24 | load(prepDataFile); % containing gNb 25 | end 26 | 27 | nmVec = zeros(length(gNb),1); 28 | for gni=1:length(gNb) 29 | nmVec(gni) = length(gNb{gni}); 30 | end 31 | nM = median(nmVec); 32 | 33 | if nargin==17 34 | sbjData = prepareFuncData_hcp_func(sbjListFile,wbPath); 35 | elseif nargin==19 36 | sbjData = prepareFuncData_hcp_func(sbjListFile,wbPath,svFileL,svFileR); 37 | end 38 | 39 | numUsed = length(sbjData); 40 | pS = round((alpha*tNum*numUsed)/K); 41 | pL = round((beta*tNum*numUsed)/(K*nM)); 42 | 43 | tic; 44 | func_initialization_woLoadSrc(sbjData,prepDataFile,outDir,resId,numUsed,K,pS,pL,spaR,vxI,ard,iterNum); 45 | toc; 46 | 47 | if isdeployed 48 | exit; 49 | end 50 | -------------------------------------------------------------------------------- /src/deployFuncInit_vol.m: -------------------------------------------------------------------------------- 1 | function deployFuncInit_vol(sbjListFile,maskFile,prepDataFile,outDir,spaR,vxI,ard,iterNum,K,tNum,alpha,beta,resId) 2 | 3 | if nargin~=13 4 | error('number of input should be 13 !'); 5 | end 6 | 7 | if isdeployed 8 | spaR = str2double(spaR); 9 | vxI = str2double(vxI); 10 | ard = str2double(ard); 11 | iterNum = str2double(iterNum); 12 | K = str2double(K); 13 | tNum = str2double(tNum); 14 | alpha = str2double(alpha); 15 | beta = str2double(beta); 16 | end 17 | 18 | if ~exist(prepDataFile,'file') 19 | maskNii = load_untouch_nii(maskFile); 20 | maskMat = int32(maskNii.img~=0); 21 | gNb = constructW_vol(maskMat,spaR); 22 | 23 | save(prepDataFile,'gNb','-v7.3'); 24 | else 25 | load(prepDataFile); % containing gNb 26 | end 27 | 28 | nmVec = zeros(length(gNb),1); 29 | for gni=1:length(gNb) 30 | nmVec(gni) = length(gNb{gni}); 31 | end 32 | nM = median(nmVec); 33 | 34 | sbjData = prepareFuncData_vol_func(sbjListFile,maskFile); 35 | 36 | numUsed = length(sbjData); 37 | pS = round((alpha*tNum*numUsed)/K); 38 | pL = round((beta*tNum*numUsed)/(K*nM)); 39 | 40 | tic; 41 | func_initialization_woLoadSrc(sbjData,prepDataFile,outDir,resId,numUsed,K,pS,pL,spaR,vxI,ard,iterNum); 42 | toc; 43 | 44 | if isdeployed 45 | exit; 46 | end 47 | -------------------------------------------------------------------------------- /src/deployFuncMvnmfL21p1_func_mat.m: -------------------------------------------------------------------------------- 1 | function deployFuncMvnmfL21p1_func_mat(sbjListFile,maskFile,prepDataFile,outDir,resId,initName,K,alphaS21,alphaL,vxI,spaR,ard,eta,iterNum,calcGrp,parforOn) 2 | 3 | if nargin~=16 4 | error('number of input should be 16 !'); 5 | end 6 | 7 | if isdeployed 8 | K = str2double(K); 9 | alphaS21 = str2double(alphaS21); 10 | alphaL = str2double(alphaL); 11 | vxI = str2double(vxI); 12 | spaR = str2double(spaR); 13 | ard = str2double(ard); 14 | eta = str2double(eta); 15 | iterNum = str2double(iterNum); 16 | calcGrp = str2double(calcGrp); 17 | parforOn = str2double(parforOn); 18 | end 19 | 20 | sbjData = prepareFuncData_mat_func(sbjListFile,maskFile); 21 | sbjNum = length(sbjData); 22 | 23 | if ~exist(outDir,'dir') 24 | mkdir(outDir); 25 | end 26 | 27 | tic; 28 | func_mMvNMF4fmri_l21p1_ard_woSrcLoad(sbjData,prepDataFile,outDir,resId,initName,sbjNum,K,alphaS21,alphaL,spaR,vxI,ard,eta,iterNum,calcGrp,parforOn); 29 | toc; 30 | 31 | if isdeployed 32 | exit; 33 | else 34 | disp('Done!'); 35 | end 36 | -------------------------------------------------------------------------------- /src/deployFuncMvnmfL21p1_func_surf_fs.m: -------------------------------------------------------------------------------- 1 | function deployFuncMvnmfL21p1_func_surf_fs(sbjListFile,medialWallFileL,medialWallFileR,prepDataFile,outDir,resId,initName,K,alphaS21,alphaL,vxI,spaR,ard,eta,iterNum,calcGrp,parforOn) 2 | 3 | if nargin~=17 4 | error('number of input should be 17 !'); 5 | end 6 | 7 | if isdeployed 8 | K = str2double(K); 9 | alphaS21 = str2double(alphaS21); 10 | alphaL = str2double(alphaL); 11 | vxI = str2double(vxI); 12 | spaR = str2double(spaR); 13 | ard = str2double(ard); 14 | eta = str2double(eta); 15 | iterNum = str2double(iterNum); 16 | calcGrp = str2double(calcGrp); 17 | parforOn = str2double(parforOn); 18 | end 19 | 20 | sbjData = prepareFuncData_fs_func(sbjListFile,medialWallFileL,medialWallFileR); 21 | sbjNum = length(sbjData); 22 | 23 | if ~exist(outDir,'dir') 24 | mkdir(outDir); 25 | end 26 | 27 | tic; 28 | func_mMvNMF4fmri_l21p1_ard_woSrcLoad(sbjData,prepDataFile,outDir,resId,initName,sbjNum,K,alphaS21,alphaL,spaR,vxI,ard,eta,iterNum,calcGrp,parforOn); 29 | toc; 30 | 31 | if isdeployed 32 | exit; 33 | else 34 | disp('Done!'); 35 | end 36 | -------------------------------------------------------------------------------- /src/deployFuncMvnmfL21p1_func_surf_hcp.m: -------------------------------------------------------------------------------- 1 | function deployFuncMvnmfL21p1_func_surf_hcp(sbjListFile,wbPath,prepDataFile,outDir,resId,initName,K,alphaS21,alphaL,vxI,spaR,ard,eta,iterNum,calcGrp,parforOn) 2 | 3 | if nargin~=16 4 | error('number of input should be 16 !'); 5 | end 6 | 7 | if isdeployed 8 | K = str2double(K); 9 | alphaS21 = str2double(alphaS21); 10 | alphaL = str2double(alphaL); 11 | vxI = str2double(vxI); 12 | spaR = str2double(spaR); 13 | ard = str2double(ard); 14 | eta = str2double(eta); 15 | iterNum = str2double(iterNum); 16 | calcGrp = str2double(calcGrp); 17 | parforOn = str2double(parforOn); 18 | end 19 | 20 | sbjData = prepareFuncData_hcp_func(sbjListFile,wbPath); 21 | sbjNum = length(sbjData); 22 | 23 | if ~exist(outDir,'dir') 24 | mkdir(outDir); 25 | end 26 | 27 | tic; 28 | func_mMvNMF4fmri_l21p1_ard_woSrcLoad(sbjData,prepDataFile,outDir,resId,initName,sbjNum,K,alphaS21,alphaL,spaR,vxI,ard,eta,iterNum,calcGrp,parforOn); 29 | toc; 30 | 31 | if isdeployed 32 | exit; 33 | else 34 | disp('Done!'); 35 | end 36 | -------------------------------------------------------------------------------- /src/deployFuncMvnmfL21p1_func_vol.m: -------------------------------------------------------------------------------- 1 | function deployFuncMvnmfL21p1_func_vol(sbjListFile,maskFile,prepDataFile,outDir,resId,initName,K,alphaS21,alphaL,vxI,spaR,ard,eta,iterNum,calcGrp,parforOn) 2 | 3 | if nargin~=16 4 | error('number of input should be 16 !'); 5 | end 6 | 7 | if isdeployed 8 | K = str2double(K); 9 | alphaS21 = str2double(alphaS21); 10 | alphaL = str2double(alphaL); 11 | vxI = str2double(vxI); 12 | spaR = str2double(spaR); 13 | ard = str2double(ard); 14 | eta = str2double(eta); 15 | iterNum = str2double(iterNum); 16 | calcGrp = str2double(calcGrp); 17 | parforOn = str2double(parforOn); 18 | end 19 | 20 | sbjData = prepareFuncData_vol_func(sbjListFile,maskFile); 21 | sbjNum = length(sbjData); 22 | 23 | if ~exist(outDir,'dir') 24 | mkdir(outDir); 25 | end 26 | 27 | tic; 28 | func_mMvNMF4fmri_l21p1_ard_woSrcLoad(sbjData,prepDataFile,outDir,resId,initName,sbjNum,K,alphaS21,alphaL,spaR,vxI,ard,eta,iterNum,calcGrp,parforOn); 29 | toc; 30 | 31 | if isdeployed 32 | exit; 33 | else 34 | disp('Done!'); 35 | end 36 | -------------------------------------------------------------------------------- /src/func_saveVolRes2Nii.m: -------------------------------------------------------------------------------- 1 | function func_saveVolRes2Nii(resFileName,maskName,outDir,saveFig,refNiiName) 2 | 3 | if nargin==3 4 | saveFig = 0; 5 | end 6 | 7 | if nargin~=3 && nargin~=5 8 | error('Usage: func_saveVolRes2Nii( resFileName,maskName,outDir,(saveFig,refNiiName) )'); 9 | end 10 | 11 | res = load(resFileName); 12 | 13 | if ~exist('initV','var') 14 | initV = res.V_centroid; 15 | else 16 | initV = res.initV; 17 | end 18 | 19 | maskNii = load_untouch_nii(maskName); 20 | 21 | if ~exist(outDir,'dir') 22 | mkdir(outDir); 23 | end 24 | 25 | smInd = initV ./ max(repmat(max(initV),size(initV,1),1),eps) < 1e-2; 26 | initV(smInd) = 0; 27 | 28 | K = size(initV,2); 29 | for ki=1:K 30 | if ki<10 31 | kStr = ['00',num2str(ki)]; 32 | elseif ki<100 33 | kStr = ['0',num2str(ki)]; 34 | else 35 | kStr = num2str(ki); 36 | end 37 | disp(['save nii -- icn ',kStr]); 38 | 39 | kNii = maskNii; 40 | kNii.img(maskNii.img~=0) = initV(:,ki); 41 | 42 | outName = [outDir,filesep,'icn_',kStr,'.nii.gz']; 43 | save_untouch_nii(kNii,outName); 44 | end 45 | 46 | if saveFig==1 47 | for ki=1:K 48 | if ki<10 49 | kStr = ['00',num2str(ki)]; 50 | elseif ki<100 51 | kStr = ['0',num2str(ki)]; 52 | else 53 | kStr = num2str(ki); 54 | end 55 | disp(['save fig -- icn ',kStr]); 56 | 57 | smNiiName = [outDir,filesep,'icn_',kStr,'.nii.gz']; 58 | outFigName = [outDir,filesep,'cutoff_icn_',kStr,'.tif']; 59 | 60 | dispSM_func(smNiiName,refNiiName,outFigName); 61 | end 62 | end 63 | -------------------------------------------------------------------------------- /src/func_saveVolRes2Nii_idv.m: -------------------------------------------------------------------------------- 1 | function func_saveVolRes2Nii_idv(resFileName,maskName,sbjId,outDir,saveFig,refNiiName) 2 | 3 | if nargin==4 4 | saveFig = 0; 5 | end 6 | 7 | if nargin~=4 && nargin~=6 8 | error('Usage: func_saveVolRes2Nii_idv( resFileName,maskName,sbjId,outDir,(saveFig,refNiiName) )'); 9 | end 10 | 11 | ld_res = load(resFileName); 12 | allRes = ld_res.V; 13 | clear V; 14 | 15 | maskNii = load_untouch_nii(maskName); 16 | 17 | if sbjId<10 18 | sbjIdStr = ['00',num2str(sbjId)]; 19 | elseif ki<100 20 | sbjIdStr = ['0',num2str(sbjId)]; 21 | else 22 | sbjIdStr = num2str(sbjId); 23 | end 24 | 25 | if ~exist(outDir,'dir') 26 | mkdir(outDir); 27 | end 28 | 29 | res = allRes{sbjId}; 30 | smInd = res ./ max(repmat(max(res),size(res,1),1),eps) < 1e-2; 31 | res(smInd) = 0; 32 | 33 | K = size(res,2); 34 | for ki=1:K 35 | if ki<10 36 | kStr = ['00',num2str(ki)]; 37 | elseif ki<100 38 | kStr = ['0',num2str(ki)]; 39 | else 40 | kStr = num2str(ki); 41 | end 42 | disp(['save nii -- icn ',kStr]); 43 | 44 | kNii = maskNii; 45 | kNii.img(maskNii.img~=0) = res(:,ki); 46 | 47 | outName = [outDir,filesep,'icn_',kStr,'.nii.gz']; 48 | save_untouch_nii(kNii,outName); 49 | end 50 | 51 | if saveFig==1 52 | for ki=1:K 53 | if ki<10 54 | kStr = ['00',num2str(ki)]; 55 | elseif ki<100 56 | kStr = ['0',num2str(ki)]; 57 | else 58 | kStr = num2str(ki); 59 | end 60 | disp(['save fig -- icn ',kStr]); 61 | 62 | smNiiName = [outDir,filesep,'icn_',kStr,'.nii.gz']; 63 | outFigName = [outDir,filesep,'cutoff_icn_',kStr,'.tif']; 64 | 65 | dispSM_func(smNiiName,refNiiName,outFigName); 66 | end 67 | end 68 | -------------------------------------------------------------------------------- /src/getFsSurf.m: -------------------------------------------------------------------------------- 1 | function [surfStru, surfMask] = getFsSurf(surfNameL, surfNameR, surfMaskNameL, surfMaskNameR) 2 | % prep freesurfer surface structure 3 | % for example, surf file 4 | % surfNameL = [fs_path,filesep,'subjects',filesep,'fsaverage5',filesep,'surf',filesep,'lh.pial']; 5 | % surfNameR = [fs_path,filesep,'subjects',filesep,'fsaverage5',filesep,'surf',filesep,'rh.pial']; 6 | % surfMaskNameL = [fs_path,filesep,'subjects',filesep,'fsaverage5',filesep,'label',filesep,'lh.Medial_wall.label']; 7 | % surfMaskNameR = [fs_path,filesep,'subjects',filesep,'fsaverage5',filesep,'label',filesep,'rh.Medial_wall.label']; 8 | 9 | % surface topology 10 | [vx_l, faces_l] = read_surf(surfNameL); 11 | [vx_r, faces_r] = read_surf(surfNameR); 12 | 13 | surfStru.vx_l = vx_l; 14 | surfStru.faces_l = faces_l + 1; 15 | surfStru.vx_r = vx_r; 16 | surfStru.faces_r = faces_r + 1; 17 | 18 | % surface mask 19 | l_l = read_label([],surfMaskNameL); 20 | l_r = read_label([],surfMaskNameR); 21 | 22 | l_l_ind = l_l(:,1) + 1; 23 | l_r_ind = l_r(:,1) + 1; 24 | 25 | surfMask.l = ones(length(vx_l),1); 26 | surfMask.l(l_l_ind) = 0; 27 | 28 | surfMask.r = ones(length(vx_r),1); 29 | surfMask.r(l_r_ind) = 0; 30 | -------------------------------------------------------------------------------- /src/getHcpSurf.m: -------------------------------------------------------------------------------- 1 | function [surfStru, surfMask] = getHcpSurf(surfNameL, surfNameR, surfMaskNameL, surfMaskNameR) 2 | % prep hcp surface structure 3 | 4 | %gdPath = 'C:\Users\LiHon\Google Drive'; 5 | %giftiPath = [gdPath,filesep,'Code\Download',filesep,'gifti-1.6']; 6 | %addpath(genpath(giftiPath)); 7 | 8 | % surface topology 9 | g_l = gifti(surfNameL); 10 | g_r = gifti(surfNameR); 11 | 12 | surfStru.faces_l = g_l.faces; 13 | surfStru.faces_r = g_r.faces; 14 | surfStru.vx_l = g_l.vertices; 15 | surfStru.vx_r = g_r.vertices; 16 | 17 | % surface mask 18 | cm_l = gifti(surfMaskNameL); 19 | cm_r = gifti(surfMaskNameR); 20 | 21 | surfMask.l = cm_l.cdata > 0; 22 | surfMask.r = cm_r.cdata > 0; 23 | -------------------------------------------------------------------------------- /src/prepareFuncData_mat_func.m: -------------------------------------------------------------------------------- 1 | function sbjData = prepareFuncData_mat_func(fileList,maskFile) 2 | % fileList -- path of functional files (.mat): each row for one subject, each .mat file contains 3 | % a variable named data (a matrix with size t x v, # of time points by # of voxels before masking) 4 | % maskFile -- mask data file, containing vectorized mask, named data 5 | % 6 | % output 7 | % sbjData contains a cell structure sbjData (cell(sbjNum,1)), in which each 8 | % entry sbjData{i} is a matrix of size t x v (# of time points by # of voxels) 9 | % 10 | 11 | if nargin~=2 12 | error('Usage: prepareFuncData_mat_func fileList maskFile'); 13 | end 14 | 15 | % read image list 16 | fileID = fopen(fileList); 17 | sbjList = textscan(fileID,'%s'); 18 | sbjList = sbjList{1}; 19 | fclose(fileID); 20 | 21 | % load brain mask image 22 | maskMat = load(maskFile); 23 | maskVec = maskMat.data > 0; 24 | 25 | %maskNii = load_untouch_nii(maskImageFile); 26 | %maskVec = maskNii.img(:) > 0; 27 | 28 | sbjNum = length(sbjList); 29 | sbjData = cell(sbjNum,1); 30 | 31 | disp('Read data...'); 32 | for si=1:sbjNum 33 | disp([' ',num2str(si),'. ',sbjList{si}]); 34 | sbjMat = load(sbjList{si}); 35 | 36 | sbjData{si} = sbjMat.data(:,maskVec); 37 | end 38 | 39 | 40 | -------------------------------------------------------------------------------- /src/prepareFuncData_vol_func.m: -------------------------------------------------------------------------------- 1 | function sbjData = prepareFuncData_vol_func(fileList,maskName) 2 | % fileList -- path of functional files (.nii): each row for one image 3 | % maskName -- path of the brain mask nii 4 | % 5 | % output 6 | % sbjData contains a cell structure sbjData (cell(sbjNum,1)), in which each 7 | % entry sbjData{i} is a matrix of size t x v (# of time points by # of voxels) 8 | % 9 | 10 | if nargin~=2 11 | error('Usage: prepareFuncData_vol fileList maskName'); 12 | end 13 | 14 | % read image list 15 | fileID = fopen(fileList); 16 | sbjList = textscan(fileID,'%s'); 17 | sbjList = sbjList{1}; 18 | fclose(fileID); 19 | 20 | % load brain mask image 21 | maskNii = load_untouch_nii(maskName); 22 | maskMat = maskNii.img~=0; 23 | 24 | sbjNum = length(sbjList); 25 | sbjData = cell(sbjNum,1); 26 | 27 | disp('Read images...'); 28 | for si=1:sbjNum 29 | disp([' ',num2str(si),'. ',sbjList{si}]); 30 | sbjNii = load_untouch_nii(sbjList{si}); 31 | 32 | vxNum = sum(maskMat(:)~=0); 33 | tNum = size(sbjNii.img,4); 34 | dataMat = zeros(tNum,vxNum,'single'); 35 | for ti=1:tNum 36 | tImg = sbjNii.img(:,:,:,ti); 37 | dataMat(ti,:) = tImg(maskMat); 38 | end 39 | sbjData{si} = dataMat; 40 | end 41 | 42 | 43 | --------------------------------------------------------------------------------