Skip to content

Commit 80fd057

Browse files
authored
Revert "Reduce warnings (#252)"
This reverts commit dce54cf.
1 parent dce54cf commit 80fd057

File tree

5 files changed

+39
-87
lines changed

5 files changed

+39
-87
lines changed

news/reduce-warns.rst

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/diffpy/morph/morph_io.py

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

1919
import inspect
2020
import sys
21-
import warnings
2221
from pathlib import Path
2322

2423
import numpy
@@ -27,13 +26,6 @@
2726
from diffpy.morph import __save_morph_as__
2827

2928

30-
def custom_formatwarning(msg, *args, **kwargs):
31-
return f"{msg}\n"
32-
33-
34-
warnings.formatwarning = custom_formatwarning
35-
36-
3729
def single_morph_output(
3830
morph_inputs,
3931
morph_results,
@@ -406,34 +398,3 @@ def tabulate_results(multiple_morph_results):
406398
}
407399
)
408400
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: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,6 @@ def single_morph(
546546
# Squeeze
547547
squeeze_poly_deg = -1
548548
squeeze_dict_in = {}
549-
squeeze_morph = None
550549
if opts.squeeze is not None:
551550
# Handles both list and csv input
552551
if (
@@ -571,8 +570,7 @@ def single_morph(
571570
except ValueError:
572571
parser.error(f"{coeff} could not be converted to float.")
573572
squeeze_poly_deg = len(squeeze_dict_in.keys())
574-
squeeze_morph = morphs.MorphSqueeze()
575-
chain.append(squeeze_morph)
573+
chain.append(morphs.MorphSqueeze())
576574
config["squeeze"] = squeeze_dict_in
577575
# config["extrap_index_low"] = None
578576
# config["extrap_index_high"] = None
@@ -698,9 +696,6 @@ def single_morph(
698696
else:
699697
chain(x_morph, y_morph, x_target, y_target)
700698

701-
# THROW ANY WARNINGS HERE
702-
io.handle_warnings(squeeze_morph)
703-
704699
# Get Rw for the morph range
705700
rw = tools.getRw(chain)
706701
pcc = tools.get_pearson(chain)

src/diffpy/morph/morphs/morphsqueeze.py

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

4+
import warnings
5+
46
import numpy as np
57
from numpy.polynomial import Polynomial
68
from scipy.interpolate import CubicSpline
79

810
from diffpy.morph.morphs.morph import LABEL_GR, LABEL_RA, Morph
911

1012

13+
def custom_formatwarning(msg, *args, **kwargs):
14+
return f"{msg}\n"
15+
16+
17+
warnings.formatwarning = custom_formatwarning
18+
19+
1120
class MorphSqueeze(Morph):
1221
"""Squeeze the morph function.
1322
@@ -66,11 +75,6 @@ class MorphSqueeze(Morph):
6675
# extrap_index_high: first index after interpolation region
6776
extrap_index_low = None
6877
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)
7478

7579
def morph(self, x_morph, y_morph, x_target, y_target):
7680
"""Apply a polynomial to squeeze the morph function.
@@ -83,14 +87,34 @@ def morph(self, x_morph, y_morph, x_target, y_target):
8387
coeffs = [self.squeeze[f"a{i}"] for i in range(len(self.squeeze))]
8488
squeeze_polynomial = Polynomial(coeffs)
8589
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)
8890
self.y_morph_out = CubicSpline(x_squeezed, self.y_morph_in)(
8991
self.x_morph_in
9092
)
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]
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]
9395
self.extrap_index_low = low_extrap[-1] if low_extrap.size else None
9496
self.extrap_index_high = high_extrap[0] if high_extrap.size else None
95-
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+
)
96120
return self.xyallout

tests/test_morphsqueeze.py

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

5-
import diffpy.morph.morphpy as morphpy
65
from diffpy.morph.morphapp import create_option_parser, single_morph
76
from diffpy.morph.morphs.morphsqueeze import MorphSqueeze
87

@@ -124,19 +123,16 @@ def test_morphsqueeze_extrapolate(
124123
):
125124
x_morph = np.linspace(0, 10, 101)
126125
y_morph = np.sin(x_morph)
127-
x_target = x_morph.copy()
128-
y_target = y_morph.copy()
126+
x_target = x_morph
127+
y_target = y_morph
129128
morph = MorphSqueeze()
130129
morph.squeeze = squeeze_coeffs
131130
coeffs = [squeeze_coeffs[f"a{i}"] for i in range(len(squeeze_coeffs))]
132131
squeeze_polynomial = Polynomial(coeffs)
133132
x_squeezed = x_morph + squeeze_polynomial(x_morph)
134133
with pytest.warns() as w:
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,
134+
x_morph_actual, y_morph_actual, x_target_actual, y_target_actual = (
135+
morph(x_morph, y_morph, x_target, y_target)
140136
)
141137
assert len(w) == 1
142138
assert w[0].category is UserWarning
@@ -156,7 +152,6 @@ def test_morphsqueeze_extrapolate(
156152
",".join(map(str, coeffs)),
157153
f"{morph_file.as_posix()}",
158154
f"{target_file.as_posix()}",
159-
"--apply",
160155
"-n",
161156
]
162157
)

0 commit comments

Comments
 (0)