Changeset 25

Show
Ignore:
Timestamp:
05/22/07 01:01:01 (2 years ago)
Author:
emuller
Message:

--with-nrnpython optional python binary, conditional numpy support in nrnpython

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/pygetsetcall/configure.in

    r17 r25  
    441441 
    442442if test "$exec_prefix" = "NONE" ; then 
    443         exec_prefix='${prefix}/@host_cpu@' 
     443dnl     exec_prefix='${prefix}/@host_cpu@' 
     444        exec_prefix=${prefix}/$host_cpu 
     445 
    444446fi 
    445447 
  • branches/pygetsetcall/m4/nrnpython.m4

    r1 r25  
    22        dnl determine configuration if able to  run python 
    33        ac_nrn_pyconf_val="" 
    4         ac_nrn_pyconf_val=`python -c "import distutils.sysconfig 
     4        ac_nrn_pyconf_val=`$4 -c "import distutils.sysconfig 
    55print distutils.sysconfig.get_config_var('[$2]')"` 
    66        if test $? != 0 ; then 
     
    5959 
    6060        AC_ARG_WITH([nrnpython], 
    61                 AC_HELP_STRING([--with-nrnpython], 
     61                AC_HELP_STRING([--with-nrnpython=[desired python binary]], 
    6262                        [Python interpreter can be used (default is NO) 
    6363Probably need to set PYLIBDIR to find libpython... 
     
    6767        ) 
    6868 
     69        if test "$ac_nrn_python" = "yes" ; then 
     70                ac_nrn_python="python" 
     71        fi 
     72 
     73 
    6974        if test "$ac_nrn_python" != "no" ; then 
     75                 
    7076                AC_MSG_CHECKING([nrnpython configuration]) 
    7177                NRN_DEFINE(USE_PYTHON,1,[define if Python available]) 
    7278                if test "$PYVER" = "" ; then 
    73                         AC_NRN_PYCONF(xxx,VERSION,2.4
     79                        AC_NRN_PYCONF(xxx,VERSION,2.4,$ac_nrn_python
    7480                        PYVER=python${xxx} 
    7581                fi 
    7682                if test "$PYINCDIR" = "" ; then 
    77                         AC_NRN_PYCONF(xxx,INCLUDEDIR,""
     83                        AC_NRN_PYCONF(xxx,INCLUDEDIR,"",$ac_nrn_python
    7884                        if test "$xxx" = "" ; then 
    7985                                PYINCDIR="$HOME/python/include/${PYVER}" 
     
    8389                fi 
    8490                if test "$EXTRAPYLIBS" = "" ; then 
    85                         AC_NRN_PYCONF(EXTRAPYLIBS,LIBS,""
     91                        AC_NRN_PYCONF(EXTRAPYLIBS,LIBS,"",$ac_nrn_python
    8692                fi 
    8793                if test "$PYLIB" = "" ; then 
    8894                        case "$host_os" in 
    8995                        darwin*) 
    90                                 AC_NRN_PYCONF(xxx,LINKFORSHARED,""
     96                                AC_NRN_PYCONF(xxx,LINKFORSHARED,"",$ac_nrn_python
    9197                                PYLIB="$xxx" 
    9298                                PYLIBLINK="$PYLIB" 
    9399                                ;; 
    94100                        *) 
    95                                 AC_NRN_PYCONF(xxx,LINKFORSHARED,""
     101                                AC_NRN_PYCONF(xxx,LINKFORSHARED,"",$ac_nrn_python
    96102                                PYLINKFORSHARED="$xxx"                           
    97                                 AC_NRN_PYCONF(xxx,LIBDEST,""
     103                                AC_NRN_PYCONF(xxx,LIBDEST,"",$ac_nrn_python
    98104                                if test "$xxx" = "" ; then 
    99105                                        PYLIBDIR="$HOME/python/lib" 
     
    110116                NRNPYTHON_DEP="../nrnpython/libnrnpython.la" 
    111117                NRNPYTHON_INCLUDES="-I${PYINCDIR}" 
     118 
     119                CMD="import numpy;print numpy.__path__@<:@0@:>@ + '''/core/include''' " 
     120                PYTHON_NUMPY_INCLUDE=`${ac_nrn_python} -c "${CMD}"` 
     121 
     122                if test "$PYTHON_NUMPY_INCLUDE" != ""; then 
     123                        HAVE_NUMPY="yes" 
     124                        NRNPYTHON_INCLUDES="${NRNPYTHON_INCLUDES} -I${PYTHON_NUMPY_INCLUDE}" 
     125                        NRNPYTHON_DEFINES="-DWITH_NUMPY" 
     126                else 
     127                        HAVE_NUMPY="no" 
     128                        NRNPYTHON_DEFINES="-UWITH_NUMPY" 
     129                fi 
     130 
     131                build_nrnpython=yes 
     132 
    112133                build_nrnpython=yes 
    113134                AC_NRN_RUNPYTHON 
     
    117138        AC_SUBST(NRNPYTHON_DEP) 
    118139        AC_SUBST(NRNPYTHON_INCLUDES) 
     140        AC_SUBST(NRNPYTHON_DEFINES) 
     141 
    119142 
    120143]) dnl end of AC_NRN_PYTHON 
  • branches/pygetsetcall/src/nrnpython/nrnpy_hoc.cpp

    r22 r25  
    55#include <ivoc.h> 
    66#include <ocjump.h> 
     7 
     8#ifdef WITH_NUMPY 
     9 
     10// needed for C extension using NumPy 
     11#define PY_ARRAY_UNIQUE_SYMBOL _nrnpy_arrayu 
     12 
     13 
     14// Numeric Python header 
     15#include "numpy/arrayobject.h" 
     16 
     17#endif // WITH_NUMPY 
     18 
    719 
    820extern "C" { 
     
    402414//printf("hocobj_getattro %s\n", n); 
    403415 
    404         # return None by default 
    405416        PyObject* result = 0; 
    406417 
     
    410421        if (self->type_ == 1 && !self->ho_) { 
    411422                Py_DECREF(name); 
    412                 return Py_BuildValue(""); 
     423                PyErr_SetString(PyExc_TypeError, "not a compound type"); 
     424                //return Py_BuildValue(""); 
     425                return NULL; 
    413426        } 
    414427        Symbol* sym = getsym(n, self->ho_); 
     
    722735}; 
    723736 
     737#ifdef WITH_NUMPY 
     738 
     739 
     740static PyObject* PyObj_FromNrnObj(Object* obj) { 
     741 
     742  if (obj==NULL) { 
     743    printf("Warning: PyObj_FromNrnObj obj NULL\n"); 
     744    //return null; 
     745    Py_INCREF(Py_None); 
     746    return Py_None; 
     747  } 
     748   
     749 
     750  if (is_obj_type(obj,"Vector")) { 
     751    // we can deal with vectors 
     752    Vect* v = (Vect*)obj->u.this_pointer; 
     753    PyArrayObject*array = NULL; 
     754    int i,n = v->capacity(); 
     755 
     756    array = (PyArrayObject*)PyArray_FromDims(1,&n,PyArray_DOUBLE); 
     757    for (i=0;i<n;i++) { 
     758      *(double*)(array->data + i*array->strides[0]) = (*v)[i]; 
     759    } 
     760 
     761    return (PyObject*)array; 
     762 
     763  } 
     764 
     765  printf("Warning: PyObj_FromNrnObj cannot handle obj type, returning NULL\n"); 
     766  //return NULL; 
     767  Py_INCREF(Py_None); 
     768  return Py_None; 
     769   
     770} 
     771 
     772 
     773 
     774 
     775 
     776static int hocobj_tonumpy(PyObject* self, PyObject* args) { 
     777 
     778 
     779  if (!PyArg_ParseTuple(args, "")) { 
     780    return NULL; 
     781  } 
     782 
     783  PyArrayObject* array = NULL; 
     784 
     785 
     786  if (self->type_ == 2 || self->type_ == 3) { 
     787 
     788    Symbol* sym = self->sym_; 
     789 
     790    if (!sym) { 
     791      PyErr_SetString(PyExc_RuntimeError, "sym==NULL"); 
     792      return NULL; 
     793    } 
     794 
     795    if (sym->type == VAR) { 
     796 
     797      if (ISARRAY(sym)) { 
     798 
     799        // Make a numpy array from an ndim NEURON array 
     800 
     801        int total = hoc_total_array(sym); 
     802        PyArrayObject* array = NULL; 
     803        int i; 
     804 
     805        Arrayinfo* a = sym->arayinfo; 
     806        double* p = OPVAL(sym); 
     807           
     808        if (a) { 
     809          PyObject* pDims = PyList_New(a->nsub); 
     810 
     811          for (i= a->nsub-1;i>=0;--i) { 
     812            PyList_SetItem(pDims,i,PyInt_FromLong(a->sub[i])); 
     813          } 
     814 
     815          // first contiguous, then reshape 
     816          array = (PyArrayObject*)PyArray_FromDims(1,&total,PyArray_DOUBLE); 
     817          if (array==NULL) { 
     818            PyErr_SetString(PyExc_RuntimeError,"hoc.get hoc error allocating array."); 
     819            return NULL; 
     820          } 
     821 
     822          // fill array memory 
     823 
     824          for (i=0;i<total;i++) { 
     825            *(double*)(array->data + i*array->strides[0]) = p[i]; 
     826          } 
     827 
     828          // return shaped array 
     829 
     830          pObj = (PyObject*)PyArray_Reshape(array,pDims); 
     831          // throw away pDims 
     832          Py_DECREF(pDims); 
     833 
     834        } 
     835 
     836      } 
     837      else { 
     838        // VAR is not an array 
     839 
     840        pObj = PyFloat_FromDouble(*OPVAL(sym)); 
     841      } 
     842 
     843    } 
     844    else if (sym->type == OBJECTVAR) { 
     845 
     846      if (ISARRAY(sym)) { 
     847 
     848        // create numpy array of type 'O' (PyObjects) from NEURON OBJREF array 
     849 
     850        int total = hoc_total_array(sym); 
     851        PyArrayObject* array = NULL; 
     852        int i; 
     853 
     854        Arrayinfo* a = sym->arayinfo; 
     855 
     856        if (a) { 
     857          PyObject* pDims = PyList_New(a->nsub); 
     858 
     859          for (i= a->nsub-1;i>=0;--i) { 
     860            PyList_SetItem(pDims,i,PyInt_FromLong(a->sub[i])); 
     861          } 
     862 
     863 
     864          // first contiguous, then reshape 
     865 
     866          array = (PyArrayObject*)PyArray_FromDims(1,&total,PyArray_OBJECT); 
     867          if (array==NULL) { 
     868            PyErr_SetString(PyExc_RuntimeError,"hoc.get hoc error allocating array."); 
     869            return NULL; 
     870          } 
     871 
     872          // fill array memory 
     873 
     874          for (i=0;i<total;i++) { 
     875            *(PyObject**)(array->data + i*array->strides[0]) = PyObj_FromNrnObj(OPOBJ(sym)[i]); 
     876          } 
     877 
     878          // return shaped array 
     879 
     880          pObj = (PyObject*)PyArray_Reshape(array,pDims); 
     881          // throw away pDims 
     882          Py_DECREF(pDims); 
     883               
     884          if (pObj==NULL) { 
     885            PyErr_SetString(PyExc_RuntimeError,"hoc.get hoc error reshaping array."); 
     886            return NULL; 
     887          } 
     888 
     889 
     890        } 
     891 
     892      } 
     893      else { 
     894        // return single object 
     895        pObj = PyObj_FromNrnObj(OPOBJ(sym)[0]); 
     896      } 
     897 
     898 
     899  } 
     900  else { 
     901    PyErr_SetString(PyExc_TypeError, "not a compound type"); 
     902    return NULL; 
     903  } 
     904 
     905} 
     906 
     907 
     908 
     909 
     910static PyMethodDef hocobj_methods[] = { 
     911  {"toarray",hocobj_tonumpy,METH_VARARGS,"toarray(self) returns a numpy array of self."}, 
     912  {NULL, NULL, 0, NULL} 
     913}; 
     914 
     915 
     916#else 
     917 
    724918static PyMethodDef hocobj_methods[] = { 
    725919        {NULL, NULL, 0, NULL} 
    726920}; 
     921 
     922 
     923#endif //WITH_NUMPY 
    727924 
    728925static PyMemberDef hocobj_members[] = { 
     
    759956    0,                         /* tp_iter */ 
    760957    0,                         /* tp_iternext */ 
    761     0,//hocobj_methods,             /* tp_methods */ 
     958    hocobj_methods,             /* tp_methods */ 
    762959    0,//hocobj_members,             /* tp_members */ 
    763960    0,                         /* tp_getset */ 
     
    784981//printf("AddObject HocObject\n"); 
    785982        PyModule_AddObject(m, "HocObject", (PyObject*)hocobject_type); 
     983 
     984#ifdef WITH_NUMPY 
     985 
     986        // setup for numpy 
     987        import_array(); 
     988 
     989#endif // WITH_NUMPY 
     990 
     991 
    786992        nrnpy_nrn(); 
    787993} 
  • branches/pygetsetcall/src/nrnpython/setup.py.in

    r22 r25  
    2020        print "Warning: Old numpy found and not being used.  Please upgrade your version of numpy to > 1.0rc1" 
    2121    else: 
    22     numpy_include_path = numpy.__path__[0]+'/core/include' 
    23     defines.append(('WITH_NUMPY',None)) 
     22        numpy_include_path = numpy.__path__[0]+'/core/include' 
     23        defines.append(('WITH_NUMPY',None)) 
    2424 
    2525 
     
    4646        "readline" 
    4747      ], 
    48       include_dirs = numpy_include_path
     48      include_dirs = [numpy_include_path]
    4949      define_macros=defines 
    5050    )