Mathematical formulation of the Coordinate Map

Using the CoordinateMap can be a little hard to get used to. For some users, a mathematical description, free of any python syntax and code design and snippets may be helpful. After following through this description, the code design and usage may be clearer.

We return to the normalization example in Use of the Coordinate Map for spatial normalization and try to write it out mathematically. Conceptually, to do normalization, we need to be able to answer each of these three questions:

  1. Voxel-to-world (subject) Given the subjects’ anatomical image read off the scanner: which physical location, expressed in (xs,ys,zs) coordinates (s for subject), corresponds to the voxel of data (is,js,ks)? This question is answered by subject_im.coordmap. The actual function that computes this, i.e that takes 3 floats and returns 3 floats, is subject_im.coordmap.mapping.

  2. World-to-world (subject to Tailarach) Given a location (xs,ys,zs) in an anatomical image of the subject, where does it lie in the Tailarach coordinates (xa,ya,za)? This is answered by the matrix T and knowing that T maps a point in the subject’s world to Tailarach world. Hence, this question is answered by subject_world_to_tailarach_world above.

  3. Voxel-to-world (Tailarach) Since we want to produce a resampled Image that has the same shape and coordinate information as atlas_im, we need to know what location in Tailarach space, (xa,ya,za) (a for atlas) corresponds to the voxel (ia,ja,ka). This question is answered by tailarach_cmap.

Each of these three questions are answered by, in code, what we called a class called CoordinateMap. Mathematically, let’s define a mapping as a tuple (D,R,f) where D is the domain, R is the range and f:DR is a function. It may seem redundant to pair (D,R) with f because a function must surely know its domain and hence, implicitly, its range. However, we will see that when it comes time to implement the notion of mapping, the tuple we do use to construct CoordinateMap is almost, but not quite (D,R,f) and, in the tuple we use, D and R are not redundant.

Since these mappings are going to be used and called with modules like numpy, we should restrict our definition a little bit. We assume the following:

  1. D is isomorphic to one of Zn,Rn,Cn for some n. This isomorphism is determined by a basis [u1,,un] of D which maps ui to ei the canonical i-th coordinate vector of whichever of Zn,Rn,Cn. This isomorphism is denoted by ID. Strictly speaking, if D is isomorphic to Zn then the term basis is possibly misleading because D because it is not a vector space, but it is a group so we might call the basis a set of generators instead. In any case, the implication is that whatever properties the appropriate Z,R,C, so D (and R) has as well.

  2. R is similarly isomorphic to one of Zm,Rm,Cm for some m with isomorphism IR and basis [v1,,vm].

Above, and throughout, the brackets “[“,”]” represent things interpretable as python lists, i.e. sequences.

These isomorphisms are just fancy ways of saying that the point x=3,y=4,z=5 is represented by the 3 real numbers (3,4,5). In this case the basis is [x,y,z] and for any a,b,cR

ID(ax+by+cz)=ae1+be2+ce3

We might call the pairs ([u1,...,un],ID),([v1,...,vm],IR) coordinate systems. Actually, the bases in effect determine the maps ID,IR as long as we know which of Z,R,C we are talking about so in effect, ([u1,...,un],R) could be called a coordinate system. This is how it is implemented in the code with [u1,,un] being replaced by a list of strings naming the basis vectors and R replaced by a builtin numpy.dtype().

In our normalization example, we therefore have 3 mappings:

  1. Voxel-to-world (subject) In standard notation for functions, we can write

    (is,js,ks)f(xs,ys,zs).

    The domain is D=[is,js,ks], the range is R=[xs,ys,zs] and the function is f:DR.

  2. World-to-world (subject to Tailarach) Again, we can write

    (xs,ys,zs)g(xa,ya,za)

    The domain is D=[xs,ys,zs], the range is R=[xa,ya,za] and the function is g:DR.

  3. Voxel-to-world (Tailarach) Again, we can write

    (ia,ja,ka)h(xa,ya,za).

    The domain is D=[ia,ja,ka], the range is R=[xa,ya,za] and the function is h:DR.

Note that each of the functions f,g,h can be, when we know the necessary isomorphisms, thought of as functions from R3 to itself. In fact, that is what we are doing when we write

(ia,ja,ka)h(xa,ya,za)

as a function that takes 3 numbers and gives 3 numbers.

Formally, these functions that take 3 numbers and return 3 numbers can be written as f~=IRfID1. When this is implemented in code, it is actually the functions f~,g~,h~ we specify, rather then f,g,h. The functions f~,g~,h~ have domains and ranges that are just R3. We therefore call a coordinate map a tuple

