| 18 | | PyArg_ParseTuple(args, "|d", &hoc_ac_); |
|---|
| 19 | | return Py_BuildValue("d", hoc_ac_); |
|---|
| 20 | | } |
|---|
| | 38 | |
|---|
| | 39 | if (!PyArg_ParseTuple(args, "|d", &hoc_ac_)) { |
|---|
| | 40 | PyErr_SetString(PyExc_RuntimeError,"hoc.hoc_ac error parsing args."); |
|---|
| | 41 | return NULL; |
|---|
| | 42 | } |
|---|
| | 43 | |
|---|
| | 44 | return Py_BuildValue("d", hoc_ac_); |
|---|
| | 45 | } |
|---|
| | 46 | |
|---|
| | 47 | static PyObject* hoc_gettype(PyObject* self, PyObject* args) { |
|---|
| | 48 | const char* name; |
|---|
| | 49 | if (!PyArg_ParseTuple(args, "s", &name)) { |
|---|
| | 50 | PyErr_SetString(PyExc_RuntimeError,"hoc.get error parsing args."); |
|---|
| | 51 | return NULL; |
|---|
| | 52 | } |
|---|
| | 53 | |
|---|
| | 54 | Symbol* sym; |
|---|
| | 55 | sym = hoc_table_lookup(name, hoc_top_level_symlist); |
|---|
| | 56 | if (!sym) { |
|---|
| | 57 | sym = hoc_table_lookup(name, hoc_built_in_symlist); |
|---|
| | 58 | |
|---|
| | 59 | if (!sym) { |
|---|
| | 60 | PyErr_SetString(PyExc_RuntimeError,"hoc.get hoc name not defined."); |
|---|
| | 61 | return NULL; |
|---|
| | 62 | |
|---|
| | 63 | } |
|---|
| | 64 | |
|---|
| | 65 | } |
|---|
| | 66 | |
|---|
| | 67 | |
|---|
| | 68 | |
|---|
| | 69 | |
|---|
| | 70 | // return type |
|---|
| | 71 | |
|---|
| | 72 | return Py_BuildValue("i", sym->type); |
|---|
| | 73 | } |
|---|
| | 74 | |
|---|
| | 75 | |
|---|
| | 76 | static PyObject* PyObj_FromNrnObj(Object* obj) { |
|---|
| | 77 | |
|---|
| | 78 | if (obj==NULL) { |
|---|
| | 79 | printf("Warning: PyObj_FromNrnObj obj NULL\n"); |
|---|
| | 80 | //return null; |
|---|
| | 81 | Py_INCREF(Py_None); |
|---|
| | 82 | return Py_None; |
|---|
| | 83 | } |
|---|
| | 84 | |
|---|
| | 85 | |
|---|
| | 86 | if (is_obj_type(obj,"Vector")) { |
|---|
| | 87 | // we can deal with vectors |
|---|
| | 88 | Vect* v = (Vect*)obj->u.this_pointer; |
|---|
| | 89 | PyArrayObject*array = NULL; |
|---|
| | 90 | int i,n = v->capacity(); |
|---|
| | 91 | |
|---|
| | 92 | array = (PyArrayObject*)PyArray_FromDims(1,&n,PyArray_DOUBLE); |
|---|
| | 93 | for (i=0;i<n;i++) { |
|---|
| | 94 | *(double*)(array->data + i*array->strides[0]) = (*v)[i]; |
|---|
| | 95 | } |
|---|
| | 96 | |
|---|
| | 97 | return (PyObject*)array; |
|---|
| | 98 | |
|---|
| | 99 | } |
|---|
| | 100 | |
|---|
| | 101 | printf("Warning: PyObj_FromNrnObj cannot handle obj type, returning NULL\n"); |
|---|
| | 102 | //return NULL; |
|---|
| | 103 | Py_INCREF(Py_None); |
|---|
| | 104 | return Py_None; |
|---|
| | 105 | |
|---|
| | 106 | } |
|---|
| | 107 | |
|---|
| | 108 | static PyObject* hoc_get(PyObject* self, PyObject* args) { |
|---|
| | 109 | const char* name; |
|---|
| | 110 | if (!PyArg_ParseTuple(args, "s", &name)) { |
|---|
| | 111 | PyErr_SetString(PyExc_RuntimeError,"hoc.get error parsing args."); |
|---|
| | 112 | return NULL; |
|---|
| | 113 | } |
|---|
| | 114 | |
|---|
| | 115 | Symbol* sym; |
|---|
| | 116 | sym = hoc_table_lookup(name, hoc_top_level_symlist); |
|---|
| | 117 | if (!sym) { |
|---|
| | 118 | sym = hoc_table_lookup(name, hoc_built_in_symlist); |
|---|
| | 119 | |
|---|
| | 120 | if (!sym) { |
|---|
| | 121 | PyErr_SetString(PyExc_RuntimeError,"hoc.get hoc name not defined."); |
|---|
| | 122 | return NULL; |
|---|
| | 123 | |
|---|
| | 124 | } |
|---|
| | 125 | |
|---|
| | 126 | |
|---|
| | 127 | } |
|---|
| | 128 | |
|---|
| | 129 | // switch on type |
|---|
| | 130 | |
|---|
| | 131 | PyObject *pObj = NULL; |
|---|
| | 132 | |
|---|
| | 133 | if (sym->type == STRING) { |
|---|
| | 134 | //printf("narg=%d",sym->narg); |
|---|
| | 135 | //double * p = hoc_objectdata[sym->u.oboff].pval |
|---|
| | 136 | pObj = PyString_FromString(*OPSTR(sym)); |
|---|
| | 137 | } |
|---|
| | 138 | else if (sym->type == VAR) { |
|---|
| | 139 | |
|---|
| | 140 | if (ISARRAY(sym)) { |
|---|
| | 141 | |
|---|
| | 142 | // Make a numpy array from an ndim NEURON array |
|---|
| | 143 | |
|---|
| | 144 | int total = hoc_total_array(sym); |
|---|
| | 145 | PyArrayObject* array = NULL; |
|---|
| | 146 | int i; |
|---|
| | 147 | |
|---|
| | 148 | Arrayinfo* a = sym->arayinfo; |
|---|
| | 149 | double* p = OPVAL(sym); |
|---|
| | 150 | |
|---|
| | 151 | if (a) { |
|---|
| | 152 | PyObject* pDims = PyList_New(a->nsub); |
|---|
| | 153 | |
|---|
| | 154 | for (i= a->nsub-1;i>=0;--i) { |
|---|
| | 155 | PyList_SetItem(pDims,i,PyInt_FromLong(a->sub[i])); |
|---|
| | 156 | } |
|---|
| | 157 | |
|---|
| | 158 | // first contiguous, then reshape |
|---|
| | 159 | array = (PyArrayObject*)PyArray_FromDims(1,&total,PyArray_DOUBLE); |
|---|
| | 160 | if (array==NULL) { |
|---|
| | 161 | PyErr_SetString(PyExc_RuntimeError,"hoc.get hoc error allocating array."); |
|---|
| | 162 | return NULL; |
|---|
| | 163 | } |
|---|
| | 164 | |
|---|
| | 165 | // fill array memory |
|---|
| | 166 | |
|---|
| | 167 | for (i=0;i<total;i++) { |
|---|
| | 168 | *(double*)(array->data + i*array->strides[0]) = p[i]; |
|---|
| | 169 | } |
|---|
| | 170 | |
|---|
| | 171 | // return shaped array |
|---|
| | 172 | |
|---|
| | 173 | pObj = (PyObject*)PyArray_Reshape(array,pDims); |
|---|
| | 174 | |
|---|
| | 175 | } |
|---|
| | 176 | |
|---|
| | 177 | } |
|---|
| | 178 | else { |
|---|
| | 179 | // VAR is not an array |
|---|
| | 180 | |
|---|
| | 181 | pObj = PyFloat_FromDouble(*OPVAL(sym)); |
|---|
| | 182 | } |
|---|
| | 183 | |
|---|
| | 184 | } |
|---|
| | 185 | else if (sym->type == OBJECTVAR) { |
|---|
| | 186 | |
|---|
| | 187 | if (ISARRAY(sym)) { |
|---|
| | 188 | |
|---|
| | 189 | // create numpy array of type 'O' (PyObjects) from NEURON OBJREF array |
|---|
| | 190 | |
|---|
| | 191 | int total = hoc_total_array(sym); |
|---|
| | 192 | PyArrayObject* array = NULL; |
|---|
| | 193 | int i; |
|---|
| | 194 | |
|---|
| | 195 | Arrayinfo* a = sym->arayinfo; |
|---|
| | 196 | |
|---|
| | 197 | if (a) { |
|---|
| | 198 | PyObject* pDims = PyList_New(a->nsub); |
|---|
| | 199 | |
|---|
| | 200 | for (i= a->nsub-1;i>=0;--i) { |
|---|
| | 201 | PyList_SetItem(pDims,i,PyInt_FromLong(a->sub[i])); |
|---|
| | 202 | } |
|---|
| | 203 | |
|---|
| | 204 | |
|---|
| | 205 | // first contiguous, then reshape |
|---|
| | 206 | |
|---|
| | 207 | array = (PyArrayObject*)PyArray_FromDims(1,&total,PyArray_OBJECT); |
|---|
| | 208 | if (array==NULL) { |
|---|
| | 209 | PyErr_SetString(PyExc_RuntimeError,"hoc.get hoc error allocating array."); |
|---|
| | 210 | return NULL; |
|---|
| | 211 | } |
|---|
| | 212 | |
|---|
| | 213 | // fill array memory |
|---|
| | 214 | |
|---|
| | 215 | for (i=0;i<total;i++) { |
|---|
| | 216 | *(PyObject**)(array->data + i*array->strides[0]) = PyObj_FromNrnObj(OPOBJ(sym)[i]); |
|---|
| | 217 | } |
|---|
| | 218 | |
|---|
| | 219 | // return shaped array |
|---|
| | 220 | |
|---|
| | 221 | pObj = (PyObject*)PyArray_Reshape(array,pDims); |
|---|
| | 222 | |
|---|
| | 223 | if (pObj==NULL) { |
|---|
| | 224 | PyErr_SetString(PyExc_RuntimeError,"hoc.get hoc error reshaping array."); |
|---|
| | 225 | return NULL; |
|---|
| | 226 | } |
|---|
| | 227 | |
|---|
| | 228 | |
|---|
| | 229 | } |
|---|
| | 230 | |
|---|
| | 231 | } |
|---|
| | 232 | else { |
|---|
| | 233 | // return single object |
|---|
| | 234 | pObj = PyObj_FromNrnObj(*OPOBJ(sym)); |
|---|
| | 235 | } |
|---|
| | 236 | |
|---|
| | 237 | |
|---|
| | 238 | } |
|---|
| | 239 | else { |
|---|
| | 240 | PyErr_SetString(PyExc_RuntimeError,"hoc.get unsuported hoc type."); |
|---|
| | 241 | return NULL; |
|---|
| | 242 | } |
|---|
| | 243 | |
|---|
| | 244 | |
|---|
| | 245 | if (pObj==NULL) { |
|---|
| | 246 | PyErr_SetString(PyExc_RuntimeError,"hoc.get unexpected *pObj==NULL."); |
|---|
| | 247 | return NULL; |
|---|
| | 248 | } |
|---|
| | 249 | |
|---|
| | 250 | return Py_BuildValue("N", pObj); |
|---|
| | 251 | } |
|---|
| | 252 | |
|---|
| | 253 | |
|---|
| | 254 | |
|---|