Skip to content
Snippets Groups Projects
Commit 51123396 authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

Fixed up fslmaths module

parent b118c2d0
No related branches found
No related tags found
No related merge requests found
...@@ -9,7 +9,8 @@ for the ``fslmaths`` command-line tool. ...@@ -9,7 +9,8 @@ for the ``fslmaths`` command-line tool.
""" """
import fsl.utils.run as run import fsl.utils.run as run
from . import wrapperutils as wutils
class fslmaths(object): class fslmaths(object):
...@@ -17,142 +18,143 @@ class fslmaths(object): ...@@ -17,142 +18,143 @@ class fslmaths(object):
def __init__(self, input): def __init__(self, input):
"""Constructor.""" """Constructor."""
self.inputImage = input self.__input = input
self.outputImage = None self.__args = []
self.operations = []
def abs(self): def abs(self):
"""Absolute value.""" """Absolute value."""
self.operations.append("-abs") self.__args.append("-abs")
return self return self
def bin(self): def bin(self):
"""Use (current image>0) to binarise.""" """Use (current image>0) to binarise."""
self.operations.append("-bin") self.__args.append("-bin")
return self return self
def binv(self): def binv(self):
"""Binarise and invert (binarisation and logical inversion).""" """Binarise and invert (binarisation and logical inversion)."""
self.operations.append("-binv") self.__args.append("-binv")
return self return self
def recip(self): def recip(self):
"""Reciprocal (1/current image).""" """Reciprocal (1/current image)."""
self.operations.append("-recip") self.__args.append("-recip")
return self return self
def Tmean(self): def Tmean(self):
"""Mean across time.""" """Mean across time."""
self.operations.append("-Tmean") self.__args.append("-Tmean")
return self return self
def Tstd(self): def Tstd(self):
"""Standard deviation across time.""" """Standard deviation across time."""
self.operations.append("-Tstd") self.__args.append("-Tstd")
return self return self
def Tmin(self): def Tmin(self):
"""Min across time.""" """Min across time."""
self.operations.append("-Tmin") self.__args.append("-Tmin")
return self return self
def Tmax(self): def Tmax(self):
"""Max across time.""" """Max across time."""
self.operations.append("-Tmax") self.__args.append("-Tmax")
return self return self
def fillh(self): def fillh(self):
"""fill holes in a binary mask (holes are internal - i.e. do not touch """fill holes in a binary mask (holes are internal - i.e. do not touch
the edge of the FOV).""" the edge of the FOV)."""
self.operations.append("-fillh") self.__args.append("-fillh")
return self return self
def ero(self, repeat=1): def ero(self, repeat=1):
"""Erode by zeroing non-zero voxels when zero voxels in kernel.""" """Erode by zeroing non-zero voxels when zero voxels in kernel."""
for i in range(repeat): for i in range(repeat):
self.operations.append("-ero") self.__args.append("-ero")
return self return self
def dilM(self, repeat=1): def dilM(self, repeat=1):
"""Mean Dilation of non-zero voxels.""" """Mean Dilation of non-zero voxels."""
for i in range(repeat): for i in range(repeat):
self.operations.append("-dilM") self.__args.append("-dilM")
return self return self
def dilF(self, repeat=1): def dilF(self, repeat=1):
"""Maximum filtering of all voxels.""" """Maximum filtering of all voxels."""
for i in range(repeat): for i in range(repeat):
self.operations.append("-dilF") self.__args.append("-dilF")
return self return self
def add(self, input): def add(self, image):
"""Add input to current image.""" """Add input to current image."""
self.operations.append("-add {0}".format(input)) self.__args.extend(("-add", image))
return self return self
def sub(self, input): def sub(self, image):
"""Subtract input from current image.""" """Subtract image from current image."""
self.operations.append("-sub {0}".format(input)) self.__args.extend(("-sub", image))
return self return self
def mul(self, input): def mul(self, image):
"""Multiply current image by input.""" """Multiply current image by image."""
self.operations.append("-mul {0}".format(input)) self.__args.extend(("-mul", image))
return self return self
def div(self, input): def div(self, image):
"""Divide current image by input.""" """Divide current image by image."""
self.operations.append("-div {0}".format(input)) self.__args.extend(("-div", image))
return self return self
def mas(self, image): def mas(self, image):
"""Use image (>0) to mask current image.""" """Use image (>0) to mask current image."""
self.operations.append("-mas {0}".format(image)) self.__args.extend(("-mas", image))
return self return self
def rem(self, input): def rem(self, image):
"""Divide current image by following input and take remainder.""" """Divide current image by following image and take remainder."""
self.operations.append("-rem {0}".format(input)) self.__args.extend(("-rem", image))
return self return self
def thr(self, input): def thr(self, image):
"""use input number to threshold current image (zero < input).""" """use image number to threshold current image (zero < image)."""
self.operations.append("-thr {0}".format(input)) self.__args.extend(("-thr", image))
return self return self
def uthr(self, input): def uthr(self, image):
"""use input number to upper-threshold current image (zero """use image number to upper-threshold current image (zero
anything above the number).""" anything above the number)."""
self.operations.append("-uthr {0}".format(input)) self.__args.extend(("-uthr", image))
return self return self
def inm(self, input): def inm(self, image):
"""Intensity normalisation (per 3D volume mean)""" """Intensity normalisation (per 3D volume mean)"""
self.operations.append("-inm {0}".format(input)) self.__args.extend(("-inm", image))
return self return self
def bptf(self, hp_sigma, lp_sigma): def bptf(self, hp_sigma, lp_sigma):
"""Bandpass temporal filtering; nonlinear highpass and Gaussian linear """Bandpass temporal filtering; nonlinear highpass and Gaussian linear
lowpass (with sigmas in volumes, not seconds); set either sigma<0 to lowpass (with sigmas in volumes, not seconds); set either sigma<0 to
skip that filter.""" skip that filter."""
self.operations.append("-bptf {0} {1}".format(hp_sigma, lp_sigma)) self.__args.extend(("-bptf", hp_sigma, lp_sigma))
return self return self
def toString(self):
"""Generate fslmaths command string."""
cmd = "fslmaths {0} ".format(self.inputImage)
for oper in self.operations:
cmd = cmd + oper + " "
cmd = cmd + self.outputImage
return cmd
def run(self, output=None): def run(self, output=None):
"""Save output of operations to image.""" """Save output of operations to image."""
if output:
self.outputImage = output
else:
self.outputImage = self.inputImage
run.runfsl(self.toString()) cmd = ['fslmaths', self.__input] + self.__args
if output is None: cmd += [wutils.LOAD]
else: cmd += [output]
result = self.__run(*cmd)
# if output is LOADed, there
# will only be one entry in
# the result dict.
if output is None: return list(result.values())[0]
else: return output
return self.outputImage @wutils.fileOrImage()
def __run(self, *cmd):
"""Run the given ``fslmaths`` command. """
return run.runfsl([str(c) for c in cmd])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment