| 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 | } |
|---|
| | 833 | |
|---|
| | 834 | |
|---|
| | 835 | |
|---|
| | 836 | static 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 | |
|---|
| | 1125 | static 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 | |
|---|