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 | */ |
---|
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 | PyObject * CinvertorError; |
---|
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 |
---|
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 | */ |
---|
42 | typedef struct { |
---|
43 | PyObject_HEAD |
---|
44 | /// Internal data structure |
---|
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 | |
---|
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"; |
---|
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; |
---|
98 | int i; |
---|
99 | |
---|
100 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
101 | OUTVECTOR(data_obj,data,ndata); |
---|
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; |
---|
117 | self->params.npoints = ndata; |
---|
118 | return Py_BuildValue("i", self->params.npoints); |
---|
119 | } |
---|
120 | |
---|
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 | |
---|
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; |
---|
133 | OUTVECTOR(data_obj, data, ndata); |
---|
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 | |
---|
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 | |
---|
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; |
---|
163 | int i; |
---|
164 | |
---|
165 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
166 | OUTVECTOR(data_obj,data,ndata); |
---|
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; |
---|
182 | self->params.ny = ndata; |
---|
183 | return Py_BuildValue("i", self->params.ny); |
---|
184 | } |
---|
185 | |
---|
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 | |
---|
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; |
---|
198 | OUTVECTOR(data_obj, data, ndata); |
---|
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 | |
---|
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 | |
---|
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; |
---|
228 | int i; |
---|
229 | |
---|
230 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
231 | OUTVECTOR(data_obj,data,ndata); |
---|
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; |
---|
247 | self->params.nerr = ndata; |
---|
248 | return Py_BuildValue("i", self->params.nerr); |
---|
249 | } |
---|
250 | |
---|
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 | |
---|
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; |
---|
263 | OUTVECTOR(data_obj, data, ndata); |
---|
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 | |
---|
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 | |
---|
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 | |
---|
296 | const char set_has_bck_doc[] = |
---|
297 | "Sets background flag\n"; |
---|
298 | |
---|
299 | /** |
---|
300 | * Sets the maximum distance |
---|
301 | */ |
---|
302 | static PyObject * set_has_bck(Cinvertor *self, PyObject *args) { |
---|
303 | int has_bck; |
---|
304 | |
---|
305 | if (!PyArg_ParseTuple(args, "i", &has_bck)) return NULL; |
---|
306 | self->params.has_bck = has_bck; |
---|
307 | return Py_BuildValue("i", self->params.has_bck); |
---|
308 | } |
---|
309 | |
---|
310 | const char get_has_bck_doc[] = |
---|
311 | "Gets background flag\n"; |
---|
312 | |
---|
313 | /** |
---|
314 | * Gets the maximum distance |
---|
315 | */ |
---|
316 | static PyObject * get_has_bck(Cinvertor *self, PyObject *args) { |
---|
317 | return Py_BuildValue("i", self->params.has_bck); |
---|
318 | } |
---|
319 | |
---|
320 | const char set_dmax_doc[] = |
---|
321 | "Sets the maximum distance\n"; |
---|
322 | |
---|
323 | /** |
---|
324 | * Sets the maximum distance |
---|
325 | */ |
---|
326 | static PyObject * set_dmax(Cinvertor *self, PyObject *args) { |
---|
327 | double d_max; |
---|
328 | |
---|
329 | if (!PyArg_ParseTuple(args, "d", &d_max)) return NULL; |
---|
330 | self->params.d_max = d_max; |
---|
331 | return Py_BuildValue("d", self->params.d_max); |
---|
332 | } |
---|
333 | |
---|
334 | const char get_dmax_doc[] = |
---|
335 | "Gets the maximum distance\n"; |
---|
336 | |
---|
337 | /** |
---|
338 | * Gets the maximum distance |
---|
339 | */ |
---|
340 | static PyObject * get_dmax(Cinvertor *self, PyObject *args) { |
---|
341 | return Py_BuildValue("d", self->params.d_max); |
---|
342 | } |
---|
343 | |
---|
344 | const char set_slit_height_doc[] = |
---|
345 | "Sets the slit height in units of q [A-1]\n"; |
---|
346 | |
---|
347 | /** |
---|
348 | * Sets the slit height |
---|
349 | */ |
---|
350 | static PyObject * set_slit_height(Cinvertor *self, PyObject *args) { |
---|
351 | double slit_height; |
---|
352 | |
---|
353 | if (!PyArg_ParseTuple(args, "d", &slit_height)) return NULL; |
---|
354 | self->params.slit_height = slit_height; |
---|
355 | return Py_BuildValue("d", self->params.slit_height); |
---|
356 | } |
---|
357 | |
---|
358 | const char get_slit_height_doc[] = |
---|
359 | "Gets the slit height\n"; |
---|
360 | |
---|
361 | /** |
---|
362 | * Gets the slit height |
---|
363 | */ |
---|
364 | static PyObject * get_slit_height(Cinvertor *self, PyObject *args) { |
---|
365 | return Py_BuildValue("d", self->params.slit_height); |
---|
366 | } |
---|
367 | |
---|
368 | const char set_slit_width_doc[] = |
---|
369 | "Sets the slit width in units of q [A-1]\n"; |
---|
370 | |
---|
371 | /** |
---|
372 | * Sets the slit width |
---|
373 | */ |
---|
374 | static PyObject * set_slit_width(Cinvertor *self, PyObject *args) { |
---|
375 | double slit_width; |
---|
376 | |
---|
377 | if (!PyArg_ParseTuple(args, "d", &slit_width)) return NULL; |
---|
378 | self->params.slit_width = slit_width; |
---|
379 | return Py_BuildValue("d", self->params.slit_width); |
---|
380 | } |
---|
381 | |
---|
382 | const char get_slit_width_doc[] = |
---|
383 | "Gets the slit width\n"; |
---|
384 | |
---|
385 | /** |
---|
386 | * Gets the slit width |
---|
387 | */ |
---|
388 | static PyObject * get_slit_width(Cinvertor *self, PyObject *args) { |
---|
389 | return Py_BuildValue("d", self->params.slit_width); |
---|
390 | } |
---|
391 | |
---|
392 | |
---|
393 | const char set_qmin_doc[] = |
---|
394 | "Sets the minimum q\n"; |
---|
395 | |
---|
396 | /** |
---|
397 | * Sets the minimum q |
---|
398 | */ |
---|
399 | static PyObject * set_qmin(Cinvertor *self, PyObject *args) { |
---|
400 | double q_min; |
---|
401 | |
---|
402 | if (!PyArg_ParseTuple(args, "d", &q_min)) return NULL; |
---|
403 | self->params.q_min = q_min; |
---|
404 | return Py_BuildValue("d", self->params.q_min); |
---|
405 | } |
---|
406 | |
---|
407 | const char get_qmin_doc[] = |
---|
408 | "Gets the minimum q\n"; |
---|
409 | |
---|
410 | /** |
---|
411 | * Gets the minimum q |
---|
412 | */ |
---|
413 | static PyObject * get_qmin(Cinvertor *self, PyObject *args) { |
---|
414 | return Py_BuildValue("d", self->params.q_min); |
---|
415 | } |
---|
416 | |
---|
417 | const char set_qmax_doc[] = |
---|
418 | "Sets the maximum q\n"; |
---|
419 | |
---|
420 | /** |
---|
421 | * Sets the maximum q |
---|
422 | */ |
---|
423 | static PyObject * set_qmax(Cinvertor *self, PyObject *args) { |
---|
424 | double q_max; |
---|
425 | |
---|
426 | if (!PyArg_ParseTuple(args, "d", &q_max)) return NULL; |
---|
427 | self->params.q_max = q_max; |
---|
428 | return Py_BuildValue("d", self->params.q_max); |
---|
429 | } |
---|
430 | |
---|
431 | const char get_qmax_doc[] = |
---|
432 | "Gets the maximum q\n"; |
---|
433 | |
---|
434 | /** |
---|
435 | * Gets the maximum q |
---|
436 | */ |
---|
437 | static PyObject * get_qmax(Cinvertor *self, PyObject *args) { |
---|
438 | return Py_BuildValue("d", self->params.q_max); |
---|
439 | } |
---|
440 | |
---|
441 | const char set_alpha_doc[] = |
---|
442 | "Sets the alpha parameter\n"; |
---|
443 | |
---|
444 | static PyObject * set_alpha(Cinvertor *self, PyObject *args) { |
---|
445 | double alpha; |
---|
446 | |
---|
447 | if (!PyArg_ParseTuple(args, "d", &alpha)) return NULL; |
---|
448 | self->params.alpha = alpha; |
---|
449 | return Py_BuildValue("d", self->params.alpha); |
---|
450 | } |
---|
451 | |
---|
452 | const char get_alpha_doc[] = |
---|
453 | "Gets the alpha parameter\n"; |
---|
454 | |
---|
455 | /** |
---|
456 | * Gets the maximum distance |
---|
457 | */ |
---|
458 | static PyObject * get_alpha(Cinvertor *self, PyObject *args) { |
---|
459 | return Py_BuildValue("d", self->params.alpha); |
---|
460 | } |
---|
461 | |
---|
462 | const char get_nx_doc[] = |
---|
463 | "Gets the number of x points\n"; |
---|
464 | |
---|
465 | /** |
---|
466 | * Gets the number of x points |
---|
467 | */ |
---|
468 | static PyObject * get_nx(Cinvertor *self, PyObject *args) { |
---|
469 | return Py_BuildValue("i", self->params.npoints); |
---|
470 | } |
---|
471 | |
---|
472 | const char get_ny_doc[] = |
---|
473 | "Gets the number of y points\n"; |
---|
474 | |
---|
475 | /** |
---|
476 | * Gets the number of y points |
---|
477 | */ |
---|
478 | static PyObject * get_ny(Cinvertor *self, PyObject *args) { |
---|
479 | return Py_BuildValue("i", self->params.ny); |
---|
480 | } |
---|
481 | |
---|
482 | const char get_nerr_doc[] = |
---|
483 | "Gets the number of err points\n"; |
---|
484 | |
---|
485 | /** |
---|
486 | * Gets the number of error points |
---|
487 | */ |
---|
488 | static PyObject * get_nerr(Cinvertor *self, PyObject *args) { |
---|
489 | return Py_BuildValue("i", self->params.nerr); |
---|
490 | } |
---|
491 | |
---|
492 | |
---|
493 | const char residuals_doc[] = |
---|
494 | "Function to call to evaluate the residuals\n" |
---|
495 | "for P(r) inversion\n" |
---|
496 | " @param args: input parameters\n" |
---|
497 | " @return: list of residuals"; |
---|
498 | |
---|
499 | /** |
---|
500 | * Function to call to evaluate the residuals |
---|
501 | * @param args: input parameters |
---|
502 | * @return: list of residuals |
---|
503 | */ |
---|
504 | static PyObject * residuals(Cinvertor *self, PyObject *args) { |
---|
505 | double *pars; |
---|
506 | PyObject* residuals; |
---|
507 | int i; |
---|
508 | double residual, diff; |
---|
509 | // Regularization factor |
---|
510 | double regterm = 0.0; |
---|
511 | double tmp = 0.0; |
---|
512 | // Number of slices in regularization term estimate |
---|
513 | int nslice = 25; |
---|
514 | |
---|
515 | PyObject *data_obj; |
---|
516 | Py_ssize_t npars; |
---|
517 | |
---|
518 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
519 | |
---|
520 | OUTVECTOR(data_obj,pars,npars); |
---|
521 | |
---|
522 | // PyList of residuals |
---|
523 | // Should create this list only once and refill it |
---|
524 | residuals = PyList_New(self->params.npoints); |
---|
525 | |
---|
526 | regterm = reg_term(pars, self->params.d_max, npars, nslice); |
---|
527 | |
---|
528 | for(i=0; i<self->params.npoints; i++) { |
---|
529 | diff = self->params.y[i] - iq(pars, self->params.d_max, npars, self->params.x[i]); |
---|
530 | residual = diff*diff / (self->params.err[i]*self->params.err[i]); |
---|
531 | tmp = residual; |
---|
532 | |
---|
533 | // regularization term |
---|
534 | residual += self->params.alpha * regterm; |
---|
535 | |
---|
536 | if (PyList_SetItem(residuals, i, Py_BuildValue("d",residual) ) < 0){ |
---|
537 | PyErr_SetString(CinvertorError, |
---|
538 | "Cinvertor.residuals: error setting residual."); |
---|
539 | return NULL; |
---|
540 | }; |
---|
541 | |
---|
542 | } |
---|
543 | |
---|
544 | return residuals; |
---|
545 | } |
---|
546 | |
---|
547 | const char pr_residuals_doc[] = |
---|
548 | "Function to call to evaluate the residuals\n" |
---|
549 | "for P(r) minimization (for testing purposes)\n" |
---|
550 | " @param args: input parameters\n" |
---|
551 | " @return: list of residuals"; |
---|
552 | |
---|
553 | /** |
---|
554 | * Function to call to evaluate the residuals |
---|
555 | * for P(r) minimization (for testing purposes) |
---|
556 | * @param args: input parameters |
---|
557 | * @return: list of residuals |
---|
558 | */ |
---|
559 | static PyObject * pr_residuals(Cinvertor *self, PyObject *args) { |
---|
560 | double *pars; |
---|
561 | PyObject* residuals; |
---|
562 | int i; |
---|
563 | double residual, diff; |
---|
564 | // Regularization factor |
---|
565 | double regterm = 0.0; |
---|
566 | double tmp = 0.0; |
---|
567 | // Number of slices in regularization term estimate |
---|
568 | int nslice = 25; |
---|
569 | |
---|
570 | PyObject *data_obj; |
---|
571 | Py_ssize_t npars; |
---|
572 | |
---|
573 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
574 | |
---|
575 | OUTVECTOR(data_obj,pars,npars); |
---|
576 | |
---|
577 | // Should create this list only once and refill it |
---|
578 | residuals = PyList_New(self->params.npoints); |
---|
579 | |
---|
580 | regterm = reg_term(pars, self->params.d_max, npars, nslice); |
---|
581 | |
---|
582 | |
---|
583 | for(i=0; i<self->params.npoints; i++) { |
---|
584 | diff = self->params.y[i] - pr(pars, self->params.d_max, npars, self->params.x[i]); |
---|
585 | residual = diff*diff / (self->params.err[i]*self->params.err[i]); |
---|
586 | tmp = residual; |
---|
587 | |
---|
588 | // regularization term |
---|
589 | residual += self->params.alpha * regterm; |
---|
590 | |
---|
591 | if (PyList_SetItem(residuals, i, Py_BuildValue("d",residual) ) < 0){ |
---|
592 | PyErr_SetString(CinvertorError, |
---|
593 | "Cinvertor.residuals: error setting residual."); |
---|
594 | return NULL; |
---|
595 | }; |
---|
596 | |
---|
597 | } |
---|
598 | |
---|
599 | return residuals; |
---|
600 | } |
---|
601 | |
---|
602 | const char get_iq_doc[] = |
---|
603 | "Function to call to evaluate the scattering intensity\n" |
---|
604 | " @param args: c-parameters, and q\n" |
---|
605 | " @return: I(q)"; |
---|
606 | |
---|
607 | /** |
---|
608 | * Function to call to evaluate the scattering intensity |
---|
609 | * @param args: c-parameters, and q |
---|
610 | * @return: I(q) |
---|
611 | */ |
---|
612 | static PyObject * get_iq(Cinvertor *self, PyObject *args) { |
---|
613 | double *pars; |
---|
614 | double q, iq_value; |
---|
615 | PyObject *data_obj; |
---|
616 | Py_ssize_t npars; |
---|
617 | |
---|
618 | if (!PyArg_ParseTuple(args, "Od", &data_obj, &q)) return NULL; |
---|
619 | OUTVECTOR(data_obj,pars,npars); |
---|
620 | |
---|
621 | iq_value = iq(pars, self->params.d_max, npars, q); |
---|
622 | return Py_BuildValue("f", iq_value); |
---|
623 | } |
---|
624 | |
---|
625 | const char get_iq_smeared_doc[] = |
---|
626 | "Function to call to evaluate the scattering intensity.\n" |
---|
627 | "The scattering intensity is slit-smeared." |
---|
628 | " @param args: c-parameters, and q\n" |
---|
629 | " @return: I(q)"; |
---|
630 | |
---|
631 | /** |
---|
632 | * Function to call to evaluate the scattering intensity |
---|
633 | * The scattering intensity is slit-smeared. |
---|
634 | * @param args: c-parameters, and q |
---|
635 | * @return: I(q) |
---|
636 | */ |
---|
637 | static PyObject * get_iq_smeared(Cinvertor *self, PyObject *args) { |
---|
638 | double *pars; |
---|
639 | double q, iq_value; |
---|
640 | PyObject *data_obj; |
---|
641 | Py_ssize_t npars; |
---|
642 | |
---|
643 | if (!PyArg_ParseTuple(args, "Od", &data_obj, &q)) return NULL; |
---|
644 | OUTVECTOR(data_obj,pars,npars); |
---|
645 | |
---|
646 | iq_value = iq_smeared(pars, self->params.d_max, npars, |
---|
647 | self->params.slit_height, self->params.slit_width, |
---|
648 | q, 21); |
---|
649 | return Py_BuildValue("f", iq_value); |
---|
650 | } |
---|
651 | |
---|
652 | const char get_pr_doc[] = |
---|
653 | "Function to call to evaluate P(r)\n" |
---|
654 | " @param args: c-parameters and r\n" |
---|
655 | " @return: P(r)"; |
---|
656 | |
---|
657 | /** |
---|
658 | * Function to call to evaluate P(r) |
---|
659 | * @param args: c-parameters and r |
---|
660 | * @return: P(r) |
---|
661 | */ |
---|
662 | static PyObject * get_pr(Cinvertor *self, PyObject *args) { |
---|
663 | double *pars; |
---|
664 | double r, pr_value; |
---|
665 | PyObject *data_obj; |
---|
666 | Py_ssize_t npars; |
---|
667 | |
---|
668 | if (!PyArg_ParseTuple(args, "Od", &data_obj, &r)) return NULL; |
---|
669 | OUTVECTOR(data_obj,pars,npars); |
---|
670 | |
---|
671 | pr_value = pr(pars, self->params.d_max, npars, r); |
---|
672 | return Py_BuildValue("f", pr_value); |
---|
673 | } |
---|
674 | |
---|
675 | const char get_pr_err_doc[] = |
---|
676 | "Function to call to evaluate P(r) with errors\n" |
---|
677 | " @param args: c-parameters and r\n" |
---|
678 | " @return: (P(r),dP(r))"; |
---|
679 | |
---|
680 | /** |
---|
681 | * Function to call to evaluate P(r) with errors |
---|
682 | * @param args: c-parameters and r |
---|
683 | * @return: P(r) |
---|
684 | */ |
---|
685 | static PyObject * get_pr_err(Cinvertor *self, PyObject *args) { |
---|
686 | double *pars; |
---|
687 | double *pars_err; |
---|
688 | double pr_err_value; |
---|
689 | double r, pr_value; |
---|
690 | PyObject *data_obj; |
---|
691 | Py_ssize_t npars; |
---|
692 | PyObject *err_obj; |
---|
693 | Py_ssize_t npars2; |
---|
694 | |
---|
695 | if (!PyArg_ParseTuple(args, "OOd", &data_obj, &err_obj, &r)) return NULL; |
---|
696 | OUTVECTOR(data_obj,pars,npars); |
---|
697 | |
---|
698 | if (err_obj == Py_None) { |
---|
699 | pr_value = pr(pars, self->params.d_max, npars, r); |
---|
700 | pr_err_value = 0.0; |
---|
701 | } else { |
---|
702 | OUTVECTOR(err_obj,pars_err,npars2); |
---|
703 | pr_err(pars, pars_err, self->params.d_max, npars, r, &pr_value, &pr_err_value); |
---|
704 | } |
---|
705 | return Py_BuildValue("ff", pr_value, pr_err_value); |
---|
706 | } |
---|
707 | |
---|
708 | const char basefunc_ft_doc[] = |
---|
709 | "Returns the value of the nth Fourier transofrmed base function\n" |
---|
710 | " @param args: c-parameters, n and q\n" |
---|
711 | " @return: nth Fourier transformed base function, evaluated at q"; |
---|
712 | |
---|
713 | static PyObject * basefunc_ft(Cinvertor *self, PyObject *args) { |
---|
714 | double d_max, q; |
---|
715 | int n; |
---|
716 | |
---|
717 | if (!PyArg_ParseTuple(args, "did", &d_max, &n, &q)) return NULL; |
---|
718 | return Py_BuildValue("f", ortho_transformed(d_max, n, q)); |
---|
719 | |
---|
720 | } |
---|
721 | |
---|
722 | const char oscillations_doc[] = |
---|
723 | "Returns the value of the oscillation figure of merit for\n" |
---|
724 | "the given set of coefficients. For a sphere, the oscillation\n" |
---|
725 | "figure of merit is 1.1.\n" |
---|
726 | " @param args: c-parameters\n" |
---|
727 | " @return: oscillation figure of merit"; |
---|
728 | |
---|
729 | static PyObject * oscillations(Cinvertor *self, PyObject *args) { |
---|
730 | double *pars; |
---|
731 | PyObject *data_obj; |
---|
732 | Py_ssize_t npars; |
---|
733 | double oscill, norm; |
---|
734 | |
---|
735 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
736 | OUTVECTOR(data_obj,pars,npars); |
---|
737 | |
---|
738 | oscill = reg_term(pars, self->params.d_max, npars, 100); |
---|
739 | norm = int_p2(pars, self->params.d_max, npars, 100); |
---|
740 | return Py_BuildValue("f", sqrt(oscill/norm)/acos(-1.0)*self->params.d_max ); |
---|
741 | |
---|
742 | } |
---|
743 | |
---|
744 | const char get_peaks_doc[] = |
---|
745 | "Returns the number of peaks in the output P(r) distrubution\n" |
---|
746 | "for the given set of coefficients.\n" |
---|
747 | " @param args: c-parameters\n" |
---|
748 | " @return: number of P(r) peaks"; |
---|
749 | |
---|
750 | static PyObject * get_peaks(Cinvertor *self, PyObject *args) { |
---|
751 | double *pars; |
---|
752 | PyObject *data_obj; |
---|
753 | Py_ssize_t npars; |
---|
754 | int count; |
---|
755 | |
---|
756 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
757 | OUTVECTOR(data_obj,pars,npars); |
---|
758 | |
---|
759 | count = npeaks(pars, self->params.d_max, npars, 100); |
---|
760 | |
---|
761 | return Py_BuildValue("i", count ); |
---|
762 | |
---|
763 | } |
---|
764 | |
---|
765 | const char get_positive_doc[] = |
---|
766 | "Returns the fraction of P(r) that is positive over\n" |
---|
767 | "the full range of r for the given set of coefficients.\n" |
---|
768 | " @param args: c-parameters\n" |
---|
769 | " @return: fraction of P(r) that is positive"; |
---|
770 | |
---|
771 | static PyObject * get_positive(Cinvertor *self, PyObject *args) { |
---|
772 | double *pars; |
---|
773 | PyObject *data_obj; |
---|
774 | Py_ssize_t npars; |
---|
775 | double fraction; |
---|
776 | |
---|
777 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
778 | OUTVECTOR(data_obj,pars,npars); |
---|
779 | |
---|
780 | fraction = positive_integral(pars, self->params.d_max, npars, 100); |
---|
781 | |
---|
782 | return Py_BuildValue("f", fraction ); |
---|
783 | |
---|
784 | } |
---|
785 | |
---|
786 | const char get_pos_err_doc[] = |
---|
787 | "Returns the fraction of P(r) that is 1 standard deviation\n" |
---|
788 | "above zero over the full range of r for the given set of coefficients.\n" |
---|
789 | " @param args: c-parameters\n" |
---|
790 | " @return: fraction of P(r) that is positive"; |
---|
791 | |
---|
792 | static PyObject * get_pos_err(Cinvertor *self, PyObject *args) { |
---|
793 | double *pars; |
---|
794 | double *pars_err; |
---|
795 | PyObject *data_obj; |
---|
796 | PyObject *err_obj; |
---|
797 | Py_ssize_t npars; |
---|
798 | Py_ssize_t npars2; |
---|
799 | double fraction; |
---|
800 | |
---|
801 | if (!PyArg_ParseTuple(args, "OO", &data_obj, &err_obj)) return NULL; |
---|
802 | OUTVECTOR(data_obj,pars,npars); |
---|
803 | OUTVECTOR(err_obj,pars_err,npars2); |
---|
804 | |
---|
805 | fraction = positive_errors(pars, pars_err, self->params.d_max, npars, 51); |
---|
806 | |
---|
807 | return Py_BuildValue("f", fraction ); |
---|
808 | |
---|
809 | } |
---|
810 | |
---|
811 | const char get_rg_doc[] = |
---|
812 | "Returns the value of the radius of gyration Rg.\n" |
---|
813 | " @param args: c-parameters\n" |
---|
814 | " @return: Rg"; |
---|
815 | |
---|
816 | static PyObject * get_rg(Cinvertor *self, PyObject *args) { |
---|
817 | double *pars; |
---|
818 | PyObject *data_obj; |
---|
819 | Py_ssize_t npars; |
---|
820 | double value; |
---|
821 | |
---|
822 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
823 | OUTVECTOR(data_obj,pars,npars); |
---|
824 | |
---|
825 | value = rg(pars, self->params.d_max, npars, 101); |
---|
826 | |
---|
827 | return Py_BuildValue("f", value ); |
---|
828 | |
---|
829 | } |
---|
830 | |
---|
831 | const char get_iq0_doc[] = |
---|
832 | "Returns the value of I(q=0).\n" |
---|
833 | " @param args: c-parameters\n" |
---|
834 | " @return: I(q=0)"; |
---|
835 | |
---|
836 | static PyObject * get_iq0(Cinvertor *self, PyObject *args) { |
---|
837 | double *pars; |
---|
838 | PyObject *data_obj; |
---|
839 | Py_ssize_t npars; |
---|
840 | double value; |
---|
841 | |
---|
842 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
843 | OUTVECTOR(data_obj,pars,npars); |
---|
844 | |
---|
845 | value = 4.0*acos(-1.0)*int_pr(pars, self->params.d_max, npars, 101); |
---|
846 | |
---|
847 | return Py_BuildValue("f", value ); |
---|
848 | |
---|
849 | } |
---|
850 | |
---|
851 | /** |
---|
852 | * Check whether a q-value is within acceptabel limits |
---|
853 | * Return 1 if accepted, 0 if rejected. |
---|
854 | */ |
---|
855 | int accept_q(Cinvertor *self, double q) { |
---|
856 | if (self->params.q_min>0 && q<self->params.q_min) return 0; |
---|
857 | if (self->params.q_max>0 && q>self->params.q_max) return 0; |
---|
858 | return 1; |
---|
859 | } |
---|
860 | |
---|
861 | const char get_matrix_doc[] = |
---|
862 | "Returns A matrix and b vector for least square problem.\n" |
---|
863 | " @param nfunc: number of base functions\n" |
---|
864 | " @param nr: number of r-points used when evaluating reg term.\n" |
---|
865 | " @param a: A array to fill\n" |
---|
866 | " @param b: b vector to fill\n" |
---|
867 | " @return: 0"; |
---|
868 | |
---|
869 | static PyObject * get_matrix(Cinvertor *self, PyObject *args) { |
---|
870 | double *a; |
---|
871 | double *b; |
---|
872 | PyObject *a_obj; |
---|
873 | PyObject *b_obj; |
---|
874 | Py_ssize_t n_a; |
---|
875 | Py_ssize_t n_b; |
---|
876 | // Number of bins for regularization term evaluation |
---|
877 | int nr, nfunc; |
---|
878 | int i, j, i_r; |
---|
879 | double r, sqrt_alpha, pi; |
---|
880 | double tmp; |
---|
881 | int offset; |
---|
882 | |
---|
883 | if (!PyArg_ParseTuple(args, "iiOO", &nfunc, &nr, &a_obj, &b_obj)) return NULL; |
---|
884 | OUTVECTOR(a_obj,a,n_a); |
---|
885 | OUTVECTOR(b_obj,b,n_b); |
---|
886 | |
---|
887 | assert(n_b>=nfunc); |
---|
888 | assert(n_a>=nfunc*(nr+self->params.npoints)); |
---|
889 | |
---|
890 | sqrt_alpha = sqrt(self->params.alpha); |
---|
891 | pi = acos(-1.0); |
---|
892 | offset = (self->params.has_bck==1) ? 0 : 1; |
---|
893 | |
---|
894 | for (j=0; j<nfunc; j++) { |
---|
895 | for (i=0; i<self->params.npoints; i++) { |
---|
896 | if (self->params.err[i]==0.0) { |
---|
897 | PyErr_SetString(CinvertorError, |
---|
898 | "Cinvertor.get_matrix: Some I(Q) points have no error."); |
---|
899 | return NULL; |
---|
900 | } |
---|
901 | if (accept_q(self, self->params.x[i])){ |
---|
902 | if (self->params.has_bck==1 && j==0) { |
---|
903 | a[i*nfunc+j] = 1.0/self->params.err[i]; |
---|
904 | } else { |
---|
905 | if (self->params.slit_width>0 || self->params.slit_height>0) { |
---|
906 | a[i*nfunc+j] = ortho_transformed_smeared(self->params.d_max, |
---|
907 | j+offset, self->params.slit_height, self->params.slit_width, |
---|
908 | self->params.x[i], 21)/self->params.err[i]; |
---|
909 | } else { |
---|
910 | a[i*nfunc+j] = ortho_transformed(self->params.d_max, j+offset, self->params.x[i])/self->params.err[i]; |
---|
911 | } |
---|
912 | } |
---|
913 | } |
---|
914 | } |
---|
915 | for (i_r=0; i_r<nr; i_r++){ |
---|
916 | if (self->params.has_bck==1 && j==0) { |
---|
917 | a[(i_r+self->params.npoints)*nfunc+j] = 0.0; |
---|
918 | } else { |
---|
919 | r = self->params.d_max/nr*i_r; |
---|
920 | tmp = pi*(j+offset)/self->params.d_max; |
---|
921 | a[(i_r+self->params.npoints)*nfunc+j] = sqrt_alpha * 1.0/nr*self->params.d_max*2.0* |
---|
922 | (2.0*pi*(j+offset)/self->params.d_max*cos(pi*(j+offset)*r/self->params.d_max) + |
---|
923 | tmp*tmp*r * sin(pi*(j+offset)*r/self->params.d_max)); |
---|
924 | } |
---|
925 | } |
---|
926 | } |
---|
927 | |
---|
928 | for (i=0; i<self->params.npoints; i++) { |
---|
929 | if (accept_q(self, self->params.x[i])){ |
---|
930 | b[i] = self->params.y[i]/self->params.err[i]; |
---|
931 | } |
---|
932 | } |
---|
933 | |
---|
934 | return Py_BuildValue("i", 0); |
---|
935 | |
---|
936 | } |
---|
937 | |
---|
938 | const char get_invcov_matrix_doc[] = |
---|
939 | " Compute the inverse covariance matrix, defined as inv_cov = a_transposed x a.\n" |
---|
940 | " @param nfunc: number of base functions\n" |
---|
941 | " @param nr: number of r-points used when evaluating reg term.\n" |
---|
942 | " @param a: A array to fill\n" |
---|
943 | " @param inv_cov: inverse covariance array to be filled\n" |
---|
944 | " @return: 0"; |
---|
945 | |
---|
946 | static PyObject * get_invcov_matrix(Cinvertor *self, PyObject *args) { |
---|
947 | double *a; |
---|
948 | PyObject *a_obj; |
---|
949 | Py_ssize_t n_a; |
---|
950 | double *inv_cov; |
---|
951 | PyObject *cov_obj; |
---|
952 | Py_ssize_t n_cov; |
---|
953 | int nr, nfunc; |
---|
954 | int i, j, k; |
---|
955 | |
---|
956 | if (!PyArg_ParseTuple(args, "iiOO", &nfunc, &nr, &a_obj, &cov_obj)) return NULL; |
---|
957 | OUTVECTOR(a_obj,a,n_a); |
---|
958 | OUTVECTOR(cov_obj,inv_cov,n_cov); |
---|
959 | |
---|
960 | assert(n_cov>=nfunc*nfunc); |
---|
961 | assert(n_a>=nfunc*(nr+self->params.npoints)); |
---|
962 | |
---|
963 | for (i=0; i<nfunc; i++) { |
---|
964 | for (j=0; j<nfunc; j++) { |
---|
965 | inv_cov[i*nfunc+j] = 0.0; |
---|
966 | for (k=0; k<nr+self->params.npoints; k++) { |
---|
967 | inv_cov[i*nfunc+j] += a[k*nfunc+i]*a[k*nfunc+j]; |
---|
968 | } |
---|
969 | } |
---|
970 | } |
---|
971 | return Py_BuildValue("i", 0); |
---|
972 | } |
---|
973 | |
---|
974 | const char get_reg_size_doc[] = |
---|
975 | " Compute the covariance matrix, defined as inv_cov = a_transposed x a.\n" |
---|
976 | " @param nfunc: number of base functions\n" |
---|
977 | " @param nr: number of r-points used when evaluating reg term.\n" |
---|
978 | " @param a: A array to fill\n" |
---|
979 | " @param inv_cov: inverse covariance array to be filled\n" |
---|
980 | " @return: 0"; |
---|
981 | |
---|
982 | static PyObject * get_reg_size(Cinvertor *self, PyObject *args) { |
---|
983 | double *a; |
---|
984 | PyObject *a_obj; |
---|
985 | Py_ssize_t n_a; |
---|
986 | int nr, nfunc; |
---|
987 | int i, j; |
---|
988 | double sum_sig, sum_reg; |
---|
989 | |
---|
990 | if (!PyArg_ParseTuple(args, "iiO", &nfunc, &nr, &a_obj)) return NULL; |
---|
991 | OUTVECTOR(a_obj,a,n_a); |
---|
992 | |
---|
993 | assert(n_a>=nfunc*(nr+self->params.npoints)); |
---|
994 | |
---|
995 | sum_sig = 0.0; |
---|
996 | sum_reg = 0.0; |
---|
997 | for (j=0; j<nfunc; j++){ |
---|
998 | for (i=0; i<self->params.npoints; i++){ |
---|
999 | if (accept_q(self, self->params.x[i])==1) |
---|
1000 | sum_sig += (a[i*nfunc+j])*(a[i*nfunc+j]); |
---|
1001 | } |
---|
1002 | for (i=0; i<nr; i++){ |
---|
1003 | sum_reg += (a[(i+self->params.npoints)*nfunc+j])*(a[(i+self->params.npoints)*nfunc+j]); |
---|
1004 | } |
---|
1005 | } |
---|
1006 | return Py_BuildValue("ff", sum_sig, sum_reg); |
---|
1007 | } |
---|
1008 | |
---|
1009 | const char eeeget_qmin_doc[] = "\ |
---|
1010 | This is a multiline doc string.\n\ |
---|
1011 | \n\ |
---|
1012 | This is the second line."; |
---|
1013 | const char eeeset_qmin_doc[] = |
---|
1014 | "This is a multiline doc string.\n" |
---|
1015 | "\n" |
---|
1016 | "This is the second line."; |
---|
1017 | |
---|
1018 | static PyMethodDef Cinvertor_methods[] = { |
---|
1019 | {"residuals", (PyCFunction)residuals, METH_VARARGS, residuals_doc}, |
---|
1020 | {"pr_residuals", (PyCFunction)pr_residuals, METH_VARARGS, pr_residuals_doc}, |
---|
1021 | {"set_x", (PyCFunction)set_x, METH_VARARGS, set_x_doc}, |
---|
1022 | {"get_x", (PyCFunction)get_x, METH_VARARGS, get_x_doc}, |
---|
1023 | {"set_y", (PyCFunction)set_y, METH_VARARGS, set_y_doc}, |
---|
1024 | {"get_y", (PyCFunction)get_y, METH_VARARGS, get_y_doc}, |
---|
1025 | {"set_err", (PyCFunction)set_err, METH_VARARGS, set_err_doc}, |
---|
1026 | {"get_err", (PyCFunction)get_err, METH_VARARGS, get_err_doc}, |
---|
1027 | {"set_dmax", (PyCFunction)set_dmax, METH_VARARGS, set_dmax_doc}, |
---|
1028 | {"get_dmax", (PyCFunction)get_dmax, METH_VARARGS, get_dmax_doc}, |
---|
1029 | {"set_qmin", (PyCFunction)set_qmin, METH_VARARGS, set_qmin_doc}, |
---|
1030 | {"get_qmin", (PyCFunction)get_qmin, METH_VARARGS, get_qmin_doc}, |
---|
1031 | {"set_qmax", (PyCFunction)set_qmax, METH_VARARGS, set_qmax_doc}, |
---|
1032 | {"get_qmax", (PyCFunction)get_qmax, METH_VARARGS, get_qmax_doc}, |
---|
1033 | {"set_alpha", (PyCFunction)set_alpha, METH_VARARGS, set_alpha_doc}, |
---|
1034 | {"get_alpha", (PyCFunction)get_alpha, METH_VARARGS, get_alpha_doc}, |
---|
1035 | {"set_slit_width", (PyCFunction)set_slit_width, METH_VARARGS, set_slit_width_doc}, |
---|
1036 | {"get_slit_width", (PyCFunction)get_slit_width, METH_VARARGS, get_slit_width_doc}, |
---|
1037 | {"set_slit_height", (PyCFunction)set_slit_height, METH_VARARGS, set_slit_height_doc}, |
---|
1038 | {"get_slit_height", (PyCFunction)get_slit_height, METH_VARARGS, get_slit_height_doc}, |
---|
1039 | {"set_has_bck", (PyCFunction)set_has_bck, METH_VARARGS, set_has_bck_doc}, |
---|
1040 | {"get_has_bck", (PyCFunction)get_has_bck, METH_VARARGS, get_has_bck_doc}, |
---|
1041 | {"get_nx", (PyCFunction)get_nx, METH_VARARGS, get_nx_doc}, |
---|
1042 | {"get_ny", (PyCFunction)get_ny, METH_VARARGS, get_ny_doc}, |
---|
1043 | {"get_nerr", (PyCFunction)get_nerr, METH_VARARGS, get_nerr_doc}, |
---|
1044 | {"iq", (PyCFunction)get_iq, METH_VARARGS, get_iq_doc}, |
---|
1045 | {"iq_smeared", (PyCFunction)get_iq_smeared, METH_VARARGS, get_iq_smeared_doc}, |
---|
1046 | {"pr", (PyCFunction)get_pr, METH_VARARGS, get_pr_doc}, |
---|
1047 | {"get_pr_err", (PyCFunction)get_pr_err, METH_VARARGS, get_pr_err_doc}, |
---|
1048 | {"is_valid", (PyCFunction)is_valid, METH_VARARGS, is_valid_doc}, |
---|
1049 | {"basefunc_ft", (PyCFunction)basefunc_ft, METH_VARARGS, basefunc_ft_doc}, |
---|
1050 | {"oscillations", (PyCFunction)oscillations, METH_VARARGS, oscillations_doc}, |
---|
1051 | {"get_peaks", (PyCFunction)get_peaks, METH_VARARGS, get_peaks_doc}, |
---|
1052 | {"get_positive", (PyCFunction)get_positive, METH_VARARGS, get_positive_doc}, |
---|
1053 | {"get_pos_err", (PyCFunction)get_pos_err, METH_VARARGS, get_pos_err_doc}, |
---|
1054 | {"rg", (PyCFunction)get_rg, METH_VARARGS, get_rg_doc}, |
---|
1055 | {"iq0", (PyCFunction)get_iq0, METH_VARARGS, get_iq0_doc}, |
---|
1056 | {"_get_matrix", (PyCFunction)get_matrix, METH_VARARGS, get_matrix_doc}, |
---|
1057 | {"_get_invcov_matrix", (PyCFunction)get_invcov_matrix, METH_VARARGS, get_invcov_matrix_doc}, |
---|
1058 | {"_get_reg_size", (PyCFunction)get_reg_size, METH_VARARGS, get_reg_size_doc}, |
---|
1059 | |
---|
1060 | {NULL} |
---|
1061 | }; |
---|
1062 | |
---|
1063 | static PyTypeObject CinvertorType = { |
---|
1064 | PyObject_HEAD_INIT(NULL) |
---|
1065 | 0, /*ob_size*/ |
---|
1066 | "Cinvertor", /*tp_name*/ |
---|
1067 | sizeof(Cinvertor), /*tp_basicsize*/ |
---|
1068 | 0, /*tp_itemsize*/ |
---|
1069 | (destructor)Cinvertor_dealloc, /*tp_dealloc*/ |
---|
1070 | 0, /*tp_print*/ |
---|
1071 | 0, /*tp_getattr*/ |
---|
1072 | 0, /*tp_setattr*/ |
---|
1073 | 0, /*tp_compare*/ |
---|
1074 | 0, /*tp_repr*/ |
---|
1075 | 0, /*tp_as_number*/ |
---|
1076 | 0, /*tp_as_sequence*/ |
---|
1077 | 0, /*tp_as_mapping*/ |
---|
1078 | 0, /*tp_hash */ |
---|
1079 | 0, /*tp_call*/ |
---|
1080 | 0, /*tp_str*/ |
---|
1081 | 0, /*tp_getattro*/ |
---|
1082 | 0, /*tp_setattro*/ |
---|
1083 | 0, /*tp_as_buffer*/ |
---|
1084 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
1085 | "Cinvertor objects", /* tp_doc */ |
---|
1086 | 0, /* tp_traverse */ |
---|
1087 | 0, /* tp_clear */ |
---|
1088 | 0, /* tp_richcompare */ |
---|
1089 | 0, /* tp_weaklistoffset */ |
---|
1090 | 0, /* tp_iter */ |
---|
1091 | 0, /* tp_iternext */ |
---|
1092 | Cinvertor_methods, /* tp_methods */ |
---|
1093 | Cinvertor_members, /* tp_members */ |
---|
1094 | 0, /* tp_getset */ |
---|
1095 | 0, /* tp_base */ |
---|
1096 | 0, /* tp_dict */ |
---|
1097 | 0, /* tp_descr_get */ |
---|
1098 | 0, /* tp_descr_set */ |
---|
1099 | 0, /* tp_dictoffset */ |
---|
1100 | (initproc)Cinvertor_init, /* tp_init */ |
---|
1101 | 0, /* tp_alloc */ |
---|
1102 | Cinvertor_new, /* tp_new */ |
---|
1103 | }; |
---|
1104 | |
---|
1105 | |
---|
1106 | static PyMethodDef module_methods[] = { |
---|
1107 | {NULL} |
---|
1108 | }; |
---|
1109 | |
---|
1110 | /** |
---|
1111 | * Function used to add the model class to a module |
---|
1112 | * @param module: module to add the class to |
---|
1113 | */ |
---|
1114 | void addCinvertor(PyObject *module) { |
---|
1115 | PyObject *d; |
---|
1116 | |
---|
1117 | if (PyType_Ready(&CinvertorType) < 0) |
---|
1118 | return; |
---|
1119 | |
---|
1120 | Py_INCREF(&CinvertorType); |
---|
1121 | PyModule_AddObject(module, "Cinvertor", (PyObject *)&CinvertorType); |
---|
1122 | |
---|
1123 | d = PyModule_GetDict(module); |
---|
1124 | CinvertorError = PyErr_NewException("sans.pr.invertor.Cinvertor.InvertorError", PyExc_RuntimeError, NULL); |
---|
1125 | PyDict_SetItemString(d, "CinvertorError", CinvertorError); |
---|
1126 | } |
---|
1127 | |
---|
1128 | |
---|
1129 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
---|
1130 | #define PyMODINIT_FUNC void |
---|
1131 | #endif |
---|
1132 | PyMODINIT_FUNC |
---|
1133 | initpr_inversion(void) |
---|
1134 | { |
---|
1135 | PyObject* m; |
---|
1136 | |
---|
1137 | m = Py_InitModule3("pr_inversion", module_methods, |
---|
1138 | "C extension module for inversion to P(r)."); |
---|
1139 | |
---|
1140 | addCinvertor(m); |
---|
1141 | } |
---|