Skip to content

Commit

Permalink
Adding fit_log
Browse files Browse the repository at this point in the history
  • Loading branch information
tdegeus committed Mar 7, 2022
1 parent 0446c70 commit c0e3f8b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
30 changes: 27 additions & 3 deletions GooseMPL/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ def fit_powerlaw(
with ``x`` replaced with the specified string.
:param extrapolate:
Plot the powerlaw on the full range of ``axis.get_xlim()``.
Plot the function on the full range of ``axis.get_xlim()``.
Instead of ``True``, one can specify plot options for the extrapolated line, e.g.
``..., extrapolate=dict(ls="--", c="r"), ...``.
Expand Down Expand Up @@ -1471,7 +1471,7 @@ def fit_exp(
with ``x`` replaced with the specified string.
:param extrapolate:
Plot the powerlaw on the full range of ``axis.get_xlim()``.
Plot the function on the full range of ``axis.get_xlim()``.
Instead of ``True'', one can specify plot options for the extrapolated line, e.g.
``..., extrapolate=dict(ls="--", c="r"), ...``.
Expand Down Expand Up @@ -1570,6 +1570,30 @@ def fit_exp(
return (prefactor, exponent, details)


def fit_log(
xdata: ArrayLike,
ydata: ArrayLike,
**kwargs,
) -> (float, float, dict):
r"""
Fit a logarithm :math:`y = a + b \ln x`.
See documentation of :py:func:`fit_linear`.
"""

xdata = np.array(xdata)
ydata = np.array(ydata)

i = xdata > 0
logx = np.log(xdata[i])
y = ydata[i]

j = np.isnan(logx)
logx = logx[~j]
y = y[~j]

return fit_linear(logx, y, **kwargs)


def fit_linear(
xdata: ArrayLike,
ydata: ArrayLike,
Expand Down Expand Up @@ -1605,7 +1629,7 @@ def fit_linear(
with ``x`` replaced with the specified string.
:param extrapolate:
Plot the powerlaw on the full range of ``axis.get_xlim()``.
Plot the function on the full range of ``axis.get_xlim()``.
Instead of ``True``, one can specify plot options for the extrapolated line, e.g.
``..., extrapolate=dict(ls="--", c="r"), ...``.
Expand Down
38 changes: 38 additions & 0 deletions test/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,44 @@ def test_exponent(self):
self.assertTrue(np.isclose(exponent, 3.4))


class Test_fit_log(unittest.TestCase):
"""
Fit an logarithmic function.
"""

def test_prefactor_exponent(self):

x = np.linspace(0, 1, 1000)[1:]
y = 1.2 + 3.4 * np.log(x)
offset, prefactor, _ = gplt.fit_log(x, y)
self.assertTrue(np.isclose(offset, 1.2))
self.assertTrue(np.isclose(prefactor, 3.4))

def test_prefactor_negative_prefactor(self):

x = np.linspace(0, 1, 1000)[1:]
y = 1.2 - 3.4 * np.log(x)
offset, prefactor, _ = gplt.fit_log(x, y)
self.assertTrue(np.isclose(offset, 1.2))
self.assertTrue(np.isclose(prefactor, -3.4))

def test_prefactor(self):

x = np.linspace(0, 1, 1000)[1:]
y = 1.2 + 3.4 * np.log(x)
offset, prefactor, _ = gplt.fit_log(x, y, prefactor=3.4)
self.assertTrue(np.isclose(offset, 1.2))
self.assertTrue(np.isclose(prefactor, 3.4))

def test_exponent(self):

x = np.linspace(0, 1, 1000)[1:]
y = 1.2 + 3.4 * np.log(x)
offset, prefactor, _ = gplt.fit_log(x, y, offset=1.2)
self.assertTrue(np.isclose(offset, 1.2))
self.assertTrue(np.isclose(prefactor, 3.4))


class Test_fit_linear(unittest.TestCase):
"""
Fit a linear.
Expand Down

0 comments on commit c0e3f8b

Please sign in to comment.