1 | |
---|
2 | ################################################################################ |
---|
3 | #This software was developed by the University of Tennessee as part of the |
---|
4 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
5 | #project funded by the US National Science Foundation. |
---|
6 | # |
---|
7 | #See the license text in license.txt |
---|
8 | # |
---|
9 | #copyright 2009, University of Tennessee |
---|
10 | ################################################################################ |
---|
11 | |
---|
12 | """ |
---|
13 | Module to explore the P(r) inversion results for a range |
---|
14 | of D_max value. User picks a number of points and a range of |
---|
15 | distances, then get a series of outputs as a function of D_max |
---|
16 | over that range. |
---|
17 | """ |
---|
18 | import sys |
---|
19 | |
---|
20 | |
---|
21 | class Results(object): |
---|
22 | """ |
---|
23 | Class to hold the inversion output parameters |
---|
24 | as a function of D_max |
---|
25 | """ |
---|
26 | def __init__(self): |
---|
27 | """ |
---|
28 | Initialization. Create empty arrays |
---|
29 | and dictionary of labels. |
---|
30 | """ |
---|
31 | # Array of output for each inversion |
---|
32 | self.chi2 = [] |
---|
33 | self.osc = [] |
---|
34 | self.pos = [] |
---|
35 | self.pos_err = [] |
---|
36 | self.rg = [] |
---|
37 | self.iq0 = [] |
---|
38 | self.bck = [] |
---|
39 | self.d_max = [] |
---|
40 | ## List of errors found during the last exploration |
---|
41 | self.errors = [] |
---|
42 | |
---|
43 | |
---|
44 | class DistExplorer(object): |
---|
45 | """ |
---|
46 | The explorer class |
---|
47 | """ |
---|
48 | |
---|
49 | def __init__(self, pr_state): |
---|
50 | """ |
---|
51 | Initialization. |
---|
52 | |
---|
53 | :param pr_state: sas.sascalc.pr.invertor.Invertor object |
---|
54 | |
---|
55 | """ |
---|
56 | self.pr_state = pr_state |
---|
57 | self._default_min = 0.8 * self.pr_state.d_max |
---|
58 | self._default_max = 1.2 * self.pr_state.d_max |
---|
59 | |
---|
60 | def __call__(self, dmin=None, dmax=None, npts=10): |
---|
61 | """ |
---|
62 | Compute the outputs as a function of D_max. |
---|
63 | |
---|
64 | :param dmin: minimum value for D_max |
---|
65 | :param dmax: maximum value for D_max |
---|
66 | :param npts: number of points for D_max |
---|
67 | |
---|
68 | """ |
---|
69 | # Take care of the defaults if needed |
---|
70 | if dmin is None: |
---|
71 | dmin = self._default_min |
---|
72 | |
---|
73 | if dmax is None: |
---|
74 | dmax = self._default_max |
---|
75 | |
---|
76 | # Results object to store the computation outputs. |
---|
77 | results = Results() |
---|
78 | |
---|
79 | # Loop over d_max values |
---|
80 | for i in range(npts): |
---|
81 | d = dmin + i * (dmax - dmin) / (npts - 1.0) |
---|
82 | self.pr_state.d_max = d |
---|
83 | try: |
---|
84 | out, cov = self.pr_state.invert(self.pr_state.nfunc) |
---|
85 | |
---|
86 | # Store results |
---|
87 | iq0 = self.pr_state.iq0(out) |
---|
88 | rg = self.pr_state.rg(out) |
---|
89 | pos = self.pr_state.get_positive(out) |
---|
90 | pos_err = self.pr_state.get_pos_err(out, cov) |
---|
91 | osc = self.pr_state.oscillations(out) |
---|
92 | |
---|
93 | results.d_max.append(self.pr_state.d_max) |
---|
94 | results.bck.append(self.pr_state.background) |
---|
95 | results.chi2.append(self.pr_state.chi2) |
---|
96 | results.iq0.append(iq0) |
---|
97 | results.rg.append(rg) |
---|
98 | results.pos.append(pos) |
---|
99 | results.pos_err.append(pos_err) |
---|
100 | results.osc.append(osc) |
---|
101 | except Exception as exc: |
---|
102 | # This inversion failed, skip this D_max value |
---|
103 | msg = "ExploreDialog: inversion failed for " |
---|
104 | msg += "D_max=%s\n %s" % (str(d), exc) |
---|
105 | results.errors.append(msg) |
---|
106 | |
---|
107 | return results |
---|