[896abb3] | 1 | /** |
---|
| 2 | * C implementation of the P(r) inversion |
---|
| 3 | * Cinvertor is the base class for the Invertor class |
---|
| 4 | * and provides the underlying computations. |
---|
| 5 | * |
---|
| 6 | */ |
---|
[9e8dc22] | 7 | #include <Python.h> |
---|
| 8 | #include "structmember.h" |
---|
| 9 | #include <stdio.h> |
---|
| 10 | #include <stdlib.h> |
---|
| 11 | #include <math.h> |
---|
| 12 | #include <time.h> |
---|
| 13 | |
---|
| 14 | #include "invertor.h" |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | /// Error object for raised exceptions |
---|
| 18 | static PyObject * CinvertorError = NULL; |
---|
| 19 | |
---|
| 20 | #define INVECTOR(obj,buf,len) \ |
---|
| 21 | do { \ |
---|
| 22 | int err = PyObject_AsReadBuffer(obj, (const void **)(&buf), &len); \ |
---|
| 23 | if (err < 0) return NULL; \ |
---|
| 24 | len /= sizeof(*buf); \ |
---|
| 25 | } while (0) |
---|
| 26 | |
---|
| 27 | #define OUTVECTOR(obj,buf,len) \ |
---|
| 28 | do { \ |
---|
| 29 | int err = PyObject_AsWriteBuffer(obj, (void **)(&buf), &len); \ |
---|
| 30 | if (err < 0) return NULL; \ |
---|
| 31 | len /= sizeof(*buf); \ |
---|
| 32 | } while (0) |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | // Class definition |
---|
[896abb3] | 36 | /** |
---|
| 37 | * C implementation of the P(r) inversion |
---|
| 38 | * Cinvertor is the base class for the Invertor class |
---|
| 39 | * and provides the underlying computations. |
---|
| 40 | * |
---|
| 41 | */ |
---|
[9e8dc22] | 42 | typedef struct { |
---|
| 43 | PyObject_HEAD |
---|
[896abb3] | 44 | /// Internal data structure |
---|
[9e8dc22] | 45 | Invertor_params params; |
---|
| 46 | } Cinvertor; |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | static void |
---|
| 50 | Cinvertor_dealloc(Cinvertor* self) |
---|
| 51 | { |
---|
| 52 | invertor_dealloc(&(self->params)); |
---|
| 53 | |
---|
| 54 | self->ob_type->tp_free((PyObject*)self); |
---|
| 55 | |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | static PyObject * |
---|
| 59 | Cinvertor_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
| 60 | { |
---|
| 61 | Cinvertor *self; |
---|
| 62 | |
---|
| 63 | self = (Cinvertor *)type->tp_alloc(type, 0); |
---|
| 64 | |
---|
| 65 | return (PyObject *)self; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | static int |
---|
| 69 | Cinvertor_init(Cinvertor *self, PyObject *args, PyObject *kwds) |
---|
| 70 | { |
---|
| 71 | if (self != NULL) { |
---|
| 72 | // Create parameters |
---|
| 73 | invertor_init(&(self->params)); |
---|
| 74 | } |
---|
| 75 | return 0; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | static PyMemberDef Cinvertor_members[] = { |
---|
| 79 | //{"params", T_OBJECT, offsetof(Cinvertor, params), 0, |
---|
| 80 | // "Parameters"}, |
---|
| 81 | {NULL} /* Sentinel */ |
---|
| 82 | }; |
---|
| 83 | |
---|
[896abb3] | 84 | const char set_x_doc[] = |
---|
| 85 | "Function to set the x data\n" |
---|
| 86 | "Takes an array of doubles as input.\n" |
---|
| 87 | " @return: number of entries found"; |
---|
[9e8dc22] | 88 | |
---|
| 89 | /** |
---|
| 90 | * Function to set the x data |
---|
| 91 | * Takes an array of doubles as input |
---|
| 92 | * Returns the number of entries found |
---|
| 93 | */ |
---|
| 94 | static PyObject * set_x(Cinvertor *self, PyObject *args) { |
---|
| 95 | PyObject *data_obj; |
---|
| 96 | Py_ssize_t ndata; |
---|
| 97 | double *data; |
---|
[2d06beb] | 98 | int i; |
---|
[9e8dc22] | 99 | |
---|
| 100 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 101 | OUTVECTOR(data_obj,data,ndata); |
---|
[2d06beb] | 102 | |
---|
| 103 | free(self->params.x); |
---|
| 104 | self->params.x = (double*) malloc(ndata*sizeof(double)); |
---|
| 105 | |
---|
| 106 | if(self->params.x==NULL) { |
---|
| 107 | PyErr_SetString(CinvertorError, |
---|
| 108 | "Cinvertor.set_x: problem allocating memory."); |
---|
| 109 | return NULL; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | for (i=0; i<ndata; i++) { |
---|
| 113 | self->params.x[i] = data[i]; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | //self->params.x = data; |
---|
[9e8dc22] | 117 | self->params.npoints = ndata; |
---|
| 118 | return Py_BuildValue("i", self->params.npoints); |
---|
| 119 | } |
---|
| 120 | |
---|
[896abb3] | 121 | const char get_x_doc[] = |
---|
| 122 | "Function to get the x data\n" |
---|
| 123 | "Takes an array of doubles as input.\n" |
---|
| 124 | " @return: number of entries found"; |
---|
| 125 | |
---|
[9e8dc22] | 126 | static PyObject * get_x(Cinvertor *self, PyObject *args) { |
---|
| 127 | PyObject *data_obj; |
---|
| 128 | Py_ssize_t ndata; |
---|
| 129 | double *data; |
---|
| 130 | int i; |
---|
| 131 | |
---|
| 132 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
[2d06beb] | 133 | OUTVECTOR(data_obj, data, ndata); |
---|
[9e8dc22] | 134 | |
---|
| 135 | // Check that the input array is large enough |
---|
| 136 | if (ndata < self->params.npoints) { |
---|
| 137 | PyErr_SetString(CinvertorError, |
---|
| 138 | "Cinvertor.get_x: input array too short for data."); |
---|
| 139 | return NULL; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | for(i=0; i<self->params.npoints; i++){ |
---|
| 143 | data[i] = self->params.x[i]; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | return Py_BuildValue("i", self->params.npoints); |
---|
| 147 | } |
---|
| 148 | |
---|
[896abb3] | 149 | const char set_y_doc[] = |
---|
| 150 | "Function to set the y data\n" |
---|
| 151 | "Takes an array of doubles as input.\n" |
---|
| 152 | " @return: number of entries found"; |
---|
| 153 | |
---|
[9e8dc22] | 154 | /** |
---|
| 155 | * Function to set the y data |
---|
| 156 | * Takes an array of doubles as input |
---|
| 157 | * Returns the number of entries found |
---|
| 158 | */ |
---|
| 159 | static PyObject * set_y(Cinvertor *self, PyObject *args) { |
---|
| 160 | PyObject *data_obj; |
---|
| 161 | Py_ssize_t ndata; |
---|
| 162 | double *data; |
---|
[2d06beb] | 163 | int i; |
---|
[9e8dc22] | 164 | |
---|
| 165 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 166 | OUTVECTOR(data_obj,data,ndata); |
---|
[2d06beb] | 167 | |
---|
| 168 | free(self->params.y); |
---|
| 169 | self->params.y = (double*) malloc(ndata*sizeof(double)); |
---|
| 170 | |
---|
| 171 | if(self->params.y==NULL) { |
---|
| 172 | PyErr_SetString(CinvertorError, |
---|
| 173 | "Cinvertor.set_y: problem allocating memory."); |
---|
| 174 | return NULL; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | for (i=0; i<ndata; i++) { |
---|
| 178 | self->params.y[i] = data[i]; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | //self->params.y = data; |
---|
[9e8dc22] | 182 | self->params.ny = ndata; |
---|
| 183 | return Py_BuildValue("i", self->params.ny); |
---|
| 184 | } |
---|
| 185 | |
---|
[896abb3] | 186 | const char get_y_doc[] = |
---|
| 187 | "Function to get the y data\n" |
---|
| 188 | "Takes an array of doubles as input.\n" |
---|
| 189 | " @return: number of entries found"; |
---|
| 190 | |
---|
[9e8dc22] | 191 | static PyObject * get_y(Cinvertor *self, PyObject *args) { |
---|
| 192 | PyObject *data_obj; |
---|
| 193 | Py_ssize_t ndata; |
---|
| 194 | double *data; |
---|
| 195 | int i; |
---|
| 196 | |
---|
| 197 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
[2d06beb] | 198 | OUTVECTOR(data_obj, data, ndata); |
---|
[9e8dc22] | 199 | |
---|
| 200 | // Check that the input array is large enough |
---|
| 201 | if (ndata < self->params.ny) { |
---|
| 202 | PyErr_SetString(CinvertorError, |
---|
| 203 | "Cinvertor.get_y: input array too short for data."); |
---|
| 204 | return NULL; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | for(i=0; i<self->params.ny; i++){ |
---|
| 208 | data[i] = self->params.y[i]; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | return Py_BuildValue("i", self->params.npoints); |
---|
| 212 | } |
---|
| 213 | |
---|
[896abb3] | 214 | const char set_err_doc[] = |
---|
| 215 | "Function to set the err data\n" |
---|
| 216 | "Takes an array of doubles as input.\n" |
---|
| 217 | " @return: number of entries found"; |
---|
| 218 | |
---|
[9e8dc22] | 219 | /** |
---|
| 220 | * Function to set the x data |
---|
| 221 | * Takes an array of doubles as input |
---|
| 222 | * Returns the number of entries found |
---|
| 223 | */ |
---|
| 224 | static PyObject * set_err(Cinvertor *self, PyObject *args) { |
---|
| 225 | PyObject *data_obj; |
---|
| 226 | Py_ssize_t ndata; |
---|
| 227 | double *data; |
---|
[2d06beb] | 228 | int i; |
---|
[9e8dc22] | 229 | |
---|
| 230 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 231 | OUTVECTOR(data_obj,data,ndata); |
---|
[2d06beb] | 232 | |
---|
| 233 | free(self->params.err); |
---|
| 234 | self->params.err = (double*) malloc(ndata*sizeof(double)); |
---|
| 235 | |
---|
| 236 | if(self->params.err==NULL) { |
---|
| 237 | PyErr_SetString(CinvertorError, |
---|
| 238 | "Cinvertor.set_err: problem allocating memory."); |
---|
| 239 | return NULL; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | for (i=0; i<ndata; i++) { |
---|
| 243 | self->params.err[i] = data[i]; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | //self->params.err = data; |
---|
[9e8dc22] | 247 | self->params.nerr = ndata; |
---|
| 248 | return Py_BuildValue("i", self->params.nerr); |
---|
| 249 | } |
---|
| 250 | |
---|
[896abb3] | 251 | const char get_err_doc[] = |
---|
| 252 | "Function to get the err data\n" |
---|
| 253 | "Takes an array of doubles as input.\n" |
---|
| 254 | " @return: number of entries found"; |
---|
| 255 | |
---|
[9e8dc22] | 256 | static PyObject * get_err(Cinvertor *self, PyObject *args) { |
---|
| 257 | PyObject *data_obj; |
---|
| 258 | Py_ssize_t ndata; |
---|
| 259 | double *data; |
---|
| 260 | int i; |
---|
| 261 | |
---|
| 262 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
[2d06beb] | 263 | OUTVECTOR(data_obj, data, ndata); |
---|
[9e8dc22] | 264 | |
---|
| 265 | // Check that the input array is large enough |
---|
| 266 | if (ndata < self->params.nerr) { |
---|
| 267 | PyErr_SetString(CinvertorError, |
---|
| 268 | "Cinvertor.get_err: input array too short for data."); |
---|
| 269 | return NULL; |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | for(i=0; i<self->params.nerr; i++){ |
---|
| 273 | data[i] = self->params.err[i]; |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | return Py_BuildValue("i", self->params.npoints); |
---|
| 277 | } |
---|
| 278 | |
---|
[896abb3] | 279 | const char is_valid_doc[] = |
---|
| 280 | "Check the validity of the stored data\n" |
---|
| 281 | " @return: Returns the number of points if it's all good, -1 otherwise"; |
---|
| 282 | |
---|
[9e8dc22] | 283 | /** |
---|
| 284 | * Check the validity of the stored data |
---|
| 285 | * Returns the number of points if it's all good, -1 otherwise |
---|
| 286 | */ |
---|
| 287 | static PyObject * is_valid(Cinvertor *self, PyObject *args) { |
---|
| 288 | if(self->params.npoints==self->params.ny && |
---|
| 289 | self->params.npoints==self->params.nerr) { |
---|
| 290 | return Py_BuildValue("i", self->params.npoints); |
---|
| 291 | } else { |
---|
| 292 | return Py_BuildValue("i", -1); |
---|
| 293 | } |
---|
| 294 | } |
---|
| 295 | |
---|
[896abb3] | 296 | const char set_dmax_doc[] = |
---|
| 297 | "Sets the maximum distance\n"; |
---|
| 298 | |
---|
[9e8dc22] | 299 | /** |
---|
| 300 | * Sets the maximum distance |
---|
| 301 | */ |
---|
| 302 | static PyObject * set_dmax(Cinvertor *self, PyObject *args) { |
---|
| 303 | double d_max; |
---|
| 304 | |
---|
| 305 | if (!PyArg_ParseTuple(args, "d", &d_max)) return NULL; |
---|
| 306 | self->params.d_max = d_max; |
---|
| 307 | return Py_BuildValue("d", self->params.d_max); |
---|
| 308 | } |
---|
| 309 | |
---|
[896abb3] | 310 | const char get_dmax_doc[] = |
---|
| 311 | "Gets the maximum distance\n"; |
---|
| 312 | |
---|
[9e8dc22] | 313 | /** |
---|
| 314 | * Gets the maximum distance |
---|
| 315 | */ |
---|
| 316 | static PyObject * get_dmax(Cinvertor *self, PyObject *args) { |
---|
| 317 | return Py_BuildValue("d", self->params.d_max); |
---|
| 318 | } |
---|
| 319 | |
---|
[896abb3] | 320 | const char set_qmin_doc[] = |
---|
| 321 | "Sets the minimum q\n"; |
---|
| 322 | |
---|
[f71287f4] | 323 | /** |
---|
| 324 | * Sets the minimum q |
---|
| 325 | */ |
---|
| 326 | static PyObject * set_qmin(Cinvertor *self, PyObject *args) { |
---|
| 327 | double q_min; |
---|
| 328 | |
---|
| 329 | if (!PyArg_ParseTuple(args, "d", &q_min)) return NULL; |
---|
| 330 | self->params.q_min = q_min; |
---|
| 331 | return Py_BuildValue("d", self->params.q_min); |
---|
| 332 | } |
---|
| 333 | |
---|
[896abb3] | 334 | const char get_qmin_doc[] = |
---|
| 335 | "Gets the minimum q\n"; |
---|
| 336 | |
---|
[f71287f4] | 337 | /** |
---|
| 338 | * Gets the minimum q |
---|
| 339 | */ |
---|
| 340 | static PyObject * get_qmin(Cinvertor *self, PyObject *args) { |
---|
| 341 | return Py_BuildValue("d", self->params.q_min); |
---|
| 342 | } |
---|
| 343 | |
---|
[896abb3] | 344 | const char set_qmax_doc[] = |
---|
| 345 | "Sets the maximum q\n"; |
---|
[f71287f4] | 346 | |
---|
| 347 | /** |
---|
| 348 | * Sets the maximum q |
---|
| 349 | */ |
---|
| 350 | static PyObject * set_qmax(Cinvertor *self, PyObject *args) { |
---|
| 351 | double q_max; |
---|
| 352 | |
---|
| 353 | if (!PyArg_ParseTuple(args, "d", &q_max)) return NULL; |
---|
| 354 | self->params.q_max = q_max; |
---|
| 355 | return Py_BuildValue("d", self->params.q_max); |
---|
| 356 | } |
---|
| 357 | |
---|
[896abb3] | 358 | const char get_qmax_doc[] = |
---|
| 359 | "Gets the maximum q\n"; |
---|
| 360 | |
---|
[f71287f4] | 361 | /** |
---|
| 362 | * Gets the maximum q |
---|
| 363 | */ |
---|
| 364 | static PyObject * get_qmax(Cinvertor *self, PyObject *args) { |
---|
| 365 | return Py_BuildValue("d", self->params.q_max); |
---|
| 366 | } |
---|
| 367 | |
---|
[896abb3] | 368 | const char set_alpha_doc[] = |
---|
| 369 | "Sets the alpha parameter\n"; |
---|
[f71287f4] | 370 | |
---|
[eca05c8] | 371 | static PyObject * set_alpha(Cinvertor *self, PyObject *args) { |
---|
| 372 | double alpha; |
---|
| 373 | |
---|
| 374 | if (!PyArg_ParseTuple(args, "d", &alpha)) return NULL; |
---|
| 375 | self->params.alpha = alpha; |
---|
| 376 | return Py_BuildValue("d", self->params.alpha); |
---|
| 377 | } |
---|
| 378 | |
---|
[896abb3] | 379 | const char get_alpha_doc[] = |
---|
| 380 | "Gets the alpha parameter\n"; |
---|
| 381 | |
---|
[eca05c8] | 382 | /** |
---|
| 383 | * Gets the maximum distance |
---|
| 384 | */ |
---|
| 385 | static PyObject * get_alpha(Cinvertor *self, PyObject *args) { |
---|
| 386 | return Py_BuildValue("d", self->params.alpha); |
---|
| 387 | } |
---|
| 388 | |
---|
[896abb3] | 389 | const char get_nx_doc[] = |
---|
| 390 | "Gets the number of x points\n"; |
---|
| 391 | |
---|
[9e8dc22] | 392 | /** |
---|
| 393 | * Gets the number of x points |
---|
| 394 | */ |
---|
| 395 | static PyObject * get_nx(Cinvertor *self, PyObject *args) { |
---|
| 396 | return Py_BuildValue("i", self->params.npoints); |
---|
| 397 | } |
---|
| 398 | |
---|
[896abb3] | 399 | const char get_ny_doc[] = |
---|
| 400 | "Gets the number of y points\n"; |
---|
| 401 | |
---|
[9e8dc22] | 402 | /** |
---|
| 403 | * Gets the number of y points |
---|
| 404 | */ |
---|
| 405 | static PyObject * get_ny(Cinvertor *self, PyObject *args) { |
---|
| 406 | return Py_BuildValue("i", self->params.ny); |
---|
| 407 | } |
---|
| 408 | |
---|
[896abb3] | 409 | const char get_nerr_doc[] = |
---|
| 410 | "Gets the number of err points\n"; |
---|
| 411 | |
---|
[9e8dc22] | 412 | /** |
---|
| 413 | * Gets the number of error points |
---|
| 414 | */ |
---|
| 415 | static PyObject * get_nerr(Cinvertor *self, PyObject *args) { |
---|
| 416 | return Py_BuildValue("i", self->params.nerr); |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | |
---|
[896abb3] | 420 | const char residuals_doc[] = |
---|
| 421 | "Function to call to evaluate the residuals\n" |
---|
| 422 | "for P(r) inversion\n" |
---|
| 423 | " @param args: input parameters\n" |
---|
| 424 | " @return: list of residuals"; |
---|
| 425 | |
---|
[9e8dc22] | 426 | /** |
---|
| 427 | * Function to call to evaluate the residuals |
---|
[eca05c8] | 428 | * @param args: input parameters |
---|
| 429 | * @return: list of residuals |
---|
[9e8dc22] | 430 | */ |
---|
| 431 | static PyObject * residuals(Cinvertor *self, PyObject *args) { |
---|
| 432 | double *pars; |
---|
| 433 | PyObject* residuals; |
---|
| 434 | PyObject* temp; |
---|
| 435 | double *res; |
---|
| 436 | int i; |
---|
| 437 | double residual, diff; |
---|
| 438 | // Regularization factor |
---|
[eca05c8] | 439 | double regterm = 0.0; |
---|
| 440 | double tmp = 0.0; |
---|
[abad620] | 441 | // Number of slices in regularization term estimate |
---|
| 442 | int nslice = 25; |
---|
[9e8dc22] | 443 | |
---|
| 444 | PyObject *data_obj; |
---|
| 445 | Py_ssize_t npars; |
---|
| 446 | |
---|
| 447 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 448 | |
---|
| 449 | OUTVECTOR(data_obj,pars,npars); |
---|
| 450 | |
---|
| 451 | // PyList of residuals |
---|
| 452 | // Should create this list only once and refill it |
---|
| 453 | residuals = PyList_New(self->params.npoints); |
---|
[eca05c8] | 454 | |
---|
[abad620] | 455 | regterm = reg_term(pars, self->params.d_max, npars, nslice); |
---|
[9e8dc22] | 456 | |
---|
| 457 | for(i=0; i<self->params.npoints; i++) { |
---|
| 458 | diff = self->params.y[i] - iq(pars, self->params.d_max, npars, self->params.x[i]); |
---|
| 459 | residual = diff*diff / (self->params.err[i]*self->params.err[i]); |
---|
[eca05c8] | 460 | tmp = residual; |
---|
| 461 | |
---|
| 462 | // regularization term |
---|
| 463 | residual += self->params.alpha * regterm; |
---|
| 464 | |
---|
| 465 | if (PyList_SetItem(residuals, i, Py_BuildValue("d",residual) ) < 0){ |
---|
| 466 | PyErr_SetString(CinvertorError, |
---|
| 467 | "Cinvertor.residuals: error setting residual."); |
---|
| 468 | return NULL; |
---|
| 469 | }; |
---|
| 470 | |
---|
| 471 | } |
---|
| 472 | |
---|
| 473 | return residuals; |
---|
| 474 | } |
---|
[896abb3] | 475 | |
---|
| 476 | const char pr_residuals_doc[] = |
---|
| 477 | "Function to call to evaluate the residuals\n" |
---|
| 478 | "for P(r) minimization (for testing purposes)\n" |
---|
| 479 | " @param args: input parameters\n" |
---|
| 480 | " @return: list of residuals"; |
---|
| 481 | |
---|
[eca05c8] | 482 | /** |
---|
| 483 | * Function to call to evaluate the residuals |
---|
| 484 | * for P(r) minimization (for testing purposes) |
---|
| 485 | * @param args: input parameters |
---|
| 486 | * @return: list of residuals |
---|
| 487 | */ |
---|
| 488 | static PyObject * pr_residuals(Cinvertor *self, PyObject *args) { |
---|
| 489 | double *pars; |
---|
| 490 | PyObject* residuals; |
---|
| 491 | PyObject* temp; |
---|
| 492 | double *res; |
---|
| 493 | int i; |
---|
| 494 | double residual, diff; |
---|
| 495 | // Regularization factor |
---|
| 496 | double regterm = 0.0; |
---|
| 497 | double tmp = 0.0; |
---|
[abad620] | 498 | // Number of slices in regularization term estimate |
---|
| 499 | int nslice = 25; |
---|
[eca05c8] | 500 | |
---|
| 501 | PyObject *data_obj; |
---|
| 502 | Py_ssize_t npars; |
---|
| 503 | |
---|
| 504 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 505 | |
---|
| 506 | OUTVECTOR(data_obj,pars,npars); |
---|
| 507 | |
---|
| 508 | // Should create this list only once and refill it |
---|
| 509 | residuals = PyList_New(self->params.npoints); |
---|
| 510 | |
---|
[abad620] | 511 | regterm = reg_term(pars, self->params.d_max, npars, nslice); |
---|
[eca05c8] | 512 | |
---|
| 513 | |
---|
| 514 | for(i=0; i<self->params.npoints; i++) { |
---|
| 515 | diff = self->params.y[i] - pr(pars, self->params.d_max, npars, self->params.x[i]); |
---|
| 516 | residual = diff*diff / (self->params.err[i]*self->params.err[i]); |
---|
| 517 | tmp = residual; |
---|
| 518 | |
---|
| 519 | // regularization term |
---|
| 520 | residual += self->params.alpha * regterm; |
---|
| 521 | |
---|
[9e8dc22] | 522 | if (PyList_SetItem(residuals, i, Py_BuildValue("d",residual) ) < 0){ |
---|
| 523 | PyErr_SetString(CinvertorError, |
---|
| 524 | "Cinvertor.residuals: error setting residual."); |
---|
| 525 | return NULL; |
---|
| 526 | }; |
---|
| 527 | |
---|
| 528 | } |
---|
| 529 | |
---|
| 530 | return residuals; |
---|
| 531 | } |
---|
| 532 | |
---|
[896abb3] | 533 | const char get_iq_doc[] = |
---|
| 534 | "Function to call to evaluate the scattering intensity\n" |
---|
| 535 | " @param args: c-parameters, and q\n" |
---|
| 536 | " @return: I(q)"; |
---|
| 537 | |
---|
[9e8dc22] | 538 | /** |
---|
| 539 | * Function to call to evaluate the scattering intensity |
---|
| 540 | * @param args: c-parameters, and q |
---|
| 541 | * @return: I(q) |
---|
| 542 | */ |
---|
| 543 | static PyObject * get_iq(Cinvertor *self, PyObject *args) { |
---|
| 544 | double *pars; |
---|
| 545 | double q, iq_value; |
---|
| 546 | PyObject *data_obj; |
---|
| 547 | Py_ssize_t npars; |
---|
| 548 | |
---|
| 549 | if (!PyArg_ParseTuple(args, "Od", &data_obj, &q)) return NULL; |
---|
| 550 | OUTVECTOR(data_obj,pars,npars); |
---|
| 551 | |
---|
| 552 | iq_value = iq(pars, self->params.d_max, npars, q); |
---|
| 553 | return Py_BuildValue("f", iq_value); |
---|
| 554 | } |
---|
| 555 | |
---|
[896abb3] | 556 | const char get_pr_doc[] = |
---|
| 557 | "Function to call to evaluate P(r)\n" |
---|
| 558 | " @param args: c-parameters and r\n" |
---|
| 559 | " @return: P(r)"; |
---|
| 560 | |
---|
[9e8dc22] | 561 | /** |
---|
| 562 | * Function to call to evaluate P(r) |
---|
| 563 | * @param args: c-parameters and r |
---|
| 564 | * @return: P(r) |
---|
| 565 | */ |
---|
| 566 | static PyObject * get_pr(Cinvertor *self, PyObject *args) { |
---|
| 567 | double *pars; |
---|
| 568 | double r, pr_value; |
---|
| 569 | PyObject *data_obj; |
---|
| 570 | Py_ssize_t npars; |
---|
| 571 | |
---|
| 572 | if (!PyArg_ParseTuple(args, "Od", &data_obj, &r)) return NULL; |
---|
| 573 | OUTVECTOR(data_obj,pars,npars); |
---|
| 574 | |
---|
| 575 | pr_value = pr(pars, self->params.d_max, npars, r); |
---|
| 576 | return Py_BuildValue("f", pr_value); |
---|
| 577 | } |
---|
| 578 | |
---|
[896abb3] | 579 | const char get_pr_err_doc[] = |
---|
| 580 | "Function to call to evaluate P(r) with errors\n" |
---|
| 581 | " @param args: c-parameters and r\n" |
---|
| 582 | " @return: (P(r),dP(r))"; |
---|
| 583 | |
---|
[eca05c8] | 584 | /** |
---|
| 585 | * Function to call to evaluate P(r) with errors |
---|
| 586 | * @param args: c-parameters and r |
---|
| 587 | * @return: P(r) |
---|
| 588 | */ |
---|
| 589 | static PyObject * get_pr_err(Cinvertor *self, PyObject *args) { |
---|
| 590 | double *pars; |
---|
| 591 | double *pars_err; |
---|
| 592 | double pr_err_value; |
---|
| 593 | double r, pr_value; |
---|
| 594 | PyObject *data_obj; |
---|
| 595 | Py_ssize_t npars; |
---|
| 596 | PyObject *err_obj; |
---|
| 597 | Py_ssize_t npars2; |
---|
[43c0a8e] | 598 | int i; |
---|
| 599 | |
---|
[eca05c8] | 600 | if (!PyArg_ParseTuple(args, "OOd", &data_obj, &err_obj, &r)) return NULL; |
---|
[43c0a8e] | 601 | OUTVECTOR(data_obj,pars,npars); |
---|
[eca05c8] | 602 | OUTVECTOR(err_obj,pars_err,npars2); |
---|
[43c0a8e] | 603 | |
---|
[eca05c8] | 604 | pr_err(pars, pars_err, self->params.d_max, npars, r, &pr_value, &pr_err_value); |
---|
| 605 | return Py_BuildValue("ff", pr_value, pr_err_value); |
---|
| 606 | } |
---|
| 607 | |
---|
[896abb3] | 608 | const char basefunc_ft_doc[] = |
---|
| 609 | "Returns the value of the nth Fourier transofrmed base function\n" |
---|
| 610 | " @param args: c-parameters, n and q\n" |
---|
| 611 | " @return: nth Fourier transformed base function, evaluated at q"; |
---|
| 612 | |
---|
[2d06beb] | 613 | static PyObject * basefunc_ft(Cinvertor *self, PyObject *args) { |
---|
| 614 | double d_max, q; |
---|
| 615 | int n; |
---|
| 616 | |
---|
| 617 | if (!PyArg_ParseTuple(args, "did", &d_max, &n, &q)) return NULL; |
---|
| 618 | return Py_BuildValue("f", ortho_transformed(d_max, n, q)); |
---|
| 619 | |
---|
| 620 | } |
---|
[9e8dc22] | 621 | |
---|
[896abb3] | 622 | const char oscillations_doc[] = |
---|
| 623 | "Returns the value of the oscillation figure of merit for\n" |
---|
| 624 | "the given set of coefficients. For a sphere, the oscillation\n" |
---|
| 625 | "figure of merit is 1.1.\n" |
---|
| 626 | " @param args: c-parameters\n" |
---|
| 627 | " @return: oscillation figure of merit"; |
---|
| 628 | |
---|
[abad620] | 629 | static PyObject * oscillations(Cinvertor *self, PyObject *args) { |
---|
| 630 | double *pars; |
---|
| 631 | PyObject *data_obj; |
---|
| 632 | Py_ssize_t npars; |
---|
| 633 | double oscill, norm; |
---|
| 634 | |
---|
| 635 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 636 | OUTVECTOR(data_obj,pars,npars); |
---|
| 637 | |
---|
| 638 | oscill = reg_term(pars, self->params.d_max, npars, 100); |
---|
| 639 | norm = int_p2(pars, self->params.d_max, npars, 100); |
---|
| 640 | return Py_BuildValue("f", sqrt(oscill/norm)/acos(-1.0)*self->params.d_max ); |
---|
| 641 | |
---|
| 642 | } |
---|
| 643 | |
---|
[896abb3] | 644 | const char get_peaks_doc[] = |
---|
| 645 | "Returns the number of peaks in the output P(r) distrubution\n" |
---|
| 646 | "for the given set of coefficients.\n" |
---|
| 647 | " @param args: c-parameters\n" |
---|
| 648 | " @return: number of P(r) peaks"; |
---|
| 649 | |
---|
[4f63160] | 650 | static PyObject * get_peaks(Cinvertor *self, PyObject *args) { |
---|
| 651 | double *pars; |
---|
| 652 | PyObject *data_obj; |
---|
| 653 | Py_ssize_t npars; |
---|
| 654 | int count; |
---|
| 655 | |
---|
| 656 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 657 | OUTVECTOR(data_obj,pars,npars); |
---|
| 658 | |
---|
| 659 | count = npeaks(pars, self->params.d_max, npars, 100); |
---|
| 660 | |
---|
| 661 | return Py_BuildValue("i", count ); |
---|
| 662 | |
---|
| 663 | } |
---|
| 664 | |
---|
[896abb3] | 665 | const char get_positive_doc[] = |
---|
| 666 | "Returns the fraction of P(r) that is positive over\n" |
---|
| 667 | "the full range of r for the given set of coefficients.\n" |
---|
| 668 | " @param args: c-parameters\n" |
---|
| 669 | " @return: fraction of P(r) that is positive"; |
---|
| 670 | |
---|
[43c0a8e] | 671 | static PyObject * get_positive(Cinvertor *self, PyObject *args) { |
---|
| 672 | double *pars; |
---|
| 673 | PyObject *data_obj; |
---|
| 674 | Py_ssize_t npars; |
---|
| 675 | double fraction; |
---|
| 676 | |
---|
| 677 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 678 | OUTVECTOR(data_obj,pars,npars); |
---|
| 679 | |
---|
| 680 | fraction = positive_integral(pars, self->params.d_max, npars, 100); |
---|
| 681 | |
---|
| 682 | return Py_BuildValue("f", fraction ); |
---|
| 683 | |
---|
| 684 | } |
---|
| 685 | |
---|
[896abb3] | 686 | const char get_pos_err_doc[] = |
---|
| 687 | "Returns the fraction of P(r) that is 1 standard deviation\n" |
---|
| 688 | "above zero over the full range of r for the given set of coefficients.\n" |
---|
| 689 | " @param args: c-parameters\n" |
---|
| 690 | " @return: fraction of P(r) that is positive"; |
---|
| 691 | |
---|
[43c0a8e] | 692 | static PyObject * get_pos_err(Cinvertor *self, PyObject *args) { |
---|
| 693 | double *pars; |
---|
| 694 | double *pars_err; |
---|
| 695 | PyObject *data_obj; |
---|
| 696 | PyObject *err_obj; |
---|
| 697 | Py_ssize_t npars; |
---|
| 698 | Py_ssize_t npars2; |
---|
| 699 | double fraction; |
---|
| 700 | |
---|
| 701 | if (!PyArg_ParseTuple(args, "OO", &data_obj, &err_obj)) return NULL; |
---|
| 702 | OUTVECTOR(data_obj,pars,npars); |
---|
| 703 | OUTVECTOR(err_obj,pars_err,npars2); |
---|
| 704 | |
---|
| 705 | fraction = positive_errors(pars, pars_err, self->params.d_max, npars, 51); |
---|
| 706 | |
---|
| 707 | return Py_BuildValue("f", fraction ); |
---|
| 708 | |
---|
| 709 | } |
---|
| 710 | |
---|
[896abb3] | 711 | |
---|
| 712 | const char eeeget_qmin_doc[] = "\ |
---|
| 713 | This is a multiline doc string.\n\ |
---|
| 714 | \n\ |
---|
| 715 | This is the second line."; |
---|
| 716 | const char eeeset_qmin_doc[] = |
---|
| 717 | "This is a multiline doc string.\n" |
---|
| 718 | "\n" |
---|
| 719 | "This is the second line."; |
---|
| 720 | |
---|
[9e8dc22] | 721 | static PyMethodDef Cinvertor_methods[] = { |
---|
[896abb3] | 722 | {"residuals", (PyCFunction)residuals, METH_VARARGS, residuals_doc}, |
---|
| 723 | {"pr_residuals", (PyCFunction)pr_residuals, METH_VARARGS, pr_residuals_doc}, |
---|
| 724 | {"set_x", (PyCFunction)set_x, METH_VARARGS, set_x_doc}, |
---|
| 725 | {"get_x", (PyCFunction)get_x, METH_VARARGS, get_x_doc}, |
---|
| 726 | {"set_y", (PyCFunction)set_y, METH_VARARGS, set_y_doc}, |
---|
| 727 | {"get_y", (PyCFunction)get_y, METH_VARARGS, get_y_doc}, |
---|
| 728 | {"set_err", (PyCFunction)set_err, METH_VARARGS, set_err_doc}, |
---|
| 729 | {"get_err", (PyCFunction)get_err, METH_VARARGS, get_err_doc}, |
---|
| 730 | {"set_dmax", (PyCFunction)set_dmax, METH_VARARGS, set_dmax_doc}, |
---|
| 731 | {"get_dmax", (PyCFunction)get_dmax, METH_VARARGS, get_dmax_doc}, |
---|
| 732 | {"set_qmin", (PyCFunction)set_qmin, METH_VARARGS, set_qmin_doc}, |
---|
| 733 | {"get_qmin", (PyCFunction)get_qmin, METH_VARARGS, get_qmin_doc}, |
---|
| 734 | {"set_qmax", (PyCFunction)set_qmax, METH_VARARGS, set_qmax_doc}, |
---|
| 735 | {"get_qmax", (PyCFunction)get_qmax, METH_VARARGS, get_qmax_doc}, |
---|
| 736 | {"set_alpha", (PyCFunction)set_alpha, METH_VARARGS, set_alpha_doc}, |
---|
| 737 | {"get_alpha", (PyCFunction)get_alpha, METH_VARARGS, get_alpha_doc}, |
---|
| 738 | {"get_nx", (PyCFunction)get_nx, METH_VARARGS, get_nx_doc}, |
---|
| 739 | {"get_ny", (PyCFunction)get_ny, METH_VARARGS, get_ny_doc}, |
---|
| 740 | {"get_nerr", (PyCFunction)get_nerr, METH_VARARGS, get_nerr_doc}, |
---|
| 741 | {"iq", (PyCFunction)get_iq, METH_VARARGS, get_iq_doc}, |
---|
| 742 | {"pr", (PyCFunction)get_pr, METH_VARARGS, get_pr_doc}, |
---|
| 743 | {"get_pr_err", (PyCFunction)get_pr_err, METH_VARARGS, get_pr_err_doc}, |
---|
| 744 | {"is_valid", (PyCFunction)is_valid, METH_VARARGS, is_valid_doc}, |
---|
| 745 | {"basefunc_ft", (PyCFunction)basefunc_ft, METH_VARARGS, basefunc_ft_doc}, |
---|
| 746 | {"oscillations", (PyCFunction)oscillations, METH_VARARGS, oscillations_doc}, |
---|
| 747 | {"get_peaks", (PyCFunction)get_peaks, METH_VARARGS, get_peaks_doc}, |
---|
| 748 | {"get_positive", (PyCFunction)get_positive, METH_VARARGS, get_positive_doc}, |
---|
| 749 | {"get_pos_err", (PyCFunction)get_pos_err, METH_VARARGS, get_pos_err_doc}, |
---|
[9e8dc22] | 750 | |
---|
| 751 | {NULL} |
---|
| 752 | }; |
---|
| 753 | |
---|
| 754 | static PyTypeObject CinvertorType = { |
---|
| 755 | PyObject_HEAD_INIT(NULL) |
---|
| 756 | 0, /*ob_size*/ |
---|
| 757 | "Cinvertor", /*tp_name*/ |
---|
| 758 | sizeof(Cinvertor), /*tp_basicsize*/ |
---|
| 759 | 0, /*tp_itemsize*/ |
---|
| 760 | (destructor)Cinvertor_dealloc, /*tp_dealloc*/ |
---|
| 761 | 0, /*tp_print*/ |
---|
| 762 | 0, /*tp_getattr*/ |
---|
| 763 | 0, /*tp_setattr*/ |
---|
| 764 | 0, /*tp_compare*/ |
---|
| 765 | 0, /*tp_repr*/ |
---|
| 766 | 0, /*tp_as_number*/ |
---|
| 767 | 0, /*tp_as_sequence*/ |
---|
| 768 | 0, /*tp_as_mapping*/ |
---|
| 769 | 0, /*tp_hash */ |
---|
| 770 | 0, /*tp_call*/ |
---|
| 771 | 0, /*tp_str*/ |
---|
| 772 | 0, /*tp_getattro*/ |
---|
| 773 | 0, /*tp_setattro*/ |
---|
| 774 | 0, /*tp_as_buffer*/ |
---|
| 775 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
| 776 | "Cinvertor objects", /* tp_doc */ |
---|
| 777 | 0, /* tp_traverse */ |
---|
| 778 | 0, /* tp_clear */ |
---|
| 779 | 0, /* tp_richcompare */ |
---|
| 780 | 0, /* tp_weaklistoffset */ |
---|
| 781 | 0, /* tp_iter */ |
---|
| 782 | 0, /* tp_iternext */ |
---|
| 783 | Cinvertor_methods, /* tp_methods */ |
---|
| 784 | Cinvertor_members, /* tp_members */ |
---|
| 785 | 0, /* tp_getset */ |
---|
| 786 | 0, /* tp_base */ |
---|
| 787 | 0, /* tp_dict */ |
---|
| 788 | 0, /* tp_descr_get */ |
---|
| 789 | 0, /* tp_descr_set */ |
---|
| 790 | 0, /* tp_dictoffset */ |
---|
| 791 | (initproc)Cinvertor_init, /* tp_init */ |
---|
| 792 | 0, /* tp_alloc */ |
---|
| 793 | Cinvertor_new, /* tp_new */ |
---|
| 794 | }; |
---|
| 795 | |
---|
| 796 | |
---|
| 797 | static PyMethodDef module_methods[] = { |
---|
| 798 | {NULL} |
---|
| 799 | }; |
---|
| 800 | |
---|
| 801 | /** |
---|
| 802 | * Function used to add the model class to a module |
---|
| 803 | * @param module: module to add the class to |
---|
| 804 | */ |
---|
| 805 | void addCinvertor(PyObject *module) { |
---|
| 806 | PyObject *d; |
---|
| 807 | |
---|
| 808 | if (PyType_Ready(&CinvertorType) < 0) |
---|
| 809 | return; |
---|
| 810 | |
---|
| 811 | Py_INCREF(&CinvertorType); |
---|
| 812 | PyModule_AddObject(module, "Cinvertor", (PyObject *)&CinvertorType); |
---|
| 813 | |
---|
| 814 | d = PyModule_GetDict(module); |
---|
| 815 | CinvertorError = PyErr_NewException("Cinvertor.error", NULL, NULL); |
---|
| 816 | PyDict_SetItemString(d, "CinvertorError", CinvertorError); |
---|
| 817 | } |
---|
| 818 | |
---|
| 819 | |
---|
| 820 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
---|
| 821 | #define PyMODINIT_FUNC void |
---|
| 822 | #endif |
---|
| 823 | PyMODINIT_FUNC |
---|
| 824 | initpr_inversion(void) |
---|
| 825 | { |
---|
| 826 | PyObject* m; |
---|
| 827 | |
---|
| 828 | m = Py_InitModule3("pr_inversion", module_methods, |
---|
| 829 | "C extension module for inversion to P(r)."); |
---|
| 830 | |
---|
| 831 | addCinvertor(m); |
---|
| 832 | } |
---|