From 62b151c71d1f04e5488aee2350c225b854a4e4dd Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauld.mccarthy@gmail.com>
Date: Tue, 24 Jan 2017 17:07:47 +0000
Subject: [PATCH] New function to transform module to generate a FLIRT matrix.
 Bugfix in melodic labels - failing when trying to load a label file with no
 noise components.

---
 fsl/data/melodiclabels.py |  6 ++++--
 fsl/utils/transform.py    | 30 ++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/fsl/data/melodiclabels.py b/fsl/data/melodiclabels.py
index 1b09858a8..add891dc5 100644
--- a/fsl/data/melodiclabels.py
+++ b/fsl/data/melodiclabels.py
@@ -423,8 +423,10 @@ def loadLabelFile(filename, includeLabel=None, excludeLabel=None):
     else:
 
         melDir     = lines[0]
-        noisyComps = map(int, lines[-1].strip(' []').split(','))
-
+        noisyComps = lines[-1].strip(' []').split(',')
+        noisyComps = [c      for c in noisyComps if c != '']
+        noisyComps = [int(c) for c in noisyComps]
+        
         # The melodic directory path should
         # either be an absolute path, or
         # be specified relative to the location
diff --git a/fsl/utils/transform.py b/fsl/utils/transform.py
index f7bd2f21f..36af95c95 100644
--- a/fsl/utils/transform.py
+++ b/fsl/utils/transform.py
@@ -20,6 +20,7 @@ spaces. The following functions are provided:
    axisAnglesToRotMat
    axisBounds
    flirtMatrixToSform
+   sformToFlirtMatrix
 """
 
 import numpy        as np
@@ -413,3 +414,32 @@ def flirtMatrixToSform(flirtMat, srcImage, refImage):
                   refInvScaledVoxelMat,
                   flirtMat,
                   srcScaledVoxelMat)
+
+
+def sformToFlirtMatrix(srcImage, refImage, srcXform=None):
+    """Under the assumption that the given ``srcImage`` and ``refImage``
+    share a common world coordinate system (defined by their
+    :attr:`voxToWorldMat` attributes), this function will calculate and
+    return a transformation matrix from the ``srcImage`` scaled voxel
+    coordinate system to the ``refImage`` scaled voxel coordinate system,
+    that can be saved to disk and used with FLIRT, to resample the source
+    image to the reference image.
+
+    :arg srcImage: Source :class:`.Image`
+    :arg refImage: Reference :class:`.Image`
+    :arg srcXform: Optionally used in place of the ``scrImage``
+                   :attr:`.voxToWorldMat`
+    """
+
+    srcScaledVoxelsToVoxelsMat = invert(srcImage.voxelsToScaledVoxels())
+    srcVoxToWorldMat           =        srcImage.voxToWorldMat
+    refWorldToVoxMat           = invert(refImage.voxToWorldMat)
+    refVoxelsToScaledVoxelsMat =        refImage.voxelsToScaledVoxels()
+
+    if srcXform is not None:
+        srcVoxToWorldMat = srcXform
+
+    return concat(refVoxelsToScaledVoxelsMat,
+                  refWorldToVoxMat,
+                  srcVoxToWorldMat,
+                  srcScaledVoxelsToVoxelsMat)
-- 
GitLab