Changeset 25
- Timestamp:
- 05/22/07 01:01:01 (2 years ago)
- Files:
-
- branches/pygetsetcall/configure.in (modified) (1 diff)
- branches/pygetsetcall/m4/nrnpython.m4 (modified) (6 diffs)
- branches/pygetsetcall/src/nrnpython/nrnpy_hoc.cpp (modified) (6 diffs)
- branches/pygetsetcall/src/nrnpython/setup.py.in (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/pygetsetcall/configure.in
r17 r25 441 441 442 442 if test "$exec_prefix" = "NONE" ; then 443 exec_prefix='${prefix}/@host_cpu@' 443 dnl exec_prefix='${prefix}/@host_cpu@' 444 exec_prefix=${prefix}/$host_cpu 445 444 446 fi 445 447 branches/pygetsetcall/m4/nrnpython.m4
r1 r25 2 2 dnl determine configuration if able to run python 3 3 ac_nrn_pyconf_val="" 4 ac_nrn_pyconf_val=` python-c "import distutils.sysconfig4 ac_nrn_pyconf_val=`$4 -c "import distutils.sysconfig 5 5 print distutils.sysconfig.get_config_var('[$2]')"` 6 6 if test $? != 0 ; then … … 59 59 60 60 AC_ARG_WITH([nrnpython], 61 AC_HELP_STRING([--with-nrnpython ],61 AC_HELP_STRING([--with-nrnpython=[desired python binary]], 62 62 [Python interpreter can be used (default is NO) 63 63 Probably need to set PYLIBDIR to find libpython... … … 67 67 ) 68 68 69 if test "$ac_nrn_python" = "yes" ; then 70 ac_nrn_python="python" 71 fi 72 73 69 74 if test "$ac_nrn_python" != "no" ; then 75 70 76 AC_MSG_CHECKING([nrnpython configuration]) 71 77 NRN_DEFINE(USE_PYTHON,1,[define if Python available]) 72 78 if test "$PYVER" = "" ; then 73 AC_NRN_PYCONF(xxx,VERSION,2.4 )79 AC_NRN_PYCONF(xxx,VERSION,2.4,$ac_nrn_python) 74 80 PYVER=python${xxx} 75 81 fi 76 82 if test "$PYINCDIR" = "" ; then 77 AC_NRN_PYCONF(xxx,INCLUDEDIR,"" )83 AC_NRN_PYCONF(xxx,INCLUDEDIR,"",$ac_nrn_python) 78 84 if test "$xxx" = "" ; then 79 85 PYINCDIR="$HOME/python/include/${PYVER}" … … 83 89 fi 84 90 if test "$EXTRAPYLIBS" = "" ; then 85 AC_NRN_PYCONF(EXTRAPYLIBS,LIBS,"" )91 AC_NRN_PYCONF(EXTRAPYLIBS,LIBS,"",$ac_nrn_python) 86 92 fi 87 93 if test "$PYLIB" = "" ; then 88 94 case "$host_os" in 89 95 darwin*) 90 AC_NRN_PYCONF(xxx,LINKFORSHARED,"" )96 AC_NRN_PYCONF(xxx,LINKFORSHARED,"",$ac_nrn_python) 91 97 PYLIB="$xxx" 92 98 PYLIBLINK="$PYLIB" 93 99 ;; 94 100 *) 95 AC_NRN_PYCONF(xxx,LINKFORSHARED,"" )101 AC_NRN_PYCONF(xxx,LINKFORSHARED,"",$ac_nrn_python) 96 102 PYLINKFORSHARED="$xxx" 97 AC_NRN_PYCONF(xxx,LIBDEST,"" )103 AC_NRN_PYCONF(xxx,LIBDEST,"",$ac_nrn_python) 98 104 if test "$xxx" = "" ; then 99 105 PYLIBDIR="$HOME/python/lib" … … 110 116 NRNPYTHON_DEP="../nrnpython/libnrnpython.la" 111 117 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 112 133 build_nrnpython=yes 113 134 AC_NRN_RUNPYTHON … … 117 138 AC_SUBST(NRNPYTHON_DEP) 118 139 AC_SUBST(NRNPYTHON_INCLUDES) 140 AC_SUBST(NRNPYTHON_DEFINES) 141 119 142 120 143 ]) dnl end of AC_NRN_PYTHON branches/pygetsetcall/src/nrnpython/nrnpy_hoc.cpp
r22 r25 5 5 #include <ivoc.h> 6 6 #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 7 19 8 20 extern "C" { … … 402 414 //printf("hocobj_getattro %s\n", n); 403 415 404 # return None by default405 416 PyObject* result = 0; 406 417 … … 410 421 if (self->type_ == 1 && !self->ho_) { 411 422 Py_DECREF(name); 412 return Py_BuildValue(""); 423 PyErr_SetString(PyExc_TypeError, "not a compound type"); 424 //return Py_BuildValue(""); 425 return NULL; 413 426 } 414 427 Symbol* sym = getsym(n, self->ho_); … … 722 735 }; 723 736 737 #ifdef WITH_NUMPY 738 739 740 static 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 776 static 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 910 static 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 724 918 static PyMethodDef hocobj_methods[] = { 725 919 {NULL, NULL, 0, NULL} 726 920 }; 921 922 923 #endif //WITH_NUMPY 727 924 728 925 static PyMemberDef hocobj_members[] = { … … 759 956 0, /* tp_iter */ 760 957 0, /* tp_iternext */ 761 0,//hocobj_methods, /* tp_methods */958 hocobj_methods, /* tp_methods */ 762 959 0,//hocobj_members, /* tp_members */ 763 960 0, /* tp_getset */ … … 784 981 //printf("AddObject HocObject\n"); 785 982 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 786 992 nrnpy_nrn(); 787 993 } branches/pygetsetcall/src/nrnpython/setup.py.in
r22 r25 20 20 print "Warning: Old numpy found and not being used. Please upgrade your version of numpy to > 1.0rc1" 21 21 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)) 24 24 25 25 … … 46 46 "readline" 47 47 ], 48 include_dirs = numpy_include_path,48 include_dirs = [numpy_include_path], 49 49 define_macros=defines 50 50 )

