Example #1
0
class AnalysisMixins_test(unittest.TestCase):
    """Path to sample Data File"""
    datadir = path.join(pth, "sample-data")

    def setUp(self):

        x_data = np.linspace(-10, 10, 101)
        y_data = 0.01 * x_data**2 + 0.3 * x_data - 2

        y_data *= np.random.normal(size=101, loc=1.0, scale=0.01)
        x_data += np.random.normal(size=101, scale=0.02)

        self.data = Data(x_data, y_data, column_headers=["X", "Y"])
        self.data.setas = "xy"

    def test_cuve_fit(self):
        for output, fmt in zip(["fit", "row", "full", "dict", "data"],
                               [tuple, np.ndarray, tuple, dict, Data]):
            res = self.data.curve_fit(fit, p0=[0.02, 0.2, 2], output=output)
            self.assertTrue(
                isinstance(res, fmt),
                "Failed to get expected output from curve_fit for {} (got {})".
                format(output, type(res)))

    def test_lmfit(self):
        for output, fmt in zip(["fit", "row", "full", "dict", "data"],
                               [tuple, np.ndarray, tuple, dict, Data]):
            res = self.data.lmfit(fit, p0=[0.02, 0.2, 2], output=output)
            self.assertTrue(
                isinstance(res, fmt),
                "Failed to get expected output from lmfit for {} (got {})".
                format(output, type(res)))

    def test_odr(self):
        for output, fmt in zip(["fit", "row", "full", "dict", "data"],
                               [tuple, np.ndarray, tuple, dict, Data]):
            res = self.data.odr(fit, p0=[0.02, 0.2, 2], output=output)
            self.assertTrue(
                isinstance(res, fmt),
                "Failed to get expected output from idr for {} (got {})".
                format(output, type(res)))

    def test_differential_evolution(self):
        for output, fmt in zip(["fit", "row", "full", "dict", "data"],
                               [tuple, np.ndarray, tuple, dict, Data]):
            res = self.data.differential_evolution(fit,
                                                   p0=[0.02, 0.2, 2],
                                                   output=output)
            self.assertTrue(
                isinstance(res, fmt),
                "Failed to get expected output from differential_evolution for {} (got {})"
                .format(output, type(res)))
from numpy import linspace, exp, random

# Make some data
x = linspace(0, 10.0, 101)
y = 2 + 4 * exp(-x / 1.7) + random.normal(scale=0.2, size=101)

d = Data(x, y, column_headers=["Time", "Signal"], setas="xy")

d.plot(fmt="ro")  # plot our data

func = lambda x, A, B, C: A + B * exp(-x / C)


# Do the fitting and plot the result
fit = d.differential_evolution(
    func, result=True, header="Fit", A=1, B=1, C=1, prefix="Model", residuals=True
)

# Reset labels
d.labels = []

# Make nice two panel plot layout
ax = d.subplot2grid((3, 1), (2, 0))
d.setas = "x..y"
d.plot(fmt="g+")
d.title = ""

ax = d.subplot2grid((3, 1), (0, 0), rowspan=2)
d.setas = "xyy"
d.plot(fmt=["r.", "b-"])
d.xticklabels = [[]]
Example #3
0
x = linspace(0, 10.0, 101)
y = 2 + 4 * exp(-x / 1.7) + random.normal(scale=0.2, size=101)

d = Data(x, y, column_headers=["Time", "Signal"], setas="xy")

d.plot(fmt="ro")  # plot our data

func = lambda x, A, B, C: A + B * exp(-x / C)


# Do the fitting and plot the result
fit = d.differential_evolution(
    func,
    result=True,
    header="Fit",
    A=1,
    B=1,
    C=1,
    prefix="Model",
    residuals=True,
)

# Reset labels
d.labels = []

# Make nice two panel plot layout
ax = d.subplot2grid((3, 1), (2, 0))
d.setas = "x..y"
d.plot(fmt="g+")
d.title = ""

ax = d.subplot2grid((3, 1), (0, 0), rowspan=2)