Skip to content

Commit

Permalink
Merge pull request #84 from SasView/ticket-1188-magnetic-colon
Browse files Browse the repository at this point in the history
Ticket 1188: rename magnetic parameters to not contain colon
  • Loading branch information
Wojciech Potrzebowski authored Oct 4, 2018
2 parents a1ec908 + e39202b commit 353e899
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 24 deletions.
14 changes: 7 additions & 7 deletions sasmodels/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ def _randomize_one(model_info, name, value):
return np.random.uniform(-0.5, 12)

# Limit magnetic SLDs to a smaller range, from zero to iron=5/A^2
if par.name.startswith('M0:'):
if par.name.endswith('_M0'):
return np.random.uniform(0, 5)

# Guess at the random length/radius/thickness. In practice, all models
Expand Down Expand Up @@ -537,7 +537,7 @@ def parlist(model_info, pars, is2d):
magnetic = False
magnetic_pars = []
for p in parameters.user_parameters(pars, is2d):
if any(p.id.startswith(x) for x in ('M0:', 'mtheta:', 'mphi:')):
if any(p.id.endswith(x) for x in ('_M0', '_mtheta', '_mphi')):
continue
if p.id.startswith('up:'):
magnetic_pars.append("%s=%s"%(p.id, pars.get(p.id, p.default)))
Expand All @@ -549,9 +549,9 @@ def parlist(model_info, pars, is2d):
nsigma=pars.get(p.id+"_pd_nsgima", 3.),
pdtype=pars.get(p.id+"_pd_type", 'gaussian'),
relative_pd=p.relative_pd,
M0=pars.get('M0:'+p.id, 0.),
mphi=pars.get('mphi:'+p.id, 0.),
mtheta=pars.get('mtheta:'+p.id, 0.),
M0=pars.get(p.id+'_M0', 0.),
mphi=pars.get(p.id+'_mphi', 0.),
mtheta=pars.get(p.id+'_mtheta', 0.),
)
lines.append(_format_par(p.name, **fields))
magnetic = magnetic or fields['M0'] != 0.
Expand Down Expand Up @@ -617,13 +617,13 @@ def suppress_magnetism(pars, suppress=True):
pars = pars.copy()
if suppress:
for p in pars:
if p.startswith("M0:"):
if p.endswith("_M0"):
pars[p] = 0
else:
any_mag = False
first_mag = None
for p in pars:
if p.startswith("M0:"):
if p.endswith("_M0"):
any_mag |= (pars[p] != 0)
if first_mag is None:
first_mag = p
Expand Down
18 changes: 18 additions & 0 deletions sasmodels/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,26 @@ def _conversion_target(model_name, version=(3, 1, 2)):
def _hand_convert(name, oldpars, version=(3, 1, 2)):
if version == (3, 1, 2):
oldpars = _hand_convert_3_1_2_to_4_1(name, oldpars)
if version < (4, 2, 0):
oldpars = _rename_magnetic_pars(oldpars)
return oldpars

def _rename_magnetic_pars(pars):
"""
Change from M0:par to par_M0, etc.
"""
keys = list(pars.items())
for k in keys:
if k.startswith('M0:'):
pars[k[3:]+'_M0'] = pars.pop(k)
elif k.startswith('mtheta:'):
pars[k[7:]+'_mtheta'] = pars.pop(k)
elif k.startswith('mphi:'):
pars[k[5:]+'_mphi'] = pars.pop(k)
elif k.startswith('up:'):
pars['up_'+k[3:]] = pars.pop(k)
return pars

def _hand_convert_3_1_2_to_4_1(name, oldpars):
if name == 'core_shell_parallelepiped':
# Make sure pd on rim parameters defaults to zero
Expand Down
37 changes: 21 additions & 16 deletions sasmodels/modelinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ def __init__(self, parameters):
for p in self.kernel_parameters)
self.is_asymmetric = any(p.name == 'psi' for p in self.kernel_parameters)
self.magnetism_index = [k for k, p in enumerate(self.call_parameters)
if p.id.startswith('M0:')]
if p.id.endswith('_M0')]

