From df1135e488e9d21dcc0e3ce7e2459760f04f11c7 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauld.mccarthy@gmail.com> Date: Tue, 29 Nov 2016 10:37:34 +0000 Subject: [PATCH] fsl.data.model module has a couple of convenience functions for trying to find identifying reference image associated with a FIRST vtk file. --- fsl/data/model.py | 37 +++++++++++++++++++++++++++++++++++++ fsl/utils/async.py | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/fsl/data/model.py b/fsl/data/model.py index ededc41a5..13b3510e6 100644 --- a/fsl/data/model.py +++ b/fsl/data/model.py @@ -23,6 +23,8 @@ import numpy as np import six +from . import image as fslimage + log = logging.getLogger(__name__) @@ -149,3 +151,38 @@ def loadVTKPolydataFile(infile): indexOffset += polygonLengths[i] return vertices, polygonLengths, indices + + +def getFIRSTPrefix(modelfile): + """If the given ``vtk`` file was generated by `FIRST + <https://fsl.fmrib.ox.ac.uk/fsl/fslwiki/FIRST>`_, this function + will return the file prefix. Otherwise an ``Exception`` will be + raised. + """ + + if not modelfile.endswith('first.vtk'): + raise ValueError('Not a first vtk file: {}'.format(modelfile)) + + modelfile = op.basename(modelfile) + prefix = modelfile.split('-') + prefix = '-'.join(prefix[:-1]) + + return prefix + + +def findReferenceImage(modelfile): + """Given a ``vtk`` file, attempts to find a corresponding ``NIFTI`` + image file. Return the path to the image, or ``None`` if no image was + found. + + Currently this function will only return an image for ``vtk`` files + generated by `FIRST <https://fsl.fmrib.ox.ac.uk/fsl/fslwiki/FIRST>`_. + """ + + try: + + prefix = getFIRSTPrefix(modelfile) + return fslimage.addExt(prefix, mustExist=True) + + except: + return None diff --git a/fsl/utils/async.py b/fsl/utils/async.py index 4b835e28e..4b403d794 100644 --- a/fsl/utils/async.py +++ b/fsl/utils/async.py @@ -372,7 +372,7 @@ def idle(task, *args, **kwargs): .. note:: You will run into difficulties if you schedule a function that expects/accepts its own keyword arguments called ``name``, - ``skipIfQueued``, ``after``, or ``timeout``. + ``skipIfQueued``, ``after``, ``timeout``, or ``alwaysQueue``. """ global _idleRegistered -- GitLab