import numpy as np
from scipy.signal import savgol_filter
import matplotlib.pyplot as plt
import MadDog

x = []
y = []


def generate():
    # Generate random data
    base = np.linspace(0, 5, 11)
    # base = np.random.randint(0, 10, 5)
    outliers = np.random.randint(10, 20, 2)
    data = np.concatenate((base, outliers))
    np.random.shuffle(data)
    return data


def fill_data():
    # Build random data
    return np.concatenate((np.array([0]), MadDog.find_outliers(generate()))), np.concatenate(
        (np.array([0]), MadDog.find_outliers(generate())))  # np.sin(x) + np.cos(x) + np.random.random(100)
    # np.linspace(0, 2*np.pi, 100)


def savitzky(x, y, ploy_nom):
    return savgol_filter(x, len(x) - 1, 10), savgol_filter(y, len(y) - 1, 10)


def map(x_filtered, y_filtered, x, y, title="title"):
    # Generate some test data

    heatmap, xedges, yedges = np.histogram2d(x, y, bins=50)
    extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

    plt.clf()
    plt.imshow(heatmap.T, extent=extent, origin='lower')
    plt.show()

    heatmap, xedges, yedges = np.histogram2d(x_filtered, y_filtered, bins=50)
    extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

    plt.clf()
    plt.imshow(heatmap.T, extent=extent, origin='lower')
    plt.show()


def show(x_filtered, y_filtered, x, y, title="Lorem ipsum"):
    # Plotting
    fig = plt.figure()
    ax = fig.subplots()
    plt.plot(x_filtered, y_filtered, 'red', marker="o")
    plt.plot(x, y, 'green', marker="o")
    plt.subplots_adjust(bottom=0.25)
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title(title)
    plt.legend(["Filter", "Raw"])
    plt.show()


# Generating the noisy signal
x, y = fill_data()
print(len(y))
# Savitzky-Golay filter
x_filtered, y_filtered = savitzky(x, y, 2)
print("X unfiltered>> ", x)
print("Y unfiltered>> ", y)
print("X filtered>> ", x_filtered)
print("Y filtered>> ", y_filtered)
show(x_filtered, y_filtered, x, y)

[((175, 196), 'numpy.linspace', 'np.linspace', (['(0)', '(5)', '(11)'], {}), '(0, 5, 11)\n', (186, 196), True, 'import numpy as np\n'), ((253, 281), 'numpy.random.randint', 'np.random.randint', (['(10)', '(20)', '(2)'], {}), '(10, 20, 2)\n', (270, 281), True, 'import numpy as np\n'), ((293, 325), 'numpy.concatenate', 'np.concatenate', (['(base, outliers)'], {}), '((base, outliers))\n', (307, 325), True, 'import numpy as np\n'), ((330, 353), 'numpy.random.shuffle', 'np.random.shuffle', (['data'], {}), '(data)\n', (347, 353), True, 'import numpy as np\n'), ((877, 906), 'numpy.histogram2d', 'np.histogram2d', (['x', 'y'], {'bins': '(50)'}), '(x, y, bins=50)\n', (891, 906), True, 'import numpy as np\n'), ((972, 981), 'matplotlib.pyplot.clf', 'plt.clf', ([], {}), '()\n', (979, 981), True, 'import matplotlib.pyplot as plt\n'), ((986, 1038), 'matplotlib.pyplot.imshow', 'plt.imshow', (['heatmap.T'], {'extent': 'extent', 'origin': '"""lower"""'}), "(heatmap.T, extent=extent, origin='lower')\n", (996, 1038), True, 'import matplotlib.pyplot as plt\n'), ((1043, 1053), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (1051, 1053), True, 'import matplotlib.pyplot as plt\n'), ((1085, 1132), 'numpy.histogram2d', 'np.histogram2d', (['x_filtered', 'y_filtered'], {'bins': '(50)'}), '(x_filtered, y_filtered, bins=50)\n', (1099, 1132), True, 'import numpy as np\n'), ((1198, 1207), 'matplotlib.pyplot.clf', 'plt.clf', ([], {}), '()\n', (1205, 1207), True, 'import matplotlib.pyplot as plt\n'), ((1212, 1264), 'matplotlib.pyplot.imshow', 'plt.imshow', (['heatmap.T'], {'extent': 'extent', 'origin': '"""lower"""'}), "(heatmap.T, extent=extent, origin='lower')\n", (1222, 1264), True, 'import matplotlib.pyplot as plt\n'), ((1269, 1279), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (1277, 1279), True, 'import matplotlib.pyplot as plt\n'), ((1368, 1380), 'matplotlib.pyplot.figure', 'plt.figure', ([], {}), '()\n', (1378, 1380), True, 'import matplotlib.pyplot as plt\n'), ((1409, 1460), 'matplotlib.pyplot.plot', 'plt.plot', (['x_filtered', 'y_filtered', '"""red"""'], {'marker': '"""o"""'}), "(x_filtered, y_filtered, 'red', marker='o')\n", (1417, 1460), True, 'import matplotlib.pyplot as plt\n'), ((1465, 1500), 'matplotlib.pyplot.plot', 'plt.plot', (['x', 'y', '"""green"""'], {'marker': '"""o"""'}), "(x, y, 'green', marker='o')\n", (1473, 1500), True, 'import matplotlib.pyplot as plt\n'), ((1505, 1537), 'matplotlib.pyplot.subplots_adjust', 'plt.subplots_adjust', ([], {'bottom': '(0.25)'}), '(bottom=0.25)\n', (1524, 1537), True, 'import matplotlib.pyplot as plt\n'), ((1542, 1557), 'matplotlib.pyplot.xlabel', 'plt.xlabel', (['"""x"""'], {}), "('x')\n", (1552, 1557), True, 'import matplotlib.pyplot as plt\n'), ((1562, 1577), 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""y"""'], {}), "('y')\n", (1572, 1577), True, 'import matplotlib.pyplot as plt\n'), ((1582, 1598), 'matplotlib.pyplot.title', 'plt.title', (['title'], {}), '(title)\n', (1591, 1598), True, 'import matplotlib.pyplot as plt\n'), ((1603, 1632), 'matplotlib.pyplot.legend', 'plt.legend', (["['Filter', 'Raw']"], {}), "(['Filter', 'Raw'])\n", (1613, 1632), True, 'import matplotlib.pyplot as plt\n'), ((1637, 1647), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (1645, 1647), True, 'import matplotlib.pyplot as plt\n'), ((440, 453), 'numpy.array', 'np.array', (['[0]'], {}), '([0])\n', (448, 453), True, 'import numpy as np\n'), ((516, 529), 'numpy.array', 'np.array', (['[0]'], {}), '([0])\n', (524, 529), True, 'import numpy as np\n')]