Skip to content

Commit

Permalink
Working plot_fits method (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgkirsch committed Jul 19, 2021
1 parent 386225c commit 244fa3e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 43 deletions.
46 changes: 11 additions & 35 deletions gpfit/fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,21 @@ def residual(self, params):

def plot_fit(self):
"""Plots fit"""
cstrt, _ = fit(np.log(udata), np.log(wdata), K, fitclass)
uu = np.linspace(min(udata), max(udata), 1000)
WW = super().plot_fit_data(cstrt, uu)
stringlist = super().print_fit()
f, ax = plt.subplots()
udata = np.exp(self.xdata)
wdata = np.exp(self.ydata)
ax.plot(udata, wdata, "+r")
for ww in WW:
ax.plot(uu, ww)

xx = np.linspace(min(self.xdata), max(self.xdata), 10)
yy, _ = self.evaluate(xx, self.params)
uu = np.exp(xx)
ww = np.exp(yy)
ax.plot(uu, ww)

stringlist = self.print_fit()
ax.set_xlabel("u")
ax.set_ylabel("w")
ax.legend(["Data"] + stringlist, loc="best")

return f, ax


Expand Down Expand Up @@ -162,16 +166,6 @@ def print_fit(self):
print(print_string)
return string_list

#def plot_fit_data(self):
# """Return data for plotting"""
# (uvarkey,) = cstrt[0].left.varkeys
# A = [c.left.exps[0][uvarkey] for c in cstrt]
# B = np.log([c.left.cs[0] for c in cstrt])
# WW = []
# for k in range(K):
# WW += [np.exp(B[k])*uu**A[k]]
# return WW


class SoftmaxAffine(Fit):
"""Softmax Affine fit class"""
Expand Down Expand Up @@ -255,20 +249,6 @@ def print_fit(self):
print(print_string)
return ["".join(string_list)]

#def plot_fit_data(self):
# """Return data for plotting"""
# (wexps,) = cstrt[0].left.exps
# alpha = list(wexps.values())[0]
# (uvarkey,) = cstrt[0].right.varkeys
# A = [d[uvarkey]/alpha for d in cstrt[0].right.exps]
# B = np.log(cstrt[0].right.cs)/alpha

# ww = 0
# for k in range(K):
# ww += np.exp(alpha*B[k])*uu**(alpha*A[k])
# WW = [ww**(1/alpha)]
# return WW


class ImplicitSoftmaxAffine(Fit):
"""Implicit Softmax Affine fit class"""
Expand Down Expand Up @@ -351,7 +331,3 @@ def print_fit(self):
string_list[k] = print_string
print(print_string)
return ["".join(string_list)]

def plot_fit_data(self):
"""Return data for plotting"""
raise NotImplementedError
Binary file added gpfit/tests/plots/isma_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gpfit/tests/plots/ma_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gpfit/tests/plots/sma_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions gpfit/tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
from gpfit.tests import t_print_fit
TESTS += t_print_fit.TESTS

#from gpfit.tests import t_plot_fit
#TESTS += t_plot_fit.TESTS
from gpfit.tests import t_plot_fit
TESTS += t_plot_fit.TESTS

from gpfit.tests import t_examples
TESTS += t_examples.TESTS
Expand Down
33 changes: 27 additions & 6 deletions gpfit/tests/t_plot_fit.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
"unit tests for gpfit.print_fit module"
"unit tests for plot fit methods"
import unittest
import numpy as np
from gpfit.plot_fit import plot_fit_1d
from gpfit.fit import MaxAffine, SoftmaxAffine, ImplicitSoftmaxAffine

SEED = 33404


class TestPlotFit(unittest.TestCase):
"Unit tests for plot_fit_1d"
"Unit tests for plot_fit methods"

np.random.seed(SEED)
N = 51
u = np.logspace(0, np.log10(3), N)
w = (u**2 + 3)/(u + 1)**2

def test_plot_fit_1d(self):
plot_fit_1d(self.u, self.w, K=2, fitclass='SMA', plotspace="linear")
x = np.log(u)
y = np.log(w)
K = 2

def test_max_affine(self):
f = MaxAffine(self.x, self.y, self.K)
fig, _ = f.plot_fit()
fig.savefig("plots/ma_test.png")

def test_softmax_affine(self):
f = SoftmaxAffine(self.x, self.y, self.K)
f.plot_fit()
fig, _ = f.plot_fit()
fig.savefig("plots/sma_test.png")

def test_implicit_softmax_affine(self):
f = ImplicitSoftmaxAffine(self.x, self.y, self.K)
f.plot_fit()
fig, _ = f.plot_fit()
fig.savefig("plots/isma_test.png")


TESTS = [TestPlotFit]
Expand Down

0 comments on commit 244fa3e

Please sign in to comment.