Exemple #1
0

@pluto_class
class ToWorldNode(FlowNode):
    pins = [
        FlowPin('In', FlowPin.In),
        FlowPin('Out', FlowPin.Out)
    ]

    properties = [
        IntProperty('order', 1)
    ]

    def __init__(self):
        super(ToWorldNode, self).__init__()
        self.node_class = 'image.resample.ToWorldNode'
        self.title = 'ToWorld'
        self.category = 'Image/Resample'
        self.ui_class = 'one_to_one_node'

    def run(self, ctx):
        img = ctx.read_pin('In')
        if hasattr(img, 'spacing'):
            out = Image(zoom(img, img.spacing[::-1], order=self.order), img.pixel_type)
            out.spacing = [1]*len(img.spacing)
            ctx.write_pin('Out', out)
        else:
            ctx.write_pin('Out', img)

install_node_template(ToWorldNode())
Exemple #2
0
    def run(self, ctx):
        img = ctx.read_pin('In')
        if img is None or not isinstance(img, np.ndarray):
            raise ValueError('Expected an Image object')

        ctx.write_pin('Out', img.shape)


@pluto_class
class DTypeNode(FlowNode):
    pins = [FlowPin('In', FlowPin.In), FlowPin('Out', FlowPin.Out)]

    def __init__(self):
        super(DTypeNode, self).__init__()
        self.node_class = 'image.info.DTypeNode'
        self.title = 'DType'
        self.category = 'Image/Info'
        self.ui_class = 'one_to_one_node'

    def run(self, ctx):
        img = ctx.read_pin('In')
        if img is None or not isinstance(img, np.ndarray):
            raise ValueError('Expected an Image object')

        ctx.write_pin('Out', str(img.dtype))


install_node_template(ImageInfoNode())
install_node_template(ShapeNode())
install_node_template(DTypeNode())
Exemple #3
0
        shape = eval(self.shape)
        ctx.write_pin('Out', Image(np.ones(shape)))


@pluto_class
class ZerosNode(FlowNode):
    pins = [FlowPin('Out', FlowPin.Out)]
    properties = [
        StringProperty('shape', '(0, 0)'),
    ]

    def __init__(self):
        super(ZerosNode, self).__init__()
        self.node_class = 'image.new.ZerosNode'
        self.title = 'Zeros'
        self.category = 'Image/New'
        self.ui_class = 'constant_node'
        self.ui_node_title_var = 'shape'

    def run(self, ctx):
        if re.match('\((\s*\d+\s*,?\s*)*\)$', self.shape.strip()) is None:
            raise ValueError('Invalid shape specifier: %s' % self.shape)

        shape = eval(self.shape)
        ctx.write_pin('Out', Image(np.zeros(shape)))


install_node_template(OnesNode())
install_node_template(ZerosNode())
Exemple #4
0
 def dec(fn):
     flow.install_node_template(
         ContextFunctionNode(fn=fn, title=title, category=category))
     return fn
Exemple #5
0
def register_node(fn, title, category):
    flow.install_node_template(NumpyNode(fn, title, category))
Exemple #6
0
            fixed_mask = os.path.join(temp_dir, 'fmask.mhd')

        if moving_mask == image.Image:
            medkit.write(moving_mask, os.path.join(temp_dir, 'mmask.mhd'))
            moving_mask = os.path.join(temp_dir, 'mmask.mhd')

        t = ctx.read_pin('T')
        if t:
            t = list(t)
            for i in range(0, len(t)):
                if type(t[i]) == Parameters:
                    t[i].write(os.path.join(temp_dir, 't'+str(i)+'.txt'))
                    t[i] = os.path.join(temp_dir, 't'+str(i)+'.txt')


        if self.param_file == '':
            raise ValueError('Parameter file not set')

        param_file = os.path.abspath(self.param_file)
        run_elastix(param_file, fixed, moving, temp_dir, t=t, fixed_mask=fixed_mask, moving_mask=moving_mask, fp=fp, mp=mp)

        if self.is_pin_linked('Out'):
            ctx.write_pin('Out', medkit.read(os.path.join(temp_dir, 'result.0.mhd')))

        if self.is_pin_linked('Transform'):
            ctx.write_pin('Transform', Parameters(os.path.join(temp_dir, 'TransformParameters.0.txt')))


install_node_template(ElastixNode())

Exemple #7
0
        self.node_class = 'flow.constant.FloatNode'
        self.title = 'Float'
        self.category = 'Constants'
        self.ui_class = 'constant_node'

    def run(self, ctx):
        ctx.write_pin('Out', self.value)


@pluto_class
class StringNode(FlowNode):
    pins = [FlowPin('Out', FlowPin.Out)]
    properties = [
        StringProperty('value', ''),
    ]

    def __init__(self):
        super(StringNode, self).__init__()
        self.node_class = 'flow.constant.StringNode'
        self.title = 'String'
        self.category = 'Constants'
        self.ui_class = 'constant_node'

    def run(self, ctx):
        ctx.write_pin('Out', self.value)


