Changeset ff7119b in sasmodels for sasmodels/bumps_model.py


Ignore:
Timestamp:
Aug 26, 2014 8:27:06 PM (10 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
5d4777d
Parents:
a7684e5
Message:

docu update

File:
1 moved

Legend:

Unmodified
Added
Removed
  • sasmodels/bumps_model.py

    r13d86bc rff7119b  
    1 #!/usr/bin/env python 
    2 # -*- coding: utf-8 -*- 
    3  
     1""" 
     2Sasmodels core. 
     3""" 
    44import sys, os 
    55import datetime 
    6 import warnings 
    76 
    87import numpy as np 
    98 
    109 
    11 def opencl_model(kernel_module, dtype="single"): 
    12     from sasmodels import gen, gpu 
    13  
    14     source, info = gen.make(kernel_module) 
    15     ## for debugging, save source to a .cl file, edit it, and reload as model 
    16     #open(modelname+'.cl','w').write(source) 
    17     #source = open(modelname+'.cl','r').read() 
    18     return gpu.GpuModel(source, info, dtype) 
    19  
    20  
    21 if sys.platform == 'darwin': 
    22     COMPILE = "gcc-mp-4.7 -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm -lgomp" 
    23 elif os.name == 'nt': 
    24     COMPILE = "gcc -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm" 
    25 else: 
    26     COMPILE = "cc -shared -fPIC -std=c99 -fopenmp -O2 -Wall %s -o %s -lm" 
    27 DLL_PATH = "/tmp" 
    28  
    29  
    30 def dll_path(info): 
    31     from os.path import join as joinpath, split as splitpath, splitext 
    32     basename = splitext(splitpath(info['filename'])[1])[0] 
    33     return joinpath(DLL_PATH, basename+'.so') 
    34  
    35  
    36 def dll_model(kernel_module): 
    37     import os 
    38     import tempfile 
    39     from sasmodels import gen, dll 
    40  
    41     source, info = gen.make(kernel_module) 
    42     dllpath = dll_path(info) 
    43     if not os.path.exists(dllpath) \ 
    44             or (os.path.getmtime(dllpath) < os.path.getmtime(info['filename'])): 
    45         # Replace with a proper temp file 
    46         srcfile = tempfile.mkstemp(suffix=".c",prefix="sas_"+info['name']) 
    47         open(srcfile, 'w').write(source) 
    48         os.system(COMPILE%(srcfile, dllpath)) 
    49         ## comment the following to keep the generated c file 
    50         #os.unlink(srcfile) 
    51     return dll.DllModel(dllpath, info) 
    52  
    53  
    54 TIC = None 
    5510def tic(): 
    56     global TIC 
     11    """ 
     12    Timer function. 
     13 
     14    Use "toc=tic()" to start the clock and "toc()" to measure 
     15    a time interval. 
     16    """ 
    5717    then = datetime.datetime.now() 
    58     TIC = lambda: (datetime.datetime.now()-then).total_seconds() 
    59     return TIC 
    60  
    61  
    62 def toc(): 
    63     return TIC() 
     18    return lambda: (datetime.datetime.now()-then).total_seconds() 
    6419 
    6520 
    6621def load_data(filename): 
     22    """ 
     23    Load data using a sasview loader. 
     24    """ 
    6725    from sans.dataloader.loader import Loader 
    6826    loader = Loader() 
     
    7331 
    7432 
     33def empty_data1D(q): 
     34    """ 
     35    Create empty 1D data using the given *q* as the x value. 
     36 
     37    Resolutions dq/q is 5%. 
     38    """ 
     39 
     40    from sans.dataloader.data_info import Data1D 
     41 
     42    Iq = 100*np.ones_like(q) 
     43    dIq = np.sqrt(Iq) 
     44    data = Data1D(q, Iq, dx=0.05*q, dy=dIq) 
     45    data.filename = "fake data" 
     46    data.qmin, data.qmax = q.min(), q.max() 
     47    return data 
     48 
     49 
    7550def empty_data2D(qx, qy=None): 
     51    """ 
     52    Create empty 2D data using the given mesh. 
     53 
     54    If *qy* is missing, create a square mesh with *qy=qx*. 
     55 
     56    Resolution dq/q is 5%. 
     57    """ 
    7658    from sans.dataloader.data_info import Data2D, Detector 
    7759 
     
    11496 
    11597 
    116 def empty_data1D(q): 
    117     from sans.dataloader.data_info import Data1D 
    118  
    119     Iq = 100*np.ones_like(q) 
    120     dIq = np.sqrt(Iq) 
    121     data = Data1D(q, Iq, dx=0.05*q, dy=dIq) 
    122     data.filename = "fake data" 
    123     data.qmin, data.qmax = q.min(), q.max() 
    124     return data 
    125  
    126  
    12798def set_beam_stop(data, radius, outer=None): 
     99    """ 
     100    Add a beam stop of the given *radius*.  If *outer*, make an annulus. 
     101    """ 
    128102    from sans.dataloader.manipulations import Ringcut 
    129103    if hasattr(data, 'qx_data'): 
     
    138112 
    139113def set_half(data, half): 
     114    """ 
     115    Select half of the data, either "right" or "left". 
     116    """ 
    140117    from sans.dataloader.manipulations import Boxcut 
    141118    if half == 'right': 
     
    146123 
    147124def set_top(data, max): 
     125    """ 
     126    Chop the top off the data, above *max*. 
     127    """ 
    148128    from sans.dataloader.manipulations import Boxcut 
    149129    data.mask += Boxcut(x_min=-np.inf, x_max=np.inf, y_min=-np.inf, y_max=max)(data) 
     
    151131 
    152132def plot_data(data, iq, vmin=None, vmax=None, scale='log'): 
     133    """ 
     134    Plot the target value for the data.  This could be the data itself, 
     135    the theory calculation, or the residuals. 
     136 
     137    *scale* can be 'log' for log scale data, or 'linear'. 
     138    """ 
    153139    from numpy.ma import masked_array, masked 
    154140    import matplotlib.pyplot as plt 
     
    172158 
    173159 
    174 def plot_result2D(data, theory, view='log'): 
     160def _plot_result1D(data, theory, view): 
     161    """ 
     162    Plot the data and residuals for 1D data. 
     163    """ 
     164    import matplotlib.pyplot as plt 
     165    from numpy.ma import masked_array, masked 
     166    #print "not a number",sum(np.isnan(data.y)) 
     167    #data.y[data.y<0.05] = 0.5 
     168    mdata = masked_array(data.y, data.mask) 
     169    mdata[np.isnan(mdata)] = masked 
     170    if view is 'log': 
     171        mdata[mdata <= 0] = masked 
     172    mtheory = masked_array(theory, mdata.mask) 
     173    mresid = masked_array((theory-data.y)/data.dy, mdata.mask) 
     174 
     175    plt.subplot(121) 
     176    plt.errorbar(data.x, mdata, yerr=data.dy) 
     177    plt.plot(data.x, mtheory, '-', hold=True) 
     178    plt.yscale(view) 
     179    plt.subplot(122) 
     180    plt.plot(data.x, mresid, 'x') 
     181    #plt.axhline(1, color='black', ls='--',lw=1, hold=True) 
     182    #plt.axhline(0, color='black', lw=1, hold=True) 
     183    #plt.axhline(-1, color='black', ls='--',lw=1, hold=True) 
     184 
     185 
     186def _plot_result2D(data, theory, view): 
     187    """ 
     188    Plot the data and residuals for 2D data. 
     189    """ 
    175190    import matplotlib.pyplot as plt 
    176191    resid = (theory-data.data)/data.err_data 
     
    185200    plt.colorbar() 
    186201 
    187  
    188 def plot_result1D(data, theory, view='log'): 
    189     import matplotlib.pyplot as plt 
    190     from numpy.ma import masked_array, masked 
    191     #print "not a number",sum(np.isnan(data.y)) 
    192     #data.y[data.y<0.05] = 0.5 
    193     mdata = masked_array(data.y, data.mask) 
    194     mdata[np.isnan(mdata)] = masked 
    195     if view is 'log': 
    196         mdata[mdata <= 0] = masked 
    197     mtheory = masked_array(theory, mdata.mask) 
    198     mresid = masked_array((theory-data.y)/data.dy, mdata.mask) 
    199  
    200     plt.subplot(121) 
    201     plt.errorbar(data.x, mdata, yerr=data.dy) 
    202     plt.plot(data.x, mtheory, '-', hold=True) 
    203     plt.yscale(view) 
    204     plt.subplot(122) 
    205     plt.plot(data.x, mresid, 'x') 
    206     #plt.axhline(1, color='black', ls='--',lw=1, hold=True) 
    207     #plt.axhline(0, color='black', lw=1, hold=True) 
    208     #plt.axhline(-1, color='black', ls='--',lw=1, hold=True) 
     202def plot_result(data, theory, view='log'): 
     203    """ 
     204    Plot the data and residuals. 
     205    """ 
     206    if hasattr(data, 'qx_data'): 
     207        _plot_result2D(data, theory, view) 
     208    else: 
     209        _plot_result1D(data, theory, view) 
    209210 
    210211 
    211212class BumpsModel(object): 
     213    """ 
     214    Return a bumps wrapper for a SAS model. 
     215 
     216    *data* is the data to be fitted. 
     217 
     218    *model* is the SAS model, e.g., from :func:`gen.opencl_model`. 
     219 
     220    *cutoff* is the integration cutoff, which avoids computing the 
     221    the SAS model where the polydispersity weight is low. 
     222 
     223    Model parameters can be initialized with additional keyword 
     224    arguments, or by assigning to model.parameter_name.value. 
     225 
     226    The resulting bumps model can be used directly in a FitProblem call. 
     227    """ 
    212228    def __init__(self, data, model, cutoff=1e-5, **kw): 
    213229        from bumps.names import Parameter 
    214         from . import gpu 
    215230 
    216231        # interpret data 
    217         self.is2D = hasattr(data,'qx_data') 
    218232        self.data = data 
    219         if self.is2D: 
     233        if hasattr(data, 'qx_data'): 
    220234            self.index = (data.mask==0) & (~np.isnan(data.data)) 
    221235            self.iq = data.data[self.index] 
     
    294308 
    295309    def plot(self, view='log'): 
    296         if self.is2D: 
    297             plot_result2D(self.data, self.theory(), view=view) 
    298         else: 
    299             plot_result1D(self.data, self.theory(), view=view) 
     310        plot_result(self.data, self.theory(), view=view) 
    300311 
    301312    def save(self, basename): 
Note: See TracChangeset for help on using the changeset viewer.