Ignore:
Timestamp:
May 3, 2012 4:54:44 PM (11 years ago)
Author:
srkline
Message:

Added a menu option in the SANS Models ->1D to map chi-squared as a function of a chosen parameter. Instructive to see how if varies for any parameter, or to see if you're really at a minimum. Right now shows only a single parameter, could be made fancier in the future if anyone cares.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sans/Dev/trunk/NCNR_User_Procedures/Analysis/Packages/Wrapper_v40.ipf

    r855 r856  
    14591459End 
    14601460 
     1461 
     1462 
     1463////////////////////////////// 
     1464// 
    14611465// displays the covariance matrix for the current data set in the popup 
    14621466// AND whatever was the last fit for that data set. it may not necessarily 
     
    15511555        return 0 
    15521556End 
     1557 
     1558 
     1559////////////////////////////////// 
     1560// this is a snippet from Andy Nelson, posted at the Igor Exchange web site. 
     1561// 
     1562// search area appears to be a percent (so enter 10 to get +/- 10% variation in the parameter) 
     1563// 
     1564// TODO: 
     1565//              x- rename the function for just me 
     1566//              x- make the edata a mandatory parameter 
     1567//              x- remove rhs, lhs? or keep these if cursors were used to properly trim the data set 
     1568//              x- label the graph 
     1569//              x- make a panel to control this - to either pick a single parameter, or show all of them 
     1570//              x- have it re-use the same graph, not draw a (new) duplicate one 
     1571//              x- update it to use the AAO as the input function (new func template -- see Gauss Utils) 
     1572//              x- the wrapper must be data folder aware, and data set aware like in the Wrapper panel 
     1573//              x- need a different wrapper for smeared and unsmeared functions 
     1574// 
     1575// 
     1576 
     1577 
     1578 
     1579Proc MapChiSquared(paramNum,percent) 
     1580        Variable paramNum=0,percent=10 
     1581        Prompt paramNum, "Enter parameter number: " 
     1582        Prompt percent, "Enter percent variation +/- : " 
     1583 
     1584        fChiMap(paramNum,percent) 
     1585End 
     1586 
     1587 
     1588// this does the math for a single value of "whichParam" 
     1589Function chi2gen(funcStr,folderStr,xw,yw,sw,cw,whichParam,searchArea,lhs,rhs,useResol) 
     1590        String funcStr,folderStr 
     1591        Wave xw,yw,sw,cw      //        x data, y data, error wave, and coefficient wave 
     1592        variable whichParam, searchArea  //which of the parameters to you want to vary, how far from the original value do you want to search (%) 
     1593        variable lhs, rhs    //specify a region of interest in the data using a left hand side and right hand side. 
     1594        variable useResol               // =1 if smeared used 
     1595  
     1596        variable originalvalue = cw[whichparam] 
     1597        variable range = originalValue * searchArea/100 
     1598        variable ii,err 
     1599  
     1600        duplicate/o yw, :theoretical_data, :chi2_data 
     1601        Wave theoretical_data, chi2_data 
     1602  
     1603        make/o/n=200/d chi2_map 
     1604        setscale/I x,  originalvalue - range/2, originalvalue + range/2, chi2_map 
     1605  
     1606        String DF="root:"+folderStr+":"  
     1607        String suffix=getModelSuffix(funcStr) 
     1608 
     1609        // fill a struct instance whether it is needed or not 
     1610        Struct ResSmearAAOStruct fs 
     1611        WAVE/Z resW = $(DF+folderStr+"_res")                    //these may not exist, if 3-column data is used  
     1612        WAVE/Z fs.resW =  resW 
     1613//              WAVE yw=$(DF+folderStr+"_i") 
     1614//              WAVE xw=$(DF+folderStr+"_q") 
     1615//              WAVE sw=$(DF+folderStr+"_s") 
     1616        Wave fs.coefW = cw 
     1617        Wave fs.yW = theoretical_data 
     1618        Wave fs.xW = xw  
     1619  
     1620  
     1621  
     1622        for(ii=0 ; ii < numpnts(chi2_map) ; ii+=1) 
     1623                cw[whichparam] = pnt2x(chi2_map, ii) 
     1624  
     1625                if(useResol) 
     1626                        FUNCREF SANSModelSTRUCT_proto func1=$funcStr 
     1627                        err = func1(fs) 
     1628                else 
     1629                        FUNCREF SANSModelAAO_proto func2=$funcStr 
     1630                        func2(cw,theoretical_data,xw) 
     1631                endif 
     1632                 
     1633                chi2_data = (yw-theoretical_data)^2 
     1634  
     1635                chi2_data /= sw^2 
     1636 
     1637                Wavestats/q/R=[lhs, rhs] chi2_data 
     1638  
     1639                chi2_map[ii] = V_avg * V_npnts 
     1640        endfor 
     1641  
     1642        cw[whichparam] = originalvalue 
     1643  
     1644        DoWindow/F Chi2 
     1645        if(V_flag==0) 
     1646                display/K=1/N=Chi2 chi2_map 
     1647                Label left "Chi^2" 
     1648        endif 
     1649 
     1650        String parStr=GetWavesDataFolder(cw,1)+ WaveList("*param*"+"_"+suffix, "", "TEXT:1," )          // this is *hopefully* one wave 
     1651        Wave/T parW = $parStr 
     1652        Label bottom parW[whichParam] 
     1653         
     1654         
     1655        killwaves/z theoretical_data, chi2_data 
     1656End 
     1657  
     1658 
     1659// this does the setup 
     1660Function fChiMap(paramNum,percent) 
     1661        Variable paramNum,percent 
     1662 
     1663        String folderStr,funcStr,coefStr 
     1664        Variable useCursors,useResol=0,pt1,pt2 
     1665         
     1666        ControlInfo/W=WrapperPanel popup_0 
     1667        folderStr=S_Value 
     1668         
     1669        ControlInfo/W=WrapperPanel popup_1 
     1670        funcStr=S_Value 
     1671         
     1672        ControlInfo/W=WrapperPanel popup_2 
     1673        coefStr=S_Value 
     1674         
     1675        ControlInfo/W=WrapperPanel check_0 
     1676        useCursors=V_Value 
     1677         
     1678         
     1679// first, figure out where we are... 
     1680        String suffix=getModelSuffix(funcStr) 
     1681         
     1682        SetDataFolder $("root:"+folderStr) 
     1683        if(!exists(coefStr)) 
     1684                // must be unsmeared model, work in the root folder 
     1685                SetDataFolder root:                              
     1686                if(!exists(coefStr))            //this should be fine if the coef filter is working, but check anyhow 
     1687                        DoAlert 0,"the coefficient and data sets do not match" 
     1688                        return 0 
     1689                endif 
     1690        endif 
     1691                 
     1692        WAVE cw=$(coefStr) 
     1693 
     1694 
     1695// test for smeared function 
     1696        if(stringmatch(funcStr, "Smear*"))              // if it's a smeared function, need a struct 
     1697                useResol=1 
     1698        endif 
     1699         
     1700        // fill a struct instance whether I need one or not 
     1701        String DF="root:"+folderStr+":"  
     1702         
     1703//      Struct ResSmearAAOStruct fs 
     1704//      WAVE/Z resW = $(DF+folderStr+"_res")                    //these may not exist, if 3-column data is used  
     1705//      WAVE/Z fs.resW =  resW 
     1706        WAVE yw=$(DF+folderStr+"_i") 
     1707        WAVE xw=$(DF+folderStr+"_q") 
     1708        WAVE sw=$(DF+folderStr+"_s") 
     1709//      Wave fs.coefW = cw 
     1710//      Wave fs.yW = yw 
     1711//      Wave fs.xW = xw  
     1712         
     1713        if(useCursors) 
     1714                if(pcsr(A) > pcsr(B)) 
     1715                        pt1 = pcsr(B) 
     1716                        pt2 = pcsr(A) 
     1717                else 
     1718                        pt1 = pcsr(A) 
     1719                        pt2 = pcsr(B) 
     1720                endif 
     1721        else 
     1722                //if cursors are not being used, find the first and last points of the data set, and pass them 
     1723                pt1 = 0 
     1724                pt2 = numpnts(yw)-1 
     1725        endif 
     1726         
     1727                 
     1728         
     1729        chi2gen(funcStr,folderStr,xw,yw,sw,cw,paramNum,percent,pt1,pt2,useResol) 
     1730         
     1731 
     1732End 
     1733 
     1734//////////////////////////////////// 
Note: See TracChangeset for help on using the changeset viewer.