11"""Class MorphSqueeze -- Apply a polynomial to squeeze the morph
22function."""
33
4+ import warnings
5+
46import numpy as np
57from numpy .polynomial import Polynomial
68from scipy .interpolate import CubicSpline
79
810from 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+
1120class 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
0 commit comments