Skip to content

Commit 91fa347

Browse files
committed
fix: use a custom error to stop leatsq
1 parent 935dd8e commit 91fa347

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

src/diffpy/morph/log.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,13 @@ def set_verbosity(vb):
5050
return
5151

5252

53+
class MorphOptimizationError(Exception):
54+
"Custom error for morph optimization process."
55+
56+
def __init__(self, value):
57+
super().__init__(value)
58+
59+
pass
60+
61+
5362
# End of file

src/diffpy/morph/morphapp.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import diffpy.morph.refine as refine
2828
import diffpy.morph.tools as tools
2929
from diffpy.morph import __save_morph_as__
30+
from diffpy.morph.log import MorphOptimizationError
3031
from diffpy.morph.version import __version__
3132

3233

@@ -680,7 +681,7 @@ def single_morph(
680681
refiner.refine(*rptemp)
681682
# Adjust all parameters
682683
refiner.refine(*refpars)
683-
except ValueError as e:
684+
except (ValueError, MorphOptimizationError) as e:
684685
parser.custom_error(str(e))
685686
# Smear is not being refined, but baselineslope needs to refined to apply
686687
# smear

src/diffpy/morph/refine.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
# See LICENSE.txt for license information.
1313
#
1414
##############################################################################
15-
"""refine -- Refine a morph or morph chain
16-
"""
15+
"""refine -- Refine a morph or morph chain"""
1716

1817
from numpy import array, concatenate, dot, exp, ones_like
1918
from scipy.optimize import leastsq
2019
from scipy.stats import pearsonr
2120

21+
from diffpy.morph.log import MorphOptimizationError
22+
2223
# Map of scipy minimizer names to the method that uses them
2324

2425

@@ -84,7 +85,7 @@ def _residual(self, pvals):
8485
)
8586
rvec = _y_target - _y_morph
8687
if len(rvec) < len(pvals):
87-
raise ValueError(
88+
raise MorphOptimizationError(
8889
f"\nNumber of parameters (currently {len(pvals)}) cannot "
8990
"exceed the number of shared grid points "
9091
f"(currently {len(rvec)}). "

tests/test_refine.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import numpy
77
import pytest
88

9+
from diffpy.morph.log import MorphOptimizationError
910
from diffpy.morph.morph_helpers.transformpdftordf import TransformXtalPDFtoRDF
1011
from diffpy.morph.morph_helpers.transformrdftopdf import TransformXtalRDFtoPDF
1112
from diffpy.morph.morphs.morphchain import MorphChain
@@ -202,11 +203,11 @@ def test_refine_grid_bad(self, user_filesystem):
202203
"shared grid points."
203204
)
204205
with pytest.raises(
205-
ValueError,
206+
MorphOptimizationError,
206207
) as error:
207208
refiner.refine(*refpars)
208-
actual_error_message = str(error.value)
209-
assert actual_error_message == expected_error_message
209+
actual_error_message = str(error.value)
210+
assert actual_error_message == expected_error_message
210211

211212
# call from command line
212213
import subprocess

0 commit comments

Comments
 (0)