-
Notifications
You must be signed in to change notification settings - Fork 19
feat: add set_extrapolation_info function in morph.Morph
#255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| **Added:** | ||
|
|
||
| * Enable ``diffpy.morph`` to detect extrapolation. | ||
|
|
||
| **Changed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Deprecated:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Removed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Fixed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Security:** | ||
|
|
||
| * <news item> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -410,27 +410,30 @@ def tabulate_results(multiple_morph_results): | |
|
|
||
| def handle_warnings(squeeze_morph): | ||
| if squeeze_morph is not None: | ||
| eil = squeeze_morph.extrap_index_low | ||
| eih = squeeze_morph.extrap_index_high | ||
|
|
||
| if eil is not None or eih is not None: | ||
| if eih is None: | ||
| extrapolation_info = squeeze_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 or is_extrap_high: | ||
| if not is_extrap_high: | ||
| wmsg = ( | ||
| "Warning: points with grid value below " | ||
|
||
| f"{squeeze_morph.squeeze_cutoff_low} " | ||
| f"{cutoff_low} " | ||
| f"will be extrapolated." | ||
| ) | ||
| elif eil is None: | ||
| elif not is_extrap_low: | ||
| wmsg = ( | ||
| "Warning: points with grid value above " | ||
| f"{squeeze_morph.squeeze_cutoff_high} " | ||
| f"{cutoff_high} " | ||
| f"will be extrapolated." | ||
| ) | ||
| else: | ||
| wmsg = ( | ||
| "Warning: points with grid value below " | ||
| f"{squeeze_morph.squeeze_cutoff_low} and above " | ||
| f"{squeeze_morph.squeeze_cutoff_high} " | ||
| f"{cutoff_low} and above " | ||
| f"{cutoff_high} " | ||
| f"will be extrapolated." | ||
| ) | ||
| warnings.warn( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,8 +12,7 @@ | |
| # See LICENSE.txt for license information. | ||
| # | ||
| ############################################################################## | ||
| """Morph -- base class for defining a morph. | ||
| """ | ||
| """Morph -- base class for defining a morph.""" | ||
|
|
||
|
|
||
| LABEL_RA = "r (A)" # r-grid | ||
|
|
@@ -246,6 +245,23 @@ def plotOutputs(self, xylabels=True, **plotargs): | |
| ylabel(self.youtlabel) | ||
| return rv | ||
|
|
||
| def checkExtrapolation(self, x_true, x_extrapolate): | ||
|
||
| import numpy | ||
|
||
|
|
||
| cutoff_low = min(x_true) | ||
| cutoff_high = max(x_true) | ||
| low_extrap = numpy.where(x_extrapolate < cutoff_low)[0] | ||
| high_extrap = numpy.where(x_extrapolate > cutoff_high)[0] | ||
| is_extrap_low = False if len(low_extrap) == 0 else True | ||
| is_extrap_high = False if len(high_extrap) == 0 else True | ||
| extrapolation_info = { | ||
| "is_extrap_low": is_extrap_low, | ||
| "cutoff_low": cutoff_low, | ||
| "is_extrap_high": is_extrap_high, | ||
| "cutoff_high": cutoff_high, | ||
| } | ||
| return extrapolation_info | ||
|
|
||
| def __getattr__(self, name): | ||
| """Obtain the value from self.config, when normal lookup fails. | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,23 +46,27 @@ | |
| @pytest.mark.parametrize("squeeze_coeffs", squeeze_coeffs_dic) | ||
| def test_morphsqueeze(x_morph, x_target, squeeze_coeffs): | ||
| y_target = np.sin(x_target) | ||
| y_morph = np.sin(x_morph) | ||
| # expected output | ||
| coeffs = [squeeze_coeffs[f"a{i}"] for i in range(len(squeeze_coeffs))] | ||
| squeeze_polynomial = Polynomial(coeffs) | ||
| x_squeezed = x_morph + squeeze_polynomial(x_morph) | ||
| y_morph = np.sin(x_squeezed) | ||
| low_extrap = np.where(x_morph < x_squeezed[0])[0] | ||
| high_extrap = np.where(x_morph > x_squeezed[-1])[0] | ||
| extrap_index_low_expected = low_extrap[-1] if low_extrap.size else None | ||
| extrap_index_high_expected = high_extrap[0] if high_extrap.size else None | ||
| y_morph_expected = y_morph | ||
| x_morph_expected = x_morph | ||
| y_morph_expected = np.sin(x_morph) | ||
| x_target_expected = x_target | ||
| y_target_expected = y_target | ||
| # actual output | ||
| morph = MorphSqueeze() | ||
| y_morph = np.sin(x_squeezed) | ||
| morph.squeeze = squeeze_coeffs | ||
| x_morph_actual, y_morph_actual, x_target_actual, y_target_actual = morph( | ||
| x_morph, y_morph, x_target, y_target | ||
| ) | ||
| extrap_index_low = morph.extrap_index_low | ||
| extrap_index_high = morph.extrap_index_high | ||
|
|
||
| extrap_low = np.where(x_morph < min(x_squeezed))[0] | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code to compute
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there an argument to be made that this work could be done in |
||
| extrap_high = np.where(x_morph > max(x_squeezed))[0] | ||
| extrap_index_low = extrap_low[-1] if extrap_low.size else None | ||
| extrap_index_high = extrap_high[0] if extrap_high.size else None | ||
| if extrap_index_low is None: | ||
| extrap_index_low = 0 | ||
| elif extrap_index_high is None: | ||
|
|
@@ -82,11 +86,9 @@ def test_morphsqueeze(x_morph, x_target, squeeze_coeffs): | |
| y_morph_expected[extrap_index_high:], | ||
| atol=1e-3, | ||
| ) | ||
| assert morph.extrap_index_low == extrap_index_low_expected | ||
| assert morph.extrap_index_high == extrap_index_high_expected | ||
| assert np.allclose(x_morph_actual, x_morph_expected) | ||
| assert np.allclose(x_target_actual, x_target) | ||
| assert np.allclose(y_target_actual, y_target) | ||
| assert np.allclose(x_target_actual, x_target_expected) | ||
| assert np.allclose(y_target_actual, y_target_expected) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is getting better, but I still think it would be more readable with something like: