Skip to content

Commit dce54cf

Browse files
authored
Reduce warnings (#252)
1 parent 9b44581 commit dce54cf

File tree

5 files changed

+87
-39
lines changed

5 files changed

+87
-39
lines changed

news/reduce-warns.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* <news item>
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* Only give user extrapolation warning after refinement.
20+
21+
**Security:**
22+
23+
* <news item>

src/diffpy/morph/morph_io.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import inspect
2020
import sys
21+
import warnings
2122
from pathlib import Path
2223

2324
import numpy
@@ -26,6 +27,13 @@
2627
from diffpy.morph import __save_morph_as__
2728

2829

30+
def custom_formatwarning(msg, *args, **kwargs):
31+
return f"{msg}\n"
32+
33+
34+
warnings.formatwarning = custom_formatwarning
35+
36+
2937
def single_morph_output(
3038
morph_inputs,
3139
morph_results,
@@ -398,3 +406,34 @@ def tabulate_results(multiple_morph_results):
398406
}
399407
)
400408
return tabulated_results
409+
410+
411+
def handle_warnings(squeeze_morph):
412+
if squeeze_morph is not None:
413+
eil = squeeze_morph.extrap_index_low
414+
eih = squeeze_morph.extrap_index_high
415+
416+
if eil is not None or eih is not None:
417+
if eih is None:
418+
wmsg = (
419+
"Warning: points with grid value below "
420+
f"{squeeze_morph.squeeze_cutoff_low} "
421+
f"will be extrapolated."
422+
)
423+
elif eil is None:
424+
wmsg = (
425+
"Warning: points with grid value above "
426+
f"{squeeze_morph.squeeze_cutoff_high} "
427+
f"will be extrapolated."
428+
)
429+
else:
430+
wmsg = (
431+
"Warning: points with grid value below "
432+
f"{squeeze_morph.squeeze_cutoff_low} and above "
433+
f"{squeeze_morph.squeeze_cutoff_high} "
434+
f"will be extrapolated."
435+
)
436+
warnings.warn(
437+
wmsg,
438+
UserWarning,
439+
)

