Changeset 33e91b1 in sasmodels


Ignore:
Timestamp:
Mar 3, 2015 4:28:50 PM (9 years ago)
Author:
Doucet, Mathieu <doucetm@…>
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:
53d0e24, 3a45c2c
Parents:
6c8db9e
Message:

pylint fixes

Location:
sasmodels
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/generate.py

    ra5d0d00 r33e91b1  
    201201# Scale and background, which are parameters common to every form factor 
    202202COMMON_PARAMETERS = [ 
    203     [ "scale", "", 1, [0, np.inf], "", "Source intensity" ], 
    204     [ "background", "1/cm", 0, [0, np.inf], "", "Source background" ], 
     203    ["scale", "", 1, [0, np.inf], "", "Source intensity"], 
     204    ["background", "1/cm", 0, [0, np.inf], "", "Source background"], 
    205205    ] 
    206206 
     
    233233# description and its parameter table.  The remainder of the doc comes 
    234234# from the module docstring. 
    235 DOC_HEADER=""".. _%(name)s: 
     235DOC_HEADER = """.. _%(name)s: 
    236236 
    237237%(label)s 
     
    259259        ] 
    260260    column_widths = [max(w, len(h)) 
    261                      for w,h in zip(column_widths, PARTABLE_HEADERS)] 
     261                     for w, h in zip(column_widths, PARTABLE_HEADERS)] 
    262262 
    263263    sep = " ".join("="*w for w in column_widths) 
    264264    lines = [ 
    265265        sep, 
    266         " ".join("%-*s"%(w,h) for w,h in zip(column_widths, PARTABLE_HEADERS)), 
     266        " ".join("%-*s" % (w, h) for w, h in zip(column_widths, PARTABLE_HEADERS)), 
    267267        sep, 
    268268        ] 
    269269    for p in pars: 
    270270        lines.append(" ".join([ 
    271             "%-*s"%(column_widths[0],p[0]), 
    272             "%-*s"%(column_widths[1],p[-1]), 
    273             "%-*s"%(column_widths[2],RST_UNITS[p[1]]), 
    274             "%*g"%(column_widths[3],p[2]), 
     271            "%-*s" % (column_widths[0], p[0]), 
     272            "%-*s" % (column_widths[1], p[-1]), 
     273            "%-*s" % (column_widths[2], RST_UNITS[p[1]]), 
     274            "%*g" % (column_widths[3], p[2]), 
    275275            ])) 
    276276    lines.append(sep) 
     
    287287        if exists(target): 
    288288            return target 
    289     raise ValueError("%r not found in %s"%(filename, search_path)) 
     289    raise ValueError("%r not found in %s" % (filename, search_path)) 
    290290 
    291291def sources(info): 
     
    293293    Return a list of the sources file paths for the module. 
    294294    """ 
    295     search_path = [ dirname(info['filename']), 
    296                     abspath(joinpath(dirname(__file__),'models')) ] 
     295    search_path = [dirname(info['filename']), 
     296                   abspath(joinpath(dirname(__file__), 'models'))] 
    297297    return [_search(search_path, f) for f in info['source']] 
    298298 
     
    333333 
    334334    for p in pars: 
    335         name,ptype = p[0],p[4] 
     335        name, ptype = p[0], p[4] 
    336336        if ptype == 'volume': 
    337337            partype['pd-1d'].append(name) 
     
    346346            partype['fixed-2d'].append(name) 
    347347        else: 
    348             raise ValueError("unknown parameter type %r"%ptype) 
     348            raise ValueError("unknown parameter type %r" % ptype) 
    349349        partype[ptype].append(name) 
    350350 
     
    356356    """ 
    357357    spaces = " "*depth 
    358     sep = "\n"+spaces 
     358    sep = "\n" + spaces 
    359359    return spaces + sep.join(s.split("\n")) 
    360360 
     
    366366    Returns loop opening and loop closing 
    367367    """ 
    368     LOOP_OPEN="""\ 
     368    LOOP_OPEN = """\ 
    369369for (int %(name)s_i=0; %(name)s_i < N%(name)s; %(name)s_i++) { 
    370370  const double %(name)s = loops[2*(%(name)s_i%(offset)s)]; 
     
    376376    loop_end = [] 
    377377    for name in pd_pars: 
    378         subst = { 'name': name, 'offset': offset } 
    379         loop_head.append(indent(LOOP_OPEN%subst, depth)) 
     378        subst = {'name': name, 'offset': offset} 
     379        loop_head.append(indent(LOOP_OPEN % subst, depth)) 
    380380        loop_end.insert(0, (" "*depth) + "}") 
    381         offset += '+N'+name 
     381        offset += '+N' + name 
    382382        depth += 2 
    383383    return "\n".join(loop_head), "\n".join(loop_end) 
    384384 
    385 C_KERNEL_TEMPLATE=None 
     385C_KERNEL_TEMPLATE = None 
    386386def make_model(info): 
    387387    """ 
     
    416416 
    417417    iq_parameters = [p[0] 
    418         for p in info['parameters'][2:] # skip scale, background 
    419         if p[0] in set(fixed_1d+pd_1d)] 
     418                     for p in info['parameters'][2:] # skip scale, background 
     419                     if p[0] in set(fixed_1d + pd_1d)] 
    420420    iqxy_parameters = [p[0] 
    421         for p in info['parameters'][2:] # skip scale, background 
    422         if p[0] in set(fixed_2d+pd_2d)] 
     421                       for p in info['parameters'][2:] # skip scale, background 
     422                       if p[0] in set(fixed_2d + pd_2d)] 
    423423    volume_parameters = [p[0] 
    424         for p in info['parameters'] 
    425         if p[4]=='volume'] 
     424                         for p in info['parameters'] 
     425                         if p[4] == 'volume'] 
    426426 
    427427    # Fill in defintions for volume parameters 
     
    430430                        ','.join(volume_parameters))) 
    431431        defines.append(('VOLUME_WEIGHT_PRODUCT', 
    432                         '*'.join(p+'_w' for p in volume_parameters))) 
     432                        '*'.join(p + '_w' for p in volume_parameters))) 
    433433 
    434434    # Generate form_volume function from body only 
    435435    if info['form_volume'] is not None: 
    436436        if volume_parameters: 
    437             vol_par_decl = ', '.join('double '+p for p in volume_parameters) 
     437            vol_par_decl = ', '.join('double ' + p for p in volume_parameters) 
    438438        else: 
    439439            vol_par_decl = 'void' 
     
    445445    %(body)s 
    446446} 
    447 """%{'body':info['form_volume']} 
     447""" % {'body':info['form_volume']} 
    448448        source.append(fn) 
    449449 
    450450    # Fill in definitions for Iq parameters 
    451     defines.append(('IQ_KERNEL_NAME', info['name']+'_Iq')) 
     451    defines.append(('IQ_KERNEL_NAME', info['name'] + '_Iq')) 
    452452    defines.append(('IQ_PARAMETERS', ', '.join(iq_parameters))) 
    453453    if fixed_1d: 
    454454        defines.append(('IQ_FIXED_PARAMETER_DECLARATIONS', 
    455                         ', \\\n    '.join('const double %s'%p for p in fixed_1d))) 
     455                        ', \\\n    '.join('const double %s' % p for p in fixed_1d))) 
    456456    if pd_1d: 
    457457        defines.append(('IQ_WEIGHT_PRODUCT', 
    458                         '*'.join(p+'_w' for p in pd_1d))) 
     458                        '*'.join(p + '_w' for p in pd_1d))) 
    459459        defines.append(('IQ_DISPERSION_LENGTH_DECLARATIONS', 
    460                         ', \\\n    '.join('const int N%s'%p for p in pd_1d))) 
     460                        ', \\\n    '.join('const int N%s' % p for p in pd_1d))) 
    461461        defines.append(('IQ_DISPERSION_LENGTH_SUM', 
    462                         '+'.join('N'+p for p in pd_1d))) 
     462                        '+'.join('N' + p for p in pd_1d))) 
    463463        open_loops, close_loops = build_polydispersity_loops(pd_1d) 
    464464        defines.append(('IQ_OPEN_LOOPS', 
    465                         open_loops.replace('\n',' \\\n'))) 
     465                        open_loops.replace('\n', ' \\\n'))) 
    466466        defines.append(('IQ_CLOSE_LOOPS', 
    467                         close_loops.replace('\n',' \\\n'))) 
     467                        close_loops.replace('\n', ' \\\n'))) 
    468468    if info['Iq'] is not None: 
    469469        defines.append(('IQ_PARAMETER_DECLARATIONS', 
    470                        ', '.join('double '+p for p in iq_parameters))) 
     470                        ', '.join('double ' + p for p in iq_parameters))) 
    471471        fn = """\ 
    472472double Iq(double q, IQ_PARAMETER_DECLARATIONS); 
     
    474474    %(body)s 
    475475} 
    476 """%{'body':info['Iq']} 
     476""" % {'body':info['Iq']} 
    477477        source.append(fn) 
    478478 
    479479    # Fill in definitions for Iqxy parameters 
    480     defines.append(('IQXY_KERNEL_NAME', info['name']+'_Iqxy')) 
     480    defines.append(('IQXY_KERNEL_NAME', info['name'] + '_Iqxy')) 
    481481    defines.append(('IQXY_PARAMETERS', ', '.join(iqxy_parameters))) 
    482482    if fixed_2d: 
    483483        defines.append(('IQXY_FIXED_PARAMETER_DECLARATIONS', 
    484                         ', \\\n    '.join('const double %s'%p for p in fixed_2d))) 
     484                        ', \\\n    '.join('const double %s' % p for p in fixed_2d))) 
    485485    if pd_2d: 
    486486        defines.append(('IQXY_WEIGHT_PRODUCT', 
    487                         '*'.join(p+'_w' for p in pd_2d))) 
     487                        '*'.join(p + '_w' for p in pd_2d))) 
    488488        defines.append(('IQXY_DISPERSION_LENGTH_DECLARATIONS', 
    489                         ', \\\n    '.join('const int N%s'%p for p in pd_2d))) 
     489                        ', \\\n    '.join('const int N%s' % p for p in pd_2d))) 
    490490        defines.append(('IQXY_DISPERSION_LENGTH_SUM', 
    491                         '+'.join('N'+p for p in pd_2d))) 
     491                        '+'.join('N' + p for p in pd_2d))) 
    492492        open_loops, close_loops = build_polydispersity_loops(pd_2d) 
    493493        defines.append(('IQXY_OPEN_LOOPS', 
    494                         open_loops.replace('\n',' \\\n'))) 
     494                        open_loops.replace('\n', ' \\\n'))) 
    495495        defines.append(('IQXY_CLOSE_LOOPS', 
    496                         close_loops.replace('\n',' \\\n'))) 
     496                        close_loops.replace('\n', ' \\\n'))) 
    497497    if info['Iqxy'] is not None: 
    498498        defines.append(('IQXY_PARAMETER_DECLARATIONS', 
    499                        ', '.join('double '+p for p in iqxy_parameters))) 
     499                        ', '.join('double ' + p for p in iqxy_parameters))) 
    500500        fn = """\ 
    501501double Iqxy(double qx, double qy, IQXY_PARAMETER_DECLARATIONS); 
     
    503503    %(body)s 
    504504} 
    505 """%{'body':info['Iqxy']} 
     505""" % {'body':info['Iqxy']} 
    506506        source.append(fn) 
    507507 
     
    513513 
    514514    #for d in defines: print d 
    515     DEFINES='\n'.join('#define %s %s'%(k,v) for k,v in defines) 
    516     SOURCES='\n\n'.join(source) 
    517     return C_KERNEL_TEMPLATE%{ 
     515    DEFINES = '\n'.join('#define %s %s' % (k, v) for k, v in defines) 
     516    SOURCES = '\n\n'.join(source) 
     517    return C_KERNEL_TEMPLATE % { 
    518518        'DEFINES':DEFINES, 
    519519        'SOURCES':SOURCES, 
     
    531531    #print kernelfile 
    532532    info = dict( 
    533         filename = abspath(kernel_module.__file__), 
    534         name = kernel_module.name, 
    535         title = kernel_module.title, 
    536         description = kernel_module.description, 
    537         parameters = COMMON_PARAMETERS + kernel_module.parameters, 
    538         source = getattr(kernel_module, 'source', []), 
    539         oldname = kernel_module.oldname, 
    540         oldpars = kernel_module.oldpars, 
     533        filename=abspath(kernel_module.__file__), 
     534        name=kernel_module.name, 
     535        title=kernel_module.title, 
     536        description=kernel_module.description, 
     537        parameters=COMMON_PARAMETERS + kernel_module.parameters, 
     538        source=getattr(kernel_module, 'source', []), 
     539        oldname=kernel_module.oldname, 
     540        oldpars=kernel_module.oldpars, 
    541541        ) 
    542542    # Fill in attributes which default to None 
    543     info.update((k,getattr(kernel_module, k, None)) 
     543    info.update((k, getattr(kernel_module, k, None)) 
    544544                for k in ('ER', 'VR', 'form_volume', 'Iq', 'Iqxy')) 
    545545    # Fill in the derived attributes 
    546     info['limits'] = dict((p[0],p[3]) for p in info['parameters']) 
     546    info['limits'] = dict((p[0], p[3]) for p in info['parameters']) 
    547547    info['partype'] = categorize_parameters(info['parameters']) 
    548     info['defaults'] = dict((p[0],p[2]) for p in info['parameters']) 
     548    info['defaults'] = dict((p[0], p[2]) for p in info['parameters']) 
    549549 
    550550    # Assume if one part of the kernel is python then all parts are. 
     
    556556    Return the documentation for the model. 
    557557    """ 
    558     subst = dict(name=kernel_module.name.replace('_','-'), 
     558    subst = dict(name=kernel_module.name.replace('_', '-'), 
    559559                 label=" ".join(kernel_module.name.split('_')).capitalize(), 
    560560                 title=kernel_module.title, 
    561561                 parameters=make_partable(kernel_module.parameters), 
    562562                 docs=kernel_module.__doc__) 
    563     return DOC_HEADER%subst 
     563    return DOC_HEADER % subst 
    564564 
    565565 
     
    568568    import datetime 
    569569    from .models import cylinder 
    570     toc = lambda: (datetime.datetime.now()-tic).total_seconds() 
    571570    tic = datetime.datetime.now() 
    572     source, info = make(cylinder) 
    573     print "time:",toc() 
     571    toc = lambda: (datetime.datetime.now() - tic).total_seconds() 
     572    make(cylinder) 
     573    print "time:", toc() 
    574574 
    575575def main(): 
     
    579579        name = sys.argv[1] 
    580580        import sasmodels.models 
    581         __import__('sasmodels.models.'+name) 
     581        __import__('sasmodels.models.' + name) 
    582582        model = getattr(sasmodels.models, name) 
    583         source, info = make(model); print source 
    584         #print doc(model) 
     583        source, _ = make(model) 
     584        print source 
    585585 
    586586if __name__ == "__main__": 
  • sasmodels/models/parallelepiped.py

    ra5d0d00 r33e91b1  
    99---------- 
    1010 
    11 This model provides the form factor, *P(q)*, for a rectangular parallelepiped  
    12 (below) where the form factor is normalized by the volume of the  
    13 parallelepiped. If you need to apply polydispersity, see also the  
     11This model provides the form factor, *P(q)*, for a rectangular parallelepiped 
     12(below) where the form factor is normalized by the volume of the 
     13parallelepiped. If you need to apply polydispersity, see also the 
    1414RectangularPrismModel_. 
    1515 
     
    2020    P(Q) = {\text{scale} \over V} F^2(Q) + \text{background} 
    2121 
    22 where the volume *V* = *A B C* and the averaging < > is applied over all  
     22where the volume *V* = *A B C* and the averaging < > is applied over all 
    2323orientations for 1D. 
    2424 
     
    2727*Figure. Parallelepiped with the corresponding Definition of sides. 
    2828 
    29 The edge of the solid must satisfy the condition that** *A* < *B* < *C*.  
    30 Then, assuming *a* = *A* / *B* < 1, *b* = *B* / *B* = 1, and  
     29The edge of the solid must satisfy the condition that** *A* < *B* < *C*. 
     30Then, assuming *a* = *A* / *B* < 1, *b* = *B* / *B* = 1, and 
    3131*c* = *C* / *B* > 1, the form factor is 
    3232 
    3333.. math:: 
    3434 
    35     P(q) = \frac{\textstyle{scale}}{V}\int_0^1 \phi(\mu \sqrt{1-\sigma^2},a)  
     35    P(q) = \frac{\textstyle{scale}}{V}\int_0^1 \phi(\mu \sqrt{1-\sigma^2},a) 
    3636    [S(\mu c \sigma/2)]^2 d\sigma 
    3737 
     
    4040.. math:: 
    4141 
    42     \phi(\mu,a) = \int_0^1 \{S[\frac{\mu}{2}\cos(\frac{\pi}{2}u)]  
     42    \phi(\mu,a) = \int_0^1 \{S[\frac{\mu}{2}\cos(\frac{\pi}{2}u)] 
    4343    S[\frac{\mu a}{2}\sin(\frac{\pi}{2}u)]\}^2 du 
    4444 
    4545    S(x) = \frac{\sin x}{x} 
    46      
     46 
    4747    \mu = qB 
    4848 
     
    5353    \Delta\rho = \rho_{\textstyle p} - \rho_{\textstyle solvent} 
    5454 
    55 The scattering intensity per unit volume is returned in units of |cm^-1|;  
     55The scattering intensity per unit volume is returned in units of |cm^-1|; 
    5656ie, *I(q)* = |phi| *P(q)*\ . 
    5757 
    58 NB: The 2nd virial coefficient of the parallelpiped is calculated based on  
    59 the averaged effective radius (= sqrt(*short_a* \* *short_b* / |pi|)) and  
     58NB: The 2nd virial coefficient of the parallelpiped is calculated based on 
     59the averaged effective radius (= sqrt(*short_a* \* *short_b* / |pi|)) and 
    6060length(= *long_c*) values, and used as the effective radius for 
    6161*S(Q)* when *P(Q)* \* *S(Q)* is applied. 
    6262 
    63 To provide easy access to the orientation of the parallelepiped, we define  
     63To provide easy access to the orientation of the parallelepiped, we define 
    6464three angles |theta|, |phi| and |bigpsi|. The definition of |theta| and |phi| 
    65 is the same as for the cylinder model (see also figures below).  
    66 The angle |bigpsi| is the rotational angle around the *long_c* axis against  
    67 the *q* plane. For example, |bigpsi| = 0 when the *short_b* axis is parallel  
     65is the same as for the cylinder model (see also figures below). 
     66The angle |bigpsi| is the rotational angle around the *long_c* axis against 
     67the *q* plane. For example, |bigpsi| = 0 when the *short_b* axis is parallel 
    6868to the *x*-axis of the detector. 
    6969 
     
    8383---------- 
    8484 
    85 Validation of the code was done by comparing the output of the 1D calculation  
    86 to the angular average of the output of a 2D calculation over all possible  
    87 angles. The Figure below shows the comparison where the solid dot refers to  
    88 averaged 2D while the line represents the result of the 1D calculation (for  
    89 the averaging, 76, 180, 76 points are taken for the angles of |theta|, |phi|,  
     85Validation of the code was done by comparing the output of the 1D calculation 
     86to the angular average of the output of a 2D calculation over all possible 
     87angles. The Figure below shows the comparison where the solid dot refers to 
     88averaged 2D while the line represents the result of the 1D calculation (for 
     89the averaging, 76, 180, 76 points are taken for the angles of |theta|, |phi|, 
    9090and |psi| respectively). 
    9191 
     
    9696*Figure. Comparison between 1D and averaged 2D.* 
    9797 
    98 This model reimplements the form factor calculations implemented in a c-library  
     98This model reimplements the form factor calculations implemented in a c-library 
    9999provided by the NIST Center for Neutron Research (Kline, 2006). 
    100100 
    101101""" 
    102102 
    103 from numpy import pi, inf 
     103from numpy import pi, inf, sqrt 
    104104 
    105105name = "parallelepiped" 
     
    108108     P(q)= scale/V*integral from 0 to 1 of ... 
    109109           phi(mu*sqrt(1-sigma^2),a) * S(mu*c*sigma/2)^2 * dsigma 
    110       
     110 
    111111            phi(mu,a) = integral from 0 to 1 of .. 
    112             (S((mu/2)*cos(pi*u/2))*S((mu*a/2)*sin(pi*u/2)))^2 * du 
     112        (S((mu/2)*cos(pi*u/2))*S((mu*a/2)*sin(pi*u/2)))^2 * du 
    113113            S(x) = sin(x)/x 
    114             mu = q*B 
     114        mu = q*B 
    115115""" 
    116116category = "shape:parallelpiped" 
    117117 
    118118parameters = [ 
    119 #   [ "name", "units", default, [lower, upper], "type", 
    120 #     "description" ], 
    121     [ "sld", "1e-6/Ang^2", 4, [-inf,inf], "", 
    122       "Parallelepiped scattering length density" ], 
    123     [ "solvent_sld", "1e-6/Ang^2", 1, [-inf,inf], "", 
    124       "Solvent scattering length density" ], 
    125     [ "a_side", "Ang", 35, [0, inf], "volume", 
    126       "Shorter side of the parallelepiped" ], 
    127     [ "b_side", "Ang", 75, [0, inf], "volume", 
    128       "Second side of the parallelepiped" ], 
    129     [ "c_side", "Ang", 400, [0, inf], "volume", 
    130       "Larger side of the parallelepiped" ], 
    131     [ "theta", "degrees", 60, [-inf, inf], "orientation", 
    132       "In plane angle" ], 
    133     [ "phi", "degrees", 60, [-inf, inf], "orientation", 
    134       "Out of plane angle" ], 
    135     [ "psi", "degrees", 60, [-inf, inf], "orientation", 
    136       "Rotation angle around its own c axis against q plane" ], 
     119    #   [ "name", "units", default, [lower, upper], "type", 
     120    #     "description" ], 
     121    ["sld", "1e-6/Ang^2", 4, [-inf, inf], "", 
     122     "Parallelepiped scattering length density"], 
     123    ["solvent_sld", "1e-6/Ang^2", 1, [-inf, inf], "", 
     124     "Solvent scattering length density"], 
     125    ["a_side", "Ang", 35, [0, inf], "volume", 
     126     "Shorter side of the parallelepiped"], 
     127    ["b_side", "Ang", 75, [0, inf], "volume", 
     128     "Second side of the parallelepiped"], 
     129    ["c_side", "Ang", 400, [0, inf], "volume", 
     130     "Larger side of the parallelepiped"], 
     131    ["theta", "degrees", 60, [-inf, inf], "orientation", 
     132     "In plane angle"], 
     133    ["phi", "degrees", 60, [-inf, inf], "orientation", 
     134     "Out of plane angle"], 
     135    ["psi", "degrees", 60, [-inf, inf], "orientation", 
     136     "Rotation angle around its own c axis against q plane"], 
    137137    ] 
    138138 
    139 source = [ "lib/J1.c", "lib/gauss76.c", "parallelepiped.c" ] 
     139source = ["lib/J1.c", "lib/gauss76.c", "parallelepiped.c"] 
    140140 
    141141def ER(a_side, b_side, c_side): 
     
    148148    b = 0.5 * c_side 
    149149    t1 = a * a * b 
    150     t2 = 1.0 + (b/a)*(1.0+a/b/2.0)*(1.0+pi*a/b/2.0) 
     150    t2 = 1.0 + (b / a) * (1.0 + a / b / 2.0) * (1.0 + pi * a / b / 2.0) 
    151151    ddd = 3.0 * t1 * t2 
    152152 
    153     return 0.5 * (ddd)**(1./3.) 
     153    return 0.5 * (ddd) ** (1. / 3.) 
    154154 
    155155# parameters for demo 
     
    169169# For testing against the old sasview models, include the converted parameter 
    170170# names and the target sasview model name. 
    171 oldname='ParallelepipedModel' 
    172 oldpars=dict(theta='parallel_theta', phi='parallel_phi', psi='parallel_psi', 
    173              a_side='short_a', b_side='short_b', c_side='long_c', 
    174              sld='sldPipe', solvent_sld='sldSolv') 
     171oldname = 'ParallelepipedModel' 
     172oldpars = dict(theta='parallel_theta', phi='parallel_phi', psi='parallel_psi', 
     173               a_side='short_a', b_side='short_b', c_side='long_c', 
     174               sld='sldPipe', solvent_sld='sldSolv') 
    175175 
Note: See TracChangeset for help on using the changeset viewer.