Skip to content

[WIP] using pypes within jupyter notebooks#271

Merged
rlizzo merged 7 commits into
vmtk:develop-pypesfrom
rlizzo:notebook-input
May 2, 2018
Merged

[WIP] using pypes within jupyter notebooks#271
rlizzo merged 7 commits into
vmtk:develop-pypesfrom
rlizzo:notebook-input

Conversation

@rlizzo

@rlizzo rlizzo commented Apr 30, 2018

Copy link
Copy Markdown
Member

Summary

This is some initial work conducted to allow pypes to be run within jupyter notebook. This PR focuses on allowing arbitrary python objects to be inserted into a pype string as input members.

Syntax

I have adopted a syntax similar to python f-string formatting to indicate the names within the input string which should be replaced with objects. Any name within {curly braces} is assumed to be the name of a python object to insert as a member in the Pype. Though similar in synatx, the representation should not be an actual f-string instance.

Implementation

The core structure of PypeS has been largely preserved. In order to implement this, kwargs have been added for the PypeRun and SetArgumentString routines. When {curly braces} are detected in the input string, the variable name enclosed by the {curly braces} is extracted, and the kwargs dict queried for the matching key/value pair. The Arguments list is then modified, replacing element previously holding the {curly braces} with the live python object.

In order for the Pype instance to access python objects from the calling scope, we specify the locals() dict as the kwargs to the pype.VmtkRun() function. I'm hoping to find a cleaner way of doing this. Minor modifications have been made the the ScriptObject argument parsing to accommodate these changes.

Example

The following code seems to run without a problem within a jupyter notebook.

from vmtk import pypes

fileName = './aorta.mha'
flip = [0, 1, 1]
level = 700

a = pypes.VmtkRun('vmtkimagereader -ifile {fileName} -flip {flip} --pipe vmtkmarchingcubes -l {level}', **locals())
    Automatic piping vmtkimagereader
    Parsing options vmtkimagereader
        InputFileName = ./aorta.mha
        Flip = [0, 1, 1]
    Explicit piping vmtkimagereader
    Input vmtkimagereader members:
        Id = 0
        Disabled = 0
        Format = 
        GuessFormat = 1
        UseITKIO = 1
        Image = 0
        InputFileName = ./aorta.mha
        InputFilePrefix = 
        InputFilePattern = 
        DataExtent = [-1, -1, -1, -1, -1, -1]
        HeaderSize = 0
        DataSpacing = [1.0, 1.0, 1.0]
        DataOrigin = [0.0, 0.0, 0.0]
        DesiredOrientation = native
        DataByteOrder = littleendian
        DataScalarType = float
        FileDimensionality = 3
        Flip = [0, 1, 1]
        AutoOrientDICOMImage = 1
        ImageOutputFileName = 
    Executing vmtkimagereader ...
    Spacing 0.878906 0.878906 1.500090
    Origin 156.445000 24.609400 0.000000
    Dimensions 157 393 34
    Done executing vmtkimagereader.
    Output vmtkimagereader members:
        Id = 0
        Image = vtkImageData
        RasToIjkMatrixCoefficients = [1.137778101412438, -0.0, 0.0, -177.99969507546882, -0.0, 1.137778101412438, -0.0, -28.000036408899245, 0.0, -0.0, 0.6666266690665227, -0.0, -0.0, 0.0, -0.0, 1.0]
        XyzToRasMatrixCoefficients = [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0]
    
    Automatic piping vmtkmarchingcubes
        Image = vmtkimagereader-0.Image
    Parsing options vmtkmarchingcubes
        Level = 700
    Explicit piping vmtkmarchingcubes
    Input vmtkmarchingcubes members:
        Id = 0
        Disabled = 0
        Image = vtkImageData
        ImageInputFileName = 
        ArrayName = 
        Level = 700
        Connectivity = 0
        SurfaceOutputFileName = 
    Executing vmtkmarchingcubes ...
    Done executing vmtkmarchingcubes.
    Output vmtkmarchingcubes members:
        Id = 0
        Surface = vtkPolyData
surface = a.GetScriptObject('vmtkmarchingcubes','0').Surface

myargs2 = 'vmtksurfaceviewer -i {surface}'

b = pypes.PypeRun(myargs2, **locals())
    Automatic piping vmtksurfaceviewer
    Parsing options vmtksurfaceviewer
        Surface = vtkPolyData
    Explicit piping vmtksurfaceviewer
    Input vmtksurfaceviewer members:
        Id = 0
        Disabled = 0
        Surface = vtkPolyData
        SurfaceInputFileName = 
        vmtkRenderer = None
        Display = 1
        Representation = surface
        Opacity = 1.0
        ArrayName = 
        ScalarRange = [0.0, 0.0]
        ColorMap = cooltowarm
        NumberOfColors = 256
        Legend = 0
        FlatInterpolation = 0
        DisplayCellData = 0
        DisplayTag = False
        RegionTagArrayName = RegionTagArray
        Color = [-1.0, -1.0, -1.0]
        LineWidth = 1
        LegendTitle = 
        SurfaceOutputFileName = 
    Executing vmtksurfaceviewer ...
    Quit renderer
    Done executing vmtksurfaceviewer.
    Output vmtksurfaceviewer members:
        Id = 0
        Surface = vtkPolyData
        Actor = vtkActor

@rlizzo rlizzo force-pushed the notebook-input branch from 2f2278f to cee38b5 Compare May 1, 2018 05:19
@rlizzo

rlizzo commented May 2, 2018

Copy link
Copy Markdown
Member Author

I have rebased this PR off of the latest develop-pypes branch. This allows for the loading of the vmtk.run method just by importing vmtk. I'm going to merge this and then submit another PR for develop-pypes into master so that we can address all changes in one PR.

@rlizzo rlizzo merged commit 38b5ebc into vmtk:develop-pypes May 2, 2018
@rlizzo rlizzo mentioned this pull request May 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant