Skip to content

Commit a991072

Browse files
committed
func/test: replacing squeeze list for dictionary
1 parent 655676e commit a991072

File tree

4 files changed

+36
-31
lines changed

4 files changed

+36
-31
lines changed

src/diffpy/morph/morphs/morphsqueeze.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ def morph(self, x_morph, y_morph, x_target, y_target):
2828
2929
Configuration Variables
3030
-----------------------
31-
squeeze : list
32-
The polynomial coefficients [a0, a1, ..., an] for the squeeze
31+
squeeze : Dictionary
32+
The polynomial coefficients {a0, a1, ..., an} for the squeeze
3333
function where the polynomial would be of the form
3434
a0 + a1*x + a2*x^2 and so on. The order of the polynomial is
35-
determined by the length of the list.
35+
determined by the length of the dictionary.
3636
3737
Returns
3838
-------
@@ -46,7 +46,7 @@ def morph(self, x_morph, y_morph, x_target, y_target):
4646
Import the squeeze morph function:
4747
>>> from diffpy.morph.morphs.morphsqueeze import MorphSqueeze
4848
Provide initial guess for squeezing coefficients:
49-
>>> squeeze_coeff = [0.1, -0.01, 0.005]
49+
>>> squeeze_coeff = {"a0":0.1, "a1":-0.01, "a2":0.005}
5050
Run the squeeze morph given input morph array (x_morph, y_morph)
5151
and target array (x_target, y_target):
5252
>>> morph = MorphSqueeze()
@@ -62,7 +62,8 @@ def morph(self, x_morph, y_morph, x_target, y_target):
6262
"""
6363
Morph.morph(self, x_morph, y_morph, x_target, y_target)
6464

65-
squeeze_polynomial = Polynomial(self.squeeze)
65+
coeffs = [self.squeeze[f"a{i}"] for i in range(len(self.squeeze))]
66+
squeeze_polynomial = Polynomial(coeffs)
6667
x_squeezed = self.x_morph_in + squeeze_polynomial(self.x_morph_in)
6768
self.y_morph_out = CubicSpline(x_squeezed, self.y_morph_in)(
6869
self.x_morph_in

src/diffpy/morph/refine.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,9 @@ def _update_chain(self, pvals):
6363
updated[param] = value
6464
else:
6565
if param not in updated:
66-
updated[param] = {} if isinstance(subkey, str) else []
67-
if isinstance(updated[param], dict):
68-
updated[param][subkey] = value
69-
else:
70-
while len(updated[param]) <= subkey:
71-
updated[param].append(0.0)
72-
updated[param][subkey] = value
66+
updated[param] = {}
67+
updated[param][subkey] = value
68+
7369
# Apply the reconstructed grouped parameter back to config
7470
self.chain.config.update(updated)
7571
return
@@ -143,10 +139,6 @@ def refine(self, *args, **kw):
143139
for k, v in val.items():
144140
initial.append(v)
145141
self.flat_to_grouped[len(initial) - 1] = (p, k)
146-
elif isinstance(val, list):
147-
for i, v in enumerate(val):
148-
initial.append(v)
149-
self.flat_to_grouped[len(initial) - 1] = (p, i)
150142
else:
151143
initial.append(val)
152144
self.flat_to_grouped[len(initial) - 1] = (p, None)

tests/test_morph_func.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ def test_smear_with_morph_func():
104104

105105

106106
def test_squeeze_with_morph_func():
107-
squeeze_init = [0, -0.001, -0.0001, 0.0001]
107+
squeeze_init = {"a0": 0, "a1": -0.001, "a2": -0.0001, "a3": 0.0001}
108108
x_morph = np.linspace(0, 10, 101)
109109
y_morph = 2 * np.sin(
110110
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.001]
112+
expected_squeeze = {"a0": 0, "a1": 0.01, "a2": 0.0001, "a3": 0.001}
113113
expected_scale = 1 / 2
114114
x_target = np.linspace(0, 10, 101)
115115
y_target = np.sin(x_target)
@@ -121,7 +121,18 @@ def test_squeeze_with_morph_func():
121121
].xyallout
122122
assert np.allclose(x_morph_out, x_target_out)
123123
assert np.allclose(y_morph_out, y_target_out, atol=1e-6)
124-
assert np.allclose(expected_squeeze, morphed_cfg["squeeze"], atol=1e-6)
124+
assert np.allclose(
125+
expected_squeeze["a0"], morphed_cfg["squeeze"]["a0"], atol=1e-6
126+
)
127+
assert np.allclose(
128+
expected_squeeze["a1"], morphed_cfg["squeeze"]["a1"], atol=1e-6
129+
)
130+
assert np.allclose(
131+
expected_squeeze["a2"], morphed_cfg["squeeze"]["a2"], atol=1e-6
132+
)
133+
assert np.allclose(
134+
expected_squeeze["a3"], morphed_cfg["squeeze"]["a3"], atol=1e-6
135+
)
125136
assert np.allclose(expected_scale, morphed_cfg["scale"], atol=1e-6)
126137

127138

tests/test_morphsqueeze.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@
44

55
from diffpy.morph.morphs.morphsqueeze import MorphSqueeze
66

7-
squeeze_coeffs_list = [
8-
# The order of coefficients is [a0, a1, a2, ..., an]
7+
squeeze_coeffs_dic = [
8+
# The order of coefficients is {a0, a1, a2, ..., an}
99
# Negative cubic squeeze coefficients
10-
[-0.01, -0.0005, -0.0005, -1e-6],
10+
{"a0": -0.01, "a1": -0.0005, "a2": -0.0005, "a3": -1e-6},
1111
# Positive cubic squeeze coefficients
12-
[0.2, 0.01, 0.001, 0.0001],
12+
{"a0": 0.2, "a1": 0.01, "a2": 0.001, "a3": 0.0001},
1313
# Positive and negative cubic squeeze coefficients
14-
[0.2, -0.01, 0.002, -0.0001],
14+
{"a0": 0.2, "a1": -0.01, "a2": 0.002, "a3": -0.0001},
1515
# Quadratic squeeze coefficients
16-
[-0.2, 0.005, -0.0004],
16+
{"a0": -0.2, "a1": 0.005, "a2": -0.0004},
1717
# Linear squeeze coefficients
18-
[0.1, 0.3],
18+
{"a0": 0.1, "a1": 0.3},
1919
# 4th order squeeze coefficients
20-
[0.2, -0.01, 0.001, -0.001, 0.0001],
20+
{"a0": 0.2, "a1": -0.01, "a2": 0.001, "a3": -0.001, "a4": 0.0001},
2121
# Zeros and non-zeros, the full polynomial is applied
22-
[0, 0.03, 0, -0.0001],
22+
{"a0": 0, "a1": 0.03, "a2": 0, "a3": -0.0001},
2323
# Testing zeros, expect no squeezing
24-
[0, 0, 0, 0, 0, 0],
24+
{"a0": 0, "a1": 0, "a2": 0, "a3": 0, "a4": 0, "a5": 0},
2525
]
2626
morph_target_grids = [
2727
# UCs from issue 181: https://github.com/diffpy/diffpy.morph/issues/181
@@ -41,10 +41,11 @@
4141

4242

4343
@pytest.mark.parametrize("x_morph, x_target", morph_target_grids)
44-
@pytest.mark.parametrize("squeeze_coeffs", squeeze_coeffs_list)
44+
@pytest.mark.parametrize("squeeze_coeffs", squeeze_coeffs_dic)
4545
def test_morphsqueeze(x_morph, x_target, squeeze_coeffs):
4646
y_target = np.sin(x_target)
47-
squeeze_polynomial = Polynomial(squeeze_coeffs)
47+
coeffs = [squeeze_coeffs[f"a{i}"] for i in range(len(squeeze_coeffs))]
48+
squeeze_polynomial = Polynomial(coeffs)
4849
x_squeezed = x_morph + squeeze_polynomial(x_morph)
4950
y_morph = np.sin(x_squeezed)
5051
low_extrap = np.where(x_morph < x_squeezed[0])[0]

0 commit comments

Comments
 (0)