((uD,R),(uR,R),IRfID1)

where uD,uR are bases for D,R, respectively. It is this object that is implemented in code. There is a simple relationship between mappings and coordinate maps

((uD,R),(uR,R),f~)(D,R,f=IR1f~ID)

Because f~,g~,h~ are just functions from R3 to itself, they can all be composed with one another. But, from our description of the functions above, we know that only certain compositions make sense and others do not, such as gh. Compositions that do make sense include

  1. h1g which (ia,ja,ka) voxel corresponds to the point (xs,ys,zs)?

  2. gf which (xa,ya,za) corresponds to the voxel (i,j,k)?

The composition that is used in the normalization example is w=f1g1h which is a function

(ia,ja,ka)w(is,js,ks)

This function, or more correctly its representation w~ that takes 3 floats to 3 floats, is passed directly to scipy.ndimage.map_coordinates().

Manipulating mappings, coordinate systems and coordinate maps

In order to solve our normalization problem, we will definitely need to compose functions. We may want to carry out other formal operations as well. Before describing operations on mappings, we describe the operations you might want to consider on coordinate systems.

Coordinate systems

  1. Reorder: This is just a reordering of the basis, i.e. ([u1,u2,u3],R)([u2,u3,u1],R)

  2. Product: Topological product of the coordinate systems (with a small twist). Given two coordinate systems ([u1,u2,u3],R),([v1,v2],Z) the product is represented as

    ([u1,u2,u3],R)×([v1,v2],Z)([u1,u2,u3,v1,v2],R).

    Note that the resulting coordinate system is real valued whereas one of the input coordinate systems was integer valued. We can always embed Z into R. If one of them is complex valued, the resulting coordinate system is complex valued. In the code, this is handled by attempting to find a safe builtin numpy.dtype for the two (or more) given coordinate systems.

Mappings

  1. Inverse: Given a mapping M=(D,R,f) if the function f is invertible, this is just the obvious M1=(R,D,f1).

  2. Composition: Given two mappings, Mf=(Df,Rf,f) and Mg=(Dg,Rg,g) if Df==Rg then the composition is well defined and the composition of the mappings [Mf,Mg] is just (Dg,Rf,fg).

  3. Reorder domain / range: Given a mapping M=(D=[i,j,k],R=[x,y,z],f) you might want to specify that we’ve changed the domain by changing the ordering of its basis to [k,i,j]. Call the new domain D. This is represented by the composition of the mappings [M,O] where O=(D,D,ID1fOID) and for a,b,cR:

    fO(a,b,c)=(b,c,a).
  4. Linearize: Possibly less used, since we know that f must map one of Zn,Rn,Cn to one of Zm,Rm,Cm, we might be able differentiate it at a point pD, yielding its 1st order Taylor approximation

    fp(d)=f(d)+Dfp(dp)

    which is an affine function, thus creating an affine mapping (D,R,fp). Affine functions are discussed in more detail below.

  5. Product: Given two mappings M1=(D1,R1,f1),M2=(D2,R2,f2) we define their product as the mapping (D1+D2,R1+R2,f1f2) where

    (f1f2)(d1,d2)=(f1(d1),f2(d2)).

    Above, we have taken the liberty of expressing the product of the coordinate systems, say, D1=([u1,,un],R),D2=([v1,,vm],C) as a python addition of lists.

    The name product for this operation is not necessarily canonical. If the two coordinate systems are vector spaces and the function is linear, then we might call this map the direct sum because its domain are direct sums of vector spaces. The term product here refers to the fact that the domain and range are true topological products.

Affine mappings

An affine mapping is one in which the function f:DR is an affine function. That is, it can be written as f(d) = Ad + b for dD for some nR×nD matrix A with entries that are in one of Z,R,C.

Strictly speaking, this is a little abuse of notation because d is a point in D not a tuple of real (or integer or complex) numbers. The matrix A represents a linear transformation from D to R in a particular choice of bases for D and R.

Let us revisit some of the operations on a mapping as applied to affine mappings which we write as a tuple M=(D,R,T) with T the representation of the (A,b) in homogeneous coordinates.

  1. Inverse: If T is invertible, this is just the tuple M1=(R,D,T1).

  2. Composition: The composition of two affine mappings [(D2,R2,T2),(D1,R1,T1)] is defined whenever R1==D2 and is the tuple (D1,R2,T2T1).

  3. Reorder domain: A reordering of the domain of an affine mapping M=(D,R,T) can be represented by a (nD+1)×(nD+1) permutation matrix P (in which the last coordinate is unchanged – remember we are in homogeneous coordinates). Hence a reordering of D to D can be represented as (D,R,TP). Alternatively, it is the composition of the affine mappings [M,(D~,D,P)].

  4. Reorder range: A reordering of the range can be represented by a (nR+1)×(nR+1) permutation matrix P~. Hence a reordering of R to R can be represented as (D,R~,P~T). Alternatively, it is the composition of the affine mappings [(R,R~,P~),M].

  5. Linearize: Because the mapping M=(D,R,T) is already affine, this leaves it unchanged.

  6. Product: Given two affine mappings M1=(D1,R1,T1) and M2=(D2,R2,T2) the product is the tuple

    (D1+D2,R1+R2,(T100T2)).

