Changeset eca05c8 in sasview for pr_inversion/c_extensions
- Timestamp:
- Apr 30, 2008 4:29:36 PM (17 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 34ae302
- Parents:
- 47f695c9
- Location:
- pr_inversion/c_extensions
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
pr_inversion/c_extensions/Cinvertor.c
r9e8dc22 reca05c8 222 222 } 223 223 224 static PyObject * set_alpha(Cinvertor *self, PyObject *args) { 225 double alpha; 226 227 if (!PyArg_ParseTuple(args, "d", &alpha)) return NULL; 228 self->params.alpha = alpha; 229 return Py_BuildValue("d", self->params.alpha); 230 } 231 232 /** 233 * Gets the maximum distance 234 */ 235 static PyObject * get_alpha(Cinvertor *self, PyObject *args) { 236 return Py_BuildValue("d", self->params.alpha); 237 } 238 224 239 /** 225 240 * Gets the number of x points … … 246 261 /** 247 262 * Function to call to evaluate the residuals 248 * @param args: input q or [q,phi]249 * @return: function value263 * @param args: input parameters 264 * @return: list of residuals 250 265 */ 251 266 static PyObject * residuals(Cinvertor *self, PyObject *args) { … … 257 272 double residual, diff; 258 273 // Regularization factor 259 double lmult = 0.0; 274 double regterm = 0.0; 275 double tmp = 0.0; 260 276 261 277 PyObject *data_obj; … … 266 282 OUTVECTOR(data_obj,pars,npars); 267 283 268 //printf("npars: %i", npars);269 284 // PyList of residuals 270 285 // Should create this list only once and refill it 271 286 residuals = PyList_New(self->params.npoints); 287 288 regterm = reg_term(pars, self->params.d_max, npars); 272 289 273 290 for(i=0; i<self->params.npoints; i++) { 274 //printf("\n%i: ", i);275 291 diff = self->params.y[i] - iq(pars, self->params.d_max, npars, self->params.x[i]); 276 292 residual = diff*diff / (self->params.err[i]*self->params.err[i]); 277 //printf("%i %g\n", i, residual); 293 tmp = residual; 294 295 // regularization term 296 residual += self->params.alpha * regterm; 297 278 298 if (PyList_SetItem(residuals, i, Py_BuildValue("d",residual) ) < 0){ 279 299 PyErr_SetString(CinvertorError, … … 286 306 return residuals; 287 307 } 308 /** 309 * Function to call to evaluate the residuals 310 * for P(r) minimization (for testing purposes) 311 * @param args: input parameters 312 * @return: list of residuals 313 */ 314 static PyObject * pr_residuals(Cinvertor *self, PyObject *args) { 315 double *pars; 316 PyObject* residuals; 317 PyObject* temp; 318 double *res; 319 int i; 320 double residual, diff; 321 // Regularization factor 322 double regterm = 0.0; 323 double tmp = 0.0; 324 325 PyObject *data_obj; 326 Py_ssize_t npars; 327 328 if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; 329 330 OUTVECTOR(data_obj,pars,npars); 331 332 // Should create this list only once and refill it 333 residuals = PyList_New(self->params.npoints); 334 335 regterm = reg_term(pars, self->params.d_max, npars); 336 337 338 for(i=0; i<self->params.npoints; i++) { 339 diff = self->params.y[i] - pr(pars, self->params.d_max, npars, self->params.x[i]); 340 residual = diff*diff / (self->params.err[i]*self->params.err[i]); 341 tmp = residual; 342 343 // regularization term 344 residual += self->params.alpha * regterm; 345 346 if (PyList_SetItem(residuals, i, Py_BuildValue("d",residual) ) < 0){ 347 PyErr_SetString(CinvertorError, 348 "Cinvertor.residuals: error setting residual."); 349 return NULL; 350 }; 351 352 } 353 354 return residuals; 355 } 288 356 289 357 /** … … 323 391 } 324 392 393 /** 394 * Function to call to evaluate P(r) with errors 395 * @param args: c-parameters and r 396 * @return: P(r) 397 */ 398 static PyObject * get_pr_err(Cinvertor *self, PyObject *args) { 399 double *pars; 400 double *pars_err; 401 double pr_err_value; 402 double r, pr_value; 403 PyObject *data_obj; 404 Py_ssize_t npars; 405 PyObject *err_obj; 406 Py_ssize_t npars2; 407 408 if (!PyArg_ParseTuple(args, "OOd", &data_obj, &err_obj, &r)) return NULL; 409 OUTVECTOR(data_obj,pars,npars); 410 OUTVECTOR(err_obj,pars_err,npars2); 411 412 pr_err(pars, pars_err, self->params.d_max, npars, r, &pr_value, &pr_err_value); 413 return Py_BuildValue("ff", pr_value, pr_err_value); 414 } 415 325 416 326 417 static PyMethodDef Cinvertor_methods[] = { 327 418 {"residuals", (PyCFunction)residuals, METH_VARARGS, "Get the list of residuals"}, 419 {"pr_residuals", (PyCFunction)pr_residuals, METH_VARARGS, "Get the list of residuals"}, 328 420 {"set_x", (PyCFunction)set_x, METH_VARARGS, ""}, 329 421 {"get_x", (PyCFunction)get_x, METH_VARARGS, ""}, … … 334 426 {"set_dmax", (PyCFunction)set_dmax, METH_VARARGS, ""}, 335 427 {"get_dmax", (PyCFunction)get_dmax, METH_VARARGS, ""}, 428 {"set_alpha", (PyCFunction)set_alpha, METH_VARARGS, ""}, 429 {"get_alpha", (PyCFunction)get_alpha, METH_VARARGS, ""}, 336 430 {"get_nx", (PyCFunction)get_nx, METH_VARARGS, ""}, 337 431 {"get_ny", (PyCFunction)get_ny, METH_VARARGS, ""}, … … 339 433 {"iq", (PyCFunction)get_iq, METH_VARARGS, ""}, 340 434 {"pr", (PyCFunction)get_pr, METH_VARARGS, ""}, 435 {"get_pr_err", (PyCFunction)get_pr_err, METH_VARARGS, ""}, 341 436 {"is_valid", (PyCFunction)is_valid, METH_VARARGS, ""}, 342 437 -
pr_inversion/c_extensions/invertor.c
r9e8dc22 reca05c8 40 40 double ortho(double d_max, int n, double r) { 41 41 return 2.0*r*sin(pi*n*r/d_max); 42 43 42 } 44 43 … … 76 75 */ 77 76 double pr(double *pars, double d_max, int n_c, double r) { 78 double sum = 0.0; 77 double sum = 0.0; 79 78 int i; 80 79 for (i=0; i<n_c; i++) { … … 84 83 } 85 84 85 /** 86 * P(r) calculated from the expansion, with errors 87 */ 88 void pr_err(double *pars, double *err, double d_max, int n_c, 89 double r, double *pr_value, double *pr_value_err) { 90 double sum = 0.0; 91 double sum_err = 0.0; 92 double func_value; 93 int i; 94 for (i=0; i<n_c; i++) { 95 func_value = ortho(d_max, i+1, r); 96 sum += pars[i] * func_value; 97 sum_err += err[i]*err[i]*func_value*func_value; 98 } 99 *pr_value = sum; 100 if (sum_err>0) { 101 *pr_value_err = sqrt(sum_err); 102 } else { 103 *pr_value_err = sum; 104 } 105 } 86 106 107 /** 108 * dP(r)/dr calculated from the expansion. 109 */ 110 double dprdr(double *pars, double d_max, int n_c, double r) { 111 double sum = 0.0; 112 int i; 113 for (i=0; i<n_c; i++) { 114 sum += pars[i] * 2.0*(sin(pi*(i+1)*r/d_max) + pi*(i+1)*r/d_max * cos(pi*(i+1)*r/d_max)); 115 } 116 return sum; 117 } 118 119 /** 120 * regularization term calculated from the expansion. 121 */ 122 double reg_term(double *pars, double d_max, int n_c) { 123 double sum = 0.0; 124 double r; 125 double deriv; 126 int i; 127 for (i=0; i<25; i++) { 128 r = d_max/25.0*i; 129 deriv = dprdr(pars, d_max, n_c, r); 130 sum += deriv*deriv; 131 } 132 return sum/25.0*d_max; 133 } 134 135 -
pr_inversion/c_extensions/invertor.h
r9e8dc22 reca05c8 10 10 int npoints; 11 11 int ny; 12 int nerr; 13 } Invertor_params; 12 int nerr; 13 double alpha; 14 } Invertor_params; 14 15 15 16 void invertor_dealloc(Invertor_params *pars); … … 24 25 double iq(double *c, double d_max, int n_c, double q); 25 26 double pr(double *c, double d_max, int n_c, double r); 27 double dprdr(double *pars, double d_max, int n_c, double r); 28 double reg_term(double *pars, double d_max, int n_c); 29 void pr_err(double *pars, double *err, double d_max, int n_c, 30 double r, double *pr_value, double *pr_value_err); 26 31 27 32 #endif
Note: See TracChangeset
for help on using the changeset viewer.