Slicer3:Python:pitky

From Slicer Wiki
Revision as of 14:50, 19 June 2009 by Pieper (talk | contribs) (New page: This is part of a [http://www.na-mic.org/Wiki/index.php/Summer2009:Using_ITK_in_python NA-MIC Summer Project Week 2009 Project]. <pre> """ A simple example to show how to use weave with I...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Home < Slicer3:Python:pitky

This is part of a NA-MIC Summer Project Week 2009 Project.

""" A simple example to show how to use weave with ITK.  This lets one
pass numpy arrays to ITK for processing.

based on vtk_example.py from weave distribution.

Author: Steve Pieper
Copyright (c) 2009, Steve Pieper
License: BSD Style.
"""

d = '/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/'
packages = ['matplotlib', 'neuroimaging', 'numpy', '', 'scipy']
for p in packages:
  sys.path.append(d+p)

import scipy.weave as weave
import numpy

import sys
import time
import os

# from ITKConfig.cmake - TODO: extract automatically, along with build flags etc (or invoke cmake via distutils?!)
inc_dirs = ['/Users/pieper/slicer3/latest/Slicer3-lib/Insight-build', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Code/Algorithms', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Code/BasicFilters', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Code/Common', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Code/Numerics', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Code/IO', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Code/Numerics/FEM', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Code/Numerics/Statistics', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Code/Numerics/NeuralNetworks', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Code/SpatialObject', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Utilities/MetaIO', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Utilities/NrrdIO', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight-build/Utilities/NrrdIO', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Utilities/DICOMParser', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight-build/Utilities/DICOMParser', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight-build/Utilities/expat', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Utilities/expat', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Utilities/nifti/niftilib', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Utilities/nifti/znzlib', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Utilities/itkExtHdrs', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight-build/Utilities', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Utilities', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Utilities/vxl/v3p/netlib', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Utilities/vxl/vcl', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Utilities/vxl/core', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight-build/Utilities/vxl/v3p/netlib', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight-build/Utilities/vxl/vcl', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight-build/Utilities/vxl/core', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight-build/Utilities/gdcm', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Utilities/gdcm/src', '/Users/pieper/slicer3/latest/Slicer3-lib/Insight/Code/Review']

# The ITK library directories.
lib_dirs = ['/Users/pieper/slicer3/latest/Slicer3-lib/Insight-build/bin']


def writer_test():
    """A simple example of how you can use ITK code in weave
       to write a volume to a file.
    """

    fileName = '/tmp/rand.nrrd'
    array = numpy.arange(0,24,dtype='float64').reshape(2,3,4)

    code=r"""
    typedef itk::Image<double,3> ImageType; // TODO: construct from dtype and shape
    ImageType::Pointer image = ImageType::New();
    image->GetPixelContainer()->SetImportPointer( array, Narray[0]*Narray[1]*Narray[2], false );

    ImageType::RegionType region;
    ImageType::IndexType index;
    ImageType::SizeType size;
    index[0] = index[1] = index[2] = 0;
    size[0] = Narray[0]; size[1] = Narray[1]; size[2] = Narray[2];
    region.SetIndex(index);
    region.SetSize(size);
    image->SetLargestPossibleRegion(region);
    image->SetBufferedRegion(region);


    itk::ImageFileWriter< ImageType >::Pointer writer = WriterType::New();
    writer->SetFileName( fileName );
    writer->SetInput( image );
    writer->Update();
    """

    weave.inline(code, ['fileName', 'array'],
                  include_dirs = inc_dirs,
                  library_dirs = lib_dirs,
                  headers=['"itkImage.h"', '"itkImageFileWriter.h"'],
                  libraries=['itkCommon', 'itkIO'])


TODO = """
- apply a filter (volume in and out, where output size may be different from input)
- compile on windows
- interface with cmake?
- how to deploy built libs in cpacked binary
-- how to trigger weave compilation during build process for packaging
"""


if __name__ == "__main__":
    writer_test()