Changeset 6d6508e in sasmodels for sasmodels/modelinfo.py


Ignore:
Timestamp:
Apr 7, 2016 6:57:33 PM (8 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:
d2fc9a4
Parents:
3707eee
Message:

refactor model_info from dictionary to class

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/modelinfo.py

    r6e7ff6d r6d6508e  
     1from copy import copy 
     2from os.path import abspath, basename, splitext 
    13 
    24import numpy as np 
    35 
    4 # TODO: turn ModelInfo into a proper class 
    5 ModelInfo = dict 
     6from .details import mono_details 
    67 
    78MAX_PD = 4 
     
    116117 
    117118 
    118 def set_demo(model_info, demo): 
    119     """ 
    120     Assign demo parameters to model_info['demo'] 
    121  
    122     If demo is not defined, then use defaults. 
    123  
    124     If demo is defined, override defaults with value from demo. 
    125  
    126     If demo references vector fields, such as thickness[n], then support 
     119def make_demo_pars(partable, demo): 
     120    """ 
     121    Create demo parameter set from key-value pairs. 
     122 
     123    *demo* are the key-value pairs to use for the demo parameters.  Any 
     124    parameters not specified in *demo* are set from the *partable* defaults. 
     125 
     126    If *demo* references vector fields, such as thickness[n], then support 
    127127    different ways of assigning the demo values, including assigning a 
    128128    specific value (e.g., thickness3=50.0), assigning a new value to all 
    129129    (e.g., thickness=50.0) or assigning values using list notation. 
    130130    """ 
    131     partable = model_info['parameters'] 
    132131    if demo is None: 
    133132        result = partable.defaults 
     
    157156            result.update(demo) 
    158157 
    159     model_info['demo'] = result 
     158    return result 
     159 
     160def prefix_parameter(par, prefix): 
     161    """ 
     162    Return a copy of the parameter with its name prefixed. 
     163    """ 
     164    new_par = copy(par) 
     165    new_par.name = prefix + par.name 
     166    new_par.id = prefix + par.id 
     167 
     168def suffix_parameter(par, suffix): 
     169    """ 
     170    Return a copy of the parameter with its name prefixed. 
     171    """ 
     172    new_par = copy(par) 
     173    # If name has the form x[n], replace with x_suffix[n] 
     174    new_par.name = par.id + suffix + par.name[len(par.id):] 
     175    new_par.id = par.id + suffix 
    160176 
    161177class Parameter(object): 
     
    517533    return isinstance(x, str) 
    518534 
     535def make_model_info(kernel_module): 
     536    info = ModelInfo() 
     537    #print("make parameter table", kernel_module.parameters) 
     538    parameters = make_parameter_table(kernel_module.parameters) 
     539    demo = make_demo_pars(parameters, getattr(kernel_module, 'demo', None)) 
     540    filename = abspath(kernel_module.__file__) 
     541    kernel_id = splitext(basename(filename))[0] 
     542    name = getattr(kernel_module, 'name', None) 
     543    if name is None: 
     544        name = " ".join(w.capitalize() for w in kernel_id.split('_')) 
     545 
     546    info.id = kernel_id  # string used to load the kernel 
     547    info.filename = abspath(kernel_module.__file__) 
     548    info.name = name 
     549    info.title = getattr(kernel_module, 'title', name+" model") 
     550    info.description = getattr(kernel_module, 'description', 'no description') 
     551    info.parameters = parameters 
     552    info.demo = demo 
     553    info.composition = None 
     554    info.docs = kernel_module.__doc__ 
     555    info.category = getattr(kernel_module, 'category', None) 
     556    info.single = getattr(kernel_module, 'single', True) 
     557    info.structure_factor = getattr(kernel_module, 'structure_factor', False) 
     558    info.profile_axes = getattr(kernel_module, 'profile_axes', ['x','y']) 
     559    info.variant_info = getattr(kernel_module, 'invariant_info', None) 
     560    info.demo = getattr(kernel_module, 'demo', None) 
     561    info.source = getattr(kernel_module, 'source', []) 
     562    info.tests = getattr(kernel_module, 'tests', []) 
     563    info.ER = getattr(kernel_module, 'ER', None) 
     564    info.VR = getattr(kernel_module, 'VR', None) 
     565    info.form_volume = getattr(kernel_module, 'form_volume', None) 
     566    info.Iq = getattr(kernel_module, 'Iq', None) 
     567    info.Iqxy = getattr(kernel_module, 'Iqxy', None) 
     568    info.profile = getattr(kernel_module, 'profile', None) 
     569    info.sesans = getattr(kernel_module, 'sesans', None) 
     570 
     571    # Precalculate the monodisperse parameter details 
     572    info.mono_details = mono_details(info) 
     573    return info 
     574 
     575class ModelInfo(object): 
     576    """ 
     577    Interpret the model definition file, categorizing the parameters. 
     578 
     579    The module can be loaded with a normal python import statement if you 
     580    know which module you need, or with __import__('sasmodels.model.'+name) 
     581    if the name is in a string. 
     582 
     583    The *model_info* structure contains the following fields: 
     584 
     585    * *id* is the id of the kernel 
     586    * *name* is the display name of the kernel 
     587    * *filename* is the full path to the module defining the file (if any) 
     588    * *title* is a short description of the kernel 
     589    * *description* is a long description of the kernel (this doesn't seem 
     590      very useful since the Help button on the model page brings you directly 
     591      to the documentation page) 
     592    * *docs* is the docstring from the module.  Use :func:`make_doc` to 
     593    * *category* specifies the model location in the docs 
     594    * *parameters* is the model parameter table 
     595    * *single* is True if the model allows single precision 
     596    * *structure_factor* is True if the model is useable in a product 
     597    * *variant_info* contains the information required to select between 
     598      model variants (e.g., the list of cases) or is None if there are no 
     599      model variants 
     600    * *par_type* categorizes the model parameters. See 
     601      :func:`categorize_parameters` for details. 
     602    * *demo* contains the *{parameter: value}* map used in compare (and maybe 
     603      for the demo plot, if plots aren't set up to use the default values). 
     604      If *demo* is not given in the file, then the default values will be used. 
     605    * *tests* is a set of tests that must pass 
     606    * *source* is the list of library files to include in the C model build 
     607    * *Iq*, *Iqxy*, *form_volume*, *ER*, *VR* and *sesans* are python functions 
     608      implementing the kernel for the module, or None if they are not 
     609      defined in python 
     610    * *composition* is None if the model is independent, otherwise it is a 
     611      tuple with composition type ('product' or 'mixture') and a list of 
     612      *model_info* blocks for the composition objects.  This allows us to 
     613      build complete product and mixture models from just the info. 
     614    """ 
     615    id = None 
     616    filename = None 
     617    name = None 
     618    title = None 
     619    description = None 
     620    parameters = None 
     621    demo = None 
     622    composition = None 
     623    docs = None 
     624    category = None 
     625    single = None 
     626    structure_factor = None 
     627    profile_axes = None 
     628    variant_info = None 
     629    demo = None 
     630    source = None 
     631    tests = None 
     632    ER = None 
     633    VR = None 
     634    form_volume = None 
     635    Iq = None 
     636    Iqxy = None 
     637    profile = None 
     638    sesans = None 
     639    mono_details = None 
     640 
     641    def __init__(self): 
     642        pass 
     643 
     644 
Note: See TracChangeset for help on using the changeset viewer.