WTCN-computational-anatomy-group / shape-toolbox

Routines for diffeomorphic shape modelling
GNU General Public License v3.0
4 stars 4 forks source link

Diffeomorphic shape modelling

This software implements a statistical shape model based on diffeomorphic transforms.

Learning shape parameters – i.e., a mean shape (or template) and a principal space of deformation – from a series of 2D or 3D images is cast as a maximum a posterori inference problem in a graphical model.

This repository contains core functions and executable scripts written in Matlab.

Standalone release

TODO - there's no such release yet

We provided compiled command line routines that can be used without Matlab license. To use them, you must:

Basic use cases

The illustrative JSON files are commented. However, comments are not allowed in JSON, so they should be removed from any real file.

Learn a shape model from grey and white matter segmentations obtained with SPM's New Segment

input.json

{"f": [["rc1_sub001.nii","rc2_sub001.nii"],     // Grey and white classes for the first subject
       ["rc1_sub002.nii","rc2_sub002.nii"],
       ["rc1_sub003.nii","rc2_sub003.nii"],
       ...
       ["rc1_sub100.nii","rc2_sub100.nii"]]}    // Grey and white classes for the last subject

option.json

{"dir":   {"model": "~/my_shape_model/",        // Write all (model and subject) data in the same folder
           "dat":   "~/my_shape_model/"},
 "model": {"name": "categorical",               // Observed images are segmentations
           "nc":   3},                          // There are 3 classes, even though only 2 were provided.
                                                //   This allows the third class to be automatically created
                                                //   by "filling probabilities up to one"
 "pg":    {"K": 32},                            // Use 32 principal components
 "par":   {"subjects": "for"}                   // Do not use parallelisation
 }

Command line

./run_shape_train /path/to/matlab/runtime input.json option.json

Apply a shape model to grey and white matter segmentations obtained with SPM's New Segment

input.json

{"f": [["rc1_sub101.nii","rc2_sub101.nii"],     // Grey and white classes for the first subject
       ["rc1_sub102.nii","rc2_sub102.nii"],
       ["rc1_sub103.nii","rc2_sub103.nii"],
       ...
       ["rc1_sub200.nii","rc2_sub200.nii"]],    // Grey and white classes for the last subject
 "w": "~/my_shape_model/subspace.nii",          // Learnt principal subspace
 "a": "~/my_shape_model/log_template.nii"}      // Learnt log-template

option.json

{"dir":   {"model": "~/normalised_images/",     // Write all (model and subject) data in the same folder
           "dat":   "~/normalised_images/"},
 "model": {"name": "categorical",               // Observed images are segmentations
           "nc":   3},                          // There are 3 classes, even though only 2 were provided.
                                                //   This allows the third class to be automatically created
                                                //   by "filling probabilities up to one"
 "pg":    {"K": 32},                            // Use 32 principal components
 "z":     {"A0": [0.0016, 0.0030, ...]},        // Diagonal elements of the latent precision matrix
 "r":     {"l0": 18.8378},                      // Residual precision
 "par":   {"subjects": "for"}                   // Do not use parallelisation
 }

Command line

./run_shape_fit /path/to/matlab/runtime input.json option.json

The model

This work relies on a generative model of shape in which individual images (of brains, in particular) are assumed to be generated from a mean shape – commonly named template – deformed according to a transformation of the coordinate space. Here, these transformations are diffeomorphisms, i.e., one-to-one invertible mappings that allow for very large deformations. By using the geodesic shooting framework, we parameterise these transformations by their initial velocity, which can be seen as an infinitesimal (very small) deformation. The a posteriori covariance structure of these velocity fields is inferred by making use of a technique related to the well-known principal component analysis, adapted to the particular structure of the space on which lie velocity fields, called a Riemannian manifold. Our model also includes a rigid-body transform, whose role is to factor out all deformations induces by brains misalignment.

Inputs can be binary or categorical images (i.e., segmentations), in which case a Bernoulli or Categorical data term is used, or multimodal intensity images, in which case a uniform Gaussian or Laplace noise data term is used.

All considered, the following variables are infered:

The following parameters are manually set and impact the model's behaviour:

Algorithm

This algorithm was described in a MICCAI 2018 proceeding:

The current implementation, however, differs significantly:

Noise models

Three noise models are implemented:

User documentation

Input format

If you use compiled routines or their Matlab equivalents (shape_train, shape_fit), inputs should be JSON files containing dictionaries. These routines wrap around the main script (shape_model).

If you directly use the main scripts in Matlab, inputs are structures.

Regarding of the above choice, there are one mandatory input and one optional one:

Options

Options will be provided in their Matlab structure form. Their JSON equivalent are dictionaries whose keys are the structure field names.

Model

