Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions news/extrap_fix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* <news item>

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* Applying a function with no extrapolation no longer produces an error.

**Security:**

* <news item>
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ authors = [
maintainers = [
{ name="Simon J.L. Billinge group", email="simon.billinge@gmail.com" },
]
description = "Python package for manipulating and comparing PDF profiles"
description = "Python package for manipulating and comparing 1D signals."
keywords = ['diffpy', 'pdf', 'data interpretation']
readme = "README.rst"
requires-python = ">=3.11, <3.14"
Expand Down
63 changes: 32 additions & 31 deletions src/diffpy/morph/morph_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,38 +501,39 @@ def tabulate_results(multiple_morph_results):
def handle_extrapolation_warnings(morph):
if morph is not None:
extrapolation_info = morph.extrapolation_info
is_extrap_low = extrapolation_info["is_extrap_low"]
is_extrap_high = extrapolation_info["is_extrap_high"]
cutoff_low = extrapolation_info["cutoff_low"]
cutoff_high = extrapolation_info["cutoff_high"]

if is_extrap_low and is_extrap_high:
wmsg = (
"Warning: points with grid value below "
f"{cutoff_low} and above "
f"{cutoff_high} "
f"are extrapolated."
)
elif is_extrap_low:
wmsg = (
"Warning: points with grid value below "
f"{cutoff_low} "
f"are extrapolated."
)
elif is_extrap_high:
wmsg = (
"Warning: points with grid value above "
f"{cutoff_high} "
f"are extrapolated."
)
else:
wmsg = None
if extrapolation_info is not None:
is_extrap_low = extrapolation_info["is_extrap_low"]
is_extrap_high = extrapolation_info["is_extrap_high"]
cutoff_low = extrapolation_info["cutoff_low"]
cutoff_high = extrapolation_info["cutoff_high"]

if is_extrap_low and is_extrap_high:
wmsg = (
"Warning: points with grid value below "
f"{cutoff_low} and above "
f"{cutoff_high} "
f"are extrapolated."
)
elif is_extrap_low:
wmsg = (
"Warning: points with grid value below "
f"{cutoff_low} "
f"are extrapolated."
)
elif is_extrap_high:
wmsg = (
"Warning: points with grid value above "
f"{cutoff_high} "
f"are extrapolated."
)
else:
wmsg = None

if wmsg:
warnings.warn(
wmsg,
UserWarning,
)
if wmsg:
warnings.warn(
wmsg,
UserWarning,
)


def handle_check_increase_warning(squeeze_morph):
Expand Down
1 change: 1 addition & 0 deletions src/diffpy/morph/morphs/morph.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def __init__(self, config=None):
All configuration variables.
"""
# declare empty attributes
self.extrapolation_info = None
if config is None:
config = {}
self.x_morph_in = None
Expand Down
33 changes: 33 additions & 0 deletions tests/test_morphshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,36 @@ def test_morphshift_extrapolate(user_filesystem, capsys, hshift, wmsg_gen):
)
with pytest.warns(UserWarning, match=expected_wmsg):
single_morph(parser, opts, pargs, stdout_flag=False)


def test_morphshift_no_warning(user_filesystem):
# Apply a shift with no extrapolation
# There should be no warning or errors produced
x_morph = numpy.linspace(0, 10, 101)
y_morph = numpy.sin(x_morph)
x_target = x_morph.copy()
y_target = y_morph.copy()
morphpy.morph_arrays(
numpy.array([x_morph, y_morph]).T,
numpy.array([x_target, y_target]).T,
hshift=0,
apply=True,
)

# CLI test
morph_file, target_file = create_morph_data_file(
user_filesystem / "cwd_dir", x_morph, y_morph, x_target, y_target
)

parser = create_option_parser()
(opts, pargs) = parser.parse_args(
[
"--scale=1",
"--hshift=0",
f"{morph_file.as_posix()}",
f"{target_file.as_posix()}",
"--apply",
"-n",
]
)
single_morph(parser, opts, pargs, stdout_flag=False)
38 changes: 38 additions & 0 deletions tests/test_morphsqueeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,44 @@ def test_morphsqueeze_extrapolate(user_filesystem, squeeze_coeffs, wmsg_gen):
single_morph(parser, opts, pargs, stdout_flag=False)


def test_morphsqueeze_no_warning(user_filesystem):
# Apply a squeeze with no extrapolation
# There should be no warning or errors produced
squeeze_coeffs = {"a0": 0, "a1": 0}
x_morph = np.linspace(0, 10, 101)
y_morph = np.sin(x_morph)
x_target = x_morph.copy()
y_target = y_morph.copy()
morph = MorphSqueeze()
morph.squeeze = squeeze_coeffs
coeffs = [squeeze_coeffs[f"a{i}"] for i in range(len(squeeze_coeffs))]
morphpy.morph_arrays(
np.array([x_morph, y_morph]).T,
np.array([x_target, y_target]).T,
squeeze=coeffs,
apply=True,
)

# CLI test
morph_file, target_file = create_morph_data_file(
user_filesystem / "cwd_dir", x_morph, y_morph, x_target, y_target
)

parser = create_option_parser()
(opts, pargs) = parser.parse_args(
[
"--scale=1",
"--squeeze",
",".join(map(str, coeffs)),
f"{morph_file.as_posix()}",
f"{target_file.as_posix()}",
"--apply",
"-n",
]
)
single_morph(parser, opts, pargs, stdout_flag=False)


def test_non_unique_grid():
# Test giving morphsqueeze a non-unique grid
# Expect it to return a unique grid
Expand Down
41 changes: 37 additions & 4 deletions tests/test_morphstretch.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ def test_morphshift_extrapolate(user_filesystem, stretch, wmsg_gen):
y_morph = numpy.sin(x_morph)
x_target = x_morph.copy()
y_target = y_morph.copy()
with pytest.warns() as w:
with pytest.warns() as warning:
morphpy.morph_arrays(
numpy.array([x_morph, y_morph]).T,
numpy.array([x_target, y_target]).T,
stretch=stretch,
apply=True,
)
assert len(w) == 1
assert w[0].category is UserWarning
actual_wmsg = str(w[0].message)
assert len(warning) == 1
assert warning[0].category is UserWarning
actual_wmsg = str(warning[0].message)
expected_wmsg = wmsg_gen([min(x_morph), max(x_morph)])
assert actual_wmsg == expected_wmsg

Expand All @@ -131,3 +131,36 @@ def test_morphshift_extrapolate(user_filesystem, stretch, wmsg_gen):
)
with pytest.warns(UserWarning, match=expected_wmsg):
single_morph(parser, opts, pargs, stdout_flag=False)


def test_morphshift_no_warning(user_filesystem):
# Apply a stretch with no extrapolation
# There should be no warning or errors produced
x_morph = numpy.linspace(1, 10, 101)
y_morph = numpy.sin(x_morph)
x_target = x_morph.copy()
y_target = y_morph.copy()
morphpy.morph_arrays(
numpy.array([x_morph, y_morph]).T,
numpy.array([x_target, y_target]).T,
stretch=0,
apply=True,
)

# CLI test
morph_file, target_file = create_morph_data_file(
user_filesystem / "cwd_dir", x_morph, y_morph, x_target, y_target
)

parser = create_option_parser()
(opts, pargs) = parser.parse_args(
[
"--scale=1",
"--stretch=0",
f"{morph_file.as_posix()}",
f"{target_file.as_posix()}",
"--apply",
"-n",
]
)
single_morph(parser, opts, pargs, stdout_flag=False)
Loading