Changeset 32

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

Added HocVector? type with toarray member

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/pygetsetcall/src/nrnpython/Makefile.am

    r27 r32  
    55NRNPYTHON_INCLUDES = @NRNPYTHON_INCLUDES@ 
    66IV_INCLUDES = @IV_INCLUDE@ $(X_CFLAGS) 
    7 INCLUDES =  -I. -I$(nsrc)/nrniv -I$(nsrc)/ivoc -I$(nsrc)/nrnoc -I$(nsrc)/oc $(NRNPYTHON_INCLUDES) $(IV_INCLUDES) 
     7INCLUDES =  -I. -I$(nsrc)/nrniv -I$(nsrc)/ivoc -I$(nsrc)/nrnoc -I$(nsrc)/oc -I$(nsrc)/gnu $(NRNPYTHON_INCLUDES) $(IV_INCLUDES) 
    88AM_CPPFLAGS = -DOOP=1 -DCABLE=1 -DUSECVODE=1 -DUSEMATRIX=1 -DUSEBBS=1 
    99 
  • branches/pygetsetcall/src/nrnpython/nrnpy_hoc.cpp

    r31 r32  
    44#include <nrnoc2iv.h> 
    55#include <ivoc.h> 
     6#include <ivocvect.h> 
    67#include <ocjump.h> 
    78 
     
    4041extern PyObject* nrnpy_cas(PyObject*, PyObject*); 
    4142extern int section_object_seen; 
    42 extern int vector_capacity(void*); 
     43  //extern int vector_capacity(void*); 
    4344/* 
    4445Because python types have so many methods, attempt to do all set and get 
     
    6364} PyHocObject; 
    6465 
     66 
    6567static PyTypeObject* hocobject_type; 
     68static PyTypeObject* hocvector_type; 
    6669 
    6770static PyObject* nrnexec(PyObject* self, PyObject* args) { 
     
    523526        case OBJECTVAR: // Object* 
    524527                if (!ISARRAY(sym)) { 
     528                        Object *ho; 
     529                         
    525530                        fc.sym = sym; 
    526531                        pcsav = save_pc(&fc); 
    527532                        hoc_objectvar(); 
    528533                        hoc_pc = pcsav; 
    529                         result = hocobj_new(hocobject_type, 0, 0); 
    530                         ((PyHocObject*)result)->ho_ = *hoc_objpop(); 
    531                         ((PyHocObject*)result)->type_ = 1; 
    532                         hoc_obj_ref(((PyHocObject*)result)->ho_); 
     534                        ho = *hoc_objpop(); 
     535                         
     536                        if (is_obj_type(ho,"Vector")){ 
     537                          result = hocobj_new(hocvector_type, 0, 0); 
     538                          ((PyHocObject*)result)->ho_ = ho; 
     539                          ((PyHocObject*)result)->type_ = 1; 
     540                          hoc_obj_ref(((PyHocObject*)result)->ho_); 
     541                        } 
     542                        else { 
     543                          result = hocobj_new(hocobject_type, 0, 0); 
     544                          ((PyHocObject*)result)->ho_ = ho; 
     545                          ((PyHocObject*)result)->type_ = 1; 
     546                          hoc_obj_ref(((PyHocObject*)result)->ho_); 
     547                        } 
    533548                }else{ 
    534549                        result = (PyObject*)intermediate(self, sym, -1); 
     
    672687        // at least check the vector 
    673688        if (po->sym_ == sym_vec_x) { 
    674                 n = vector_capacity(po->ho_->u.this_pointer); 
     689                n = vector_capacity((Vect*)po->ho_->u.this_pointer); 
    675690        }else if (po->sym_ == sym_mat_x) { 
    676691                if (ix >= 0) return 0; 
     
    816831}; 
    817832 
     833 
     834 
     835 
     836static PyObject* hocvec_tonumpy(PyObject* self, PyObject* args) { 
     837   
     838  
     839 
     840#ifdef WITH_NUMPY 
     841 
     842  PyHocObject * ho = (PyHocObject*)self; 
     843 
     844  if (!PyArg_ParseTuple(args, "")) { 
     845    return NULL; 
     846  } 
     847 
     848  if (is_obj_type(ho->ho_,"Vector")) { 
     849    // we can deal with vectors 
     850    Vect* v = (Vect*)ho->ho_->u.this_pointer; 
     851    PyArrayObject*array = NULL; 
     852    int i,n = v->capacity(); 
     853 
     854    array = (PyArrayObject*)PyArray_FromDims(1,&n,PyArray_DOUBLE); 
     855    for (i=0;i<n;i++) { 
     856      *(double*)(array->data + i*array->strides[0]) = (*v)[i]; 
     857    } 
     858 
     859    return Py_BuildValue("N",(PyObject*)array); 
     860 
     861  } 
     862  else { 
     863    PyErr_SetString(PyExc_TypeError, "expected HocVector."); 
     864    return NULL; 
     865  } 
     866 
     867 
     868#else 
     869 
     870 PyErr_SetString(PyExc_RuntimeError, "numpy support was not enabled at build time."); 
     871 return NULL; 
     872 
     873 
     874#endif //WITH_NUMPY 
     875 
     876 
     877} 
     878 
     879 
    818880  /* 
     881 
    819882#ifdef WITH_NUMPY 
    820883 
     
    10061069 
    10071070  */ 
     1071 
     1072 
     1073static PyMethodDef hocvec_methods[] = { 
     1074  {"toarray",hocvec_tonumpy,METH_VARARGS,"toarray(self) returns a numpy array of self."}, 
     1075  {NULL, NULL, 0, NULL} 
     1076}; 
     1077 
    10081078 
    10091079static PyMemberDef hocobj_members[] = { 
     
    10531123}; 
    10541124 
     1125static PyTypeObject nrnpy_HocVectorType = { 
     1126    PyObject_HEAD_INIT(NULL) 
     1127    0,                         /*ob_size*/ 
     1128    "hoc.HocVector",         /*tp_name*/ 
     1129    sizeof(PyHocObject), /*tp_basicsize*/ 
     1130    0,                         /*tp_itemsize*/ 
     1131    (destructor)hocobj_dealloc,                        /*tp_dealloc*/ 
     1132    (printfunc)hocobj_print,                         /*tp_print*/ 
     1133    0,                         /*tp_getattr*/ 
     1134    0,                         /*tp_setattr*/ 
     1135    0,                         /*tp_compare*/ 
     1136    0,                         /*tp_repr*/ 
     1137    0,                         /*tp_as_number*/ 
     1138    &hocobj_seqmeth,                         /*tp_as_sequence*/ 
     1139    0,                         /*tp_as_mapping*/ 
     1140    0,                         /*tp_hash */ 
     1141    (ternaryfunc)hocobj_call,                         /*tp_call*/ 
     1142    0,                         /*tp_str*/ 
     1143    (getattrofunc)hocobj_getattro,                         /*tp_getattro*/ 
     1144    (setattrofunc)hocobj_setattro,                         /*tp_setattro*/ 
     1145    0,                         /*tp_as_buffer*/ 
     1146    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,        /*tp_flags*/ 
     1147    "Hoc Vector wrapper",         /* tp_doc */ 
     1148    0,                         /* tp_traverse */ 
     1149    0,                         /* tp_clear */ 
     1150    0,                         /* tp_richcompare */ 
     1151    0,                         /* tp_weaklistoffset */ 
     1152    0,                         /* tp_iter */ 
     1153    0,                         /* tp_iternext */ 
     1154    hocvec_methods,             /* tp_methods */ 
     1155    0,//hocobj_members,             /* tp_members */ 
     1156    0,                         /* tp_getset */ 
     1157    0,                         /* tp_base */ 
     1158    0,                         /* tp_dict */ 
     1159    0,                         /* tp_descr_get */ 
     1160    0,                         /* tp_descr_set */ 
     1161    0,                         /* tp_dictoffset */ 
     1162    (initproc)hocobj_init,      /* tp_init */ 
     1163    0,                         /* tp_alloc */ 
     1164    hocobj_new,                 /* tp_new */ 
     1165}; 
     1166 
     1167 
     1168 
     1169 
     1170 
    10551171myPyMODINIT_FUNC nrnpy_hoc() { 
    10561172        PyObject* m; 
     
    10651181        PyModule_AddObject(m, "HocObject", (PyObject*)hocobject_type); 
    10661182 
     1183        //nrnpy_HocVectorType.tp_new = PyType_GenericNew; 
     1184        hocvector_type = &nrnpy_HocVectorType; 
     1185        if (PyType_Ready(&nrnpy_HocVectorType) < 0) 
     1186                return; 
     1187 
     1188 
     1189 
    10671190        Symbol* s; 
    10681191        s = hoc_lookup("Vector"); assert(s);