install_node_template(BoolNode())
install_node_template(IntNode())
install_node_template(FloatNode())
install_node_template(StringNode())
Exemple #8
0
from flow import FlowNode, FlowPin, StringProperty, install_node_template
from pluto import pluto_class

@pluto_class
class VariableNode(FlowNode):
    pins = [
        FlowPin('Out', FlowPin.Out)
    ]
    properties = [
        StringProperty('name', ''),
        StringProperty('default', '')
    ]

    def __init__(self):
        super(VariableNode, self).__init__()
        self.node_class = 'flow.variable.VariableNode'
        self.title = 'Variable'
        self.category = 'Flow'
        self.ui_class = 'variable_node'

    def run(self, ctx):
        if self.name in ctx.env:
            ctx.write_pin('Out', ctx.env[self.name])
        else:
            ctx.write_pin('Out', self.default)

install_node_template(VariableNode())

Exemple #9
0
        self.ui_class = 'single_pin_node'

    def run(self, ctx):
        ctx.write_pin('Out', eval(self.code))



@pluto_class
class FileBrowse(FlowNode):
    pins = [
        FlowPin('Out', FlowPin.Out)
    ]
    properties = [
        FileProperty('file', ''),
    ]

    def __init__(self):
        super(FileBrowse, self).__init__()
        self.node_class = 'flow.util.FileBrowse'
        self.title = 'FileBrowse'
        self.category = 'Flow/Util'
        self.ui_class = 'single_pin_node'

    def run(self, ctx):
        ctx.write_pin('Out', self.file)



install_node_template(FileBrowse())
install_node_template(EvalNode())
Exemple #10
0
    ]

    def __init__(self):
        super(JetNode, self).__init__()
        self.node_class = 'image.colormap.JetNode'
        self.title = 'Jet'
        self.category = 'Image/Colormaps'

    def run(self, ctx):
        img = ctx.read_pin('In')

        if self.is_pin_linked('Min'):
            min = ctx.read_pin('Min')
        else:
            min = img.min()
        if self.is_pin_linked('Max'):
            max = ctx.read_pin('Max')
        else:
            max = img.max()

        out = Image(colorize(img, min, max, 'jet'), PixelType_Vec4u8)
        if hasattr(img, 'spacing'):
            out.spacing = img.spacing
        if hasattr(img, 'origin'):
            out.origin = img.origin
        ctx.write_pin('Out', out)


install_node_template(PerceptuallyUniformNode())
install_node_template(JetNode())
Exemple #11
0

@pluto_class
class WriteNode(FlowNode):
    pins = [FlowPin('Image', FlowPin.In), FlowPin('File', FlowPin.In)]
    properties = [
        FileProperty('file', '', FileProperty.File_Save, "Image (*.*)"),
    ]

    def __init__(self):
        super(WriteNode, self).__init__()
        self.node_class = 'medkit.io.WriteNode'
        self.title = 'Write'
        self.category = 'Image/IO'

    def run(self, ctx):
        f = self.file
        if self.is_pin_linked('File'):
            f = ctx.read_pin('File')

        im = ctx.read_pin('Image')
        if im is not None:
            if os.path.splitext(f)[1] == '.png':
                write(im.astype(np.uint8), f)
            else:
                write(im, f)


install_node_template(ReadNode())
install_node_template(WriteNode())
Exemple #12
0
        settings = {
            'step_size': 0.5,
            'regularization_weight': 0.05,
            'block_size': (12, 12, 12),
            'pyramid_level_min': 0,
            'pyramid_level_max': 6,
        }

        if self.param_file is not '':
            settings.update(parse_settings_old(self.param_file))

        eng = RegistrationEngine('blocked_graph_cut', image.PixelType_Float64,
                                 settings)

        if self.is_pin_linked('ConstraintValues') or self.is_pin_linked(
                'ConstraintMask'):
            eng.set_contraints(ctx.read_pin('ConstraintValues'),
                               ctx.read_pin('ConstraintMask'))

        if self.is_pin_linked('StartingGuess'):
            eng.set_starting_guess(ctx.read_pin('StartingGuess'))

        fixed = ctx.read_pin('Fixed')
        moving = ctx.read_pin('Moving')

        df = eng.execute(fixed, moving)
        ctx.write_pin('Deformation', df)


install_node_template(RegistrationNode())
Exemple #13
0
        IntProperty('time', 0),
    ]

    def __init__(self):
        super(SleepNode, self).__init__()
        self.node_class = 'flow.debug.Sleep'
        self.title = 'Sleep'
        self.category = 'Debug'
        self.value = ''

    def run(self, ctx):
        time.sleep(int(self.time))
        ctx.write_pin('Out', ctx.read_pin('In'))