model.name     - Generative data model ('normal'/'categorical'/'bernoulli') - ['normal']
model.sigma2   - If normal model: initial noise variance estimate           - [1]
model.nc       - [categorical only] Number of classes                       - [from input]
pg.K           - Number of principal geodesics                              - [32]
pg.prm         - Parameters of the geodesic operator                        - [1e-4 1e-3 0.2 0.05 0.2]
pg.bnd         - Boundary conditions for the geodesic operator              - [0 = circulant]
pg.geod        - Additional geodesic prior on velocity fields               - [true]
tpl.vs         - Lattice voxel size                                         - [auto]
tpl.lat        - Lattice dimensions                                         - [auto]
tpl.prm        - Parameters of the field operator                           - [1e-3  1e-1 0] (cat/ber)
                                                                              [0     0    0] (normal)
tpl.bnd        - Boundary conditions                                        - [1 = mirror]
tpl.itrp       - Interpolation order                                        - [1]
v.l0           - Prior expected anatomical noise precision                  - [17]
v.n0           - Prior DF of the anatomical noise precision                 - [10]
z.init         - Latent initialisation mode ('auto'/'zero'/'rand')          - ['auto']
z.A0           - Prior expected latent precision matrix                     - [eye(K)]
z.n0           - Prior DF of the latent precision matrix                    - [K]
q.A0           - Prior expected affine precision matrix                     - [eye(M)]
q.n0           - Prior DF of the affine precision matrix                    - [M]
q.B            - Affine_basis                                               - ['rigid']
q.hapx         - Approximate affine hessian                                 - [true]
f.M            - Force same voxel-to-world to all images                    - [read from file]

Optimise parameters

optimise.pg.w       - Optimise subspace                                     - [true]
optimise.z.z        - Optimise latent coordinates                           - [true]
optimise.z.A        - Optimise latent precision                             - [true]
optimise.q.q        - Optimise affine coordinates                           - [true]
optimise.q.A        - Optimise affine precision                             - [true]
optimise.v.v        - Optimise velocity fields                              - [true]
optimise.v.l        - Optimise residual precision                           - [true]
optimise.tpl.a      - Optimise template                                     - [true]

Processing

iter.em        - Maximum number of EM iterations                            - [1000]
iter.gn        - Maximum number of Gauss-Newton iterations                  - [1]
iter.ls        - Maximum number of line search iterations                   - [6]
iter.itg       - Number of integration steps for geodesic shooting          - [auto]
iter.pena      - Penalise Gauss-Newton failures                             - [true]
lb.threshold   - Convergence criterion (lower bound gain)                   - [1e-5]
lb.moving      - Moving average over LB gain                                - [3]
par.subjects   - How to parallelise subjects (see distribute_default)       - [no parallelisation]
ui.verbose     - Talk during processing                                     - [true]
ui.debug       - Further debugging talk                                     - [false]
ui.fig_pop     - Plot lower bound                                           - [true]
ui.fig_sub     - Plot a few subjects                                        - [true]

I/O

dir.model      - Directory where to store model arrays and workspace       - ['.']
dir.dat        - Directory where to store data array                       - [next to input]
fnames.result  - Filename for the result environment saved after each EM   - ['shape_model.mat']
                 iteration
fnames.model   - Structure of filenames for all file arrays
fnames.dat     - Structure of filenames for all file arrays
ondisk.model   - Structure of logical for temporary array                  - [default_ondisk]
ondisk.dat     - "      "       "       "       "       "

Content of the repository

Dependencies

This project has strong dependencies to SPM12 and its Shoot toolbox. Both of them should be added to Matlab's path. SPM can be downloaded at www.fil.ion.ucl.ac.uk/spm.

Core functions also depend on our auxiliary-functions toolbox, which gathers lots of low-level functions.

Furthermore, executable scripts depend on our distributed-computing toolbox, which helps parallelising parts of the code either on the local workstation (using Matlab's parallel processing toolbox) or on a computing cluster (see the toolbox help file for use cases and limitations).

Note that if these toolboxes are all located in the same folder, i.e.:

a call to setpath.m adds all necessary folders to Matlab's path.

How to compile

If no executable is provided for a given platform and/or runtime version, it is possible to compile one yourself using the script shape_compile.m. You will need:

Then, all you need is to run:

shape_compile('/output/directory')

which will generate the files:

Contributors

This software was developed under the Human Brain Project (SP2) flagship by John Ashburner's Computational Anatomy Group at the Wellcome Centre for Human Neuroimaging in UCL.

If you encounter any difficulty, please send an email to y.balbastre or j.ashburner at ucl.ac.uk

License

This software is released under the GNU General Public License version 3 (GPL v3). As a result, you may copy, distribute and modify the software as long as you track changes/dates in source files. Any modifications to or software including (via compiler) GPL-licensed code must also be made available under the GPL along with build & install instructions.

TL;DR: GPL v3