self.pd_1d = set(p.name for p in self.call_parameters
if p.polydisperse and p.type not in ('orientation', 'magnetic'))
Expand Down Expand Up @@ -585,21 +585,21 @@ def _get_call_parameters(self):
# Add the magnetic parameters to the end of the call parameter list.
if self.nmagnetic > 0:
full_list.extend([
Parameter('up:frac_i', '', 0., [0., 1.],
Parameter('up_frac_i', '', 0., [0., 1.],
'magnetic', 'fraction of spin up incident'),
Parameter('up:frac_f', '', 0., [0., 1.],
Parameter('up_frac_f', '', 0., [0., 1.],
'magnetic', 'fraction of spin up final'),
Parameter('up:angle', 'degrees', 0., [0., 360.],
Parameter('up_angle', 'degrees', 0., [0., 360.],
'magnetic', 'spin up angle'),
])
slds = [p for p in full_list if p.type == 'sld']
for p in slds:
full_list.extend([
Parameter('M0:'+p.id, '1e-6/Ang^2', 0., [-np.inf, np.inf],
Parameter(p.id+'_M0', '1e-6/Ang^2', 0., [-np.inf, np.inf],
'magnetic', 'magnetic amplitude for '+p.description),
Parameter('mtheta:'+p.id, 'degrees', 0., [-90., 90.],
Parameter(p.id+'_mtheta', 'degrees', 0., [-90., 90.],
'magnetic', 'magnetic latitude for '+p.description),
Parameter('mphi:'+p.id, 'degrees', 0., [-180., 180.],
Parameter(p.id+'_mphi', 'degrees', 0., [-180., 180.],
'magnetic', 'magnetic longitude for '+p.description),
])
#print("call parameters", full_list)
Expand Down Expand Up @@ -639,8 +639,8 @@ def user_parameters(self, pars, is2d=True):
early, and rerender the table when it is changed.
Parameters marked as sld will automatically have a set of associated
magnetic parameters (m0:p, mtheta:p, mphi:p), as well as polarization
information (up:theta, up:frac_i, up:frac_f).
magnetic parameters (p_M0, p_mtheta, p_mphi), as well as polarization
information (up_theta, up_frac_i, up_frac_f).
"""
# control parameters go first
control = [p for p in self.kernel_parameters if p.is_control]
Expand All @@ -667,9 +667,9 @@ def append_group(name):
"""add the named parameter, and related magnetic parameters if any"""
result.append(expanded_pars[name])
if is2d:
for tag in 'M0:', 'mtheta:', 'mphi:':
if tag+name in expanded_pars:
result.append(expanded_pars[tag+name])
for tag in '_M0', '_mtheta', '_mphi':
if name+tag in expanded_pars:
result.append(expanded_pars[name+tag])

# Gather the user parameters in order
result = control + self.COMMON
Expand Down Expand Up @@ -702,11 +702,11 @@ def append_group(name):
else:
append_group(p.id)

if is2d and 'up:angle' in expanded_pars:
if is2d and 'up_angle' in expanded_pars:
result.extend([
expanded_pars['up:frac_i'],
expanded_pars['up:frac_f'],
expanded_pars['up:angle'],
expanded_pars['up_frac_i'],
expanded_pars['up_frac_f'],
expanded_pars['up_angle'],
])

return result
Expand Down Expand Up @@ -1015,4 +1015,9 @@ def get_hidden_parameters(self, control):
for p in self.parameters.kernel_parameters
for k in range(control+1, p.length+1)
if p.length > 1)
for p in self.parameters.kernel_parameters:
if p.length > 1 and p.type == "sld":
for k in range(control+1, p.length+1):
base = p.id+str(k)
hidden.update((base+"_M0", base+"_mtheta", base+"_mphi"))
return hidden
8 changes: 7 additions & 1 deletion sasmodels/sasview_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,12 @@ def __init__(self, multiplicity=None):
hidden.add('background')
self._model_info.parameters.defaults['background'] = 0.

# Update the parameter lists to exclude any hidden parameters
self.magnetic_params = tuple(pname for pname in self.magnetic_params
if pname not in hidden)
self.orientation_params = tuple(pname for pname in self.orientation_params
if pname not in hidden)

self._persistency_dict = {}
self.params = collections.OrderedDict()
self.dispersion = collections.OrderedDict()
Expand Down Expand Up @@ -883,7 +889,7 @@ def test_old_name():
def magnetic_demo():
Model = _make_standard_model('sphere')
model = Model()
model.setParam('M0:sld', 8)
model.setParam('sld_M0', 8)
q = np.linspace(-0.35, 0.35, 500)
qx, qy = np.meshgrid(q, q)
result = model.calculate_Iq(qx.flatten(), qy.flatten())
Expand Down

0 comments on commit 353e899

Please sign in to comment.