| Total Complexity | 5 |
| Total Lines | 47 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | # Licensed under a 3-clause BSD style license - see LICENSE |
||
| 2 | """Utility functions used in mutis.""" |
||
| 3 | |||
| 4 | import numpy as np |
||
|
|
|||
| 5 | import scipy as sp |
||
| 6 | |||
| 7 | __all__ = ["get_grid", "memoize"] |
||
| 8 | |||
| 9 | |||
| 10 | def memoize(f): |
||
| 11 | """Decorator for recursive memoization.""" |
||
| 12 | memo = {} |
||
| 13 | |||
| 14 | def helper(a, b): |
||
| 15 | x = np.array([a, b], dtype="object") |
||
| 16 | y = bytes(x) |
||
| 17 | if y not in memo: |
||
| 18 | memo[y] = f(a, b) |
||
| 19 | return memo[y] |
||
| 20 | |||
| 21 | return helper |
||
| 22 | |||
| 23 | |||
| 24 | @memoize |
||
| 25 | def get_grid(x, y): |
||
| 26 | """Compute a meshgrid with memoization.""" |
||
| 27 | return np.meshgrid(x, y) |
||
| 28 | |||
| 29 | |||
| 30 | def interp_smooth_curve(x, y, s, N=None): |
||
| 31 | """Return an interpolated and smoothed curve of len N. A gaussian kernel of std = s (in units of x) is used for smoothing. |
||
| 32 | If N is None, an array of the same length is returned (but interpolated so it is equispaced). |
||
| 33 | """ |
||
| 34 | |||
| 35 | s = s/np.ptp(x)*len(x) |
||
| 36 | |||
| 37 | if N is None: |
||
| 38 | N = len(x) |
||
| 39 | |||
| 40 | spl = sp.interpolate.splrep(x, y) |
||
| 41 | xs = np.linspace(min(x), max(x), N) |
||
| 42 | ys = sp.interpolate.splev(xs, spl) |
||
| 43 | |||
| 44 | ys = sp.ndimage.gaussian_filter1d(ys, s) |
||
| 45 | |||
| 46 | return xs, ys |
||