install_node_template(SleepNode())


@pluto_class
class FailNode(FlowNode):
    pins = [
        FlowPin('In', FlowPin.In),
    ]
    properties = [
        StringProperty('error', ''),
    ]

    def __init__(self):
        super(FailNode, self).__init__()
        self.node_class = 'flow.debug.Fail'
        self.title = 'Fail'
Exemple #14
0
        IntProperty('height', 0),
        IntProperty('step', 1),
        IntProperty('thickness', 1),
    ]

    def __init__(self):
        super(Grid2DNode, self).__init__()
        self.node_class = 'image.grid.Grid2DNode'
        self.title = 'Grid2D'
        self.category = 'Image/New'
        self.ui_class = 'single_pin_node'

    def run(self, ctx):
        h = self.height
        w = self.width
        img_data = np.zeros((h, w)).astype('uint8')

        for y in range(0, int(h / self.step) + 1):
            for x in range(0, w):
                for t in range(0, min(self.thickness, h - y * self.step - 1)):
                    img_data[y * self.step + t, x] = 1
        for x in range(0, int(w / self.step) + 1):
            for y in range(0, h):
                for t in range(0, min(self.thickness, w - x * self.step - 1)):
                    img_data[y, x * self.step + t] = 1

        ctx.write_pin('Out', Image(img_data, PixelType_UInt8))


install_node_template(Grid2DNode())
Exemple #15
0
import flow
from pluto import pluto_class

@pluto_class
class PrintNode(flow.Node):
    pins = [
        flow.Pin('In', flow.Pin.In)
    ]

    def __init__(self):
        super(PrintNode, self).__init__()
        self.node_class = 'flow.Print'
        self.title = 'Print'
        self.category = 'Flow'
        self.value = ''
        self.ui_class = 'print_node'

    def run(self, ctx):
        self.value = str(ctx.read_pin('In'))
        print(self.value)


flow.install_node_template(PrintNode())

Exemple #16
0
@pluto_class
class IndexNode(FlowNode):
    pins = [FlowPin('In', FlowPin.In), FlowPin('Out', FlowPin.Out)]
    properties = [
        StringProperty('index', '0'),
    ]

    def __init__(self):
        super(IndexNode, self).__init__()
        self.node_class = 'flow.list.IndexNode'
        self.title = 'Slice'
        self.category = 'List'
        self.ui_class = 'one_to_one_node'
        self.ui_node_title_var = 'index'

    def run(self, ctx):
        lst = ctx.read_pin('In')

        # Validate index
        index = self.index.strip()
        if not re.match('[\-0-9:]+$', index):
            raise ValueError('Invalid index')

        ctx.write_pin('Out', eval('lst[%s]' % index))


install_node_template(BuildNode())
install_node_template(ConcatenateNode())
install_node_template(IndexNode())
Exemple #17
0
    def run(self, ctx):
        img = ctx.read_pin('In')
        if img is None or not isinstance(img, np.ndarray):
            raise ValueError('Expected an Image object')

        value = ctx.read_pin('Value')

        # Validate index
        index = ''
        tokens = self.slice.split(',')
        for i in range(0, len(tokens)):
            if i != len(tokens)-1 and tokens[i] == '': # Allow a trailing ','
                index = index + ','
                continue
            if re.match('[\-0-9:]+$', tokens[i].strip()):
                index = index + tokens[i].strip()
                if i != len(tokens)-1:
                    index = index + ','
            else:
                raise SyntaxError('Invalid syntax: %s' % self.slice) 

        tmp = img
        exec('tmp[%s] = value' % index)
        ctx.write_pin('Out', tmp)

install_node_template(ImageSpacingNode())
install_node_template(ImageSetSpacingNode())
install_node_template(SliceImageNode())
install_node_template(SetSliceImageNode())

Exemple #18
0
        if self.is_pin_linked('Def'):
            do_def = True
        if self.is_pin_linked('Jac'):
            do_jac = True

        transform = ctx.read_pin('Transform')
        if type(transform) == Parameters:
            transform.write(os.path.join(temp_dir, 'tp.txt'))
            transform = os.path.join(temp_dir, 'tp.txt')

        if type(transform) != str:
            raise ValueError('Unexpected type for pin \'Transform\'')

        run_transformix(transform, temp_dir, img=img, df=do_def, jac=do_jac)

        if self.is_pin_linked('Out') and img != None:
            ctx.write_pin('Out',
                          medkit.read(os.path.join(temp_dir, 'result.nii')))

        if do_def:
            ctx.write_pin(
                'Def',
                medkit.read(os.path.join(temp_dir, 'deformationField.nii')))
        if do_jac:
            ctx.write_pin(
                'Jac',
                medkit.read(os.path.join(temp_dir, 'spatialJacobian.nii')))


install_node_template(TransformixNode())