Skip to content

Commit 2b54742

Browse files
committed
Edge cases and error handling
1 parent 784b3df commit 2b54742

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

src/diffpy/morph/morphapp.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ def custom_error(self, msg):
188188
"a0+a1*x+a2*x^2+...a_n*x^n."
189189
"n is dependent on the number of values in the "
190190
"user-inputted comma-separated list. "
191+
"Repeated and trailing commas are removed before parsing."
191192
"When this option is enabled, --hshift is disabled. "
192193
"When n>1, --stretch is disabled. "
193194
"See online documentation for more information."
@@ -522,6 +523,7 @@ def single_morph(
522523

523524
# Squeeze
524525
squeeze_poly_deg = -1
526+
squeeze_dict_in = {}
525527
if opts.squeeze is not None:
526528
# Handles both list and csv input
527529
if (
@@ -537,12 +539,19 @@ def single_morph(
537539
):
538540
opts.squeeze = opts.squeeze[1:-1]
539541
squeeze_coeffs = opts.squeeze.strip().split(",")
540-
squeeze_dict_in = {}
541-
for idx, coeff in enumerate(squeeze_coeffs):
542-
squeeze_dict_in.update({f"a{idx}": float(coeff)})
543-
squeeze_poly_deg = len(squeeze_coeffs) - 1
542+
idx = 0
543+
for _, coeff in enumerate(squeeze_coeffs):
544+
if coeff.strip() != "":
545+
try:
546+
squeeze_dict_in.update({f"a{idx}": float(coeff)})
547+
idx += 1
548+
except ValueError:
549+
parser.error(f"{coeff} could not be converted to float.")
550+
squeeze_poly_deg = len(squeeze_dict_in.keys())
544551
chain.append(morphs.MorphSqueeze())
545552
config["squeeze"] = squeeze_dict_in
553+
# config["extrap_index_low"] = None
554+
# config["extrap_index_high"] = None
546555
refpars.append("squeeze")
547556
# Scale
548557
if opts.scale is not None:
@@ -679,10 +688,7 @@ def single_morph(
679688
morph_inputs.update({"hshift": hshift_in, "vshift": vshift_in})
680689
# More complex input morph parameters are only displayed conditionally
681690
if opts.squeeze is not None:
682-
squeeze_coeffs = opts.squeeze.strip().split(",")
683-
squeeze_dict = {}
684-
for idx, coeff in enumerate(squeeze_coeffs):
685-
squeeze_dict.update({f"a{idx}": float(coeff)})
691+
squeeze_dict = squeeze_dict_in.copy()
686692
for idx, _ in enumerate(squeeze_dict):
687693
morph_inputs.update({f"squeeze a{idx}": squeeze_dict[f"a{idx}"]})
688694
if pymorphs is not None:

tests/test_morphapp.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,18 @@ def test_parser_systemexits(self, setup_parser):
198198
with pytest.raises(SystemExit):
199199
multiple_targets(self.parser, opts, pargs, stdout_flag=False)
200200

201+
# Pass a non-float list to squeeze
202+
(opts, pargs) = self.parser.parse_args(
203+
[
204+
f"{nickel_PDF}",
205+
f"{nickel_PDF}",
206+
"--squeeze",
207+
"1,a,0",
208+
]
209+
)
210+
with pytest.raises(SystemExit):
211+
single_morph(self.parser, opts, pargs, stdout_flag=False)
212+
201213
def test_morphsequence(self, setup_morphsequence):
202214
# Parse arguments sorting by field
203215
(opts, pargs) = self.parser.parse_args(

tests/test_morphio.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ def test_morphsqueeze_outputs(self, setup, tmp_path):
163163
"--scale",
164164
"2",
165165
"--squeeze",
166-
"0,-0.001,-0.0001,0.0001",
166+
# Ignore duplicate commas and trailing commas
167+
# Handle spaces and non-spaces
168+
"0,, ,-0.001, -0.0001,0.0001,",
167169
"--stretch",
168170
"1",
169171
"--hshift",

0 commit comments

Comments
 (0)