List interface for FMRI images

FMRI images, and their transforms in particular, can be more simply dealt with with a list interface.

#!/bin/env python

import numpy as N

from neuroimaging.modalities.fmri.api import fromimage
from neuroimaging.core.api import load_image, Image
from neuroimaging.core.reference.api import SamplingGrid, Affine

from neuroimaging.testing import funcfile

# from algorithms.register import register -- this doesn't exist yet

# This is a generic 4D Image, with standard Image behaviour
fourd_image = load_image(funcfile)
# This is an image series as list type wrapper around
# 4D image - to simplify the interface

fmri_lister = fromimage(fourd_image, TR=2.0, slicetimes=None)
print fourd_image.shape
assert fmri_lister.TR == 2.0
try:
    a = fmri_lister.affine
except AttributeError:
    print "OK, it has no affine attribute"
    
print fmri_lister[0].affine
print N.asarray(fmri_lister).shape

fmri_aligned = fmri_lister.emptycopy()

## You might want to dump some frames
fmri_lister = fmri_lister[1:14:2]
print fmri_lister, len(fmri_lister.list)

## # I can get the iterable non-specialized (no TR) thing with
## my_iterable = fmri_lister.list
## # Which makes us all happy.  At last

# List syntax allows setting of transforms


def register(moving=None, static=None):
    """
    This function returns a world-to-world affine transformation between 
    moving.grid.output_coords and static.grid.output_coords)
    """ 
    return N.identity(4)

target_img = fmri_lister[0]
for img in fmri_lister:
    t = register(moving=img, static=target_img)
##    aligned_img = img.copy(data=False)
    newt = N.dot(t, img.affine)
    newg = SamplingGrid(Affine(newt), img.grid.input_coords, target_img.grid.output_coords)
    aligned_img = Image(N.asarray(img), newg)
    fmri_aligned.list.append(aligned_img)

print len(fmri_aligned.list)