Skip to content

Commit 82848b4

Browse files
committed
test/func: modifying refinement to allow lists and dicts and adding a test of refinement for squeeze and funcy
1 parent 40ecd00 commit 82848b4

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

src/diffpy/morph/morph_api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ def morph(
143143
- 'smear'
144144
- 'baselineslope'
145145
- 'qdamp'
146+
- 'squeeze'
147+
- 'parameters'
146148
147149
Returns
148150
-------

src/diffpy/morph/refine.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,20 @@ def __init__(self, chain, x_morph, y_morph, x_target, y_target):
5555
def _update_chain(self, pvals):
5656
"""Update the parameters in the chain."""
5757
updated = {}
58-
for idx, val in enumerate(pvals):
59-
p, subkey = self.flat_to_grouped[idx]
60-
if subkey is None:
61-
updated[p] = val
58+
for idx, value in enumerate(pvals):
59+
param, subkey = self.flat_to_grouped[idx]
60+
if subkey is None: # Scalar
61+
updated[param] = value
6262
else:
63-
if p not in updated:
64-
updated[p] = {} if isinstance(subkey, str) else []
65-
if isinstance(updated[p], dict):
66-
updated[p][subkey] = val
63+
if param not in updated:
64+
updated[param] = {} if isinstance(subkey, str) else []
65+
if isinstance(updated[param], dict):
66+
updated[param][subkey] = value
6767
else:
68-
while len(updated[p]) <= subkey:
69-
updated[p].append(0.0)
70-
updated[p][subkey] = val
71-
68+
while len(updated[param]) <= subkey:
69+
updated[param].append(0.0)
70+
updated[param][subkey] = value
71+
# Apply the reconstructed grouped parameter back to config
7272
self.chain.config.update(updated)
7373
return
7474

@@ -149,7 +149,6 @@ def refine(self, *args, **kw):
149149
initial.append(val)
150150
self.flat_to_grouped[len(initial) - 1] = (p, None)
151151

152-
# Perform least squares refinement
153152
sol, cov_sol, infodict, emesg, ier = leastsq(
154153
self.residual, initial, full_output=1
155154
)

tests/test_morph_func.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,22 @@ def test_squeeze_with_morph_func():
107107
squeeze_init = [0, -0.001, -0.0001, 0.0001]
108108
x_morph = np.linspace(0, 10, 101)
109109
y_morph = 2 * np.sin(
110-
x_morph + x_morph * (-0.01) - 0.0001 * x_morph**2 + 0.0002 * x_morph**3
110+
x_morph + x_morph * 0.01 + 0.0001 * x_morph**2 + 0.001 * x_morph**3
111111
)
112-
expected_squeeze = [0, -0.01, -0.0001, 0.0002]
112+
expected_squeeze = [0, 0.01, 0.0001, 0.001]
113113
expected_scale = 1 / 2
114-
x_target = x_morph.copy()
114+
x_target = np.linspace(0, 10, 101)
115115
y_target = np.sin(x_target)
116-
cfg = morph_default_config(scale=1.1, squeeze=squeeze_init) # off init
116+
cfg = morph_default_config(scale=1.1, squeeze=squeeze_init)
117117
morph_rv = morph(x_morph, y_morph, x_target, y_target, **cfg)
118118
morphed_cfg = morph_rv["morphed_config"]
119-
# verified they are morphable
120-
x1, y1, x0, y0 = morph_rv["morph_chain"].xyallout
121-
assert np.allclose(x0, x1)
122-
assert np.allclose(y0, y1, atol=1e-3) # numerical error -> 1e-4
123-
# verify morphed param
124-
assert np.allclose(expected_squeeze, morphed_cfg["squeeze"], atol=1e-4)
125-
assert np.allclose(expected_scale, morphed_cfg["scale"], atol=1e-4)
119+
x_morph_out, y_morph_out, x_target_out, y_target_out = morph_rv[
120+
"morph_chain"
121+
].xyallout
122+
assert np.allclose(x_morph_out, x_target_out)
123+
assert np.allclose(y_morph_out, y_target_out, atol=1e-6)
124+
assert np.allclose(expected_squeeze, morphed_cfg["squeeze"], atol=1e-6)
125+
assert np.allclose(expected_scale, morphed_cfg["scale"], atol=1e-6)
126126

127127

128128
def test_funcy_with_morph_func():
@@ -137,9 +137,11 @@ def linear_function(x, y, scale, offset):
137137
cfg["function"] = linear_function
138138
morph_rv = morph(x_morph, y_morph, x_target, y_target, **cfg)
139139
morphed_cfg = morph_rv["morphed_config"]
140-
x1, y1, x0, y0 = morph_rv["morph_chain"].xyallout
141-
assert np.allclose(x0, x1)
142-
assert np.allclose(y0, y1, atol=1e-6)
140+
x_morph_out, y_morph_out, x_target_out, y_target_out = morph_rv[
141+
"morph_chain"
142+
].xyallout
143+
assert np.allclose(x_morph_out, x_target_out)
144+
assert np.allclose(y_morph_out, y_target_out, atol=1e-6)
143145
fitted_parameters = morphed_cfg["parameters"]
144146
assert np.allclose(fitted_parameters["scale"], 2, atol=1e-6)
145147
assert np.allclose(fitted_parameters["offset"], 0.4, atol=1e-6)

0 commit comments

Comments
 (0)