Changeset d84a90c in sasview for pr_inversion/invertor.py


Ignore:
Timestamp:
Jun 3, 2010 4:47:46 PM (14 years ago)
Author:
Gervaise Alina <gervyh@…>
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:
7116b6e0
Parents:
aa36f96
Message:

working on documentation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pr_inversion/invertor.py

    r97d69d9 rd84a90c  
    11""" 
    2     Module to perform P(r) inversion. 
    3     The module contains the Invertor class. 
     2Module to perform P(r) inversion. 
     3The module contains the Invertor class. 
    44""" 
    55from sans.pr.core.pr_inversion import Cinvertor 
     
    1111def help(): 
    1212    """ 
    13         Provide general online help text 
    14         Future work: extend this function to allow topic selection 
     13    Provide general online help text 
     14    Future work: extend this function to allow topic selection 
    1515    """ 
    1616    info_txt  = "The inversion approach is based on Moore, J. Appl. Cryst. (1980) 13, 168-175.\n\n" 
     
    3535class Invertor(Cinvertor): 
    3636    """ 
    37         Invertor class to perform P(r) inversion 
    38          
    39         The problem is solved by posing the problem as  Ax = b, 
    40         where x is the set of coefficients we are looking for. 
    41          
    42         Npts is the number of points. 
    43          
    44         In the following i refers to the ith base function coefficient. 
    45         The matrix has its entries j in its first Npts rows set to 
    46             A[j][i] = (Fourier transformed base function for point j)  
    47              
    48         We them choose a number of r-points, n_r, to evaluate the second 
    49         derivative of P(r) at. This is used as our regularization term. 
    50         For a vector r of length n_r, the following n_r rows are set to 
    51             A[j+Npts][i] = (2nd derivative of P(r), d**2(P(r))/d(r)**2, evaluated at r[j]) 
    52              
    53         The vector b has its first Npts entries set to 
    54             b[j] = (I(q) observed for point j) 
    55              
    56         The following n_r entries are set to zero. 
    57          
    58         The result is found by using scipy.linalg.basic.lstsq to invert 
    59         the matrix and find the coefficients x. 
    60          
    61         Methods inherited from Cinvertor: 
    62         - get_peaks(pars): returns the number of P(r) peaks 
    63         - oscillations(pars): returns the oscillation parameters for the output P(r) 
    64         - get_positive(pars): returns the fraction of P(r) that is above zero 
    65         - get_pos_err(pars): returns the fraction of P(r) that is 1-sigma above zero 
     37    Invertor class to perform P(r) inversion 
     38     
     39    The problem is solved by posing the problem as  Ax = b, 
     40    where x is the set of coefficients we are looking for. 
     41     
     42    Npts is the number of points. 
     43     
     44    In the following i refers to the ith base function coefficient. 
     45    The matrix has its entries j in its first Npts rows set to 
     46        A[j][i] = (Fourier transformed base function for point j)  
     47         
     48    We them choose a number of r-points, n_r, to evaluate the second 
     49    derivative of P(r) at. This is used as our regularization term. 
     50    For a vector r of length n_r, the following n_r rows are set to 
     51        A[j+Npts][i] = (2nd derivative of P(r), d**2(P(r))/d(r)**2, evaluated at r[j]) 
     52         
     53    The vector b has its first Npts entries set to 
     54        b[j] = (I(q) observed for point j) 
     55         
     56    The following n_r entries are set to zero. 
     57     
     58    The result is found by using scipy.linalg.basic.lstsq to invert 
     59    the matrix and find the coefficients x. 
     60     
     61    Methods inherited from Cinvertor: 
     62    - get_peaks(pars): returns the number of P(r) peaks 
     63    - oscillations(pars): returns the oscillation parameters for the output P(r) 
     64    - get_positive(pars): returns the fraction of P(r) that is above zero 
     65    - get_pos_err(pars): returns the fraction of P(r) that is 1-sigma above zero 
    6666    """ 
    6767    ## Chisqr of the last computation 
     
    8888    def __setattr__(self, name, value): 
    8989        """ 
    90             Set the value of an attribute. 
    91             Access the parent class methods for 
    92             x, y, err, d_max, q_min, q_max and alpha 
     90        Set the value of an attribute. 
     91        Access the parent class methods for 
     92        x, y, err, d_max, q_min, q_max and alpha 
    9393        """ 
    9494        if   name=='x': 
     
    129129    def __getattr__(self, name): 
    130130        """ 
    131            Return the value of an attribute 
     131        Return the value of an attribute 
    132132        """ 
    133133        import numpy 
     
    174174    def clone(self): 
    175175        """ 
    176             Return a clone of this instance 
     176        Return a clone of this instance 
    177177        """ 
    178178        import copy 
     
    200200    def invert(self, nfunc=10, nr=20): 
    201201        """ 
    202             Perform inversion to P(r) 
    203              
    204             The problem is solved by posing the problem as  Ax = b, 
    205             where x is the set of coefficients we are looking for. 
    206              
    207             Npts is the number of points. 
    208              
    209             In the following i refers to the ith base function coefficient. 
    210             The matrix has its entries j in its first Npts rows set to 
    211                 A[i][j] = (Fourier transformed base function for point j)  
    212                  
    213             We them choose a number of r-points, n_r, to evaluate the second 
    214             derivative of P(r) at. This is used as our regularization term. 
    215             For a vector r of length n_r, the following n_r rows are set to 
    216                 A[i+Npts][j] = (2nd derivative of P(r), d**2(P(r))/d(r)**2, evaluated at r[j]) 
    217                  
    218             The vector b has its first Npts entries set to 
    219                 b[j] = (I(q) observed for point j) 
    220                  
    221             The following n_r entries are set to zero. 
    222              
    223             The result is found by using scipy.linalg.basic.lstsq to invert 
    224             the matrix and find the coefficients x. 
    225              
    226             @param nfunc: number of base functions to use. 
    227             @param nr: number of r points to evaluate the 2nd derivative at for the reg. term. 
    228             @return: c_out, c_cov - the coefficients with covariance matrix  
     202        Perform inversion to P(r) 
     203         
     204        The problem is solved by posing the problem as  Ax = b, 
     205        where x is the set of coefficients we are looking for. 
     206         
     207        Npts is the number of points. 
     208         
     209        In the following i refers to the ith base function coefficient. 
     210        The matrix has its entries j in its first Npts rows set to 
     211            A[i][j] = (Fourier transformed base function for point j)  
     212             
     213        We them choose a number of r-points, n_r, to evaluate the second 
     214        derivative of P(r) at. This is used as our regularization term. 
     215        For a vector r of length n_r, the following n_r rows are set to 
     216            A[i+Npts][j] = (2nd derivative of P(r), d**2(P(r))/d(r)**2, evaluated at r[j]) 
     217             
     218        The vector b has its first Npts entries set to 
     219            b[j] = (I(q) observed for point j) 
     220             
     221        The following n_r entries are set to zero. 
     222         
     223        The result is found by using scipy.linalg.basic.lstsq to invert 
     224        the matrix and find the coefficients x. 
     225         
     226        :param nfunc: number of base functions to use. 
     227        :param nr: number of r points to evaluate the 2nd derivative at for the reg. term. 
     228        :return: c_out, c_cov - the coefficients with covariance matrix  
     229         
    229230        """ 
    230231        # Reset the background value before proceeding 
     
    234235    def iq(self, out, q): 
    235236        """ 
    236             Function to call to evaluate the scattering intensity 
    237             @param args: c-parameters, and q 
    238             @return: I(q) 
     237        Function to call to evaluate the scattering intensity 
     238         
     239        :param args: c-parameters, and q 
     240        :return: I(q) 
     241         
    239242        """ 
    240243        return Cinvertor.iq(self, out, q)+self.background 
     
    242245    def invert_optimize(self, nfunc=10, nr=20): 
    243246        """ 
    244             Slower version of the P(r) inversion that uses scipy.optimize.leastsq. 
    245              
    246             This probably produce more reliable results, but is much slower. 
    247             The minimization function is set to sum_i[ (I_obs(q_i) - I_theo(q_i))/err**2 ] + alpha * reg_term, 
    248             where the reg_term is given by Svergun: it is the integral of the square of the first derivative 
    249             of P(r), d(P(r))/dr, integrated over the full range of r. 
    250              
    251             @param nfunc: number of base functions to use. 
    252             @param nr: number of r points to evaluate the 2nd derivative at for the reg. term. 
    253             @return: c_out, c_cov - the coefficients with covariance matrix  
     247        Slower version of the P(r) inversion that uses scipy.optimize.leastsq. 
     248         
     249        This probably produce more reliable results, but is much slower. 
     250        The minimization function is set to sum_i[ (I_obs(q_i) - I_theo(q_i))/err**2 ] + alpha * reg_term, 
     251        where the reg_term is given by Svergun: it is the integral of the square of the first derivative 
     252        of P(r), d(P(r))/dr, integrated over the full range of r. 
     253         
     254        :param nfunc: number of base functions to use. 
     255        :param nr: number of r points to evaluate the 2nd derivative at for the reg. term. 
     256         
     257        :return: c_out, c_cov - the coefficients with covariance matrix  
     258         
    254259        """ 
    255260         
     
    281286    def pr_fit(self, nfunc=5): 
    282287        """ 
    283             This is a direct fit to a given P(r). It assumes that the y data 
    284             is set to some P(r) distribution that we are trying to reproduce 
    285             with a set of base functions. 
    286              
    287             This method is provided as a test.  
     288        This is a direct fit to a given P(r). It assumes that the y data 
     289        is set to some P(r) distribution that we are trying to reproduce 
     290        with a set of base functions. 
     291         
     292        This method is provided as a test.  
    288293        """ 
    289294        from scipy import optimize 
     
    312317    def pr_err(self, c, c_cov, r): 
    313318        """     
    314             Returns the value of P(r) for a given r, and base function 
    315             coefficients, with error. 
    316              
    317             @param c: base function coefficients 
    318             @param c_cov: covariance matrice of the base function coefficients 
    319             @param r: r-value to evaluate P(r) at 
    320             @return: P(r) 
     319        Returns the value of P(r) for a given r, and base function 
     320        coefficients, with error. 
     321         
     322        :param c: base function coefficients 
     323        :param c_cov: covariance matrice of the base function coefficients 
     324        :param r: r-value to evaluate P(r) at 
     325         
     326        :return: P(r) 
     327         
    321328        """ 
    322329        return self.get_pr_err(c, c_cov, r) 
     
    324331    def _accept_q(self, q): 
    325332        """ 
    326             Check q-value against user-defined range 
     333        Check q-value against user-defined range 
    327334        """ 
    328335        if not self.q_min==None and q<self.q_min: 
     
    334341    def lstsq(self, nfunc=5, nr=20): 
    335342        """ 
    336             The problem is solved by posing the problem as  Ax = b, 
    337             where x is the set of coefficients we are looking for. 
    338              
    339             Npts is the number of points. 
    340              
    341             In the following i refers to the ith base function coefficient. 
    342             The matrix has its entries j in its first Npts rows set to 
    343                 A[i][j] = (Fourier transformed base function for point j)  
    344                  
    345             We them choose a number of r-points, n_r, to evaluate the second 
    346             derivative of P(r) at. This is used as our regularization term. 
    347             For a vector r of length n_r, the following n_r rows are set to 
    348                 A[i+Npts][j] = (2nd derivative of P(r), d**2(P(r))/d(r)**2, evaluated at r[j]) 
    349                  
    350             The vector b has its first Npts entries set to 
    351                 b[j] = (I(q) observed for point j) 
    352                  
    353             The following n_r entries are set to zero. 
    354              
    355             The result is found by using scipy.linalg.basic.lstsq to invert 
    356             the matrix and find the coefficients x. 
    357              
    358             @param nfunc: number of base functions to use. 
    359             @param nr: number of r points to evaluate the 2nd derivative at for the reg. term. 
    360  
    361             If the result does not allow us to compute the covariance matrix, 
    362             a matrix filled with zeros will be returned. 
     343        The problem is solved by posing the problem as  Ax = b, 
     344        where x is the set of coefficients we are looking for. 
     345         
     346        Npts is the number of points. 
     347         
     348        In the following i refers to the ith base function coefficient. 
     349        The matrix has its entries j in its first Npts rows set to 
     350            A[i][j] = (Fourier transformed base function for point j)  
     351             
     352        We them choose a number of r-points, n_r, to evaluate the second 
     353        derivative of P(r) at. This is used as our regularization term. 
     354        For a vector r of length n_r, the following n_r rows are set to 
     355            A[i+Npts][j] = (2nd derivative of P(r), d**2(P(r))/d(r)**2, evaluated at r[j]) 
     356             
     357        The vector b has its first Npts entries set to 
     358            b[j] = (I(q) observed for point j) 
     359             
     360        The following n_r entries are set to zero. 
     361         
     362        The result is found by using scipy.linalg.basic.lstsq to invert 
     363        the matrix and find the coefficients x. 
     364         
     365        :param nfunc: number of base functions to use. 
     366        :param nr: number of r points to evaluate the 2nd derivative at for the reg. term. 
     367 
     368        If the result does not allow us to compute the covariance matrix, 
     369        a matrix filled with zeros will be returned. 
    363370 
    364371        """ 
     
    445452    def estimate_numterms(self, isquit_func=None): 
    446453        """ 
    447             Returns a reasonable guess for the 
    448             number of terms 
    449             @param isquit_func: reference to thread function to call to  
    450                                 check whether the computation needs to 
    451                                 be stopped. 
    452              
    453             @return: number of terms, alpha, message 
     454        Returns a reasonable guess for the 
     455        number of terms 
     456         
     457        :param isquit_func: reference to thread function to call to  
     458                            check whether the computation needs to 
     459                            be stopped. 
     460         
     461        :return: number of terms, alpha, message 
     462         
    454463        """ 
    455464        from num_term import Num_terms 
     
    465474    def estimate_alpha(self, nfunc): 
    466475        """ 
    467             Returns a reasonable guess for the 
    468             regularization constant alpha 
    469              
    470             @param nfunc: number of terms to use in the expansion. 
    471             @return: alpha, message, elapsed 
    472              
    473             where alpha is the estimate for alpha, 
    474             message is a message for the user, 
    475             elapsed is the computation time 
     476        Returns a reasonable guess for the 
     477        regularization constant alpha 
     478         
     479        :param nfunc: number of terms to use in the expansion. 
     480         
     481        :return: alpha, message, elapsed 
     482         
     483        where alpha is the estimate for alpha, 
     484        message is a message for the user, 
     485        elapsed is the computation time 
    476486        """ 
    477487        import time 
     
    549559    def to_file(self, path, npts=100): 
    550560        """ 
    551             Save the state to a file that will be readable 
    552             by SliceView. 
    553             @param path: path of the file to write 
    554             @param npts: number of P(r) points to be written 
     561        Save the state to a file that will be readable 
     562        by SliceView. 
     563         
     564        :param path: path of the file to write 
     565        :param npts: number of P(r) points to be written 
     566         
    555567        """ 
    556568        file = open(path, 'w') 
     
    586598    def from_file(self, path): 
    587599        """ 
    588             Load the state of the Invertor from a file, 
    589             to be able to generate P(r) from a set of 
    590             parameters. 
    591             @param path: path of the file to load 
     600        Load the state of the Invertor from a file, 
     601        to be able to generate P(r) from a set of 
     602        parameters. 
     603         
     604        :param path: path of the file to load 
     605         
    592606        """ 
    593607        import os 
Note: See TracChangeset for help on using the changeset viewer.