Skip to content

Commit b293cec

Browse files
committed
improved utils docstrings
1 parent 4a42f8c commit b293cec

1 file changed

Lines changed: 26 additions & 9 deletions

File tree

pynumdiff/utils/utility.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,23 @@
66

77

88
def huber(x, M):
9-
"""Huber loss function, for outlier-robust applications, `see here <https://www.cvxpy.org/api_reference/cvxpy.atoms.elementwise.html#huber>`_"""
9+
"""Huber loss function, for outlier-robust applications,
10+
`see here <https://www.cvxpy.org/api_reference/cvxpy.atoms.elementwise.html#huber>`_
11+
12+
:param np.array[float] x: data points on which to evaluate the Huber function pointwise
13+
:param float M: where the loss turns from quadratic to linear
14+
:return: (np.array[float]) -- pointwise evaluations of the Huber function
15+
"""
1016
absx = np.abs(x)
1117
return np.where(absx <= M, 0.5*x**2, M*(absx - 0.5*M))
1218

1319
def huber_const(M):
1420
"""Scale that makes :code:`sum(huber())` interpolate :math:`\\sqrt{2}\\|\\cdot\\|_1` and :math:`\\frac{1}{2}\\|\\cdot\\|_2^2`,
15-
from https://jmlr.org/papers/volume14/aravkin13a/aravkin13a.pdf, with correction for missing sqrt"""
21+
from https://jmlr.org/papers/volume14/aravkin13a/aravkin13a.pdf, with correction for missing sqrt
22+
23+
:param float M: Huber parameter, where the function turns from quadratic to linear
24+
:return: (float) -- appropriate scale factor to normalize the Huber function
25+
"""
1626
a = 2*np.exp(-M**2 / 2)/M
1727
b = np.sqrt(2*np.pi)*(2*norm.cdf(M) - 1)
1828
return np.sqrt((2*a*(1 + M**2)/M**2 + b)/(a + b))
@@ -55,18 +65,27 @@ def estimate_integration_constant(x, x_hat, M=6):
5565

5666

5767
def mean_kernel(window_size):
58-
"""A uniform boxcar of total integral 1"""
68+
"""A uniform boxcar of total integral 1
69+
:param int window_size: the width of the return value
70+
:return: **kernel** (np.array[float]) -- samples of the kernel function
71+
"""
5972
return np.ones(window_size)/window_size
6073

6174
def gaussian_kernel(window_size):
62-
"""A truncated gaussian"""
75+
"""A truncated gaussian
76+
:param int window_size: the width of the return value
77+
:return: **kernel** (np.array[float]) -- samples of the kernel function
78+
"""
6379
sigma = window_size / 6.
6480
t = np.linspace(-2.7*sigma, 2.7*sigma, window_size)
6581
ker = 1/np.sqrt(2*np.pi*sigma**2) * np.exp(-(t**2)/(2*sigma**2)) # gaussian function itself
6682
return ker / np.sum(ker)
6783

6884
def friedrichs_kernel(window_size):
69-
"""A bump function"""
85+
"""A bump function
86+
:param int window_size: the width of the return value
87+
:return: **kernel** (np.array[float]) -- samples of the kernel function
88+
"""
7089
x = np.linspace(-0.999, 0.999, window_size)
7190
ker = np.exp(-1/(1-x**2))
7291
return ker / np.sum(ker)
@@ -104,8 +123,7 @@ def slide_function(func, x, dt, kernel, *args, stride=1, pass_weights=False, **k
104123
:param bool pass_weights: whether weights should be passed to func via update to kwargs
105124
:param dict kwargs: passed to func
106125
107-
:return: tuple[np.array, np.array] of\n
108-
- **x_hat** -- estimated (smoothed) x
126+
:return: - **x_hat** -- estimated (smoothed) x
109127
- **dxdt_hat** -- estimated derivative of x
110128
"""
111129
if len(kernel) % 2 == 0: raise ValueError("Kernel window size should be odd.")
@@ -148,8 +166,7 @@ def peakdet(x, delta, t=None):
148166
if it has the maximal value, and was preceded (to the left) by a value lower by delta.
149167
:param np.array[float] t: optional domain points where data comes from, to make indices into locations
150168
151-
:return: tuple[np.array, np.array] of\n
152-
- **maxtab** -- indices or locations (column 1) and values (column 2) of maxima
169+
:return: - **maxtab** -- indices or locations (column 1) and values (column 2) of maxima
153170
- **mintab** -- indices or locations (column 1) and values (column 2) of minima
154171
"""
155172
maxtab = []

0 commit comments

Comments
 (0)