[cig-commits] r13043 - in cs/cigma/trunk: . build examples pysrc pysrc/cigma pysrc/tests src src/cigma tests tests/pytests
luis at geodynamics.org
luis at geodynamics.org
Wed Oct 15 02:07:34 PDT 2008
Author: luis
Date: 2008-10-15 02:07:33 -0700 (Wed, 15 Oct 2008)
New Revision: 13043
Added:
cs/cigma/trunk/examples/
cs/cigma/trunk/examples/README
cs/cigma/trunk/src/cigma/
cs/cigma/trunk/src/cigma/Locator.py
cs/cigma/trunk/src/cigma/__init__.py
cs/cigma/trunk/src/numpy_util.cpp
cs/cigma/trunk/src/numpy_util.h
cs/cigma/trunk/src/py_DataPath.cpp
cs/cigma/trunk/src/py_ElementBlock.cpp
cs/cigma/trunk/src/py_Exception.cpp
cs/cigma/trunk/src/py_FileReader.cpp
cs/cigma/trunk/src/py_FileWriter.cpp
cs/cigma/trunk/src/py_Hello.cpp
cs/cigma/trunk/src/py_Locator.cpp
cs/cigma/trunk/src/py_MeshPart.cpp
cs/cigma/trunk/src/py_Misc.cpp
cs/cigma/trunk/src/py_NodeCoordinates.cpp
cs/cigma/trunk/src/py_Points.cpp
cs/cigma/trunk/src/py_cigma_module.cpp
cs/cigma/trunk/src/py_cigma_setup.py
cs/cigma/trunk/tests/pytests/
cs/cigma/trunk/tests/pytests/runner.py
cs/cigma/trunk/tests/pytests/test_cigma.py
cs/cigma/trunk/tests/pytests/test_locator.py
Removed:
cs/cigma/trunk/pysrc/Py_DataPath.cpp
cs/cigma/trunk/pysrc/Py_ElementBlock.cpp
cs/cigma/trunk/pysrc/Py_Exception.cpp
cs/cigma/trunk/pysrc/Py_FileReader.cpp
cs/cigma/trunk/pysrc/Py_FileWriter.cpp
cs/cigma/trunk/pysrc/Py_Hello.cpp
cs/cigma/trunk/pysrc/Py_Locator.cpp
cs/cigma/trunk/pysrc/Py_MeshPart.cpp
cs/cigma/trunk/pysrc/Py_Misc.cpp
cs/cigma/trunk/pysrc/Py_NodeCoordinates.cpp
cs/cigma/trunk/pysrc/Py_Points.cpp
cs/cigma/trunk/pysrc/README
cs/cigma/trunk/pysrc/_cigma_module.cpp
cs/cigma/trunk/pysrc/cigma/Locator.py
cs/cigma/trunk/pysrc/cigma/__init__.py
cs/cigma/trunk/pysrc/numpy_util.cpp
cs/cigma/trunk/pysrc/numpy_util.h
cs/cigma/trunk/pysrc/quux.cpp
cs/cigma/trunk/pysrc/setup.py
cs/cigma/trunk/pysrc/tests/runner.py
cs/cigma/trunk/pysrc/tests/test_cigma.py
cs/cigma/trunk/pysrc/tests/test_locator.py
Modified:
cs/cigma/trunk/Makefile.am
cs/cigma/trunk/build/runtests.sh
cs/cigma/trunk/src/README
Log:
Now that we know how the bindings work, relocate pysrc/ contents into src/
Modified: cs/cigma/trunk/Makefile.am
===================================================================
--- cs/cigma/trunk/Makefile.am 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/Makefile.am 2008-10-15 09:07:33 UTC (rev 13043)
@@ -193,7 +193,7 @@
CFLAGS="$(CPPFLAGS) -I$(top_srcdir)/src $(libcigma_a_CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)" \
LDFLAGS="$(LDFLAGS) -L$$BUILD_DIR/.libs" \
ARCHFLAGS="$(ARCHFLAGS)" PYLIBS="$(PYLIBS)" \
- $(PYTHON_BIN) pysrc/setup.py build --build-lib=$$BUILD_DIR/ --build-temp=$$BUILD_DIR/pybuild -f)
+ $(PYTHON_BIN) src/py_cigma_setup.py build --build-lib=$$BUILD_DIR/ --build-temp=$$BUILD_DIR/pybuild -f)
_cigma_so_CXXFLAGS =
_cigma_so_CXXFLAGS += -I$(top_srcdir)/src
@@ -202,19 +202,19 @@
_cigma_so_LDADD += libcigma.a
_cigma_so_SOURCES = \
- pysrc/numpy_util.h \
- pysrc/numpy_util.cpp \
- pysrc/Py_DataPath.cpp \
- pysrc/Py_Points.cpp \
- pysrc/Py_Locator.cpp \
- pysrc/Py_MeshPart.cpp \
- pysrc/Py_ElementBlock.cpp \
- pysrc/Py_NodeCoordinates.cpp \
- pysrc/Py_Misc.cpp \
- pysrc/_cigma_module.cpp
+ src/numpy_util.h \
+ src/numpy_util.cpp \
+ src/py_DataPath.cpp \
+ src/py_Points.cpp \
+ src/py_Locator.cpp \
+ src/py_MeshPart.cpp \
+ src/py_ElementBlock.cpp \
+ src/py_NodeCoordinates.cpp \
+ src/py_Misc.cpp \
+ src/py_cigma_module.cpp
-EXTRA_DIST += pysrc/setup.py
+EXTRA_DIST += src/cigma_setup.py
###############################################################################
Modified: cs/cigma/trunk/build/runtests.sh
===================================================================
--- cs/cigma/trunk/build/runtests.sh 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/build/runtests.sh 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,4 +1,4 @@
#!/bin/bash
make check
-python pysrc/tests/runner.py
+python tests/pytests/runner.py
Added: cs/cigma/trunk/examples/README
===================================================================
--- cs/cigma/trunk/examples/README (rev 0)
+++ cs/cigma/trunk/examples/README 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1 @@
+This directory contains examples
Deleted: cs/cigma/trunk/pysrc/Py_DataPath.cpp
===================================================================
--- cs/cigma/trunk/pysrc/Py_DataPath.cpp 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/Py_DataPath.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,44 +0,0 @@
-//#include <cigma/DataPath.h> // XXX: create include directory for core cigma headers?
-
-#include "DataPath.h"
-#include <boost/python.hpp>
-#include <boost/cstdint.hpp>
-
-
-const char *DataPath_doc[] = {
-// 0: DataPath.__doc__
-"An object encapsulating a dataset path.\n",
-"Included in the core bindings as a first example",
-// 1: DataPath.__init__.__doc
-"Constructs a path given a colon separated filename:datapath string"
-};
-
-
-template<class DataPath>
-boost::python::str py_repr_DataPath(const DataPath& dp)
-{
- using namespace boost::python;
- return str("<" + str(dp) + ">");
-}
-
-void Py_DataPath()
-{
- using namespace boost::python;
-
- class_<DataPath>("DataPath", DataPath_doc[0], no_init)
- .def(init<std::string>(DataPath_doc[1]))
- .def("exists", &DataPath::exists)
- .def("empty", &DataPath::empty)
- .def("extension", &DataPath::extension)
- .add_property("filename", &DataPath::filename, &DataPath::set_filename)
- .add_property("location", &DataPath::location, &DataPath::set_location)
- .def("__repr__", &py_repr_DataPath<DataPath>)
- .def(self_ns::str(self)) // __str__
- ;
-}
-
-void export_DataPath()
-{
- Py_DataPath();
-}
-
Deleted: cs/cigma/trunk/pysrc/Py_ElementBlock.cpp
===================================================================
--- cs/cigma/trunk/pysrc/Py_ElementBlock.cpp 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/Py_ElementBlock.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,12 +0,0 @@
-#include "ElementBlock.h"
-#include <boost/python.hpp>
-
-void export_ElementBlock()
-{
- using namespace cigma;
- using namespace boost::python;
-
- class_<ElementBlock, boost::noncopyable>("ElementBlock", no_init)
- ;
-}
-
Deleted: cs/cigma/trunk/pysrc/Py_Exception.cpp
===================================================================
--- cs/cigma/trunk/pysrc/Py_Exception.cpp 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/Py_Exception.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,16 +0,0 @@
-#include "Exception.cpp"
-#include <boost/python.hpp>
-
-using namespace boost::python;
-
-void translator(const cigma::Exception &x)
-{
- std::string err = x.getName() + ": " + x.getMessage();
- PyErr_SetString(PyExc_UserWarning, err.c_str());
-}
-
-void export_Exception()
-{
- register_exception_translator<cigma::Exception>(translator);
-}
-
Deleted: cs/cigma/trunk/pysrc/Py_FileReader.cpp
===================================================================
--- cs/cigma/trunk/pysrc/Py_FileReader.cpp 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/Py_FileReader.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,89 +0,0 @@
-#include "io_file_reader.h"
-#include "io_null_reader.h"
-#include "io_text_reader.h"
-
-#ifdef HAVE_HDF5
-#include "io_hdf5_reader.h"
-#endif
-
-#ifdef HAVE_VTK
-#include "io_vtk_reader.h"
-#include "io_ucd_reader.h"
-#endif
-
-#include <boost/python.hpp>
-
-using namespace boost::python;
-
-struct pyFileReader : FileReader, wrapper<FileReader>
-{
-};
-
-class pyNullReader : NullReader
-{
-};
-
-class pyTextReader : TextReader
-{
-};
-
-/* Wrap the HDF5 reader */
-#ifdef HAVE_HDF5
-class pyHDF5Reader : HDF5_Reader
-{
-};
-#endif
-
-/* Wrap the VTK reader */
-#ifdef HAVE_VTK
-class pyVTKReader : VtkReader
-{
-};
-class pyUCDReader : UCD_Reader
-{
-};
-#endif
-
-
-void export_FileReader()
-{
- using namespace boost::python;
-
-
- class_<pyNullReader>("NullReader")
- .def(init<std::string>())
- .def("open", &pyNullReader::open)
- .def("close", &pyVtkReader::close)
- ;
-
- class_<pyTextReader>("TextReader")
- .def(init<std::string>())
- .def("open", &pyTextReader::open)
- .def("close", &pyTextReader::close)
- ;
-
-/* Class definitions for HDF5 */
-#ifdef HAVE_HDF5
- class_<pyHDF5Reader>("HDF5Reader")
- .def(init<std::string>())
- .def("open", &pyHDF5Reader::open)
- .def("close", &pyHDF5Reader::close)
- ;
-#endif
-
-/* Class definitions for VTK readers */
-#ifdef HAVE_VTK
- class_<pyVTKReader>("VTKReader")
- .def(init<std::string>())
- .def("open", &pyVTKReader::open)
- .def("close", &pyVTKReader::close)
- ;
- class_<pyUCDReader>("UCDReader")
- .def(init<std::string())
- .def("open", &pyUCDReader::open)
- .def("close", &pyUCDReader::close)
- ;
-#endif
-
-
-}
Deleted: cs/cigma/trunk/pysrc/Py_FileWriter.cpp
===================================================================
--- cs/cigma/trunk/pysrc/Py_FileWriter.cpp 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/Py_FileWriter.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,71 +0,0 @@
-#include "io_file_writer.h"
-#include "io_null_writer.h"
-#include "io_text_writer.h"
-
-#ifdef HAVE_HDF5
-#include "io_hdf5_writer.h"
-#endif
-
-#ifdef HAVE_VTK
-#include "io_vtk_writer.h"
-#endif
-
-#include <boost/python.hpp>
-
-class pyNullWriter : public NullWriter
-{
-};
-
-class pyTextWriter : public TextWriter
-{
-};
-
-/* Wrapper for HDF5 */
-#ifdef HAVE_HDF5
-class pyHDF5Writer : public HDF5_Writer
-{
-};
-#endif
-
-/* Wrapper for VTK */
-#ifdef HAVE_VTK
-class pyVTKWriter : public VtkWriter
-{
-};
-#endif
-
-
-void export_FileWriter
-{
- using namespace boost::python;
-
- class_<pyNullWriter, bases<NullWriter> >("NullWriter")
- .def(init<std::string>())
- .def("open", &pyNullWriter::open)
- .def("close", &pyNullWriter::close)
- ;
-
- class_<pyTextWriter, bases<TextWriter> >("TextWriter")
- .def(init<std::string>())
- .def("open", &pyTextWriter::open)
- .def("close", &pyTextWriter::close)
- ;
-
-#ifdef HAVE_HDF5
- class_<pyHDF5Writer, bases<HDF5_Writer> >("HDF5Writer")
- .def(init<std::string>())
- .def("open", &pyHDF5Writer::open)
- .def("close", &pyHDF5Writer::close)
- ;
-#endif
-
-#ifdef HAVE_VTK
- class_<pyVTKWriter, bases<VtkWriter> >("VTKWriter")
- .def(init<std::string>())
- .def("open", &pyVTKWriter::open)
- .def("close", &pyVTKWriter::close)
- ;
-#endif
-
-}
-
Deleted: cs/cigma/trunk/pysrc/Py_Hello.cpp
===================================================================
--- cs/cigma/trunk/pysrc/Py_Hello.cpp 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/Py_Hello.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,14 +0,0 @@
-#include <boost/python.hpp>
-
-using namespace boost::python;
-
-std::string hello()
-{
- return std::string("Hello World!");
-}
-
-void export_Hello()
-{
- def("hello", hello);
-}
-
Deleted: cs/cigma/trunk/pysrc/Py_Locator.cpp
===================================================================
--- cs/cigma/trunk/pysrc/Py_Locator.cpp 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/Py_Locator.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,60 +0,0 @@
-#include "Locator.h"
-#include "AnnLocator.h"
-#include <boost/python.hpp>
-
-using namespace cigma;
-using namespace boost::python;
-
-struct pyLocator : Locator, wrapper<Locator>
-{
- int n_dim() const
- {
- return this->get_override("n_dim")();
- }
-
- int n_idx() const
- {
- return this->get_override("n_idx")();
- }
-
- int idx(int i) const
- {
- return this->get_override("idx")(i);
- }
-
- void searchBoundingBox(double *bbox)
- {
- this->get_override("searchBoundingBox")(bbox);
- }
-
- bool check_idx(int i) const
- {
- if (override c = this->get_override("check_idx"))
- {
- return c(i);
- }
- return Locator::check_idx(i);
- }
-};
-
-void export_Locator()
-{
- using namespace cigma;
- using namespace boost::python;
-
- class_<pyLocator, boost::noncopyable>("Locator")
- .def("n_dim", pure_virtual(&Locator::n_dim))
- .def("n_idx", pure_virtual(&Locator::n_idx))
- .def("idx", pure_virtual(&Locator::idx))
- //.def("search_bbox", pure_virtual(&Locator::searchBoundingBox))
- .def("check_idx", &pyLocator::check_idx)
- ;
-
- class_<AnnLocator>("AnnLocator")
- .def("n_dim", &AnnLocator::n_dim)
- .def("n_idx", &AnnLocator::n_idx)
- .def("idx", &AnnLocator::idx)
- ;
-
-}
-
Deleted: cs/cigma/trunk/pysrc/Py_MeshPart.cpp
===================================================================
--- cs/cigma/trunk/pysrc/Py_MeshPart.cpp 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/Py_MeshPart.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,11 +0,0 @@
-#include "MeshPart.h"
-#include <boost/python.hpp>
-
-void export_MeshPart()
-{
- using namespace cigma;
- using namespace boost::python;
-
- class_<MeshPart>("MeshPart")
- ;
-}
Deleted: cs/cigma/trunk/pysrc/Py_Misc.cpp
===================================================================
--- cs/cigma/trunk/pysrc/Py_Misc.cpp 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/Py_Misc.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,29 +0,0 @@
-#include "numpy_util.h"
-#include "/usr/include/python2.5/numpy/ndarrayobject.h"
-#include <boost/python.hpp>
-
-using namespace boost::python;
-
-numeric::array new_array()
-{
- return numeric::array(
- make_tuple(
- make_tuple(1,2,3),
- make_tuple(4,5,6),
- make_tuple(7,8,9)
- )
- );
-}
-
-
-numeric::array new_array2()
-{
- return numpy_util::makeArray(new_array());
-}
-
-void export_Misc()
-{
- def("new_array", &new_array);
- def("new_array2", &new_array2);
-}
-
Deleted: cs/cigma/trunk/pysrc/Py_NodeCoordinates.cpp
===================================================================
--- cs/cigma/trunk/pysrc/Py_NodeCoordinates.cpp 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/Py_NodeCoordinates.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,41 +0,0 @@
-#include "NodeCoordinates.h"
-#include <boost/python.hpp>
-#include <boost/python/numeric.hpp>
-#include <boost/python/tuple.hpp>
-
-using namespace cigma;
-using namespace boost::python;
-
-
-/*
-class NodeCoordinatesWrapper : public NodeCoordinates
-{
-public:
- NodeCoordinatesWrapper(numeric::array &x)
- {
- tuple t = x.factory.getshape();
- int rank = len(t);
- if (rank == 2)
- {
- std::cout << "rank two!\n";
- }
- }
- ~NodeCoordinatesWrapper()
- {
- }
-private:
- numeric::array _data;
-}; */
-
-
-void export_NodeCoordinates()
-{
- //typedef NodeCoordinatesWrapper X;
- typedef NodeCoordinates X;
-
- class_<X,boost::noncopyable>("NodeCoordinates", no_init)
- .def_readonly("npts", &X::n_points)
- .def_readonly("ndim", &X::n_dim)
- .def("getPoint", &X::getPoint)
- ;
-}
Deleted: cs/cigma/trunk/pysrc/Py_Points.cpp
===================================================================
--- cs/cigma/trunk/pysrc/Py_Points.cpp 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/Py_Points.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,32 +0,0 @@
-#include "Points.h"
-#include <boost/python.hpp>
-
-void export_Points()
-{
- using namespace cigma;
- using namespace boost::python;
-
- typedef Points<float,2> Points2f;
- class_<Points2f,boost::noncopyable>("Points2f", no_init)
- .def("n_points", &Points2f::n_points)
- .def("n_dim", &Points2f::n_dim)
- ;
-
- typedef Points<float,3> Points3f;
- class_<Points3f,boost::noncopyable>("Points3f", no_init)
- .def("n_points", &Points3f::n_points)
- .def("n_dim", &Points3f::n_dim)
- ;
-
- typedef Points<double,2> Points2d;
- class_<Points2d,boost::noncopyable>("Points2d", no_init)
- .def("n_points", &Points2d::n_points)
- .def("n_dim", &Points2d::n_dim)
- ;
-
- typedef Points<double,3> Points3d;
- class_<Points3d,boost::noncopyable>("Points3d", no_init)
- .def("n_points", &Points3d::n_points)
- .def("n_dim", &Points3d::n_dim)
- ;
-}
Deleted: cs/cigma/trunk/pysrc/README
===================================================================
--- cs/cigma/trunk/pysrc/README 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/README 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,14 +0,0 @@
-This directory contains the C++ bindings to Python
-
-To facilitate the build procedure, we make use of distutils to
-compile the Boost.Python module.
-
- http://pydoc.org/2.5.1/distutils.extension.html
-
-
-Other projects that use Boost.Python bindings include
-
- MeshPy: http://mathema.tician.de/software/meshpy
- CGAL-Python: http://cgal-python.gforge.inria.fr/
- Ledger: http://github.com/jwiegley/ledger/tree/master
-
Deleted: cs/cigma/trunk/pysrc/_cigma_module.cpp
===================================================================
--- cs/cigma/trunk/pysrc/_cigma_module.cpp 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/_cigma_module.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,34 +0,0 @@
-#define PY_ARRAY_UNIQUE_SYMBOL PyArrayHandle
-#include "numpy_util.h"
-#include <boost/python.hpp>
-#include <boost/cstdint.hpp>
-
-extern void export_Exception();
-extern void export_Hello();
-extern void export_DataPath();
-extern void export_Points();
-extern void export_Locator();
-extern void export_MeshPart();
-extern void export_ElementBlock();
-extern void export_NodeCoordinates();
-extern void export_Misc();
-
-using namespace boost::python;
-
-BOOST_PYTHON_MODULE(_cigma)
-{
- import_array();
- numeric::array::set_module_and_type("numpy", "ndarray");
-
- export_Exception();
-
- export_Hello();
- export_DataPath();
- export_Points();
- export_Locator();
- export_MeshPart();
- export_ElementBlock();
- export_NodeCoordinates();
- export_Misc();
-}
-
Deleted: cs/cigma/trunk/pysrc/cigma/Locator.py
===================================================================
--- cs/cigma/trunk/pysrc/cigma/Locator.py 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/cigma/Locator.py 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,7 +0,0 @@
-#!/usr/bin/env python
-
-import _cigma
-class Locator(_cigma.Locator):
- def __init__(self):
- _cigma.Locator.__init__(self)
-
Deleted: cs/cigma/trunk/pysrc/cigma/__init__.py
===================================================================
--- cs/cigma/trunk/pysrc/cigma/__init__.py 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/cigma/__init__.py 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,2 +0,0 @@
-__version__ = '1.0.0'
-
Deleted: cs/cigma/trunk/pysrc/numpy_util.cpp
===================================================================
--- cs/cigma/trunk/pysrc/numpy_util.cpp 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/numpy_util.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,410 +0,0 @@
-#define PY_ARRAY_UNIQUE_SYMBOL PyArrayHandle
-#define NO_IMPORT_ARRAY
-#include "numpy_util.h"
-
-using namespace boost::python;
-
-namespace numpy_util {
-
-
-//
-// specializations for use by makeArray
-//
-
-template <> PyArray_TYPES getEnum<unsigned char>(void) { return PyArray_UBYTE; }
-template <> PyArray_TYPES getEnum<signed char>(void) { return PyArray_BYTE; }
-template <> PyArray_TYPES getEnum<short>(void) { return PyArray_SHORT; }
-template <> PyArray_TYPES getEnum<unsigned short>(void) { return PyArray_USHORT; }
-template <> PyArray_TYPES getEnum<unsigned int>(void) { return PyArray_UINT; }
-template <> PyArray_TYPES getEnum<int>(void) { return PyArray_INT; }
-template <> PyArray_TYPES getEnum<long>(void) { return PyArray_LONG; }
-template <> PyArray_TYPES getEnum<unsigned long>(void) { return PyArray_ULONG; }
-template <> PyArray_TYPES getEnum<long long>(void) { return PyArray_LONGLONG; }
-template <> PyArray_TYPES getEnum<unsigned long long>(void) { return PyArray_ULONGLONG; }
-template <> PyArray_TYPES getEnum<float>(void) { return PyArray_FLOAT; }
-template <> PyArray_TYPES getEnum<double>(void) { return PyArray_DOUBLE; }
-template <> PyArray_TYPES getEnum<long double>(void) { return PyArray_LONGDOUBLE; }
-template <> PyArray_TYPES getEnum<std::complex<float> >(void) { return PyArray_CFLOAT; }
-template <> PyArray_TYPES getEnum<std::complex<double> >(void) { return PyArray_CDOUBLE; }
-template <> PyArray_TYPES getEnum<std::complex<long double> >(void) { return PyArray_CDOUBLE; }
-
-
-typedef KindStringMap::value_type KindStringMapEntry;
-KindStringMapEntry kindStringMapEntries[] = {
- KindStringMapEntry(PyArray_UBYTE, "PyArray_UBYTE"),
- KindStringMapEntry(PyArray_BYTE, "PyArray_BYTE" ),
- KindStringMapEntry(PyArray_SHORT, "PyArray_SHORT"),
- KindStringMapEntry(PyArray_INT, "PyArray_INT" ),
- KindStringMapEntry(PyArray_LONG, "PyArray_LONG" ),
- KindStringMapEntry(PyArray_FLOAT, "PyArray_FLOAT"),
- KindStringMapEntry(PyArray_DOUBLE, "PyArray_DOUBLE"),
- KindStringMapEntry(PyArray_CFLOAT, "PyArray_CFLOAT"),
- KindStringMapEntry(PyArray_CDOUBLE, "PyArray_CDOUBLE"),
- KindStringMapEntry(PyArray_OBJECT, "PyArray_OBJECT"),
- KindStringMapEntry(PyArray_NTYPES, "PyArray_NTYPES"),
- KindStringMapEntry(PyArray_NOTYPE, "PyArray_NOTYPE")
-};
-
-
-typedef KindCharMap::value_type KindCharMapEntry;
-KindCharMapEntry kindCharMapEntries[] = {
- KindCharMapEntry(PyArray_UBYTE, 'B'),
- KindCharMapEntry(PyArray_BYTE, 'b'),
- KindCharMapEntry(PyArray_SHORT, 'h'),
- KindCharMapEntry(PyArray_INT, 'i'),
- KindCharMapEntry(PyArray_LONG, 'l'),
- KindCharMapEntry(PyArray_FLOAT, 'f'),
- KindCharMapEntry(PyArray_DOUBLE, 'd'),
- KindCharMapEntry(PyArray_CFLOAT, 'F'),
- KindCharMapEntry(PyArray_CDOUBLE, 'D'),
- KindCharMapEntry(PyArray_OBJECT, 'O')
-};
-
-
-typedef KindTypeMap::value_type KindTypeMapEntry;
-KindTypeMapEntry kindTypeMapEntries[] = {
- KindTypeMapEntry('B', PyArray_UBYTE),
- KindTypeMapEntry('b', PyArray_BYTE),
- KindTypeMapEntry('h', PyArray_SHORT),
- KindTypeMapEntry('i', PyArray_INT),
- KindTypeMapEntry('l', PyArray_LONG),
- KindTypeMapEntry('f', PyArray_FLOAT),
- KindTypeMapEntry('d', PyArray_DOUBLE),
- KindTypeMapEntry('F', PyArray_CFLOAT),
- KindTypeMapEntry('D', PyArray_CDOUBLE),
- KindTypeMapEntry('O', PyArray_OBJECT)
-};
-
-
-int numStringEntries = sizeof(kindStringMapEntries) / sizeof(KindStringMapEntry);
-int numCharEntries = sizeof(kindCharMapEntries) / sizeof(KindCharMapEntry);
-int numTypeEntries = sizeof(kindTypeMapEntries) / sizeof(KindTypeMapEntry);
-
-
-using namespace boost::python;
-
-static KindStringMap kindstrings(kindStringMapEntries, kindStringMapEntries + numStringEntries);
-static KindCharMap kindchars (kindCharMapEntries, kindCharMapEntries + numCharEntries);
-static KindTypeMap kindtypes (kindTypeMapEntries, kindTypeMapEntries + numTypeEntries);
-
-
-// Create a numeric array referencing Python sequence object
-numeric::array makeArray(object x)
-{
- if (!PySequence_Check(x.ptr()))
- {
- PyErr_SetString(PyExc_ValueError, "expected a sequence");
- throw_error_already_set();
- }
- object obj(handle<>(PyArray_ContiguousFromObject(x.ptr(), PyArray_NOTYPE, 0, 0)));
- check_PyArrayElementType(obj);
- return extract<numeric::array>(obj);
-}
-
-// Create a one-dimensional numeric array of length n and type t
-numeric::array makeArray(intp n, PyArray_TYPES t=PyArray_DOUBLE)
-{
- object obj(handle<>(PyArray_FromDims(1, &n, t)));
- return extract<numeric::array>(obj);
-}
-
-
-// Create a numeric array with dimensions dimens and type t
-numeric::array makeArray(std::vector<intp> dimens, PyArray_TYPES t=PyArray_DOUBLE)
-{
- object obj(handle<>(PyArray_FromDims(dimens.size(), &dimens[0], t)));
- return extract<numeric::array>(obj);
-}
-
-
-numeric::array makeArray(const numeric::array& arr)
-{
- // Returns a reference of arr by calling numeric::array copy constructor.
- // The copy constructor increases arr's reference count.
- return numeric::array(arr);
-}
-
-
-PyArray_TYPES type(numeric::array arr)
-{
- return PyArray_TYPES(PyArray_TYPE(arr.ptr()));
-}
-
-void check_type(numeric::array arr, PyArray_TYPES expected_type)
-{
- PyArray_TYPES actual_type = type(arr);
- if (actual_type != expected_type)
- {
- std::ostringstream stream;
- stream << "expected numeric type " << kindstrings[expected_type]
- << ", found numeric type " << kindstrings[actual_type]
- << std::ends;
- PyErr_SetString(PyExc_TypeError, stream.str().c_str());
- throw_error_already_set();
- }
- return;
-}
-
-
-// Return the number of dimensions
-int rank(numeric::array arr)
-{
- //std::cout << "inside rank" << std::endl;
- if (!PyArray_Check(arr.ptr()))
- {
- PyErr_SetString(PyExc_ValueError, "expected a PyArrayObject");
- throw_error_already_set();
- }
- return PyArray_NDIM(arr.ptr());
-}
-
-void check_rank(numeric::array arr, int expected_rank)
-{
- int actual_rank = rank(arr);
- if (actual_rank != expected_rank)
- {
- std::ostringstream stream;
- stream << "expected rank " << expected_rank
- << ", found rank " << actual_rank
- << std::ends;
- PyErr_SetString(PyExc_RuntimeError, stream.str().c_str());
- throw_error_already_set();
- }
- return;
-}
-
-
-intp size(numeric::array arr)
-{
- if (!PyArray_Check(arr.ptr()))
- {
- PyErr_SetString(PyExc_ValueError, "expected a PyArrayObject");
- throw_error_already_set();
- }
- return PyArray_Size(arr.ptr());
-}
-
-void check_size(numeric::array arr, intp expected_size)
-{
- intp actual_size = size(arr);
- if (actual_size != expected_size)
- {
- std::ostringstream stream;
- stream << "expected size " << expected_size
- << ", found size " << actual_size
- << std::ends;
- PyErr_SetString(PyExc_RuntimeError, stream.str().c_str());
- throw_error_already_set();
- }
- return;
-}
-
-
-std::vector<intp> shape(numeric::array arr)
-{
- std::vector<intp> out_dims;
- if (!PyArray_Check(arr.ptr()))
- {
- PyErr_SetString(PyExc_ValueError, "expected a PyArrayObject");
- throw_error_already_set();
- }
- int* dims_ptr = PyArray_DIMS(arr.ptr());
- int the_rank = rank(arr);
- for (int i = 0; i < the_rank; i++)
- {
- out_dims.push_back(*(dims_ptr + i));
- }
- return out_dims;
-}
-
-
-intp get_dim(boost::python::numeric::array arr, int dimnum)
-{
- assert(dimnum >= 0);
- int the_rank = rank(arr);
- if (the_rank < dimnum)
- {
- std::ostringstream stream;
- stream << "Error: asked for length of dimension " << dimnum
- << " but rank of array is " << the_rank
- << std::ends;
- PyErr_SetString(PyExc_RuntimeError, stream.str().c_str());
- throw_error_already_set();
- }
- std::vector<intp> actual_dims = shape(arr);
- return actual_dims[dimnum];
-}
-
-void check_shape(boost::python::numeric::array arr, std::vector<intp> expected_dims)
-{
- std::vector<intp> actual_dims = shape(arr);
- if (actual_dims != expected_dims)
- {
- std::ostringstream stream;
- stream << "expected dimensions " << vector_str(expected_dims)
- << ", found dimensions " << vector_str(actual_dims)
- << std::ends;
- PyErr_SetString(PyExc_RuntimeError, stream.str().c_str());
- throw_error_already_set();
- }
- return;
-}
-
-void check_dim(boost::python::numeric::array arr, int dimnum, intp dimsize)
-{
- std::vector<intp> actual_dims = shape(arr);
- if (actual_dims[dimnum] != dimsize)
- {
- std::ostringstream stream;
- stream << "Error: expected dimension number "
- << dimnum << " to be length " << dimsize
- << ", but found length " << actual_dims[dimnum]
- << std::ends;
- PyErr_SetString(PyExc_RuntimeError, stream.str().c_str());
- throw_error_already_set();
- }
- return;
-}
-
-
-bool iscontiguous(numeric::array arr)
-{
- // return arr.iscontiguous();
- return PyArray_ISCONTIGUOUS(arr.ptr());
-}
-
-
-void check_contiguous(numeric::array arr)
-{
- if (!iscontiguous(arr))
- {
- PyErr_SetString(PyExc_RuntimeError, "expected a contiguous array");
- throw_error_already_set();
- }
- return;
-}
-
-void* data(numeric::array arr)
-{
- if (!PyArray_Check(arr.ptr()))
- {
- PyErr_SetString(PyExc_ValueError, "expected a PyArrayObject");
- throw_error_already_set();
- }
- return PyArray_DATA(arr.ptr());
-}
-
-
-// Copy data into the array
-void copy_data(boost::python::numeric::array arr, char* new_data)
-{
- char* arr_data = (char *) data(arr);
- intp nbytes = PyArray_NBYTES(arr.ptr());
- for (intp i = 0; i < nbytes; i++)
- {
- arr_data[i] = new_data[i];
- }
- return;
-}
-
-// Return a clone of this array
-numeric::array clone(numeric::array arr)
-{
- object obj(handle<>(PyArray_NewCopy((PyArrayObject*)arr.ptr(), PyArray_CORDER)));
- return makeArray(obj);
-}
-
-// Return a clone of this array with a new type
-numeric::array astype(boost::python::numeric::array arr, PyArray_TYPES t)
-{
- return (numeric::array) arr.astype(type2char(t));
-}
-
-
-int refcount(numeric::array arr)
-{
- return REFCOUNT(arr.ptr());
-}
-
-
-std::vector<intp> strides(numeric::array arr)
-{
- std::vector<intp> out_strides;
- if (!PyArray_Check(arr.ptr()))
- {
- PyErr_SetString(PyExc_ValueError, "expected a PyArrayObject");
- throw_error_already_set();
- }
- intp* strides_ptr = PyArray_STRIDES(arr.ptr());
- intp the_rank = rank(arr);
- for (intp i = 0; i < the_rank; i++)
- {
- out_strides.push_back(*(strides_ptr + i));
- }
- return out_strides;
-}
-
-
-void check_PyArrayElementType(object newo)
-{
- PyArray_TYPES theType = PyArray_TYPES(PyArray_TYPE(newo.ptr()));
- if (theType == PyArray_OBJECT)
- {
- std::ostringstream stream;
- stream << "array elements have been cast to PyArray_OBJECT, "
- << "numhandle can only accept arrays with numerical elements"
- << std::ends;
- PyErr_SetString(PyExc_TypeError, stream.str().c_str());
- throw_error_already_set();
- }
- return;
-}
-
-std::string type2string(PyArray_TYPES t_type)
-{
- return kindstrings[t_type];
-}
-
-char type2char(PyArray_TYPES t_type)
-{
- return kindchars[t_type];
-}
-
-PyArray_TYPES char2type(char e_type)
-{
- return kindtypes[e_type];
-}
-
-
-template <class T>
-inline std::string vector_str(const std::vector<T>& vec)
-{
- std::ostringstream stream;
- stream << "(" << vec[0];
- for (std::size_t i = 1; i < vec.size(); i++)
- {
- stream << ", " << vec[i];
- }
- stream << ")";
- return stream.str();
-}
-
-inline void check_size_match(std::vector<intp> dims, intp n)
-{
- intp total = std::accumulate(dims.begin(), dims.end(), 1, std::multiplies<intp>());
- if (total != n)
- {
- std::ostringstream stream;
- stream << "expected array size " << n
- << ", dimensions give array size " << total
- << std::ends;
- PyErr_SetString(PyExc_TypeError, stream.str().c_str());
- throw_error_already_set();
- }
- return;
-}
-
-
-
-} // namespace numpy_util
-
Deleted: cs/cigma/trunk/pysrc/numpy_util.h
===================================================================
--- cs/cigma/trunk/pysrc/numpy_util.h 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/numpy_util.h 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,315 +0,0 @@
-#ifndef __NUMPY_UTIL_H__
-#define __NUMPY_UTIL_H__
-
-#include <boost/python.hpp>
-#include <numpy/noprefix.h>
-#include <sstream>
-#include <vector>
-#include <numeric>
-#include <map>
-#include <complex>
-
-namespace numpy_util {
-
-
-//!
-/**
- * A free function that extracts a PyArrayObject from any sequential PyObject.
- * @param x a sequential PyObject wrapped in a Boost.Python 'object'.
- * @return a PyArrayObject wrapped in a Boost.Python numeric array.
- */
-boost::python::numeric::array makeArray(boost::python::object x);
-
-/**
- * Creates a one-dimensional numpy array of length n and numpy type t.
- * The elements of the array are initialized to zero.
- * @param n an integer representing the length of the array.
- * @param t elements' numpy type. Default is double.
- * @return a numeric array of size n with elements initialized to zero.
- */
-boost::python::numeric::array makeArray(intp n, PyArray_TYPES t);
-
-
-/**
- * Creates an n-dimensional numpy array with dimensions dimens and numpy
- * type t. The elements of the array are initialized to zero.
- * @param dimens a vector of integers specifying the dimensions of the array.
- * @param t elements' numpy type. Default is double.
- * @return a numeric array of shape dimens with elements initialized to zero.
- */
-boost::python::numeric::array makeArray(std::vector<intp> dimens, PyArray_TYPES t);
-
-
-/**
- * Function template returns PyArray_Type for C++ type.
- * See numpy_util.cpp for specializations.
- * @param T C++ type
- * @return numpy type enum
- */
-template <typename T>
-PyArray_TYPES getEnum(void)
-{
- PyErr_SetString(PyExc_ValueError, "no mapping available for this type");
- boost::python::throw_error_already_set();
- return PyArray_VOID;
-}
-
-
-/**
- * Function template creates a one-dimensional numpy array of length n
- * containing a copy of data at data*. See numpy_util.cpp::getEnum<T>() for a
- * list of specializations.
- * @param T C type of data
- * @param T* data pointer to start of data
- * @param n an integer indicating the size of the array.
- * @return a numpy array of size n with elements initialized to data.
- */
-template <typename T>
-boost::python::numeric::array makeArray(T* data, intp n=0)
-{
- boost::python::object obj(boost::python::handle<>(PyArray_FromDims(1, &n, getEnum<T>())));
- void *array_data = PyArray_DATA((PyArrayObject*) obj.ptr());
- memcpy(array_data, data, PyArray_ITEMSIZE((PyArrayObject*) obj.ptr()) * n);
- return boost::python::extract<boost::python::numeric::array>(obj);
-}
-
-
-/**
- * Function template creates an n-dimensional numpy array with
- * dimensions dims containing a copy of values starting at data.
- * See numpy_util.cpp::getEnum<T>() for a list of specializations.
- * @param T C type of data
- * @param T* data pointer to start of data
- * @param n an integer indicating the size of the array.
- * @return a numpy array of size n with elements initialized to data.
- */
-template <typename T>
-boost::python::numeric::array makeArray(T* data, std::vector<intp> dims)
-{
- intp total = std::accumulate(dims.begin(), dims.end(), 1, std::multiplies<intp>());
- boost::python::object obj(boost::python::handle<>(PyArray_FromDims(dims.size(), &dims[0], getEnum<T>())));
- void *array_data = PyArray_DATA((PyArrayObject*) obj.ptr());
- memcpy(array_data, data, PyArray_ITEMSIZE((PyArrayObject*) obj.ptr()) * total);
- return boost::python::extract<boost::python::numeric::array>(obj);
-}
-
-
-/**
- * Creates a numpy array from a numpy array, referencing the data.
- * @param arr a Boost.Python numeric array.
- * @return a numeric array referencing the input array.
- */
-boost::python::numeric::array makeArray(const boost::python::numeric::array& arr);
-
-
-/**
- * A free function that retrieves the numpy type of a numpy array.
- * @param arr a Boost.Python numeric array.
- * @return the numpy type of the array elements.
- */
-PyArray_TYPES type(boost::python::numeric::array arr);
-
-
-/**
- * Throws an exception if the actual array type is not equal to the expected type.
- * @param arr a Boost.Python numeric array.
- * @param expected_type an expected numpy type.
- */
-void check_type(boost::python::numeric::array arr, PyArray_TYPES expected_types);
-
-
-/**
- * A free function that retrieves the number of dimensions of a numpy array.
- * @param arr a Boost.Python numeric array.
- * @return an integer that indicates the rank of an array.
- */
-int rank(boost::python::numeric::array arr);
-
-
-/**
- * Throws an exception if the actual rank is not equal to the expected rank.
- * @param arr a Boost.Python numeric array.
- * @param expected_rank expected rank of the numeric array.
- */
-void check_rank(boost::python::numeric::array arr, int expected_rank);
-
-
-/**
- * A free function that returns the total size of the array.
- * @param arr a Boost.Python numeric array.
- * @return an integer that indicates the total size of the array.
- */
-intp size(boost::python::numeric::array arr);
-
-
-/**
- * Throw an exception if the actual total size of the array is not equal
- * to the expected size.
- * @param arr a Boost.Python numeric array.
- * @param expected_size the expected size of the array
- */
-void check_size(boost::python::numeric::array arr, intp expected_size);
-
-
-/**
- * Returns the dimensions in a vector.
- * @param arr a Boost.Python numeric array.
- * @return a vector with integer values that indicates the shape of the array.
- */
-std::vector<intp> shape(boost::python::numeric::array arr);
-
-
-/**
- * Returns the size of a specific dimension.
- * @param arr a Boost.Python numeric array.
- * @param dimnum an integer that identifies the dimension to retrieve.
- * @return the size of the requested dimension.
- */
-intp get_dim(boost::python::numeric::array arr, int dimnum);
-
-
-/**
- * Throws an exception if the actual dimensions of the array are not equal
- * to the expected dimensions.
- * @param arr a Boost.Python numeric array.
- * @param expected_dims an integer vector of expected dimensions.
- */
-void check_shape(boost::python::numeric::array arr, std::vector<intp> expected_dims);
-
-
-/**
- * Returns true if the array is contiguous.
- * @param arr a Boost.Python numeric array.
- * @return true if the array is contiguous, false otherwise.
- */
-bool iscontiguous(boost::python::numeric::array arr);
-
-
-/**
- * Throws an exception if the array is not contiguous.
- * @param arr a Boost.Python numeric array.
- */
-void check_contiguous(boost::python::numeric::array arr);
-
-
-/**
- * Returns a pointer to the data in the array.
- * @param arr a Boost.Python numeric array.
- * @return a char pointer pointing to the first element of the array.
- */
-void* data(boost::python::numeric::array arr);
-
-
-/**
- * Copies data into the array.
- * @param arr a Boost.Python numeric array.
- * @param new_data a char pointer referencing the new data.
- */
-void copy_data(boost::python::numeric::array arr, char* new_data);
-
-
-/**
- * Returns a clone of this array.
- * @param arr a Boost.Python numeric array.
- * @return a replicate of the Boost.Python numeric array.
- */
-boost::python::numeric::array clone(boost::python::numeric::array arr);
-
-
-/**
- * Returns a clone of this array with a new type.
- * @return arr a Boost.Python numeric array.
- * @param t PyArray_TYPES of the output array.
- * @return a replicate of 'arr' with type set to 't'.
- */
-boost::python::numeric::array astype(boost::python::numeric::array arr, PyArray_TYPES t);
-
-
-/**
- * Returns the reference count of the array
- * @param arr a Boost.Python numeric array.
- * @return the reference count of the array.
- */
-int refcount(boost::python::numeric::array arr);
-
-
-/**
- * Returns the strides array in a vector of integers.
- * @param arr a Boost.Python numeric array.
- * @return the strides of the array.
- */
-std::vector<intp> strides(boost::python::numeric::array arr);
-
-
-/**
- * Throws an exception if the element of a numpy array is type cast
- * to PyArray_OBJECT.
- * @param newo a Boost.Python object.
- */
-void check_PyArrayElementType(boost::python::object newo);
-
-
-/**
- * Mapping from a PyArray_TYPE to its corresponding name in string.
- */
-typedef std::map<PyArray_TYPES, std::string> KindStringMap;
-
-
-/**
- * Mapping from a PyArray_TYPE to its corresponding typeID in char.
- */
-typedef std::map<PyArray_TYPES, char> KindCharMap;
-
-
-/**
- * Mapping from a typeID to its corresponding PyArray_TYPE.
- */
-typedef std::map<char, PyArray_TYPES> KindTypeMap;
-
-
-/**
- * Converts a PyArray_TYPE to its name in string.
- * @param t_type a PyArray_TYPES
- * @return the corresponding name as a string.
- */
-std::string type2string(PyArray_TYPES t_type);
-
-
-/**
- * Converts a PyArray_TYPE to its single character typecode.
- * @param t_type a PyArray_TYPES.
- * @return the corresponding typecode as a char.
- */
-char type2char(PyArray_TYPES t_type);
-
-
-/**
- * Converts a single character typecode to its PyArray_TYPES.
- * @param e_type a PyArray_TYPES typecode in char.
- * @return its corresponding PyArray_TYPES.
- */
-PyArray_TYPES char2type(char e_type);
-
-
-/**
- * Constructs a string which contains a list of elements extracted
- * from the input vector.
- * @param vec a vector of any type.
- * @return a string that lists the elements from the input vector.
- */
-template <class T>
-inline std::string vector_str(const std::vector<T>& vec);
-
-
-/**
- * Throws an exception if the total size computed from a vector of integers
- * does not match with the expected size.
- * @param dims an integer vector of dimensions.
- * @param n an expected size.
- */
-inline void check_size_match(std::vector<intp> dims, intp n);
-
-
-} // namespace numpy_util
-
-#endif
Deleted: cs/cigma/trunk/pysrc/quux.cpp
===================================================================
--- cs/cigma/trunk/pysrc/quux.cpp 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/quux.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,62 +0,0 @@
-/*
-
-URLs
-
- num_util: helper functions for boost::python::numeric arrays
- http://www.eos.ubc.ca/research/clouds/software/pythonlibs/num_util/
-
- http://www.nabble.com/boost.python-and-numpy.int32-on-64-bit-system-td16741503.html
-
- http://www.google.com/search?q=num_util+2008&btnG=Search&hl=en&safe=off&sa=2
-
- Re: how to return arbitrary (python-)objects from boost? <-- good thread!
- http://article.gmane.org/gmane.comp.python.c%2B%2B/12505
- http://www.nabble.com/how-to-return-arbitrary-(python-)objects-from-boost--td16853297.html
-
-
- http://osdir.com/ml/python.c++/2003-03/msg00035.html
- http://www.gamedev.net/community/forums/topic.asp?topic_id=491860
-
- -------------------------------------------------------------
-
- http://www.google.com/search?client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&channel=s&hl=en&q=boost+python&btnG=Google+Search
- http://www.boost.org/doc/libs/1_36_0/libs/python/doc/index.html
- http://www.boost.org/doc/libs/1_36_0/libs/python/doc/v2/faq.html
- http://www.boost.org/doc/libs/1_36_0/libs/python/doc/tutorial/doc/html/index.html
- http://www.boost.org/doc/libs/1_36_0/libs/python/doc/v2/numeric.html#array-spec
- http://www.boost.org/doc/libs/1_36_0/libs/python/doc/v2/pickle.html
- http://www.boost.org/doc/libs/1_36_0/libs/python/test/pickle1.cpp
- http://www.boost.org/doc/libs/1_36_0/libs/python/doc/v2/indexing.html
- http://wiki.python.org/moin/boost.python/FAQ
- http://wiki.python.org/moin/boost.python/BuildingExtensions
- http://www.boost.org/users/download/
-
-*/
-
-
-#include <boost/python/numeric.hpp>
-#include <boost/python/tuple.hpp>
-
-using namespace boost::python;
-
-// sets the first element in a 2d numeric array
-void set_first_element(numeric::array &y, double value)
-{
- y[make_tuple(0,0)] = value;
-}
-
-// ---------------------------------------------------
-
-BOOST_PYTHON_MODULE(quux)
-{
- import_array();
- numeric::array::set_module_and_type("numpy", "ndarray");
-
- class_<QuuxWrap>("Quux", init<QuuxConfiguration>())
- .def("foo", &QuuxWrap::foo)
- .def("bar", &QuuxWrap::bar)
- .
- .
- .
- ;
-}
Deleted: cs/cigma/trunk/pysrc/setup.py
===================================================================
--- cs/cigma/trunk/pysrc/setup.py 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/setup.py 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,38 +0,0 @@
-#!/usr/bin/env python
-
-from distutils.core import setup, Extension
-
-import os
-
-defines = [('PYTHON_MODULE', 1)]
-
-#libs = os.environ["PYLIBS"].split()
-libs = ['boost_filesystem', 'boost_python']
-
-sources = [
- '_cigma_module.cpp',
- 'numpy_util.cpp',
- 'Py_Exception.cpp',
- 'Py_Hello.cpp',
- 'Py_DataPath.cpp',
- 'Py_Points.cpp',
- 'Py_Locator.cpp',
- 'Py_MeshPart.cpp',
- 'Py_ElementBlock.cpp',
- 'Py_NodeCoordinates.cpp',
- 'Py_Misc.cpp',
-]
-
-setup(name = "_cigma",
- version = "1.0",
- description = "CIG Model Analysis Framework",
- author = "Luis Armendariz",
- author_email = "luis at geodynamics.org",
- url = "http://www.geodynamics.org/",
- ext_modules = [
- Extension("_cigma", [('pysrc/%s' % f) for f in sources],
- define_macros=defines,
- libraries=libs,
- extra_objects=['libcigma.a'])
- ]
-)
Deleted: cs/cigma/trunk/pysrc/tests/runner.py
===================================================================
--- cs/cigma/trunk/pysrc/tests/runner.py 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/tests/runner.py 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,20 +0,0 @@
-#!/usr/bin/env python
-
-
-if __name__ == '__main__':
-
-
- import os
- from os.path import abspath, dirname, splitext
- rootdir = dirname(abspath(__file__))
- testfiles = [fname for fname in os.listdir(rootdir)
- if fname.startswith('test_') and fname.endswith('.py')]
- modules = [__import__(splitext(t)[0]) for t in testfiles]
-
- import unittest
- suite = unittest.TestSuite()
- for mod in modules:
- suite.addTest(mod.create_suite())
- runner = unittest.TextTestRunner(verbosity=2)
- runner.run(suite)
-
Deleted: cs/cigma/trunk/pysrc/tests/test_cigma.py
===================================================================
--- cs/cigma/trunk/pysrc/tests/test_cigma.py 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/tests/test_cigma.py 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,24 +0,0 @@
-#!/usr/bin/env python
-
-import unittest
-
-class tester_t(unittest.TestCase):
- def __init__(self, *args):
- unittest.TestCase.__init__(self, *args)
- def test_one_plus_one(self):
- self.failUnless(1 + 1 == 2)
- def test_cigma_hello(self):
- import _cigma
- msg = _cigma.hello()
- self.failUnless(msg == 'Hello World!')
-
-def create_suite():
- suite = unittest.TestSuite()
- suite.addTest(unittest.makeSuite(tester_t))
- return suite
-
-def run_suite():
- unittest.TextTestRunner(verbosity=2).run(create_suite())
-
-if __name__ == '__main__':
- run_suite()
Deleted: cs/cigma/trunk/pysrc/tests/test_locator.py
===================================================================
--- cs/cigma/trunk/pysrc/tests/test_locator.py 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/pysrc/tests/test_locator.py 2008-10-15 09:07:33 UTC (rev 13043)
@@ -1,40 +0,0 @@
-#!/usr/bin/env python
-
-from _cigma import Locator
-class MyLocator(Locator):
- def __init__(self, n):
- Locator.__init__(self)
- self.pts = range(n)
- def n_dim(self):
- return 3
- def n_idx(self):
- return len(self.pts)
- def idx(self, i):
- return self.pts[i]
-
-import unittest
-class LocatorTest(unittest.TestCase):
- def __init__(self, *args):
- unittest.TestCase.__init__(self, *args)
- def setUp(self):
- self.loc = MyLocator(8)
- def tearDown(self):
- del self.loc
- def test_n_dim(self):
- self.assertEqual(self.loc.n_dim(), 3)
- def test_n_idx(self):
- self.assertEqual(self.loc.n_idx(), 8)
- def test_check_idx(self):
- loc = self.loc
- n = loc.n_idx()
- self.assert_(loc.check_idx(0))
- self.assert_(loc.check_idx(1))
- self.assert_(loc.check_idx(n-1))
- self.assert_(not loc.check_idx(n))
-
-def create_suite():
- suite = unittest.TestSuite()
- suite.addTest(unittest.makeSuite(LocatorTest))
- return suite
-
-
Modified: cs/cigma/trunk/src/README
===================================================================
--- cs/cigma/trunk/src/README 2008-10-15 09:07:32 UTC (rev 13042)
+++ cs/cigma/trunk/src/README 2008-10-15 09:07:33 UTC (rev 13043)
@@ -124,3 +124,19 @@
contrib/cxxtl/
+Python Bindings
+===============
+This directory also contains the C++ bindings to Python
+
+To facilitate the build procedure, we make use of distutils to
+compile the Boost.Python module.
+
+ http://pydoc.org/2.5.1/distutils.extension.html
+
+
+Other projects that use Boost.Python bindings include
+
+ MeshPy: http://mathema.tician.de/software/meshpy
+ CGAL-Python: http://cgal-python.gforge.inria.fr/
+ Ledger: http://github.com/jwiegley/ledger/tree/master
+
Copied: cs/cigma/trunk/src/cigma/Locator.py (from rev 13042, cs/cigma/trunk/pysrc/cigma/Locator.py)
===================================================================
--- cs/cigma/trunk/src/cigma/Locator.py (rev 0)
+++ cs/cigma/trunk/src/cigma/Locator.py 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1,7 @@
+#!/usr/bin/env python
+
+import _cigma
+class Locator(_cigma.Locator):
+ def __init__(self):
+ _cigma.Locator.__init__(self)
+
Copied: cs/cigma/trunk/src/cigma/__init__.py (from rev 13042, cs/cigma/trunk/pysrc/cigma/__init__.py)
===================================================================
--- cs/cigma/trunk/src/cigma/__init__.py (rev 0)
+++ cs/cigma/trunk/src/cigma/__init__.py 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1,2 @@
+__version__ = '1.0.0'
+
Copied: cs/cigma/trunk/src/numpy_util.cpp (from rev 13042, cs/cigma/trunk/pysrc/numpy_util.cpp)
===================================================================
--- cs/cigma/trunk/src/numpy_util.cpp (rev 0)
+++ cs/cigma/trunk/src/numpy_util.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1,410 @@
+#define PY_ARRAY_UNIQUE_SYMBOL PyArrayHandle
+#define NO_IMPORT_ARRAY
+#include "numpy_util.h"
+
+using namespace boost::python;
+
+namespace numpy_util {
+
+
+//
+// specializations for use by makeArray
+//
+
+template <> PyArray_TYPES getEnum<unsigned char>(void) { return PyArray_UBYTE; }
+template <> PyArray_TYPES getEnum<signed char>(void) { return PyArray_BYTE; }
+template <> PyArray_TYPES getEnum<short>(void) { return PyArray_SHORT; }
+template <> PyArray_TYPES getEnum<unsigned short>(void) { return PyArray_USHORT; }
+template <> PyArray_TYPES getEnum<unsigned int>(void) { return PyArray_UINT; }
+template <> PyArray_TYPES getEnum<int>(void) { return PyArray_INT; }
+template <> PyArray_TYPES getEnum<long>(void) { return PyArray_LONG; }
+template <> PyArray_TYPES getEnum<unsigned long>(void) { return PyArray_ULONG; }
+template <> PyArray_TYPES getEnum<long long>(void) { return PyArray_LONGLONG; }
+template <> PyArray_TYPES getEnum<unsigned long long>(void) { return PyArray_ULONGLONG; }
+template <> PyArray_TYPES getEnum<float>(void) { return PyArray_FLOAT; }
+template <> PyArray_TYPES getEnum<double>(void) { return PyArray_DOUBLE; }
+template <> PyArray_TYPES getEnum<long double>(void) { return PyArray_LONGDOUBLE; }
+template <> PyArray_TYPES getEnum<std::complex<float> >(void) { return PyArray_CFLOAT; }
+template <> PyArray_TYPES getEnum<std::complex<double> >(void) { return PyArray_CDOUBLE; }
+template <> PyArray_TYPES getEnum<std::complex<long double> >(void) { return PyArray_CDOUBLE; }
+
+
+typedef KindStringMap::value_type KindStringMapEntry;
+KindStringMapEntry kindStringMapEntries[] = {
+ KindStringMapEntry(PyArray_UBYTE, "PyArray_UBYTE"),
+ KindStringMapEntry(PyArray_BYTE, "PyArray_BYTE" ),
+ KindStringMapEntry(PyArray_SHORT, "PyArray_SHORT"),
+ KindStringMapEntry(PyArray_INT, "PyArray_INT" ),
+ KindStringMapEntry(PyArray_LONG, "PyArray_LONG" ),
+ KindStringMapEntry(PyArray_FLOAT, "PyArray_FLOAT"),
+ KindStringMapEntry(PyArray_DOUBLE, "PyArray_DOUBLE"),
+ KindStringMapEntry(PyArray_CFLOAT, "PyArray_CFLOAT"),
+ KindStringMapEntry(PyArray_CDOUBLE, "PyArray_CDOUBLE"),
+ KindStringMapEntry(PyArray_OBJECT, "PyArray_OBJECT"),
+ KindStringMapEntry(PyArray_NTYPES, "PyArray_NTYPES"),
+ KindStringMapEntry(PyArray_NOTYPE, "PyArray_NOTYPE")
+};
+
+
+typedef KindCharMap::value_type KindCharMapEntry;
+KindCharMapEntry kindCharMapEntries[] = {
+ KindCharMapEntry(PyArray_UBYTE, 'B'),
+ KindCharMapEntry(PyArray_BYTE, 'b'),
+ KindCharMapEntry(PyArray_SHORT, 'h'),
+ KindCharMapEntry(PyArray_INT, 'i'),
+ KindCharMapEntry(PyArray_LONG, 'l'),
+ KindCharMapEntry(PyArray_FLOAT, 'f'),
+ KindCharMapEntry(PyArray_DOUBLE, 'd'),
+ KindCharMapEntry(PyArray_CFLOAT, 'F'),
+ KindCharMapEntry(PyArray_CDOUBLE, 'D'),
+ KindCharMapEntry(PyArray_OBJECT, 'O')
+};
+
+
+typedef KindTypeMap::value_type KindTypeMapEntry;
+KindTypeMapEntry kindTypeMapEntries[] = {
+ KindTypeMapEntry('B', PyArray_UBYTE),
+ KindTypeMapEntry('b', PyArray_BYTE),
+ KindTypeMapEntry('h', PyArray_SHORT),
+ KindTypeMapEntry('i', PyArray_INT),
+ KindTypeMapEntry('l', PyArray_LONG),
+ KindTypeMapEntry('f', PyArray_FLOAT),
+ KindTypeMapEntry('d', PyArray_DOUBLE),
+ KindTypeMapEntry('F', PyArray_CFLOAT),
+ KindTypeMapEntry('D', PyArray_CDOUBLE),
+ KindTypeMapEntry('O', PyArray_OBJECT)
+};
+
+
+int numStringEntries = sizeof(kindStringMapEntries) / sizeof(KindStringMapEntry);
+int numCharEntries = sizeof(kindCharMapEntries) / sizeof(KindCharMapEntry);
+int numTypeEntries = sizeof(kindTypeMapEntries) / sizeof(KindTypeMapEntry);
+
+
+using namespace boost::python;
+
+static KindStringMap kindstrings(kindStringMapEntries, kindStringMapEntries + numStringEntries);
+static KindCharMap kindchars (kindCharMapEntries, kindCharMapEntries + numCharEntries);
+static KindTypeMap kindtypes (kindTypeMapEntries, kindTypeMapEntries + numTypeEntries);
+
+
+// Create a numeric array referencing Python sequence object
+numeric::array makeArray(object x)
+{
+ if (!PySequence_Check(x.ptr()))
+ {
+ PyErr_SetString(PyExc_ValueError, "expected a sequence");
+ throw_error_already_set();
+ }
+ object obj(handle<>(PyArray_ContiguousFromObject(x.ptr(), PyArray_NOTYPE, 0, 0)));
+ check_PyArrayElementType(obj);
+ return extract<numeric::array>(obj);
+}
+
+// Create a one-dimensional numeric array of length n and type t
+numeric::array makeArray(intp n, PyArray_TYPES t=PyArray_DOUBLE)
+{
+ object obj(handle<>(PyArray_FromDims(1, &n, t)));
+ return extract<numeric::array>(obj);
+}
+
+
+// Create a numeric array with dimensions dimens and type t
+numeric::array makeArray(std::vector<intp> dimens, PyArray_TYPES t=PyArray_DOUBLE)
+{
+ object obj(handle<>(PyArray_FromDims(dimens.size(), &dimens[0], t)));
+ return extract<numeric::array>(obj);
+}
+
+
+numeric::array makeArray(const numeric::array& arr)
+{
+ // Returns a reference of arr by calling numeric::array copy constructor.
+ // The copy constructor increases arr's reference count.
+ return numeric::array(arr);
+}
+
+
+PyArray_TYPES type(numeric::array arr)
+{
+ return PyArray_TYPES(PyArray_TYPE(arr.ptr()));
+}
+
+void check_type(numeric::array arr, PyArray_TYPES expected_type)
+{
+ PyArray_TYPES actual_type = type(arr);
+ if (actual_type != expected_type)
+ {
+ std::ostringstream stream;
+ stream << "expected numeric type " << kindstrings[expected_type]
+ << ", found numeric type " << kindstrings[actual_type]
+ << std::ends;
+ PyErr_SetString(PyExc_TypeError, stream.str().c_str());
+ throw_error_already_set();
+ }
+ return;
+}
+
+
+// Return the number of dimensions
+int rank(numeric::array arr)
+{
+ //std::cout << "inside rank" << std::endl;
+ if (!PyArray_Check(arr.ptr()))
+ {
+ PyErr_SetString(PyExc_ValueError, "expected a PyArrayObject");
+ throw_error_already_set();
+ }
+ return PyArray_NDIM(arr.ptr());
+}
+
+void check_rank(numeric::array arr, int expected_rank)
+{
+ int actual_rank = rank(arr);
+ if (actual_rank != expected_rank)
+ {
+ std::ostringstream stream;
+ stream << "expected rank " << expected_rank
+ << ", found rank " << actual_rank
+ << std::ends;
+ PyErr_SetString(PyExc_RuntimeError, stream.str().c_str());
+ throw_error_already_set();
+ }
+ return;
+}
+
+
+intp size(numeric::array arr)
+{
+ if (!PyArray_Check(arr.ptr()))
+ {
+ PyErr_SetString(PyExc_ValueError, "expected a PyArrayObject");
+ throw_error_already_set();
+ }
+ return PyArray_Size(arr.ptr());
+}
+
+void check_size(numeric::array arr, intp expected_size)
+{
+ intp actual_size = size(arr);
+ if (actual_size != expected_size)
+ {
+ std::ostringstream stream;
+ stream << "expected size " << expected_size
+ << ", found size " << actual_size
+ << std::ends;
+ PyErr_SetString(PyExc_RuntimeError, stream.str().c_str());
+ throw_error_already_set();
+ }
+ return;
+}
+
+
+std::vector<intp> shape(numeric::array arr)
+{
+ std::vector<intp> out_dims;
+ if (!PyArray_Check(arr.ptr()))
+ {
+ PyErr_SetString(PyExc_ValueError, "expected a PyArrayObject");
+ throw_error_already_set();
+ }
+ int* dims_ptr = PyArray_DIMS(arr.ptr());
+ int the_rank = rank(arr);
+ for (int i = 0; i < the_rank; i++)
+ {
+ out_dims.push_back(*(dims_ptr + i));
+ }
+ return out_dims;
+}
+
+
+intp get_dim(boost::python::numeric::array arr, int dimnum)
+{
+ assert(dimnum >= 0);
+ int the_rank = rank(arr);
+ if (the_rank < dimnum)
+ {
+ std::ostringstream stream;
+ stream << "Error: asked for length of dimension " << dimnum
+ << " but rank of array is " << the_rank
+ << std::ends;
+ PyErr_SetString(PyExc_RuntimeError, stream.str().c_str());
+ throw_error_already_set();
+ }
+ std::vector<intp> actual_dims = shape(arr);
+ return actual_dims[dimnum];
+}
+
+void check_shape(boost::python::numeric::array arr, std::vector<intp> expected_dims)
+{
+ std::vector<intp> actual_dims = shape(arr);
+ if (actual_dims != expected_dims)
+ {
+ std::ostringstream stream;
+ stream << "expected dimensions " << vector_str(expected_dims)
+ << ", found dimensions " << vector_str(actual_dims)
+ << std::ends;
+ PyErr_SetString(PyExc_RuntimeError, stream.str().c_str());
+ throw_error_already_set();
+ }
+ return;
+}
+
+void check_dim(boost::python::numeric::array arr, int dimnum, intp dimsize)
+{
+ std::vector<intp> actual_dims = shape(arr);
+ if (actual_dims[dimnum] != dimsize)
+ {
+ std::ostringstream stream;
+ stream << "Error: expected dimension number "
+ << dimnum << " to be length " << dimsize
+ << ", but found length " << actual_dims[dimnum]
+ << std::ends;
+ PyErr_SetString(PyExc_RuntimeError, stream.str().c_str());
+ throw_error_already_set();
+ }
+ return;
+}
+
+
+bool iscontiguous(numeric::array arr)
+{
+ // return arr.iscontiguous();
+ return PyArray_ISCONTIGUOUS(arr.ptr());
+}
+
+
+void check_contiguous(numeric::array arr)
+{
+ if (!iscontiguous(arr))
+ {
+ PyErr_SetString(PyExc_RuntimeError, "expected a contiguous array");
+ throw_error_already_set();
+ }
+ return;
+}
+
+void* data(numeric::array arr)
+{
+ if (!PyArray_Check(arr.ptr()))
+ {
+ PyErr_SetString(PyExc_ValueError, "expected a PyArrayObject");
+ throw_error_already_set();
+ }
+ return PyArray_DATA(arr.ptr());
+}
+
+
+// Copy data into the array
+void copy_data(boost::python::numeric::array arr, char* new_data)
+{
+ char* arr_data = (char *) data(arr);
+ intp nbytes = PyArray_NBYTES(arr.ptr());
+ for (intp i = 0; i < nbytes; i++)
+ {
+ arr_data[i] = new_data[i];
+ }
+ return;
+}
+
+// Return a clone of this array
+numeric::array clone(numeric::array arr)
+{
+ object obj(handle<>(PyArray_NewCopy((PyArrayObject*)arr.ptr(), PyArray_CORDER)));
+ return makeArray(obj);
+}
+
+// Return a clone of this array with a new type
+numeric::array astype(boost::python::numeric::array arr, PyArray_TYPES t)
+{
+ return (numeric::array) arr.astype(type2char(t));
+}
+
+
+int refcount(numeric::array arr)
+{
+ return REFCOUNT(arr.ptr());
+}
+
+
+std::vector<intp> strides(numeric::array arr)
+{
+ std::vector<intp> out_strides;
+ if (!PyArray_Check(arr.ptr()))
+ {
+ PyErr_SetString(PyExc_ValueError, "expected a PyArrayObject");
+ throw_error_already_set();
+ }
+ intp* strides_ptr = PyArray_STRIDES(arr.ptr());
+ intp the_rank = rank(arr);
+ for (intp i = 0; i < the_rank; i++)
+ {
+ out_strides.push_back(*(strides_ptr + i));
+ }
+ return out_strides;
+}
+
+
+void check_PyArrayElementType(object newo)
+{
+ PyArray_TYPES theType = PyArray_TYPES(PyArray_TYPE(newo.ptr()));
+ if (theType == PyArray_OBJECT)
+ {
+ std::ostringstream stream;
+ stream << "array elements have been cast to PyArray_OBJECT, "
+ << "numhandle can only accept arrays with numerical elements"
+ << std::ends;
+ PyErr_SetString(PyExc_TypeError, stream.str().c_str());
+ throw_error_already_set();
+ }
+ return;
+}
+
+std::string type2string(PyArray_TYPES t_type)
+{
+ return kindstrings[t_type];
+}
+
+char type2char(PyArray_TYPES t_type)
+{
+ return kindchars[t_type];
+}
+
+PyArray_TYPES char2type(char e_type)
+{
+ return kindtypes[e_type];
+}
+
+
+template <class T>
+inline std::string vector_str(const std::vector<T>& vec)
+{
+ std::ostringstream stream;
+ stream << "(" << vec[0];
+ for (std::size_t i = 1; i < vec.size(); i++)
+ {
+ stream << ", " << vec[i];
+ }
+ stream << ")";
+ return stream.str();
+}
+
+inline void check_size_match(std::vector<intp> dims, intp n)
+{
+ intp total = std::accumulate(dims.begin(), dims.end(), 1, std::multiplies<intp>());
+ if (total != n)
+ {
+ std::ostringstream stream;
+ stream << "expected array size " << n
+ << ", dimensions give array size " << total
+ << std::ends;
+ PyErr_SetString(PyExc_TypeError, stream.str().c_str());
+ throw_error_already_set();
+ }
+ return;
+}
+
+
+
+} // namespace numpy_util
+
Copied: cs/cigma/trunk/src/numpy_util.h (from rev 13042, cs/cigma/trunk/pysrc/numpy_util.h)
===================================================================
--- cs/cigma/trunk/src/numpy_util.h (rev 0)
+++ cs/cigma/trunk/src/numpy_util.h 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1,315 @@
+#ifndef __NUMPY_UTIL_H__
+#define __NUMPY_UTIL_H__
+
+#include <boost/python.hpp>
+#include <numpy/noprefix.h>
+#include <sstream>
+#include <vector>
+#include <numeric>
+#include <map>
+#include <complex>
+
+namespace numpy_util {
+
+
+//!
+/**
+ * A free function that extracts a PyArrayObject from any sequential PyObject.
+ * @param x a sequential PyObject wrapped in a Boost.Python 'object'.
+ * @return a PyArrayObject wrapped in a Boost.Python numeric array.
+ */
+boost::python::numeric::array makeArray(boost::python::object x);
+
+/**
+ * Creates a one-dimensional numpy array of length n and numpy type t.
+ * The elements of the array are initialized to zero.
+ * @param n an integer representing the length of the array.
+ * @param t elements' numpy type. Default is double.
+ * @return a numeric array of size n with elements initialized to zero.
+ */
+boost::python::numeric::array makeArray(intp n, PyArray_TYPES t);
+
+
+/**
+ * Creates an n-dimensional numpy array with dimensions dimens and numpy
+ * type t. The elements of the array are initialized to zero.
+ * @param dimens a vector of integers specifying the dimensions of the array.
+ * @param t elements' numpy type. Default is double.
+ * @return a numeric array of shape dimens with elements initialized to zero.
+ */
+boost::python::numeric::array makeArray(std::vector<intp> dimens, PyArray_TYPES t);
+
+
+/**
+ * Function template returns PyArray_Type for C++ type.
+ * See numpy_util.cpp for specializations.
+ * @param T C++ type
+ * @return numpy type enum
+ */
+template <typename T>
+PyArray_TYPES getEnum(void)
+{
+ PyErr_SetString(PyExc_ValueError, "no mapping available for this type");
+ boost::python::throw_error_already_set();
+ return PyArray_VOID;
+}
+
+
+/**
+ * Function template creates a one-dimensional numpy array of length n
+ * containing a copy of data at data*. See numpy_util.cpp::getEnum<T>() for a
+ * list of specializations.
+ * @param T C type of data
+ * @param T* data pointer to start of data
+ * @param n an integer indicating the size of the array.
+ * @return a numpy array of size n with elements initialized to data.
+ */
+template <typename T>
+boost::python::numeric::array makeArray(T* data, intp n=0)
+{
+ boost::python::object obj(boost::python::handle<>(PyArray_FromDims(1, &n, getEnum<T>())));
+ void *array_data = PyArray_DATA((PyArrayObject*) obj.ptr());
+ memcpy(array_data, data, PyArray_ITEMSIZE((PyArrayObject*) obj.ptr()) * n);
+ return boost::python::extract<boost::python::numeric::array>(obj);
+}
+
+
+/**
+ * Function template creates an n-dimensional numpy array with
+ * dimensions dims containing a copy of values starting at data.
+ * See numpy_util.cpp::getEnum<T>() for a list of specializations.
+ * @param T C type of data
+ * @param T* data pointer to start of data
+ * @param n an integer indicating the size of the array.
+ * @return a numpy array of size n with elements initialized to data.
+ */
+template <typename T>
+boost::python::numeric::array makeArray(T* data, std::vector<intp> dims)
+{
+ intp total = std::accumulate(dims.begin(), dims.end(), 1, std::multiplies<intp>());
+ boost::python::object obj(boost::python::handle<>(PyArray_FromDims(dims.size(), &dims[0], getEnum<T>())));
+ void *array_data = PyArray_DATA((PyArrayObject*) obj.ptr());
+ memcpy(array_data, data, PyArray_ITEMSIZE((PyArrayObject*) obj.ptr()) * total);
+ return boost::python::extract<boost::python::numeric::array>(obj);
+}
+
+
+/**
+ * Creates a numpy array from a numpy array, referencing the data.
+ * @param arr a Boost.Python numeric array.
+ * @return a numeric array referencing the input array.
+ */
+boost::python::numeric::array makeArray(const boost::python::numeric::array& arr);
+
+
+/**
+ * A free function that retrieves the numpy type of a numpy array.
+ * @param arr a Boost.Python numeric array.
+ * @return the numpy type of the array elements.
+ */
+PyArray_TYPES type(boost::python::numeric::array arr);
+
+
+/**
+ * Throws an exception if the actual array type is not equal to the expected type.
+ * @param arr a Boost.Python numeric array.
+ * @param expected_type an expected numpy type.
+ */
+void check_type(boost::python::numeric::array arr, PyArray_TYPES expected_types);
+
+
+/**
+ * A free function that retrieves the number of dimensions of a numpy array.
+ * @param arr a Boost.Python numeric array.
+ * @return an integer that indicates the rank of an array.
+ */
+int rank(boost::python::numeric::array arr);
+
+
+/**
+ * Throws an exception if the actual rank is not equal to the expected rank.
+ * @param arr a Boost.Python numeric array.
+ * @param expected_rank expected rank of the numeric array.
+ */
+void check_rank(boost::python::numeric::array arr, int expected_rank);
+
+
+/**
+ * A free function that returns the total size of the array.
+ * @param arr a Boost.Python numeric array.
+ * @return an integer that indicates the total size of the array.
+ */
+intp size(boost::python::numeric::array arr);
+
+
+/**
+ * Throw an exception if the actual total size of the array is not equal
+ * to the expected size.
+ * @param arr a Boost.Python numeric array.
+ * @param expected_size the expected size of the array
+ */
+void check_size(boost::python::numeric::array arr, intp expected_size);
+
+
+/**
+ * Returns the dimensions in a vector.
+ * @param arr a Boost.Python numeric array.
+ * @return a vector with integer values that indicates the shape of the array.
+ */
+std::vector<intp> shape(boost::python::numeric::array arr);
+
+
+/**
+ * Returns the size of a specific dimension.
+ * @param arr a Boost.Python numeric array.
+ * @param dimnum an integer that identifies the dimension to retrieve.
+ * @return the size of the requested dimension.
+ */
+intp get_dim(boost::python::numeric::array arr, int dimnum);
+
+
+/**
+ * Throws an exception if the actual dimensions of the array are not equal
+ * to the expected dimensions.
+ * @param arr a Boost.Python numeric array.
+ * @param expected_dims an integer vector of expected dimensions.
+ */
+void check_shape(boost::python::numeric::array arr, std::vector<intp> expected_dims);
+
+
+/**
+ * Returns true if the array is contiguous.
+ * @param arr a Boost.Python numeric array.
+ * @return true if the array is contiguous, false otherwise.
+ */
+bool iscontiguous(boost::python::numeric::array arr);
+
+
+/**
+ * Throws an exception if the array is not contiguous.
+ * @param arr a Boost.Python numeric array.
+ */
+void check_contiguous(boost::python::numeric::array arr);
+
+
+/**
+ * Returns a pointer to the data in the array.
+ * @param arr a Boost.Python numeric array.
+ * @return a char pointer pointing to the first element of the array.
+ */
+void* data(boost::python::numeric::array arr);
+
+
+/**
+ * Copies data into the array.
+ * @param arr a Boost.Python numeric array.
+ * @param new_data a char pointer referencing the new data.
+ */
+void copy_data(boost::python::numeric::array arr, char* new_data);
+
+
+/**
+ * Returns a clone of this array.
+ * @param arr a Boost.Python numeric array.
+ * @return a replicate of the Boost.Python numeric array.
+ */
+boost::python::numeric::array clone(boost::python::numeric::array arr);
+
+
+/**
+ * Returns a clone of this array with a new type.
+ * @return arr a Boost.Python numeric array.
+ * @param t PyArray_TYPES of the output array.
+ * @return a replicate of 'arr' with type set to 't'.
+ */
+boost::python::numeric::array astype(boost::python::numeric::array arr, PyArray_TYPES t);
+
+
+/**
+ * Returns the reference count of the array
+ * @param arr a Boost.Python numeric array.
+ * @return the reference count of the array.
+ */
+int refcount(boost::python::numeric::array arr);
+
+
+/**
+ * Returns the strides array in a vector of integers.
+ * @param arr a Boost.Python numeric array.
+ * @return the strides of the array.
+ */
+std::vector<intp> strides(boost::python::numeric::array arr);
+
+
+/**
+ * Throws an exception if the element of a numpy array is type cast
+ * to PyArray_OBJECT.
+ * @param newo a Boost.Python object.
+ */
+void check_PyArrayElementType(boost::python::object newo);
+
+
+/**
+ * Mapping from a PyArray_TYPE to its corresponding name in string.
+ */
+typedef std::map<PyArray_TYPES, std::string> KindStringMap;
+
+
+/**
+ * Mapping from a PyArray_TYPE to its corresponding typeID in char.
+ */
+typedef std::map<PyArray_TYPES, char> KindCharMap;
+
+
+/**
+ * Mapping from a typeID to its corresponding PyArray_TYPE.
+ */
+typedef std::map<char, PyArray_TYPES> KindTypeMap;
+
+
+/**
+ * Converts a PyArray_TYPE to its name in string.
+ * @param t_type a PyArray_TYPES
+ * @return the corresponding name as a string.
+ */
+std::string type2string(PyArray_TYPES t_type);
+
+
+/**
+ * Converts a PyArray_TYPE to its single character typecode.
+ * @param t_type a PyArray_TYPES.
+ * @return the corresponding typecode as a char.
+ */
+char type2char(PyArray_TYPES t_type);
+
+
+/**
+ * Converts a single character typecode to its PyArray_TYPES.
+ * @param e_type a PyArray_TYPES typecode in char.
+ * @return its corresponding PyArray_TYPES.
+ */
+PyArray_TYPES char2type(char e_type);
+
+
+/**
+ * Constructs a string which contains a list of elements extracted
+ * from the input vector.
+ * @param vec a vector of any type.
+ * @return a string that lists the elements from the input vector.
+ */
+template <class T>
+inline std::string vector_str(const std::vector<T>& vec);
+
+
+/**
+ * Throws an exception if the total size computed from a vector of integers
+ * does not match with the expected size.
+ * @param dims an integer vector of dimensions.
+ * @param n an expected size.
+ */
+inline void check_size_match(std::vector<intp> dims, intp n);
+
+
+} // namespace numpy_util
+
+#endif
Copied: cs/cigma/trunk/src/py_DataPath.cpp (from rev 13042, cs/cigma/trunk/pysrc/Py_DataPath.cpp)
===================================================================
--- cs/cigma/trunk/src/py_DataPath.cpp (rev 0)
+++ cs/cigma/trunk/src/py_DataPath.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1,44 @@
+//#include <cigma/DataPath.h> // XXX: create include directory for core cigma headers?
+
+#include "DataPath.h"
+#include <boost/python.hpp>
+#include <boost/cstdint.hpp>
+
+
+const char *DataPath_doc[] = {
+// 0: DataPath.__doc__
+"An object encapsulating a dataset path.\n",
+"Included in the core bindings as a first example",
+// 1: DataPath.__init__.__doc
+"Constructs a path given a colon separated filename:datapath string"
+};
+
+
+template<class DataPath>
+boost::python::str py_repr_DataPath(const DataPath& dp)
+{
+ using namespace boost::python;
+ return str("<" + str(dp) + ">");
+}
+
+void Py_DataPath()
+{
+ using namespace boost::python;
+
+ class_<DataPath>("DataPath", DataPath_doc[0], no_init)
+ .def(init<std::string>(DataPath_doc[1]))
+ .def("exists", &DataPath::exists)
+ .def("empty", &DataPath::empty)
+ .def("extension", &DataPath::extension)
+ .add_property("filename", &DataPath::filename, &DataPath::set_filename)
+ .add_property("location", &DataPath::location, &DataPath::set_location)
+ .def("__repr__", &py_repr_DataPath<DataPath>)
+ .def(self_ns::str(self)) // __str__
+ ;
+}
+
+void export_DataPath()
+{
+ Py_DataPath();
+}
+
Copied: cs/cigma/trunk/src/py_ElementBlock.cpp (from rev 13042, cs/cigma/trunk/pysrc/Py_ElementBlock.cpp)
===================================================================
--- cs/cigma/trunk/src/py_ElementBlock.cpp (rev 0)
+++ cs/cigma/trunk/src/py_ElementBlock.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1,12 @@
+#include "ElementBlock.h"
+#include <boost/python.hpp>
+
+void export_ElementBlock()
+{
+ using namespace cigma;
+ using namespace boost::python;
+
+ class_<ElementBlock, boost::noncopyable>("ElementBlock", no_init)
+ ;
+}
+
Copied: cs/cigma/trunk/src/py_Exception.cpp (from rev 13042, cs/cigma/trunk/pysrc/Py_Exception.cpp)
===================================================================
--- cs/cigma/trunk/src/py_Exception.cpp (rev 0)
+++ cs/cigma/trunk/src/py_Exception.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1,16 @@
+#include "Exception.cpp"
+#include <boost/python.hpp>
+
+using namespace boost::python;
+
+void translator(const cigma::Exception &x)
+{
+ std::string err = x.getName() + ": " + x.getMessage();
+ PyErr_SetString(PyExc_UserWarning, err.c_str());
+}
+
+void export_Exception()
+{
+ register_exception_translator<cigma::Exception>(translator);
+}
+
Copied: cs/cigma/trunk/src/py_FileReader.cpp (from rev 13042, cs/cigma/trunk/pysrc/Py_FileReader.cpp)
===================================================================
--- cs/cigma/trunk/src/py_FileReader.cpp (rev 0)
+++ cs/cigma/trunk/src/py_FileReader.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1,89 @@
+#include "io_file_reader.h"
+#include "io_null_reader.h"
+#include "io_text_reader.h"
+
+#ifdef HAVE_HDF5
+#include "io_hdf5_reader.h"
+#endif
+
+#ifdef HAVE_VTK
+#include "io_vtk_reader.h"
+#include "io_ucd_reader.h"
+#endif
+
+#include <boost/python.hpp>
+
+using namespace boost::python;
+
+struct pyFileReader : FileReader, wrapper<FileReader>
+{
+};
+
+class pyNullReader : NullReader
+{
+};
+
+class pyTextReader : TextReader
+{
+};
+
+/* Wrap the HDF5 reader */
+#ifdef HAVE_HDF5
+class pyHDF5Reader : HDF5_Reader
+{
+};
+#endif
+
+/* Wrap the VTK reader */
+#ifdef HAVE_VTK
+class pyVTKReader : VtkReader
+{
+};
+class pyUCDReader : UCD_Reader
+{
+};
+#endif
+
+
+void export_FileReader()
+{
+ using namespace boost::python;
+
+
+ class_<pyNullReader>("NullReader")
+ .def(init<std::string>())
+ .def("open", &pyNullReader::open)
+ .def("close", &pyVtkReader::close)
+ ;
+
+ class_<pyTextReader>("TextReader")
+ .def(init<std::string>())
+ .def("open", &pyTextReader::open)
+ .def("close", &pyTextReader::close)
+ ;
+
+/* Class definitions for HDF5 */
+#ifdef HAVE_HDF5
+ class_<pyHDF5Reader>("HDF5Reader")
+ .def(init<std::string>())
+ .def("open", &pyHDF5Reader::open)
+ .def("close", &pyHDF5Reader::close)
+ ;
+#endif
+
+/* Class definitions for VTK readers */
+#ifdef HAVE_VTK
+ class_<pyVTKReader>("VTKReader")
+ .def(init<std::string>())
+ .def("open", &pyVTKReader::open)
+ .def("close", &pyVTKReader::close)
+ ;
+ class_<pyUCDReader>("UCDReader")
+ .def(init<std::string())
+ .def("open", &pyUCDReader::open)
+ .def("close", &pyUCDReader::close)
+ ;
+#endif
+
+
+}
Copied: cs/cigma/trunk/src/py_FileWriter.cpp (from rev 13042, cs/cigma/trunk/pysrc/Py_FileWriter.cpp)
===================================================================
--- cs/cigma/trunk/src/py_FileWriter.cpp (rev 0)
+++ cs/cigma/trunk/src/py_FileWriter.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1,71 @@
+#include "io_file_writer.h"
+#include "io_null_writer.h"
+#include "io_text_writer.h"
+
+#ifdef HAVE_HDF5
+#include "io_hdf5_writer.h"
+#endif
+
+#ifdef HAVE_VTK
+#include "io_vtk_writer.h"
+#endif
+
+#include <boost/python.hpp>
+
+class pyNullWriter : public NullWriter
+{
+};
+
+class pyTextWriter : public TextWriter
+{
+};
+
+/* Wrapper for HDF5 */
+#ifdef HAVE_HDF5
+class pyHDF5Writer : public HDF5_Writer
+{
+};
+#endif
+
+/* Wrapper for VTK */
+#ifdef HAVE_VTK
+class pyVTKWriter : public VtkWriter
+{
+};
+#endif
+
+
+void export_FileWriter
+{
+ using namespace boost::python;
+
+ class_<pyNullWriter, bases<NullWriter> >("NullWriter")
+ .def(init<std::string>())
+ .def("open", &pyNullWriter::open)
+ .def("close", &pyNullWriter::close)
+ ;
+
+ class_<pyTextWriter, bases<TextWriter> >("TextWriter")
+ .def(init<std::string>())
+ .def("open", &pyTextWriter::open)
+ .def("close", &pyTextWriter::close)
+ ;
+
+#ifdef HAVE_HDF5
+ class_<pyHDF5Writer, bases<HDF5_Writer> >("HDF5Writer")
+ .def(init<std::string>())
+ .def("open", &pyHDF5Writer::open)
+ .def("close", &pyHDF5Writer::close)
+ ;
+#endif
+
+#ifdef HAVE_VTK
+ class_<pyVTKWriter, bases<VtkWriter> >("VTKWriter")
+ .def(init<std::string>())
+ .def("open", &pyVTKWriter::open)
+ .def("close", &pyVTKWriter::close)
+ ;
+#endif
+
+}
+
Copied: cs/cigma/trunk/src/py_Hello.cpp (from rev 13042, cs/cigma/trunk/pysrc/Py_Hello.cpp)
===================================================================
--- cs/cigma/trunk/src/py_Hello.cpp (rev 0)
+++ cs/cigma/trunk/src/py_Hello.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1,14 @@
+#include <boost/python.hpp>
+
+using namespace boost::python;
+
+std::string hello()
+{
+ return std::string("Hello World!");
+}
+
+void export_Hello()
+{
+ def("hello", hello);
+}
+
Copied: cs/cigma/trunk/src/py_Locator.cpp (from rev 13042, cs/cigma/trunk/pysrc/Py_Locator.cpp)
===================================================================
--- cs/cigma/trunk/src/py_Locator.cpp (rev 0)
+++ cs/cigma/trunk/src/py_Locator.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1,60 @@
+#include "Locator.h"
+#include "AnnLocator.h"
+#include <boost/python.hpp>
+
+using namespace cigma;
+using namespace boost::python;
+
+struct pyLocator : Locator, wrapper<Locator>
+{
+ int n_dim() const
+ {
+ return this->get_override("n_dim")();
+ }
+
+ int n_idx() const
+ {
+ return this->get_override("n_idx")();
+ }
+
+ int idx(int i) const
+ {
+ return this->get_override("idx")(i);
+ }
+
+ void searchBoundingBox(double *bbox)
+ {
+ this->get_override("searchBoundingBox")(bbox);
+ }
+
+ bool check_idx(int i) const
+ {
+ if (override c = this->get_override("check_idx"))
+ {
+ return c(i);
+ }
+ return Locator::check_idx(i);
+ }
+};
+
+void export_Locator()
+{
+ using namespace cigma;
+ using namespace boost::python;
+
+ class_<pyLocator, boost::noncopyable>("Locator")
+ .def("n_dim", pure_virtual(&Locator::n_dim))
+ .def("n_idx", pure_virtual(&Locator::n_idx))
+ .def("idx", pure_virtual(&Locator::idx))
+ //.def("search_bbox", pure_virtual(&Locator::searchBoundingBox))
+ .def("check_idx", &pyLocator::check_idx)
+ ;
+
+ class_<AnnLocator>("AnnLocator")
+ .def("n_dim", &AnnLocator::n_dim)
+ .def("n_idx", &AnnLocator::n_idx)
+ .def("idx", &AnnLocator::idx)
+ ;
+
+}
+
Copied: cs/cigma/trunk/src/py_MeshPart.cpp (from rev 13042, cs/cigma/trunk/pysrc/Py_MeshPart.cpp)
===================================================================
--- cs/cigma/trunk/src/py_MeshPart.cpp (rev 0)
+++ cs/cigma/trunk/src/py_MeshPart.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1,11 @@
+#include "MeshPart.h"
+#include <boost/python.hpp>
+
+void export_MeshPart()
+{
+ using namespace cigma;
+ using namespace boost::python;
+
+ class_<MeshPart>("MeshPart")
+ ;
+}
Copied: cs/cigma/trunk/src/py_Misc.cpp (from rev 13042, cs/cigma/trunk/pysrc/Py_Misc.cpp)
===================================================================
--- cs/cigma/trunk/src/py_Misc.cpp (rev 0)
+++ cs/cigma/trunk/src/py_Misc.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1,29 @@
+#include "numpy_util.h"
+#include "/usr/include/python2.5/numpy/ndarrayobject.h"
+#include <boost/python.hpp>
+
+using namespace boost::python;
+
+numeric::array new_array()
+{
+ return numeric::array(
+ make_tuple(
+ make_tuple(1,2,3),
+ make_tuple(4,5,6),
+ make_tuple(7,8,9)
+ )
+ );
+}
+
+
+numeric::array new_array2()
+{
+ return numpy_util::makeArray(new_array());
+}
+
+void export_Misc()
+{
+ def("new_array", &new_array);
+ def("new_array2", &new_array2);
+}
+
Copied: cs/cigma/trunk/src/py_NodeCoordinates.cpp (from rev 13042, cs/cigma/trunk/pysrc/Py_NodeCoordinates.cpp)
===================================================================
--- cs/cigma/trunk/src/py_NodeCoordinates.cpp (rev 0)
+++ cs/cigma/trunk/src/py_NodeCoordinates.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1,41 @@
+#include "NodeCoordinates.h"
+#include <boost/python.hpp>
+#include <boost/python/numeric.hpp>
+#include <boost/python/tuple.hpp>
+
+using namespace cigma;
+using namespace boost::python;
+
+
+/*
+class NodeCoordinatesWrapper : public NodeCoordinates
+{
+public:
+ NodeCoordinatesWrapper(numeric::array &x)
+ {
+ tuple t = x.factory.getshape();
+ int rank = len(t);
+ if (rank == 2)
+ {
+ std::cout << "rank two!\n";
+ }
+ }
+ ~NodeCoordinatesWrapper()
+ {
+ }
+private:
+ numeric::array _data;
+}; */
+
+
+void export_NodeCoordinates()
+{
+ //typedef NodeCoordinatesWrapper X;
+ typedef NodeCoordinates X;
+
+ class_<X,boost::noncopyable>("NodeCoordinates", no_init)
+ .def_readonly("npts", &X::n_points)
+ .def_readonly("ndim", &X::n_dim)
+ .def("getPoint", &X::getPoint)
+ ;
+}
Copied: cs/cigma/trunk/src/py_Points.cpp (from rev 13042, cs/cigma/trunk/pysrc/Py_Points.cpp)
===================================================================
--- cs/cigma/trunk/src/py_Points.cpp (rev 0)
+++ cs/cigma/trunk/src/py_Points.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1,32 @@
+#include "Points.h"
+#include <boost/python.hpp>
+
+void export_Points()
+{
+ using namespace cigma;
+ using namespace boost::python;
+
+ typedef Points<float,2> Points2f;
+ class_<Points2f,boost::noncopyable>("Points2f", no_init)
+ .def("n_points", &Points2f::n_points)
+ .def("n_dim", &Points2f::n_dim)
+ ;
+
+ typedef Points<float,3> Points3f;
+ class_<Points3f,boost::noncopyable>("Points3f", no_init)
+ .def("n_points", &Points3f::n_points)
+ .def("n_dim", &Points3f::n_dim)
+ ;
+
+ typedef Points<double,2> Points2d;
+ class_<Points2d,boost::noncopyable>("Points2d", no_init)
+ .def("n_points", &Points2d::n_points)
+ .def("n_dim", &Points2d::n_dim)
+ ;
+
+ typedef Points<double,3> Points3d;
+ class_<Points3d,boost::noncopyable>("Points3d", no_init)
+ .def("n_points", &Points3d::n_points)
+ .def("n_dim", &Points3d::n_dim)
+ ;
+}
Copied: cs/cigma/trunk/src/py_cigma_module.cpp (from rev 13042, cs/cigma/trunk/pysrc/_cigma_module.cpp)
===================================================================
--- cs/cigma/trunk/src/py_cigma_module.cpp (rev 0)
+++ cs/cigma/trunk/src/py_cigma_module.cpp 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1,34 @@
+#define PY_ARRAY_UNIQUE_SYMBOL PyArrayHandle
+#include "numpy_util.h"
+#include <boost/python.hpp>
+#include <boost/cstdint.hpp>
+
+extern void export_Exception();
+extern void export_Hello();
+extern void export_DataPath();
+extern void export_Points();
+extern void export_Locator();
+extern void export_MeshPart();
+extern void export_ElementBlock();
+extern void export_NodeCoordinates();
+extern void export_Misc();
+
+using namespace boost::python;
+
+BOOST_PYTHON_MODULE(_cigma)
+{
+ import_array();
+ numeric::array::set_module_and_type("numpy", "ndarray");
+
+ export_Exception();
+
+ export_Hello();
+ export_DataPath();
+ export_Points();
+ export_Locator();
+ export_MeshPart();
+ export_ElementBlock();
+ export_NodeCoordinates();
+ export_Misc();
+}
+
Copied: cs/cigma/trunk/src/py_cigma_setup.py (from rev 13042, cs/cigma/trunk/pysrc/setup.py)
===================================================================
--- cs/cigma/trunk/src/py_cigma_setup.py (rev 0)
+++ cs/cigma/trunk/src/py_cigma_setup.py 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1,43 @@
+#!/usr/bin/env python
+
+#
+# Do not run this file directly. It is called from the
+# top level Makefile.am script.
+#
+
+from distutils.core import setup, Extension
+
+import os
+
+defines = [('PYTHON_MODULE', 1)]
+
+#libs = os.environ["PYLIBS"].split()
+libs = ['boost_filesystem', 'boost_python']
+
+sources = [
+ 'numpy_util.cpp',
+ 'py_cigma_module.cpp',
+ 'py_Exception.cpp',
+ 'py_Hello.cpp',
+ 'py_DataPath.cpp',
+ 'py_Points.cpp',
+ 'py_Locator.cpp',
+ 'py_MeshPart.cpp',
+ 'py_ElementBlock.cpp',
+ 'py_NodeCoordinates.cpp',
+ 'py_Misc.cpp',
+]
+
+setup(name = "_cigma",
+ version = "1.0",
+ description = "CIG Model Analysis Framework",
+ author = "Luis Armendariz",
+ author_email = "luis at geodynamics.org",
+ url = "http://www.geodynamics.org/",
+ ext_modules = [
+ Extension("_cigma", [('src/%s' % f) for f in sources],
+ define_macros=defines,
+ libraries=libs,
+ extra_objects=['libcigma.a'])
+ ]
+)
Copied: cs/cigma/trunk/tests/pytests/runner.py (from rev 13042, cs/cigma/trunk/pysrc/tests/runner.py)
===================================================================
--- cs/cigma/trunk/tests/pytests/runner.py (rev 0)
+++ cs/cigma/trunk/tests/pytests/runner.py 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+
+
+if __name__ == '__main__':
+
+
+ import os
+ from os.path import abspath, dirname, splitext
+ rootdir = dirname(abspath(__file__))
+ testfiles = [fname for fname in os.listdir(rootdir)
+ if fname.startswith('test_') and fname.endswith('.py')]
+ modules = [__import__(splitext(t)[0]) for t in testfiles]
+
+ import unittest
+ suite = unittest.TestSuite()
+ for mod in modules:
+ suite.addTest(mod.create_suite())
+ runner = unittest.TextTestRunner(verbosity=2)
+ runner.run(suite)
+
Copied: cs/cigma/trunk/tests/pytests/test_cigma.py (from rev 13042, cs/cigma/trunk/pysrc/tests/test_cigma.py)
===================================================================
--- cs/cigma/trunk/tests/pytests/test_cigma.py (rev 0)
+++ cs/cigma/trunk/tests/pytests/test_cigma.py 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1,24 @@
+#!/usr/bin/env python
+
+import unittest
+
+class tester_t(unittest.TestCase):
+ def __init__(self, *args):
+ unittest.TestCase.__init__(self, *args)
+ def test_one_plus_one(self):
+ self.failUnless(1 + 1 == 2)
+ def test_cigma_hello(self):
+ import _cigma
+ msg = _cigma.hello()
+ self.failUnless(msg == 'Hello World!')
+
+def create_suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(tester_t))
+ return suite
+
+def run_suite():
+ unittest.TextTestRunner(verbosity=2).run(create_suite())
+
+if __name__ == '__main__':
+ run_suite()
Copied: cs/cigma/trunk/tests/pytests/test_locator.py (from rev 13042, cs/cigma/trunk/pysrc/tests/test_locator.py)
===================================================================
--- cs/cigma/trunk/tests/pytests/test_locator.py (rev 0)
+++ cs/cigma/trunk/tests/pytests/test_locator.py 2008-10-15 09:07:33 UTC (rev 13043)
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+
+from _cigma import Locator
+class MyLocator(Locator):
+ def __init__(self, n):
+ Locator.__init__(self)
+ self.pts = range(n)
+ def n_dim(self):
+ return 3
+ def n_idx(self):
+ return len(self.pts)
+ def idx(self, i):
+ return self.pts[i]
+
+import unittest
+class LocatorTest(unittest.TestCase):
+ def __init__(self, *args):
+ unittest.TestCase.__init__(self, *args)
+ def setUp(self):
+ self.loc = MyLocator(8)
+ def tearDown(self):
+ del self.loc
+ def test_n_dim(self):
+ self.assertEqual(self.loc.n_dim(), 3)
+ def test_n_idx(self):
+ self.assertEqual(self.loc.n_idx(), 8)
+ def test_check_idx(self):
+ loc = self.loc
+ n = loc.n_idx()
+ self.assert_(loc.check_idx(0))
+ self.assert_(loc.check_idx(1))
+ self.assert_(loc.check_idx(n-1))
+ self.assert_(not loc.check_idx(n))
+
+def create_suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(LocatorTest))
+ return suite
+
+
More information about the cig-commits
mailing list