3-dimensional affine mappings

For an Image, by far the most common mappings associated to it are affine, and these are usually maps from a real 3-dimensional domain to a real 3-dimensional range. These can be represented by the ubiquitous 4×4 matrix (the representation of the affine mapping in homogeneous coordinates), along with choices for the axes, i.e. [i,j,k] and the spatial coordinates, i.e. [x,y,z].

We will revisit some of the operations on mappings as applied specifically to 3-dimensional affine mappings which we write as a tuple A=(D,R,T) where T is an invertible 4×4 transformation matrix with real entries.

  1. Inverse: Because we have assumed that T is invertible this is just tuple (([x,y,z],R),([i,j,k],R),T1).

  2. Composition: Given two 3-dimensional affine mappings M1=(D1,R1,T1),M2=(D2,R2,T2) the composition of [M2,M1] yields another 3-dimensional affine mapping whenever R1==D2. That is, it yields (D1,R2,T2T1).

  3. Reorder domain A reordering of the domain can be represented by a 4×4 permutation matrix P (with its last coordinate not changing). Hence the reordering of D=([i,j,k],R) to ([k,i,j],R) can be represented as (([k,i,j],R),R,TP).

  4. Reorder range: A reordering of the range can also be represented by a 4×4 permutation matrix P~ (with its last coordinate not changing). Hence the reordering of R=([x,y,z],R) to ([z,x,y],R) can be represented as (D,([z,x,y],R),P~,T).

  5. Linearize: Just as for a general affine mapping, this does nothing.

  6. Product: Because we are dealing with only 3-dimensional mappings here, it is impossible to use the product because that would give a mapping between spaces of dimension higher than 3.

Coordinate maps

As noted above coordinate maps are equivalent to mappings through the bijection

((uD,R),(uR,R),f~)(D,R,IR1f~ID)

So, any manipulations on mappings, affine mappings or 3-dimensional affine mappings can be carried out on coordinate maps, affine coordinate maps or 3-dimensional affine coordinate maps.

Implementation

Going from this mathematical description to code is fairly straightforward.

  1. A coordinate system is implemented by the class CoordinateSystem in the module nipy.core.reference.coordinate_system. Its constructor takes a list of names, naming the basis vectors of the coordinate system and an optional built-in numpy scalar dtype such as np.float32. It has no interesting methods of any kind. But there is a module level function product which implements the notion of the product of coordinate systems.

  2. A coordinate map is implemented by the class CoordinateMap in the module nipy.core.reference.coordinate_map. Its constructor takes two coordinate has a signature (mapping, input_coords(=domain), output_coords(=range)) along with an optional argument inverse_mapping specifying the inverse of mapping. This is a slightly different order from the (D,R,f) order of this document. As noted above, the tuple (D,R,f) has some redundancy because the function f must know its domain, and, implicitly its range. In numpy, it is impractical to really pass f to the constructor because f would expect something of dtype D and should return something of dtype R. Therefore, mapping is actually a callable that represents the function f~=IRfID1. Of course, the function f can be recovered as f = I_R^{-1} circ tilde{f} I_D`. In code, f is roughly equivalent to:

    >>> from nipy.core.api import CoordinateMap, CoordinateSystem
    >>> in_cs = CoordinateSystem('ijk', 'voxels')
    >>> out_cs = CoordinateSystem('xyz', 'mm')
    >>> map = lambda x : x + 1
    >>> coordmap = CoordinateMap(in_cs, out_cs, map)
    >>> domain = coordmap.function_domain
    >>> range = coordmap.function_range
    >>> f_tilde = coordmap.function
    >>> in_dtype = domain.coord_dtype
    >>> out_dtype = range.dtype
    
    >>> def f(d):
    ...    return f_tilde(d.view(in_dtype)).view(out_dtype)
    

The class CoordinateMap has an inverse property and there are module level functions called product, compose, linearize and it has methods reordered_input, reordered_output.

For more detail on the ideas behind the coordmap design, see Some discussion notes on coordinate maps