Changeset 7af652d in sasview for src/sas/sascalc/pr
- Timestamp:
- Mar 5, 2019 4:26:14 PM (6 years ago)
- Branches:
- master, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1249
- Children:
- 1342f6a
- Parents:
- dbfd307 (diff), 8c9e65c (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - git-author:
- Paul Kienzle <pkienzle@…> (03/05/19 16:26:14)
- git-committer:
- GitHub <noreply@…> (03/05/19 16:26:14)
- Location:
- src/sas/sascalc/pr
- Files:
-
- 1 deleted
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/pr/c_extensions/Cinvertor.c
ra52f32f r7ba6470 5 5 * 6 6 */ 7 #include <Python.h>8 #include "structmember.h"9 7 #include <stdio.h> 10 8 #include <stdlib.h> … … 12 10 #include <time.h> 13 11 12 //#define Py_LIMITED_API 0x03050000 13 #include <Python.h> 14 #include <structmember.h> 15 16 // Vector binding glue 17 #if (PY_VERSION_HEX > 0x03000000) && !defined(Py_LIMITED_API) 18 // Assuming that a view into a writable vector points to a 19 // non-changing pointer for the duration of the C call, capture 20 // the view pointer and immediately free the view. 21 #define VECTOR(VEC_obj, VEC_buf, VEC_len) do { \ 22 Py_buffer VEC_view; \ 23 int VEC_err = PyObject_GetBuffer(VEC_obj, &VEC_view, PyBUF_WRITABLE|PyBUF_FORMAT); \ 24 if (VEC_err < 0 || sizeof(*VEC_buf) != VEC_view.itemsize) return NULL; \ 25 VEC_buf = (typeof(VEC_buf))VEC_view.buf; \ 26 VEC_len = VEC_view.len/sizeof(*VEC_buf); \ 27 PyBuffer_Release(&VEC_view); \ 28 } while (0) 29 #else 30 #define VECTOR(VEC_obj, VEC_buf, VEC_len) do { \ 31 int VEC_err = PyObject_AsWriteBuffer(VEC_obj, (void **)(&VEC_buf), &VEC_len); \ 32 if (VEC_err < 0) return NULL; \ 33 VEC_len /= sizeof(*VEC_buf); \ 34 } while (0) 35 #endif 36 14 37 #include "invertor.h" 15 16 38 17 39 /// Error object for raised exceptions 18 40 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 41 35 42 // Class definition … … 99 106 100 107 if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; 101 OUTVECTOR(data_obj,data,ndata);108 VECTOR(data_obj,data,ndata); 102 109 103 110 free(self->params.x); … … 131 138 132 139 if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; 133 OUTVECTOR(data_obj, data, ndata);140 VECTOR(data_obj, data, ndata); 134 141 135 142 // Check that the input array is large enough … … 164 171 165 172 if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; 166 OUTVECTOR(data_obj,data,ndata);173 VECTOR(data_obj,data,ndata); 167 174 168 175 free(self->params.y); … … 196 203 197 204 if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; 198 OUTVECTOR(data_obj, data, ndata);205 VECTOR(data_obj, data, ndata); 199 206 200 207 // Check that the input array is large enough … … 229 236 230 237 if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; 231 OUTVECTOR(data_obj,data,ndata);238 VECTOR(data_obj,data,ndata); 232 239 233 240 free(self->params.err); … … 261 268 262 269 if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; 263 OUTVECTOR(data_obj, data, ndata);270 VECTOR(data_obj, data, ndata); 264 271 265 272 // Check that the input array is large enough … … 517 524 if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; 518 525 519 OUTVECTOR(data_obj,pars,npars);526 VECTOR(data_obj,pars,npars); 520 527 521 528 // PyList of residuals … … 568 575 if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; 569 576 570 OUTVECTOR(data_obj,pars,npars);577 VECTOR(data_obj,pars,npars); 571 578 572 579 // Should create this list only once and refill it … … 609 616 610 617 if (!PyArg_ParseTuple(args, "Od", &data_obj, &q)) return NULL; 611 OUTVECTOR(data_obj,pars,npars);618 VECTOR(data_obj,pars,npars); 612 619 613 620 iq_value = iq(pars, self->params.d_max, (int)npars, q); … … 634 641 635 642 if (!PyArg_ParseTuple(args, "Od", &data_obj, &q)) return NULL; 636 OUTVECTOR(data_obj,pars,npars);643 VECTOR(data_obj,pars,npars); 637 644 638 645 iq_value = iq_smeared(pars, self->params.d_max, (int)npars, … … 659 666 660 667 if (!PyArg_ParseTuple(args, "Od", &data_obj, &r)) return NULL; 661 OUTVECTOR(data_obj,pars,npars);668 VECTOR(data_obj,pars,npars); 662 669 663 670 pr_value = pr(pars, self->params.d_max, (int)npars, r); … … 686 693 687 694 if (!PyArg_ParseTuple(args, "OOd", &data_obj, &err_obj, &r)) return NULL; 688 OUTVECTOR(data_obj,pars,npars);695 VECTOR(data_obj,pars,npars); 689 696 690 697 if (err_obj == Py_None) { … … 692 699 pr_err_value = 0.0; 693 700 } else { 694 OUTVECTOR(err_obj,pars_err,npars2);701 VECTOR(err_obj,pars_err,npars2); 695 702 pr_err(pars, pars_err, self->params.d_max, (int)npars, r, &pr_value, &pr_err_value); 696 703 } … … 726 733 727 734 if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; 728 OUTVECTOR(data_obj,pars,npars);735 VECTOR(data_obj,pars,npars); 729 736 730 737 oscill = reg_term(pars, self->params.d_max, (int)npars, 100); … … 747 754 748 755 if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; 749 OUTVECTOR(data_obj,pars,npars);756 VECTOR(data_obj,pars,npars); 750 757 751 758 count = npeaks(pars, self->params.d_max, (int)npars, 100); … … 768 775 769 776 if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; 770 OUTVECTOR(data_obj,pars,npars);777 VECTOR(data_obj,pars,npars); 771 778 772 779 fraction = positive_integral(pars, self->params.d_max, (int)npars, 100); … … 792 799 793 800 if (!PyArg_ParseTuple(args, "OO", &data_obj, &err_obj)) return NULL; 794 OUTVECTOR(data_obj,pars,npars);795 OUTVECTOR(err_obj,pars_err,npars2);801 VECTOR(data_obj,pars,npars); 802 VECTOR(err_obj,pars_err,npars2); 796 803 797 804 fraction = positive_errors(pars, pars_err, self->params.d_max, (int)npars, 51); … … 813 820 814 821 if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; 815 OUTVECTOR(data_obj,pars,npars);822 VECTOR(data_obj,pars,npars); 816 823 817 824 value = rg(pars, self->params.d_max, (int)npars, 101); … … 833 840 834 841 if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; 835 OUTVECTOR(data_obj,pars,npars);842 VECTOR(data_obj,pars,npars); 836 843 837 844 value = 4.0*acos(-1.0)*int_pr(pars, self->params.d_max, (int)npars, 101); … … 874 881 875 882 if (!PyArg_ParseTuple(args, "iiOO", &nfunc, &nr, &a_obj, &b_obj)) return NULL; 876 OUTVECTOR(a_obj,a,n_a);877 OUTVECTOR(b_obj,b,n_b);883 VECTOR(a_obj,a,n_a); 884 VECTOR(b_obj,b,n_b); 878 885 879 886 assert(n_b>=nfunc); … … 947 954 948 955 if (!PyArg_ParseTuple(args, "iiOO", &nfunc, &nr, &a_obj, &cov_obj)) return NULL; 949 OUTVECTOR(a_obj,a,n_a);950 OUTVECTOR(cov_obj,inv_cov,n_cov);956 VECTOR(a_obj,a,n_a); 957 VECTOR(cov_obj,inv_cov,n_cov); 951 958 952 959 assert(n_cov>=nfunc*nfunc); … … 981 988 982 989 if (!PyArg_ParseTuple(args, "iiO", &nfunc, &nr, &a_obj)) return NULL; 983 OUTVECTOR(a_obj,a,n_a);990 VECTOR(a_obj,a,n_a); 984 991 985 992 assert(n_a>=nfunc*(nr+self->params.npoints)); … … 1121 1128 1122 1129 #define MODULE_DOC "C extension module for inversion to P(r)." 1123 #define MODULE_NAME " pr_inversion"1124 #define MODULE_INIT2 init pr_inversion1125 #define MODULE_INIT3 PyInit_ pr_inversion1130 #define MODULE_NAME "_pr_inversion" 1131 #define MODULE_INIT2 init_pr_inversion 1132 #define MODULE_INIT3 PyInit__pr_inversion 1126 1133 #define MODULE_METHODS module_methods 1127 1134 -
src/sas/sascalc/pr/fit/BumpsFitting.py
r9a5097c r3e6829d 2 2 BumpsFitting module runs the bumps optimizer. 3 3 """ 4 from __future__ import division 5 4 6 import os 5 7 from datetime import timedelta, datetime … … 34 36 class Progress(object): 35 37 def __init__(self, history, max_step, pars, dof): 36 remaining_time = int(history.time[0]*( float(max_step)/history.step[0]-1))38 remaining_time = int(history.time[0]*(max_step/history.step[0]-1)) 37 39 # Depending on the time remaining, either display the expected 38 40 # time of completion, or the amount of time remaining. Use precision -
src/sas/sascalc/pr/fit/Loader.py
r574adc7 r57e48ca 1 """ 2 class Loader to load any kind of file 3 """ 4 1 5 from __future__ import print_function 2 6 3 # class Loader to load any king of file4 #import wx5 #import string6 7 import numpy as np 7 8 -
src/sas/sascalc/pr/invertor.py
rdbfd307 r7af652d 6 6 FIXME: The way the Invertor interacts with its C component should be cleaned up 7 7 """ 8 from __future__ import division 8 9 9 10 import numpy as np … … 17 18 from numpy.linalg import lstsq 18 19 from scipy import optimize 19 from sas.sascalc.pr. core.pr_inversion import Cinvertor20 from sas.sascalc.pr._pr_inversion import Cinvertor 20 21 21 22 logger = logging.getLogger(__name__) … … 71 72 A[j][i] = (Fourier transformed base function for point j) 72 73 73 We the mchoose a number of r-points, n_r, to evaluate the second74 We then choose a number of r-points, n_r, to evaluate the second 74 75 derivative of P(r) at. This is used as our regularization term. 75 76 For a vector r of length n_r, the following n_r rows are set to :: … … 144 145 x, y, err, d_max, q_min, q_max and alpha 145 146 """ 146 if 147 if name == 'x': 147 148 if 0.0 in value: 148 149 msg = "Invertor: one of your q-values is zero. " … … 268 269 A[i][j] = (Fourier transformed base function for point j) 269 270 270 We the mchoose a number of r-points, n_r, to evaluate the second271 We then choose a number of r-points, n_r, to evaluate the second 271 272 derivative of P(r) at. This is used as our regularization term. 272 273 For a vector r of length n_r, the following n_r rows are set to :: … … 416 417 A[i][j] = (Fourier transformed base function for point j) 417 418 418 We the mchoose a number of r-points, n_r, to evaluate the second419 We then choose a number of r-points, n_r, to evaluate the second 419 420 derivative of P(r) at. This is used as our regularization term. 420 421 For a vector r of length n_r, the following n_r rows are set to :: … … 498 499 try: 499 500 cov = np.linalg.pinv(inv_cov) 500 err = math.fabs(chi2 / float(npts - nfunc)) * cov501 except :501 err = math.fabs(chi2 / (npts - nfunc)) * cov 502 except Exception as exc: 502 503 # We were not able to estimate the errors 503 504 # Return an empty error matrix 504 logger.error( sys.exc_value)505 logger.error(exc) 505 506 506 507 # Keep a copy of the last output … … 539 540 540 541 """ 541 from num_term import NTermEstimator542 from .num_term import NTermEstimator 542 543 estimator = NTermEstimator(self.clone()) 543 544 try: 544 545 return estimator.num_terms(isquit_func) 545 except :546 except Exception as exc: 546 547 # If we fail, estimate alpha and return the default 547 548 # number of terms 548 549 best_alpha, _, _ = self.estimate_alpha(self.nfunc) 549 logger.warning("Invertor.estimate_numterms: %s" % sys.exc_value)550 logger.warning("Invertor.estimate_numterms: %s" % exc) 550 551 return self.nfunc, best_alpha, "Could not estimate number of terms" 551 552 … … 633 634 return best_alpha, message, elapsed 634 635 635 except :636 message = "Invertor.estimate_alpha: %s" % sys.exc_value636 except Exception as exc: 637 message = "Invertor.estimate_alpha: %s" % exc 637 638 return 0, message, elapsed 638 639 … … 750 751 self.cov[i][i] = float(toks2[1]) 751 752 752 except :753 msg = "Invertor.from_file: corrupted file\n%s" % sys.exc_value753 except Exception as exc: 754 msg = "Invertor.from_file: corrupted file\n%s" % exc 754 755 raise RuntimeError(msg) 755 756 else: -
src/sas/sascalc/pr/num_term.py
re090ba90 r8c9e65c 1 from __future__ import print_function 1 from __future__ import print_function, division 2 2 3 3 import math … … 51 51 osc = self.sort_osc() 52 52 dv = len(osc) 53 med = float(dv) / 2.053 med = 0.5*dv 54 54 odd = self.is_odd(dv) 55 55 medi = 0 … … 140 140 nts = self.compare_err() 141 141 div = len(nts) 142 tem = float(div) / 2.0142 tem = 0.5*div 143 143 if self.is_odd(div): 144 144 nt = nts[int(tem)]
Note: See TracChangeset
for help on using the changeset viewer.