Coverage for pesummary/core/plots/interpolate.py: 0.0%

30 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-05-02 08:42 +0000

1# Licensed under an MIT style license -- see LICENSE.md 

2 

3import numpy as np 

4from scipy.interpolate import interp1d 

5 

6__author__ = ["Charlie Hoy <charlie.hoy@ligo.org>"] 

7 

8 

9class Bounded_interp1d(object): 

10 """Return a bounded 1-D interpolant. Interpolating outside of the bounded 

11 domain simply returns 0. 

12 

13 Parameters 

14 ---------- 

15 x: np.array 

16 A 1-D array of real values. 

17 y: np.array 

18 A N-D array of real values. The length of y along the interpolation axis 

19 must be equal to the length of x. 

20 xlow: float, optional 

21 the lower bound of the bounded domain 

22 xhigh: float, optional 

23 the upper bound of the bounded domain 

24 **kwargs: dict, optional 

25 all kwargs passed to scipy.interpolate.interp1d 

26 """ 

27 def __init__(self, x, y, xlow=-np.inf, xhigh=np.inf, **kwargs): 

28 if xlow > np.min(x): 

29 self._xlow = xlow 

30 else: 

31 self._xlow = np.min(x) 

32 if xhigh < np.max(x): 

33 self._xhigh = xhigh 

34 else: 

35 self._xhigh = np.max(x) 

36 self._complex = np.iscomplexobj(y) 

37 self._interp_real = interp1d(x, np.real(y), **kwargs) 

38 if self._complex: 

39 self._interp_imaginary = interp1d(x, np.imag(y), **kwargs) 

40 

41 @property 

42 def xlow(self): 

43 return self._xlow 

44 

45 @property 

46 def xhigh(self): 

47 return self._xhigh 

48 

49 def __call__(self, pts): 

50 pts = np.atleast_1d(pts) 

51 result = np.zeros_like(pts) 

52 within_bounds = np.ones_like(pts, dtype='bool') 

53 within_bounds[(pts < self.xlow) | (pts > self.xhigh)] = False 

54 result[within_bounds] = self._interp_real(pts[within_bounds]) 

55 if self._complex: 

56 result[within_bounds] += 1j * self._interp_imaginary( 

57 pts[within_bounds] 

58 ) 

59 return result