src/diffpy/morph/morphapp.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ def single_morph(
546546
# Squeeze
547547
squeeze_poly_deg = -1
548548
squeeze_dict_in = {}
549+
squeeze_morph = None
549550
if opts.squeeze is not None:
550551
# Handles both list and csv input
551552
if (
@@ -570,7 +571,8 @@ def single_morph(
570571
except ValueError:
571572
parser.error(f"{coeff} could not be converted to float.")
572573
squeeze_poly_deg = len(squeeze_dict_in.keys())
573-
chain.append(morphs.MorphSqueeze())
574+
squeeze_morph = morphs.MorphSqueeze()
575+
chain.append(squeeze_morph)
574576
config["squeeze"] = squeeze_dict_in
575577
# config["extrap_index_low"] = None
576578
# config["extrap_index_high"] = None
@@ -696,6 +698,9 @@ def single_morph(
696698
else:
697699
chain(x_morph, y_morph, x_target, y_target)
698700

701+
# THROW ANY WARNINGS HERE
702+
io.handle_warnings(squeeze_morph)
703+
699704
# Get Rw for the morph range
700705
rw = tools.getRw(chain)
701706
pcc = tools.get_pearson(chain)

src/diffpy/morph/morphs/morphsqueeze.py

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
"""Class MorphSqueeze -- Apply a polynomial to squeeze the morph
22
function."""
33

4-
import warnings
5-
64
import numpy as np
75
from numpy.polynomial import Polynomial
86
from scipy.interpolate import CubicSpline
97

108
from diffpy.morph.morphs.morph import LABEL_GR, LABEL_RA, Morph
119

1210

13-
def custom_formatwarning(msg, *args, **kwargs):
14-
return f"{msg}\n"
15-
16-
17-
warnings.formatwarning = custom_formatwarning
18-
19-
2011
class MorphSqueeze(Morph):
2112
"""Squeeze the morph function.
2213
@@ -75,6 +66,11 @@ class MorphSqueeze(Morph):
7566
# extrap_index_high: first index after interpolation region
7667
extrap_index_low = None
7768
extrap_index_high = None
69+
squeeze_cutoff_low = None
70+
squeeze_cutoff_high = None
71+
72+
def __init__(self, config=None):
73+
super().__init__(config)
7874

7975
def morph(self, x_morph, y_morph, x_target, y_target):
8076
"""Apply a polynomial to squeeze the morph function.
@@ -87,34 +83,14 @@ def morph(self, x_morph, y_morph, x_target, y_target):
8783
coeffs = [self.squeeze[f"a{i}"] for i in range(len(self.squeeze))]
8884
squeeze_polynomial = Polynomial(coeffs)
8985
x_squeezed = self.x_morph_in + squeeze_polynomial(self.x_morph_in)
86+
self.squeeze_cutoff_low = min(x_squeezed)
87+
self.squeeze_cutoff_high = max(x_squeezed)
9088
self.y_morph_out = CubicSpline(x_squeezed, self.y_morph_in)(
9189
self.x_morph_in
9290
)
93-
low_extrap = np.where(self.x_morph_in < x_squeezed[0])[0]
94-
high_extrap = np.where(self.x_morph_in > x_squeezed[-1])[0]
91+
low_extrap = np.where(self.x_morph_in < self.squeeze_cutoff_low)[0]
92+
high_extrap = np.where(self.x_morph_in > self.squeeze_cutoff_high)[0]
9593
self.extrap_index_low = low_extrap[-1] if low_extrap.size else None
9694
self.extrap_index_high = high_extrap[0] if high_extrap.size else None
97-
below_extrap = min(x_morph) < min(x_squeezed)
98-
above_extrap = max(x_morph) > max(x_squeezed)
99-
if below_extrap or above_extrap:
100-
if not above_extrap:
101-
wmsg = (
102-
"Warning: points with grid value below "
103-
f"{min(x_squeezed)} will be extrapolated."
104-
)
105-
elif not below_extrap:
106-
wmsg = (
107-
"Warning: points with grid value above "
108-
f"{max(x_squeezed)} will be extrapolated."
109-
)
110-
else:
111-
wmsg = (
112-
"Warning: points with grid value below "
113-
f"{min(x_squeezed)} and above {max(x_squeezed)} will be "
114-
"extrapolated."
115-
)
116-
warnings.warn(
117-
wmsg,
118-
UserWarning,
119-
)
95+
12096
return self.xyallout

tests/test_morphsqueeze.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pytest
33
from numpy.polynomial import Polynomial
44

5+
import diffpy.morph.morphpy as morphpy
56
from diffpy.morph.morphapp import create_option_parser, single_morph
67
from diffpy.morph.morphs.morphsqueeze import MorphSqueeze
78

@@ -123,16 +124,19 @@ def test_morphsqueeze_extrapolate(
123124
):
124125
x_morph = np.linspace(0, 10, 101)
125126
y_morph = np.sin(x_morph)
126-
x_target = x_morph
127-
y_target = y_morph
127+
x_target = x_morph.copy()
128+
y_target = y_morph.copy()
128129
morph = MorphSqueeze()
129130
morph.squeeze = squeeze_coeffs
130131
coeffs = [squeeze_coeffs[f"a{i}"] for i in range(len(squeeze_coeffs))]
131132
squeeze_polynomial = Polynomial(coeffs)
132133
x_squeezed = x_morph + squeeze_polynomial(x_morph)
133134
with pytest.warns() as w:
134-
x_morph_actual, y_morph_actual, x_target_actual, y_target_actual = (
135-
morph(x_morph, y_morph, x_target, y_target)
135+
morphpy.morph_arrays(
136+
np.array([x_morph, y_morph]).T,
137+
np.array([x_target, y_target]).T,
138+
squeeze=coeffs,
139+
apply=True,
136140
)
137141
assert len(w) == 1
138142
assert w[0].category is UserWarning
@@ -152,6 +156,7 @@ def test_morphsqueeze_extrapolate(
152156
",".join(map(str, coeffs)),
153157
f"{morph_file.as_posix()}",
154158
f"{target_file.as_posix()}",
159+
"--apply",
155160
"-n",
156161
]
157162
)

0 commit comments

Comments
 (0)