Changes in sasmodels/convert.py [07c8d46:f80f334] in sasmodels


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/convert.py

    r07c8d46 rf80f334  
    5353    ("_pd_nsigma", ".nsigmas"), 
    5454    ("_pd_type", ".type"), 
    55     (".lower", ".lower"), 
    56     (".upper", ".upper"), 
    57     (".fittable", ".fittable"), 
    58     (".std", ".std"), 
    59     (".units", ".units"), 
    60     ("", "") 
    6155    ] 
    6256 
     
    7064    if id.startswith('M0:'): 
    7165        return True 
     66    if id.startswith('volfraction') or id.startswith('radius_effective'): 
     67        return False 
    7268    if '_pd' in id or '.' in id: 
    7369        return False 
     
    7975        if p.id == id: 
    8076            return p.type == 'sld' 
    81     return False 
     77    raise ValueError("unknown parameter %r in conversion"%id) 
    8278 
    8379def _rescale_sld(model_info, pars, scale): 
     
    9288 
    9389 
    94 def _get_translation_table(model_info, version=(3,1,2)): 
    95     conv_param = CONVERSION_TABLE.get(version, {}).get(model_info.id, [None, {}]) 
    96     translation = conv_param[1].copy() 
     90def _get_translation_table(model_info): 
     91    _, translation = CONVERSION_TABLE.get(model_info.id, [None, {}]) 
     92    translation = translation.copy() 
    9793    for p in model_info.parameters.kernel_parameters: 
    9894        if p.length > 1: 
     
    123119def _pd_to_underscores(pars): 
    124120    return dict((_dot_pd_to_underscore_pd(k), v) for k, v in pars.items()) 
     121 
     122def _convert_name(conv_dict, pars): 
     123    """ 
     124    Renames parameter values (upper, lower, etc) to v4.0 names 
     125    :param conv_dict: conversion dictionary mapping new name : old name 
     126    :param pars: parameters to convert 
     127    :return: 
     128    """ 
     129    new_pars = {} 
     130    i = 0 
     131    j = 0 
     132    for key_par, value_par in pars.iteritems(): 
     133        j += 1 
     134        for key_conv, value_conv in conv_dict.iteritems(): 
     135            if re.search(value_conv, key_par): 
     136                new_pars[key_par.replace(value_conv, key_conv)] = value_par 
     137                i += 1 
     138                break 
     139            elif re.search("background", key_par) or re.search("scale", key_par): 
     140                new_pars[key_par] = value_par 
     141                i += 1 
     142                break 
     143        if i != j: 
     144            new_pars[key_par] = value_par 
     145            i += 1 
     146    return new_pars 
    125147 
    126148def _convert_pars(pars, mapping): 
     
    145167    return newpars 
    146168 
    147 def _conversion_target(model_name, version=(3,1,2)): 
     169 
     170def _conversion_target(model_name): 
    148171    """ 
    149172    Find the sasmodel name which translates into the sasview name. 
     
    153176    two variants in sasview. 
    154177    """ 
    155     for sasmodels_name, sasview_dict in \ 
    156             CONVERSION_TABLE.get(version, {}).items(): 
    157         if sasview_dict[0] == model_name: 
     178    for sasmodels_name, [sasview_name, _] in CONVERSION_TABLE.items(): 
     179        if sasview_name == model_name: 
    158180            return sasmodels_name 
    159181    return None 
    160182 
    161 def _hand_convert(name, oldpars, version=(3,1,2)): 
    162     if version == (3,1,2): 
    163         oldpars = _hand_convert_3_1_2_to_4_1(name, oldpars) 
    164     return oldpars 
    165  
    166 def _hand_convert_3_1_2_to_4_1(name, oldpars): 
     183 
     184def _hand_convert(name, oldpars): 
    167185    if name == 'core_shell_parallelepiped': 
    168186        # Make sure pd on rim parameters defaults to zero 
     
    197215            pd = oldpars['radius.width']*oldpars['radius']/thickness 
    198216            oldpars['radius.width'] = pd 
    199     elif name == 'multilayer_vesicle': 
    200         if 'scale' in oldpars: 
    201             oldpars['volfraction'] = oldpars['scale'] 
    202             oldpars['scale'] = 1.0 
    203         if 'scale.lower' in oldpars: 
    204             oldpars['volfraction.lower'] = oldpars['scale.lower'] 
    205         if 'scale.upper' in oldpars: 
    206             oldpars['volfraction.upper'] = oldpars['scale.upper'] 
    207         if 'scale.fittable' in oldpars: 
    208             oldpars['volfraction.fittable'] = oldpars['scale.fittable'] 
    209         if 'scale.std' in oldpars: 
    210             oldpars['volfraction.std'] = oldpars['scale.std'] 
    211         if 'scale.units' in oldpars: 
    212             oldpars['volfraction.units'] = oldpars['scale.units'] 
    213217    elif name == 'pearl_necklace': 
    214218        pass 
     
    232236                oldpars[p + ".upper"] /= 1e-13 
    233237    elif name == 'spherical_sld': 
    234         j = 0 
    235         while "func_inter" + str(j) in oldpars: 
    236             name = "func_inter" + str(j) 
    237             new_name = "shape" + str(j + 1) 
    238             if oldpars[name] == 'Erf(|nu|*z)': 
    239                 oldpars[new_name] = int(0) 
    240             elif oldpars[name] == 'RPower(z^|nu|)': 
    241                 oldpars[new_name] = int(1) 
    242             elif oldpars[name] == 'LPower(z^|nu|)': 
    243                 oldpars[new_name] = int(2) 
    244             elif oldpars[name] == 'RExp(-|nu|*z)': 
    245                 oldpars[new_name] = int(3) 
    246             elif oldpars[name] == 'LExp(-|nu|*z)': 
    247                 oldpars[new_name] = int(4) 
    248             else: 
    249                 oldpars[new_name] = int(0) 
    250             oldpars.pop(name) 
    251             oldpars['n_shells'] = str(j + 1) 
    252             j += 1 
     238        oldpars["CONTROL"] += 1 
    253239    elif name == 'teubner_strey': 
    254240        # basically undoing the entire Teubner-Strey calculations here. 
     
    295281    return oldpars 
    296282 
    297 def convert_model(name, pars, use_underscore=False, model_version=(3,1,2)): 
     283def convert_model(name, pars, use_underscore=False): 
    298284    """ 
    299285    Convert model from old style parameter names to new style. 
    300286    """ 
    301     newpars = pars 
    302     keys = sorted(CONVERSION_TABLE.keys()) 
    303     for i, version in enumerate(keys): 
    304         # Don't allow indices outside list 
    305         next_i = i + 1 
    306         if next_i == len(keys): 
    307             next_i = i 
    308         # If the save state is from a later version, skip the check 
    309         if model_version <= keys[next_i]: 
    310             newname = _conversion_target(name, version) 
    311         else: 
    312             newname = None 
    313         # If no conversion is found, move on 
    314         if newname is None: 
    315             newname = name 
    316             continue 
    317         if ':' in newname:   # core_shell_ellipsoid:1 
    318             model_info = load_model_info(newname[:-2]) 
    319             # Know the table exists and isn't multiplicity so grab it directly 
    320             # Can't use _get_translation_table since that will return the 'bare' 
    321             # version. 
    322             translation = CONVERSION_TABLE.get(version, {})[newname][1] 
    323         else: 
    324             model_info = load_model_info(newname) 
    325             translation = _get_translation_table(model_info, version) 
    326         newpars = _hand_convert(newname, newpars, version) 
    327         newpars = _convert_pars(newpars, translation) 
    328         # TODO: Still not convinced this is the best check 
    329         if not model_info.structure_factor and version == (3,1,2): 
    330             newpars = _rescale_sld(model_info, newpars, 1e6) 
    331         newpars.setdefault('scale', 1.0) 
    332         newpars.setdefault('background', 0.0) 
    333         if use_underscore: 
    334             newpars = _pd_to_underscores(newpars) 
    335         name = newname 
     287    newname = _conversion_target(name) 
     288    if newname is None: 
     289        return name, pars 
     290    if ':' in newname:   # core_shell_ellipsoid:1 
     291        model_info = load_model_info(newname[:-2]) 
     292        # Know that the table exists and isn't multiplicity so grab it directly 
     293        # Can't use _get_translation_table since that will return the 'bare' 
     294        # version. 
     295        translation = CONVERSION_TABLE[newname][1] 
     296    else: 
     297        model_info = load_model_info(newname) 
     298        translation = _get_translation_table(model_info) 
     299    newpars = _hand_convert(newname, pars.copy()) 
     300    newpars = _convert_name(translation, newpars) 
     301    newpars = _convert_pars(newpars, translation) 
     302    if not model_info.structure_factor: 
     303        newpars = _rescale_sld(model_info, newpars, 1e6) 
     304    newpars.setdefault('scale', 1.0) 
     305    newpars.setdefault('background', 0.0) 
     306    if use_underscore: 
     307        newpars = _pd_to_underscores(newpars) 
    336308    return newname, newpars 
     309 
    337310 
    338311# ========= BACKWARD CONVERSION sasmodels => sasview 3.x =========== 
Note: See TracChangeset for help on using the changeset viewer.