[3964f92] | 1 | """ |
---|
| 2 | Convert models to and from sasview. |
---|
| 3 | """ |
---|
[1b49bf8] | 4 | from __future__ import print_function, division |
---|
[256dfe1] | 5 | |
---|
[47fafe9] | 6 | import re |
---|
[c5ac2b2] | 7 | import math |
---|
[3964f92] | 8 | import warnings |
---|
[32e3c9b] | 9 | |
---|
| 10 | from .conversion_table import CONVERSION_TABLE |
---|
[aea515c] | 11 | from .core import load_model_info |
---|
[3964f92] | 12 | |
---|
| 13 | # List of models which SasView versions don't contain the explicit 'scale' argument. |
---|
| 14 | # When converting such a model, please update this list. |
---|
[310ddcb] | 15 | MODELS_WITHOUT_SCALE = [ |
---|
[3964f92] | 16 | 'teubner_strey', |
---|
| 17 | 'broad_peak', |
---|
| 18 | 'two_lorentzian', |
---|
[34d6cab] | 19 | "two_power_law", |
---|
[3964f92] | 20 | 'gauss_lorentz_gel', |
---|
| 21 | 'be_polyelectrolyte', |
---|
| 22 | 'correlation_length', |
---|
[74f7238] | 23 | 'fractal_core_shell', |
---|
[17bbadd] | 24 | 'binary_hard_sphere', |
---|
[bad8b12] | 25 | 'raspberry' |
---|
[3964f92] | 26 | ] |
---|
| 27 | |
---|
| 28 | # List of models which SasView versions don't contain the explicit 'background' argument. |
---|
| 29 | # When converting such a model, please update this list. |
---|
[310ddcb] | 30 | MODELS_WITHOUT_BACKGROUND = [ |
---|
[3964f92] | 31 | 'guinier', |
---|
| 32 | ] |
---|
| 33 | |
---|
[256dfe1] | 34 | MODELS_WITHOUT_VOLFRACTION = [ |
---|
| 35 | 'fractal', |
---|
| 36 | 'vesicle', |
---|
| 37 | 'multilayer_vesicle', |
---|
| 38 | ] |
---|
| 39 | |
---|
[f1765a2] | 40 | MAGNETIC_SASVIEW_MODELS = [ |
---|
| 41 | 'core_shell', |
---|
| 42 | 'core_multi_shell', |
---|
| 43 | 'cylinder', |
---|
| 44 | 'parallelepiped', |
---|
| 45 | 'sphere', |
---|
| 46 | ] |
---|
| 47 | |
---|
[256dfe1] | 48 | |
---|
[f247314] | 49 | # Convert new style names for polydispersity info to old style names |
---|
[3964f92] | 50 | PD_DOT = [ |
---|
| 51 | ("_pd", ".width"), |
---|
| 52 | ("_pd_n", ".npts"), |
---|
| 53 | ("_pd_nsigma", ".nsigmas"), |
---|
| 54 | ("_pd_type", ".type"), |
---|
| 55 | ] |
---|
[f247314] | 56 | |
---|
[aea515c] | 57 | def _rescale(par, scale): |
---|
| 58 | return [pk*scale for pk in par] if isinstance(par, list) else par*scale |
---|
[3964f92] | 59 | |
---|
[aea515c] | 60 | def _is_sld(model_info, id): |
---|
[3964f92] | 61 | """ |
---|
[aea515c] | 62 | Return True if parameter is a magnetic magnitude or SLD parameter. |
---|
[3964f92] | 63 | """ |
---|
[32e3c9b] | 64 | if id.startswith('M0:'): |
---|
| 65 | return True |
---|
[47fafe9] | 66 | if id.startswith('volfraction') or id.startswith('radius_effective'): |
---|
| 67 | return False |
---|
[aea515c] | 68 | if '_pd' in id or '.' in id: |
---|
[32e3c9b] | 69 | return False |
---|
[aea515c] | 70 | for p in model_info.parameters.call_parameters: |
---|
[32e3c9b] | 71 | if p.id == id: |
---|
| 72 | return p.type == 'sld' |
---|
| 73 | # check through kernel parameters in case it is a named as a vector |
---|
[aea515c] | 74 | for p in model_info.parameters.kernel_parameters: |
---|
[32e3c9b] | 75 | if p.id == id: |
---|
| 76 | return p.type == 'sld' |
---|
| 77 | raise ValueError("unknown parameter %r in conversion"%id) |
---|
| 78 | |
---|
[aea515c] | 79 | def _rescale_sld(model_info, pars, scale): |
---|
[3964f92] | 80 | """ |
---|
[aea515c] | 81 | rescale all sld parameters in the new model definition by *scale* so the |
---|
[3964f92] | 82 | numbers are nicer. Relies on the fact that all sld parameters in the |
---|
[aea515c] | 83 | new model definition end with sld. For backward conversion use |
---|
| 84 | *scale=1e-6*. For forward conversion use *scale=1e6*. |
---|
[3964f92] | 85 | """ |
---|
[aea515c] | 86 | return dict((id, (_rescale(v, scale) if _is_sld(model_info, id) else v)) |
---|
[32e3c9b] | 87 | for id, v in pars.items()) |
---|
[3964f92] | 88 | |
---|
[aea515c] | 89 | |
---|
| 90 | def _get_translation_table(model_info): |
---|
| 91 | _, translation = CONVERSION_TABLE.get(model_info.id, [None, {}]) |
---|
| 92 | translation = translation.copy() |
---|
| 93 | for p in model_info.parameters.kernel_parameters: |
---|
| 94 | if p.length > 1: |
---|
| 95 | newid = p.id |
---|
| 96 | oldid = translation.get(p.id, p.id) |
---|
| 97 | translation.pop(newid, None) |
---|
| 98 | for k in range(1, p.length+1): |
---|
| 99 | if newid+str(k) not in translation: |
---|
| 100 | translation[newid+str(k)] = oldid+str(k) |
---|
| 101 | # Remove control parameter from the result |
---|
| 102 | if model_info.control: |
---|
| 103 | translation[model_info.control] = "CONTROL" |
---|
| 104 | return translation |
---|
| 105 | |
---|
| 106 | # ========= FORWARD CONVERSION sasview 3.x => sasmodels =========== |
---|
| 107 | def _dot_pd_to_underscore_pd(par): |
---|
| 108 | if par.endswith(".width"): |
---|
| 109 | return par[:-6]+"_pd" |
---|
| 110 | elif par.endswith(".type"): |
---|
| 111 | return par[:-5]+"_pd_type" |
---|
| 112 | elif par.endswith(".nsigmas"): |
---|
| 113 | return par[:-8]+"_pd_nsigma" |
---|
| 114 | elif par.endswith(".npts"): |
---|
| 115 | return par[:-5]+"_pd_n" |
---|
| 116 | else: |
---|
| 117 | return par |
---|
| 118 | |
---|
| 119 | def _pd_to_underscores(pars): |
---|
| 120 | return dict((_dot_pd_to_underscore_pd(k), v) for k, v in pars.items()) |
---|
| 121 | |
---|
[88fafac] | 122 | def _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 = {} |
---|
[47fafe9] | 130 | i = 0 |
---|
| 131 | j = 0 |
---|
[88fafac] | 132 | for key_par, value_par in pars.iteritems(): |
---|
[47fafe9] | 133 | j += 1 |
---|
[88fafac] | 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 |
---|
[47fafe9] | 137 | i += 1 |
---|
[88fafac] | 138 | break |
---|
| 139 | elif re.search("background", key_par) or re.search("scale", key_par): |
---|
| 140 | new_pars[key_par] = value_par |
---|
[47fafe9] | 141 | i += 1 |
---|
[88fafac] | 142 | break |
---|
[47fafe9] | 143 | if i != j: |
---|
| 144 | new_pars[key_par] = value_par |
---|
| 145 | i += 1 |
---|
[88fafac] | 146 | return new_pars |
---|
| 147 | |
---|
[aea515c] | 148 | def _convert_pars(pars, mapping): |
---|
[3964f92] | 149 | """ |
---|
[aea515c] | 150 | Rename the parameters and any associated polydispersity attributes. |
---|
| 151 | """ |
---|
| 152 | newpars = pars.copy() |
---|
| 153 | for new, old in mapping.items(): |
---|
| 154 | if old == new: continue |
---|
| 155 | if old is None: continue |
---|
| 156 | for underscore, dot in PD_DOT: |
---|
| 157 | source = old+dot |
---|
| 158 | if source in newpars: |
---|
| 159 | if new is not None: |
---|
| 160 | target = new+dot |
---|
| 161 | else: |
---|
| 162 | target = None |
---|
| 163 | if source != target: |
---|
| 164 | if target: |
---|
| 165 | newpars[target] = pars[old+dot] |
---|
| 166 | del newpars[source] |
---|
| 167 | return newpars |
---|
[3964f92] | 168 | |
---|
[aea515c] | 169 | |
---|
| 170 | def _conversion_target(model_name): |
---|
[3964f92] | 171 | """ |
---|
[aea515c] | 172 | Find the sasmodel name which translates into the sasview name. |
---|
| 173 | |
---|
| 174 | Note: *CoreShellEllipsoidModel* translates into *core_shell_ellipsoid:1*. |
---|
| 175 | This is necessary since there is only one variant in sasmodels for the |
---|
| 176 | two variants in sasview. |
---|
| 177 | """ |
---|
| 178 | for sasmodels_name, [sasview_name, _] in CONVERSION_TABLE.items(): |
---|
| 179 | if sasview_name == model_name: |
---|
| 180 | return sasmodels_name |
---|
| 181 | return None |
---|
| 182 | |
---|
| 183 | |
---|
| 184 | def _hand_convert(name, oldpars): |
---|
| 185 | if name == 'core_shell_parallelepiped': |
---|
| 186 | # Make sure pd on rim parameters defaults to zero |
---|
| 187 | # ... probably not necessary. |
---|
| 188 | oldpars['rimA.width'] = 0.0 |
---|
| 189 | oldpars['rimB.width'] = 0.0 |
---|
| 190 | oldpars['rimC.width'] = 0.0 |
---|
[e85aa2e] | 191 | elif name == 'core_shell_ellipsoid:1': |
---|
[037aa3d] | 192 | # Reverse translation (from new to old), from core_shell_ellipsoid.c |
---|
| 193 | # equat_shell = equat_core + thick_shell |
---|
| 194 | # polar_core = equat_core * x_core |
---|
| 195 | # polar_shell = equat_core * x_core + thick_shell*x_polar_shell |
---|
| 196 | # Forward translation (from old to new), inverting reverse translation: |
---|
| 197 | # thick_shell = equat_shell - equat_core |
---|
| 198 | # x_core = polar_core / equat_core |
---|
| 199 | # x_polar_shell = (polar_shell - polar_core)/(equat_shell - equat_core) |
---|
| 200 | # Auto translation (old <=> new) happens after hand_convert |
---|
| 201 | # equat_shell <=> thick_shell |
---|
| 202 | # polar_core <=> x_core |
---|
| 203 | # polar_shell <=> x_polar_shell |
---|
| 204 | # So... |
---|
| 205 | equat_core, equat_shell = oldpars['equat_core'], oldpars['equat_shell'] |
---|
| 206 | polar_core, polar_shell = oldpars['polar_core'], oldpars['polar_shell'] |
---|
| 207 | oldpars['equat_shell'] = equat_shell - equat_core |
---|
| 208 | oldpars['polar_core'] = polar_core / equat_core |
---|
| 209 | oldpars['polar_shell'] = (polar_shell-polar_core)/(equat_shell-equat_core) |
---|
[aea515c] | 210 | elif name == 'hollow_cylinder': |
---|
| 211 | # now uses radius and thickness |
---|
| 212 | thickness = oldpars['radius'] - oldpars['core_radius'] |
---|
| 213 | oldpars['radius'] = thickness |
---|
[f8f5a37] | 214 | if 'radius.width' in oldpars: |
---|
| 215 | pd = oldpars['radius.width']*oldpars['radius']/thickness |
---|
| 216 | oldpars['radius.width'] = pd |
---|
[aea515c] | 217 | elif name == 'pearl_necklace': |
---|
| 218 | pass |
---|
| 219 | #_remove_pd(oldpars, 'num_pearls', name) |
---|
| 220 | #_remove_pd(oldpars, 'thick_string', name) |
---|
| 221 | elif name == 'polymer_micelle': |
---|
| 222 | if 'ndensity' in oldpars: |
---|
| 223 | oldpars['ndensity'] /= 1e15 |
---|
[8d06779] | 224 | if 'ndensity.lower' in oldpars: |
---|
[88fafac] | 225 | oldpars['ndensity.lower'] /= 1e15 |
---|
[8d06779] | 226 | if 'ndensity.upper' in oldpars: |
---|
[88fafac] | 227 | oldpars['ndensity.upper'] /= 1e15 |
---|
[aea515c] | 228 | elif name == 'rpa': |
---|
| 229 | # convert scattering lengths from femtometers to centimeters |
---|
| 230 | for p in "L1", "L2", "L3", "L4": |
---|
[88fafac] | 231 | if p in oldpars: |
---|
| 232 | oldpars[p] /= 1e-13 |
---|
[8d06779] | 233 | if p + ".lower" in oldpars: |
---|
[88fafac] | 234 | oldpars[p + ".lower"] /= 1e-13 |
---|
[8d06779] | 235 | if p + ".upper" in oldpars: |
---|
[88fafac] | 236 | oldpars[p + ".upper"] /= 1e-13 |
---|
[aea515c] | 237 | elif name == 'spherical_sld': |
---|
| 238 | oldpars["CONTROL"] += 1 |
---|
| 239 | elif name == 'teubner_strey': |
---|
| 240 | # basically undoing the entire Teubner-Strey calculations here. |
---|
| 241 | # drho = (sld_a - sld_b) |
---|
| 242 | # k = 2.0*math.pi*xi/d |
---|
| 243 | # a2 = (1.0 + k**2)**2 |
---|
| 244 | # c1 = 2.0 * xi**2 * (1.0 - k**2) |
---|
| 245 | # c2 = xi**4 |
---|
| 246 | # prefactor = 8.0*math.pi*phi*(1.0-phi)*drho**2*c2/xi |
---|
| 247 | # scale = 1e-4*prefactor |
---|
| 248 | # oldpars['scale'] = a2/scale |
---|
| 249 | # oldpars['c1'] = c1/scale |
---|
| 250 | # oldpars['c2'] = c2/scale |
---|
| 251 | |
---|
| 252 | # need xi, d, sld_a, sld_b, phi=volfraction_a |
---|
| 253 | # assume contrast is 1.0e-6, scale=1, background=0 |
---|
| 254 | sld_a, sld_b = 1.0, 0. |
---|
| 255 | drho = sld_a - sld_b |
---|
| 256 | |
---|
| 257 | # find xi |
---|
| 258 | p_scale = oldpars['scale'] |
---|
| 259 | p_c1 = oldpars['c1'] |
---|
| 260 | p_c2= oldpars['c2'] |
---|
[f8f5a37] | 261 | i_1 = 0.5*p_c1/p_c2 |
---|
| 262 | i_2 = math.sqrt(math.fabs(p_scale/p_c2)) |
---|
| 263 | i_3 = 2/(i_1 + i_2) |
---|
| 264 | xi = math.sqrt(math.fabs(i_3)) |
---|
[aea515c] | 265 | |
---|
| 266 | # find d from xi |
---|
[f8f5a37] | 267 | k = math.sqrt(math.fabs(1 - 0.5*p_c1/p_c2*xi**2)) |
---|
[aea515c] | 268 | d = 2*math.pi*xi/k |
---|
| 269 | |
---|
[999c87f] | 270 | # solve quadratic phi (1-phi) = xi/(1e-4 8 pi drho^2 c2) |
---|
[aea515c] | 271 | # favour volume fraction in [0, 0.5] |
---|
[999c87f] | 272 | c = xi / (1e-4 * 8.0 * math.pi * drho**2 * p_c2) |
---|
[aea515c] | 273 | phi = 0.5 - math.sqrt(0.25 - c) |
---|
| 274 | |
---|
| 275 | # scale sld_a by 1e-6 because the translator will scale it back |
---|
| 276 | oldpars.update(volfraction_a=phi, xi=xi, d=d, sld_a=sld_a*1e-6, |
---|
| 277 | sld_b=sld_b, scale=1.0) |
---|
| 278 | oldpars.pop('c1') |
---|
| 279 | oldpars.pop('c2') |
---|
| 280 | |
---|
| 281 | return oldpars |
---|
| 282 | |
---|
| 283 | def convert_model(name, pars, use_underscore=False): |
---|
| 284 | """ |
---|
| 285 | Convert model from old style parameter names to new style. |
---|
| 286 | """ |
---|
| 287 | newname = _conversion_target(name) |
---|
| 288 | if newname is None: |
---|
| 289 | return name, pars |
---|
[e85aa2e] | 290 | if ':' in newname: # core_shell_ellipsoid:1 |
---|
[aea515c] | 291 | model_info = load_model_info(newname[:-2]) |
---|
| 292 | # Know that the table exists and isn't multiplicity so grab it directly |
---|
[e85aa2e] | 293 | # Can't use _get_translation_table since that will return the 'bare' |
---|
| 294 | # version. |
---|
[f80f334] | 295 | translation = CONVERSION_TABLE[newname][1] |
---|
[aea515c] | 296 | else: |
---|
| 297 | model_info = load_model_info(newname) |
---|
| 298 | translation = _get_translation_table(model_info) |
---|
[f8f5a37] | 299 | newpars = _hand_convert(newname, pars.copy()) |
---|
| 300 | newpars = _convert_name(translation, newpars) |
---|
[aea515c] | 301 | newpars = _convert_pars(newpars, translation) |
---|
[47fafe9] | 302 | if not model_info.structure_factor: |
---|
| 303 | newpars = _rescale_sld(model_info, newpars, 1e6) |
---|
[aea515c] | 304 | newpars.setdefault('scale', 1.0) |
---|
| 305 | newpars.setdefault('background', 0.0) |
---|
| 306 | if use_underscore: |
---|
| 307 | newpars = _pd_to_underscores(newpars) |
---|
| 308 | return newname, newpars |
---|
| 309 | |
---|
| 310 | |
---|
| 311 | # ========= BACKWARD CONVERSION sasmodels => sasview 3.x =========== |
---|
[3964f92] | 312 | |
---|
| 313 | def _revert_pars(pars, mapping): |
---|
| 314 | """ |
---|
| 315 | Rename the parameters and any associated polydispersity attributes. |
---|
| 316 | """ |
---|
| 317 | newpars = pars.copy() |
---|
| 318 | |
---|
| 319 | for new, old in mapping.items(): |
---|
[40a87fa] | 320 | for underscore, dot in PD_DOT: |
---|
| 321 | if old and old+underscore == new+dot: |
---|
[3964f92] | 322 | continue |
---|
[40a87fa] | 323 | if new+underscore in newpars: |
---|
[3964f92] | 324 | if old is not None: |
---|
[40a87fa] | 325 | newpars[old+dot] = pars[new+underscore] |
---|
| 326 | del newpars[new+underscore] |
---|
[3964f92] | 327 | for k in list(newpars.keys()): |
---|
[40a87fa] | 328 | for underscore, dot in PD_DOT[1:]: # skip "" => "" |
---|
| 329 | if k.endswith(underscore): |
---|
| 330 | newpars[k[:-len(underscore)]+dot] = newpars[k] |
---|
[3964f92] | 331 | del newpars[k] |
---|
| 332 | return newpars |
---|
| 333 | |
---|
[f247314] | 334 | def revert_name(model_info): |
---|
[40a87fa] | 335 | oldname, _ = CONVERSION_TABLE.get(model_info.id, [None, {}]) |
---|
[f247314] | 336 | return oldname |
---|
| 337 | |
---|
[aea515c] | 338 | def _remove_pd(pars, key, name): |
---|
| 339 | """ |
---|
| 340 | Remove polydispersity from the parameter list. |
---|
| 341 | |
---|
| 342 | Note: operates in place |
---|
| 343 | """ |
---|
| 344 | # Bumps style parameter names |
---|
| 345 | width = pars.pop(key+".width", 0.0) |
---|
| 346 | n_points = pars.pop(key+".npts", 0) |
---|
| 347 | if width != 0.0 and n_points != 0: |
---|
| 348 | warnings.warn("parameter %s not polydisperse in sasview %s"%(key, name)) |
---|
| 349 | pars.pop(key+".nsigmas", None) |
---|
| 350 | pars.pop(key+".type", None) |
---|
| 351 | return pars |
---|
[256dfe1] | 352 | |
---|
| 353 | def _trim_vectors(model_info, pars, oldpars): |
---|
| 354 | _, translation = CONVERSION_TABLE.get(model_info.id, [None, {}]) |
---|
| 355 | for p in model_info.parameters.kernel_parameters: |
---|
| 356 | if p.length_control is not None: |
---|
| 357 | n = int(pars[p.length_control]) |
---|
| 358 | oldname = translation.get(p.id, p.id) |
---|
| 359 | for k in range(n+1, p.length+1): |
---|
| 360 | for _, old in PD_DOT: |
---|
| 361 | oldpars.pop(oldname+str(k)+old, None) |
---|
[f247314] | 362 | return oldpars |
---|
| 363 | |
---|
[17bbadd] | 364 | def revert_pars(model_info, pars): |
---|
[3964f92] | 365 | """ |
---|
| 366 | Convert model from new style parameter names to old style. |
---|
| 367 | """ |
---|
[6d6508e] | 368 | if model_info.composition is not None: |
---|
| 369 | composition_type, parts = model_info.composition |
---|
[f247314] | 370 | if composition_type == 'product': |
---|
[e85aa2e] | 371 | translation = _get_translation_table(parts[0]) |
---|
| 372 | # structure factor models include scale:scale_factor mapping |
---|
[6dc78e4] | 373 | translation.update(_get_translation_table(parts[1])) |
---|
[f247314] | 374 | else: |
---|
| 375 | raise NotImplementedError("cannot convert to sasview sum") |
---|
| 376 | else: |
---|
[256dfe1] | 377 | translation = _get_translation_table(model_info) |
---|
[aea515c] | 378 | oldpars = _revert_pars(_rescale_sld(model_info, pars, 1e-6), translation) |
---|
[6dc78e4] | 379 | oldpars = _trim_vectors(model_info, pars, oldpars) |
---|
[f247314] | 380 | |
---|
[bd49c79] | 381 | # Make sure the control parameter is an integer |
---|
| 382 | if "CONTROL" in oldpars: |
---|
| 383 | oldpars["CONTROL"] = int(oldpars["CONTROL"]) |
---|
[3964f92] | 384 | |
---|
| 385 | # Note: update compare.constrain_pars to match |
---|
[6d6508e] | 386 | name = model_info.id |
---|
| 387 | if name in MODELS_WITHOUT_SCALE or model_info.structure_factor: |
---|
[3964f92] | 388 | if oldpars.pop('scale', 1.0) != 1.0: |
---|
| 389 | warnings.warn("parameter scale not used in sasview %s"%name) |
---|
[6d6508e] | 390 | if name in MODELS_WITHOUT_BACKGROUND or model_info.structure_factor: |
---|
[3964f92] | 391 | if oldpars.pop('background', 0.0) != 0.0: |
---|
| 392 | warnings.warn("parameter background not used in sasview %s"%name) |
---|
[17bbadd] | 393 | |
---|
[f1765a2] | 394 | # Remove magnetic parameters from non-magnetic sasview models |
---|
| 395 | if name not in MAGNETIC_SASVIEW_MODELS: |
---|
[40a87fa] | 396 | oldpars = dict((k, v) for k, v in oldpars.items() if ':' not in k) |
---|
[228cbd3] | 397 | |
---|
[17bbadd] | 398 | # If it is a product model P*S, then check the individual forms for special |
---|
| 399 | # cases. Note: despite the structure factor alone not having scale or |
---|
| 400 | # background, the product model does, so this is below the test for |
---|
| 401 | # models without scale or background. |
---|
| 402 | namelist = name.split('*') if '*' in name else [name] |
---|
| 403 | for name in namelist: |
---|
[256dfe1] | 404 | if name in MODELS_WITHOUT_VOLFRACTION: |
---|
| 405 | del oldpars['volfraction'] |
---|
[f1765a2] | 406 | elif name == 'core_multi_shell': |
---|
| 407 | # kill extra shells |
---|
| 408 | for k in range(5, 11): |
---|
| 409 | oldpars.pop('sld_shell'+str(k), 0) |
---|
| 410 | oldpars.pop('thick_shell'+str(k), 0) |
---|
| 411 | oldpars.pop('mtheta:sld'+str(k), 0) |
---|
| 412 | oldpars.pop('mphi:sld'+str(k), 0) |
---|
| 413 | oldpars.pop('M0:sld'+str(k), 0) |
---|
| 414 | _remove_pd(oldpars, 'sld_shell'+str(k), 'sld') |
---|
| 415 | _remove_pd(oldpars, 'thick_shell'+str(k), 'thickness') |
---|
| 416 | elif name == 'core_shell_parallelepiped': |
---|
| 417 | _remove_pd(oldpars, 'rimA', name) |
---|
[1b49bf8] | 418 | _remove_pd(oldpars, 'rimB', name) |
---|
| 419 | _remove_pd(oldpars, 'rimC', name) |
---|
| 420 | elif name == 'hollow_cylinder': |
---|
| 421 | # now uses radius and thickness |
---|
[aea515c] | 422 | thickness = oldpars['core_radius'] |
---|
| 423 | oldpars['radius'] += thickness |
---|
| 424 | oldpars['radius.width'] *= thickness/oldpars['radius'] |
---|
| 425 | #elif name in ['mono_gauss_coil', 'poly_gauss_coil']: |
---|
| 426 | # del oldpars['i_zero'] |
---|
[f1765a2] | 427 | elif name == 'onion': |
---|
| 428 | oldpars.pop('n_shells', None) |
---|
[1b49bf8] | 429 | elif name == 'pearl_necklace': |
---|
| 430 | _remove_pd(oldpars, 'num_pearls', name) |
---|
| 431 | _remove_pd(oldpars, 'thick_string', name) |
---|
| 432 | elif name == 'polymer_micelle': |
---|
| 433 | if 'ndensity' in oldpars: |
---|
| 434 | oldpars['ndensity'] *= 1e15 |
---|
[17bbadd] | 435 | elif name == 'rpa': |
---|
| 436 | # convert scattering lengths from femtometers to centimeters |
---|
[8bd7b77] | 437 | for p in "L1", "L2", "L3", "L4": |
---|
[17bbadd] | 438 | if p in oldpars: oldpars[p] *= 1e-13 |
---|
[51ec7e8] | 439 | if pars['case_num'] < 2: |
---|
| 440 | for k in ("a", "b"): |
---|
| 441 | for p in ("L", "N", "Phi", "b", "v"): |
---|
| 442 | oldpars.pop(p+k, None) |
---|
| 443 | for k in "Kab,Kac,Kad,Kbc,Kbd".split(','): |
---|
| 444 | oldpars.pop(k, None) |
---|
| 445 | elif pars['case_num'] < 5: |
---|
| 446 | for k in ("a",): |
---|
| 447 | for p in ("L", "N", "Phi", "b", "v"): |
---|
| 448 | oldpars.pop(p+k, None) |
---|
| 449 | for k in "Kab,Kac,Kad".split(','): |
---|
| 450 | oldpars.pop(k, None) |
---|
[1b49bf8] | 451 | elif name == 'spherical_sld': |
---|
| 452 | oldpars["CONTROL"] -= 1 |
---|
| 453 | # remove polydispersity from shells |
---|
| 454 | for k in range(1, 11): |
---|
| 455 | _remove_pd(oldpars, 'thick_flat'+str(k), 'thickness') |
---|
| 456 | _remove_pd(oldpars, 'thick_inter'+str(k), 'interface') |
---|
| 457 | # remove extra shells |
---|
| 458 | for k in range(int(pars['n_shells']), 11): |
---|
| 459 | oldpars.pop('sld_flat'+str(k), 0) |
---|
| 460 | oldpars.pop('thick_flat'+str(k), 0) |
---|
| 461 | oldpars.pop('thick_inter'+str(k), 0) |
---|
| 462 | oldpars.pop('func_inter'+str(k), 0) |
---|
| 463 | oldpars.pop('nu_inter'+str(k), 0) |
---|
| 464 | elif name == 'stacked_disks': |
---|
| 465 | _remove_pd(oldpars, 'n_stacking', name) |
---|
| 466 | elif name == 'teubner_strey': |
---|
| 467 | # basically redoing the entire Teubner-Strey calculations here. |
---|
| 468 | volfraction = oldpars.pop('volfraction_a') |
---|
| 469 | xi = oldpars.pop('xi') |
---|
| 470 | d = oldpars.pop('d') |
---|
| 471 | sld_a = oldpars.pop('sld_a') |
---|
| 472 | sld_b = oldpars.pop('sld_b') |
---|
| 473 | drho = 1e6*(sld_a - sld_b) # conversion autoscaled these |
---|
| 474 | k = 2.0*math.pi*xi/d |
---|
| 475 | a2 = (1.0 + k**2)**2 |
---|
| 476 | c1 = 2.0 * xi**2 * (1.0 - k**2) |
---|
| 477 | c2 = xi**4 |
---|
| 478 | prefactor = 8.0*math.pi*volfraction*(1.0-volfraction)*drho**2*c2/xi |
---|
| 479 | scale = 1e-4*prefactor |
---|
| 480 | oldpars['scale'] = a2/scale |
---|
| 481 | oldpars['c1'] = c1/scale |
---|
| 482 | oldpars['c2'] = c2/scale |
---|
[17bbadd] | 483 | |
---|
[54bcd4a] | 484 | #print("convert from",list(sorted(pars))) |
---|
| 485 | #print("convert to",list(sorted(oldpars.items()))) |
---|
[17bbadd] | 486 | return oldpars |
---|
| 487 | |
---|
| 488 | def constrain_new_to_old(model_info, pars): |
---|
[3964f92] | 489 | """ |
---|
| 490 | Restrict parameter values to those that will match sasview. |
---|
| 491 | """ |
---|
[6d6508e] | 492 | name = model_info.id |
---|
[3964f92] | 493 | # Note: update convert.revert_model to match |
---|
[6d6508e] | 494 | if name in MODELS_WITHOUT_SCALE or model_info.structure_factor: |
---|
[3964f92] | 495 | pars['scale'] = 1 |
---|
[6d6508e] | 496 | if name in MODELS_WITHOUT_BACKGROUND or model_info.structure_factor: |
---|
[0a4628d] | 497 | pars['background'] = 0 |
---|
[667a6f2] | 498 | # sasview multiplies background by structure factor |
---|
| 499 | if '*' in name: |
---|
| 500 | pars['background'] = 0 |
---|
[9a66e65] | 501 | |
---|
[f1765a2] | 502 | # Shut off magnetism when comparing non-magnetic sasview models |
---|
| 503 | if name not in MAGNETIC_SASVIEW_MODELS: |
---|
| 504 | suppress_magnetism = False |
---|
| 505 | for key in pars.keys(): |
---|
| 506 | if key.startswith("M0:"): |
---|
[2c74c11] | 507 | suppress_magnetism = suppress_magnetism or (pars[key] != 0) |
---|
[f1765a2] | 508 | pars[key] = 0 |
---|
| 509 | if suppress_magnetism: |
---|
| 510 | warnings.warn("suppressing magnetism for comparison with sasview") |
---|
| 511 | |
---|
| 512 | # Shut off theta polydispersity since algorithm has changed |
---|
| 513 | if 'theta_pd_n' in pars: |
---|
| 514 | if pars['theta_pd_n'] != 0: |
---|
| 515 | warnings.warn("suppressing theta polydispersity for comparison with sasview") |
---|
| 516 | pars['theta_pd_n'] = 0 |
---|
| 517 | |
---|
[17bbadd] | 518 | # If it is a product model P*S, then check the individual forms for special |
---|
| 519 | # cases. Note: despite the structure factor alone not having scale or |
---|
| 520 | # background, the product model does, so this is below the test for |
---|
| 521 | # models without scale or background. |
---|
| 522 | namelist = name.split('*') if '*' in name else [name] |
---|
| 523 | for name in namelist: |
---|
[256dfe1] | 524 | if name in MODELS_WITHOUT_VOLFRACTION: |
---|
| 525 | pars['volfraction'] = 1 |
---|
[1b49bf8] | 526 | if name == 'core_multi_shell': |
---|
| 527 | pars['n'] = min(math.ceil(pars['n']), 4) |
---|
| 528 | elif name == 'gel_fit': |
---|
| 529 | pars['scale'] = 1 |
---|
[17bbadd] | 530 | elif name == 'line': |
---|
| 531 | pars['scale'] = 1 |
---|
| 532 | pars['background'] = 0 |
---|
[228cbd3] | 533 | elif name == 'mono_gauss_coil': |
---|
[aea515c] | 534 | pars['scale'] = 1 |
---|
[d119f34] | 535 | elif name == 'onion': |
---|
| 536 | pars['n_shells'] = math.ceil(pars['n_shells']) |
---|
[1b49bf8] | 537 | elif name == 'pearl_necklace': |
---|
| 538 | pars['string_thickness_pd_n'] = 0 |
---|
| 539 | pars['number_of_pearls_pd_n'] = 0 |
---|
| 540 | elif name == 'poly_gauss_coil': |
---|
[aea515c] | 541 | pars['scale'] = 1 |
---|
[1b49bf8] | 542 | elif name == 'rpa': |
---|
| 543 | pars['case_num'] = int(pars['case_num']) |
---|
[a0494e9] | 544 | elif name == 'spherical_sld': |
---|
[0dd4199] | 545 | pars['n_shells'] = math.ceil(pars['n_shells']) |
---|
[54bcd4a] | 546 | pars['n_steps'] = math.ceil(pars['n_steps']) |
---|
[51241113] | 547 | for k in range(1, 11): |
---|
[54bcd4a] | 548 | pars['shape%d'%k] = math.trunc(pars['shape%d'%k]+0.5) |
---|
[51241113] | 549 | for k in range(2, 11): |
---|
[54bcd4a] | 550 | pars['thickness%d_pd_n'%k] = 0 |
---|
| 551 | pars['interface%d_pd_n'%k] = 0 |
---|
[1b49bf8] | 552 | elif name == 'teubner_strey': |
---|
| 553 | pars['scale'] = 1 |
---|
[aea515c] | 554 | if pars['volfraction_a'] > 0.5: |
---|
| 555 | pars['volfraction_a'] = 1.0 - pars['volfraction_a'] |
---|
| 556 | elif name == 'unified_power_Rg': |
---|
| 557 | pars['level'] = int(pars['level']) |
---|
| 558 | |
---|
| 559 | def _check_one(name, seed=None): |
---|
| 560 | """ |
---|
| 561 | Generate a random set of parameters for *name*, and check that they can |
---|
| 562 | be converted back to SasView 3.x and forward again to sasmodels. Raises |
---|
| 563 | an error if the parameters are changed. |
---|
| 564 | """ |
---|
| 565 | from . import compare |
---|
| 566 | |
---|
| 567 | model_info = load_model_info(name) |
---|
| 568 | |
---|
| 569 | old_name = revert_name(model_info) |
---|
| 570 | if old_name is None: |
---|
| 571 | return |
---|
| 572 | |
---|
| 573 | pars = compare.get_pars(model_info, use_demo=False) |
---|
| 574 | pars = compare.randomize_pars(model_info, pars, seed=seed) |
---|
| 575 | if name == "teubner_strey": |
---|
| 576 | # T-S model is underconstrained, so fix the assumptions. |
---|
| 577 | pars['sld_a'], pars['sld_b'] = 1.0, 0.0 |
---|
| 578 | compare.constrain_pars(model_info, pars) |
---|
| 579 | constrain_new_to_old(model_info, pars) |
---|
| 580 | old_pars = revert_pars(model_info, pars) |
---|
| 581 | new_name, new_pars = convert_model(old_name, old_pars, use_underscore=True) |
---|
| 582 | if 1: |
---|
| 583 | print("==== %s in ====="%name) |
---|
| 584 | print(str(compare.parlist(model_info, pars, True))) |
---|
| 585 | print("==== %s ====="%old_name) |
---|
| 586 | for k, v in sorted(old_pars.items()): |
---|
| 587 | print(k, v) |
---|
| 588 | print("==== %s out ====="%new_name) |
---|
| 589 | print(str(compare.parlist(model_info, new_pars, True))) |
---|
| 590 | assert name==new_name, "%r != %r"%(name, new_name) |
---|
| 591 | for k, v in new_pars.items(): |
---|
| 592 | assert k in pars, "%s: %r appeared from conversion"%(name, k) |
---|
| 593 | if isinstance(v, float): |
---|
| 594 | assert abs(v-pars[k])<=abs(1e-12*v), "%s: %r %s != %s"%(name, k, v, pars[k]) |
---|
| 595 | else: |
---|
| 596 | assert v == pars[k], "%s: %r %s != %s"%(name, k, v, pars[k]) |
---|
| 597 | for k, v in pars.items(): |
---|
| 598 | assert k in pars, "%s: %r not converted"%(name, k) |
---|
[256dfe1] | 599 | |
---|
[aea515c] | 600 | def test_backward_forward(): |
---|
| 601 | from .core import list_models |
---|
| 602 | for name in list_models('all'): |
---|
| 603 | L = lambda: _check_one(name, seed=1) |
---|
| 604 | L.description = name |
---|
| 605 | yield L |
---|