source: sasview/src/sas/sascalc/pr/distance_explorer.py @ bacc04b

Last change on this file since bacc04b was b699768, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 9 years ago

Initial commit of the refactored SasCalc? module.

  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[7d8c41f]1
[d84a90c]2################################################################################
3#This software was developed by the University of Tennessee as part of the
4#Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
[038c00cf]5#project funded by the US National Science Foundation.
[d84a90c]6#
7#See the license text in license.txt
8#
9#copyright 2009, University of Tennessee
10################################################################################
[7d8c41f]11
12"""
[d84a90c]13Module to explore the P(r) inversion results for a range
14of D_max value. User picks a number of points and a range of
15distances, then get a series of outputs as a function of D_max
16over that range.
17"""
[7d8c41f]18import sys
19
[34f3ad0]20
[038c00cf]21class Results(object):
[7d8c41f]22    """
[d84a90c]23    Class to hold the inversion output parameters
24    as a function of D_max
[7d8c41f]25    """
26    def __init__(self):
27        """
[d84a90c]28        Initialization. Create empty arrays
29        and dictionary of labels.
[7d8c41f]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 = []
[34f3ad0]40        ## List of errors found during the last exploration
[7d8c41f]41        self.errors = []
[038c00cf]42
43
[7d8c41f]44class DistExplorer(object):
45    """
[d84a90c]46    The explorer class
[7d8c41f]47    """
[038c00cf]48
[7d8c41f]49    def __init__(self, pr_state):
50        """
[d84a90c]51        Initialization.
[038c00cf]52
[b699768]53        :param pr_state: sas.sascalc.pr.invertor.Invertor object
[038c00cf]54
[7d8c41f]55        """
[34f3ad0]56        self.pr_state = pr_state
[1db4a53]57        self._default_min = 0.8 * self.pr_state.d_max
58        self._default_max = 1.2 * self.pr_state.d_max
[038c00cf]59
[7d8c41f]60    def __call__(self, dmin=None, dmax=None, npts=10):
61        """
[d84a90c]62        Compute the outputs as a function of D_max.
[038c00cf]63
[d84a90c]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
[038c00cf]67
[7d8c41f]68        """
69        # Take care of the defaults if needed
70        if dmin is None:
71            dmin = self._default_min
[038c00cf]72
[7d8c41f]73        if dmax is None:
74            dmax = self._default_max
[038c00cf]75
[7d8c41f]76        # Results object to store the computation outputs.
77        results = Results()
[038c00cf]78
[7d8c41f]79        # Loop over d_max values
[34f3ad0]80        for i in range(npts):
81            d = dmin + i * (dmax - dmin) / (npts - 1.0)
[7d8c41f]82            self.pr_state.d_max = d
83            try:
[34f3ad0]84                out, cov = self.pr_state.invert(self.pr_state.nfunc)
[038c00cf]85
[e83c75a]86                # Store results
[7d8c41f]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)
[038c00cf]92
[7d8c41f]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)
[34f3ad0]100                results.osc.append(osc)
[7d8c41f]101            except:
102                # This inversion failed, skip this D_max value
[1db4a53]103                msg = "ExploreDialog: inversion failed for "
104                msg += "D_max=%s\n %s" % (str(d), sys.exc_value)
105                results.errors.append(msg)
[038c00cf]106
[7d8c41f]107        return results
Note: See TracBrowser for help on using the repository browser.