Changeset 61a4bd4 in sasmodels for sasmodels/core.py


Ignore:
Timestamp:
Sep 4, 2017 10:09:27 AM (7 years ago)
Author:
lewis
Branches:
master, core_shell_microgels, costrafo411, magnetic_model, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
481ff64
Parents:
65314f7
Message:

Refactor load_model_info to parse more complex model strings

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/core.py

    rfb9a3b6 r61a4bd4  
    126126 
    127127 
    128 def load_model_info(model_name, force_mixture=False): 
     128# def load_model_info(model_name, force_mixture=False): 
     129#     # type: (str) -> modelinfo.ModelInfo 
     130#     """ 
     131#     Load a model definition given the model name. 
     132 
     133#     *model_name* is the name of the model, or perhaps a model expression 
     134#     such as sphere*hardsphere or sphere+cylinder. 
     135 
     136#     *force_mixture* if true, MixtureModel will be used for combining models. 
     137#     Otherwise either MixtureModel will be used for addition and ProductModel 
     138#     will be used for multiplication 
     139 
     140#     This returns a handle to the module defining the model.  This can be 
     141#     used with functions in generate to build the docs or extract model info. 
     142#     """ 
     143#     parts = model_name.split('+') 
     144#     if len(parts) > 1: 
     145#         # Always use MixtureModel for addition 
     146#         model_info_list = [load_model_info(p) for p in parts] 
     147#         return mixture.make_mixture_info(model_info_list) 
     148 
     149#     parts = model_name.split('*') 
     150#     if len(parts) > 1: 
     151#         if force_mixture: 
     152#             # Use MixtureModel for multiplication if forced 
     153#             model_info_list = [load_model_info(p) for p in parts] 
     154#             return mixture.make_mixture_info(model_info_list, operation='*') 
     155#         if len(parts) > 2: 
     156#             raise ValueError("use P*S to apply structure factor S to model P") 
     157#         # Use ProductModel 
     158#         P_info, Q_info = [load_model_info(p) for p in parts] 
     159#         return product.make_product_info(P_info, Q_info) 
     160 
     161#     kernel_module = generate.load_kernel_module(model_name) 
     162#     return modelinfo.make_model_info(kernel_module) 
     163 
     164def load_model_info(model_string): 
    129165    # type: (str) -> modelinfo.ModelInfo 
    130166    """ 
     
    134170    such as sphere*hardsphere or sphere+cylinder. 
    135171 
    136     *force_mixture* if true, MixtureModel will be used for combining models. 
    137     Otherwise either MixtureModel will be used for addition and ProductModel 
    138     will be used for multiplication 
    139  
    140172    This returns a handle to the module defining the model.  This can be 
    141173    used with functions in generate to build the docs or extract model info. 
    142174    """ 
    143     parts = model_name.split('+') 
    144     if len(parts) > 1: 
    145         # Always use MixtureModel for addition 
    146         model_info_list = [load_model_info(p) for p in parts] 
    147         return mixture.make_mixture_info(model_info_list) 
    148  
    149     parts = model_name.split('*') 
    150     if len(parts) > 1: 
    151         if force_mixture: 
    152             # Use MixtureModel for multiplication if forced 
    153             model_info_list = [load_model_info(p) for p in parts] 
    154             return mixture.make_mixture_info(model_info_list, operation='*') 
    155         if len(parts) > 2: 
    156             raise ValueError("use P*S to apply structure factor S to model P") 
    157         # Use ProductModel 
    158         P_info, Q_info = [load_model_info(p) for p in parts] 
    159         return product.make_product_info(P_info, Q_info) 
    160  
    161     kernel_module = generate.load_kernel_module(model_name) 
    162     return modelinfo.make_model_info(kernel_module) 
     175    # TODO: parse an expression like form@structure to create a P(Q)*S(Q) model 
     176    product_parts = [] 
     177    addition_parts = [] 
     178 
     179    addition_parts_names = model_string.split('+') 
     180    if len(addition_parts_names) >= 2: 
     181        addition_parts = [load_model_info(part) for part in addition_parts_names] 
     182    elif len(addition_parts_names) == 1: 
     183        product_parts_names = model_string.split('*') 
     184        if len(product_parts_names) >= 2: 
     185            product_parts = [load_model_info(part) for part in product_parts_names] 
     186        elif len(product_parts_names) == 1: 
     187            kernel_module = generate.load_kernel_module(product_parts_names[0]) 
     188            return modelinfo.make_model_info(kernel_module) 
     189 
     190    model = None 
     191    if len(product_parts) > 1: 
     192        model = mixture.make_mixture_info(product_parts, operation='*') 
     193    if len(addition_parts) > 1: 
     194        if model is not None: 
     195            addition_parts.append(model) 
     196        model = mixture.make_mixture_info(addition_parts, operation='+') 
     197    return model 
    163198 
    164199 
Note: See TracChangeset for help on using the changeset viewer.