SPM/Programming intro
The SPM Wikibooks moved to https://www.fil.ion.ucl.ac.uk/spm/docs/ |
Aim
editThis page is intended to provide a quick-start guide to writing your own MATLAB scripts and functions using SPM as a library. SPM programming can mean simply writing batch scripts to automate common pipelines, writing short helper scripts or functions to accomplish useful tasks, writing your own SPM extensions, or even modifying your local installation of SPM.
MATLAB
editMATLAB is a programming language developed by MathWorks.
- MATLAB and MathWorks on Wikipedia
- MATLAB on WikiBooks
Many MATLAB tutorials are available online:
There are also useful books:
MATLAB courses:
SPM functions
editIntroduction
editSPM has extensive developer documentation in the headers of the source files. To view the documentation of a function either open the corresponding source file or call help function
from the MATLAB command line. Make sure that the SPM folder is in MATLABs search path.
Reading image headers and data
editspm_vol
– header informationspm_read_vols
– for reading entire volumes (see also:spm_vol
)spm_slice_vol
– for arbitrary planesspm_sample_vol
– any voxelsspm_get_data
(spm_sample_vol
) – any voxels from multiple volumesspm_bsplins
(spm_bsplinc
) – NBspm_slice_vol
andspm_sample_vol
offer polynomial or sinc interpolation; these functions provide b-spline interp as used inspm_reslice
Writing data
editspm_write_vol
(spm_vol
)spm_create_vol
spm_write_plane
Reading and writing data with the alternative NIfTI class library
editnifti
(@nifti/Contents
,@nifti/create
)file_array
(@file_array/Contents
)
Geometry, voxel-world mappings
editspm_get_space
– get the voxel-world mapping matrix (a rigid or affine transform, in homogeneous coordinates)spm_imatrix
– convert above matrix to parametrised formspm_matrix
– convert parameter vector to affine matrixspm_check_orientations
Linear (rigid/affine) registration and reslicing
editspm_realign
spm_coreg
(spm_reslice
)spm_affreg
spm_reslice
– needs reference image; see following for reslicing to specified geometryreorient
(resize_img
) – available from John's Gems 2 and 3
Preprocessing, including segmentation and non-linear normalisation
editspm_preproc
(spm_config_preproc
,spm_prep2sn
,spm_preproc_write
) – SPM5's unified segmentation and normalisationspm_normalise
– the old pre-SPM5 non-unified spatial normalisationspm_segment
– the old pre-SPM5 non-unified tissue segmentationspm_smooth
Processing
editspm_imcalc
– perform arbitrary calculations on volumes (low level function)spm_imcalc_ui
– high level convenience wrapper forspm_imcalc
Statistics
editspm_ancova
(spm_reml_ancova
) – unused by SPM itself, but often useful for scripting or educational purposes
Viewing data
editspm_check_registration
(spm_image
,spm_orthviews
) – the ubiquitous three orthogonal viewsslover
– slices through images, overlays of thresholded or raw statistics; see also slice_overlayspm_mip_ui
(spm_mip
) – maximum intensity projections or glass brain images
Configuration
editspm_defaults
spm_get_defaults
Batch System
editspm_select
spm_jobman
Illustrative examples
editReading and writing a volume (to replace NaNs with zeros)
editA simpler (but more memory-hungry) version of an old Gem. See the SPM8 version of gem for an example of plane-wise reading and writing.
fnm = spm_select(1, 'image'); [pth, bnm, ext] = spm_fileparts(fnm); VI = spm_vol(fnm); VO = VI; % copy input info for output image VO.fname = fullfile(pth, [bnm '_zn' ext]); img = spm_read_vols(VI); img(isnan(img)) = 0; % use ~isfinite instead of isnan to replace +/-inf with zero spm_write_vol(VO,img);
External links
editSee also: