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)

[(12, 'numpy.linspace', 'np.linspace', (['(0)', '(5)', '(11)'], {}), True, 'import numpy as np\n'), (14, 'numpy.random.randint', 'np.random.randint', (['(10)', '(20)', '(2)'], {}), True, 'import numpy as np\n'), (15, 'numpy.concatenate', 'np.concatenate', (['(base, outliers)'], {}), True, 'import numpy as np\n'), (16, 'numpy.random.shuffle', 'np.random.shuffle', (['data'], {}), True, 'import numpy as np\n'), (34, 'numpy.histogram2d', 'np.histogram2d', (['x', 'y'], {'bins': '(50)'}), True, 'import numpy as np\n'), (37, 'matplotlib.pyplot.clf', 'plt.clf', ([], {}), True, 'import matplotlib.pyplot as plt\n'), (38, 'matplotlib.pyplot.imshow', 'plt.imshow', (['heatmap.T'], {'extent': 'extent', 'origin': '"""lower"""'}), True, 'import matplotlib.pyplot as plt\n'), (39, 'matplotlib.pyplot.show', 'plt.show', ([], {}), True, 'import matplotlib.pyplot as plt\n'), (41, 'numpy.histogram2d', 'np.histogram2d', (['x_filtered', 'y_filtered'], {'bins': '(50)'}), True, 'import numpy as np\n'), (44, 'matplotlib.pyplot.clf', 'plt.clf', ([], {}), True, 'import matplotlib.pyplot as plt\n'), (45, 'matplotlib.pyplot.imshow', 'plt.imshow', (['heatmap.T'], {'extent': 'extent', 'origin': '"""lower"""'}), True, 'import matplotlib.pyplot as plt\n'), (46, 'matplotlib.pyplot.show', 'plt.show', ([], {}), True, 'import matplotlib.pyplot as plt\n'), (51, 'matplotlib.pyplot.figure', 'plt.figure', ([], {}), True, 'import matplotlib.pyplot as plt\n'), (53, 'matplotlib.pyplot.plot', 'plt.plot', (['x_filtered', 'y_filtered', '"""red"""'], {'marker': '"""o"""'}), True, 'import matplotlib.pyplot as plt\n'), (54, 'matplotlib.pyplot.plot', 'plt.plot', (['x', 'y', '"""green"""'], {'marker': '"""o"""'}), True, 'import matplotlib.pyplot as plt\n'), (55, 'matplotlib.pyplot.subplots_adjust', 'plt.subplots_adjust', ([], {'bottom': '(0.25)'}), True, 'import matplotlib.pyplot as plt\n'), (56, 'matplotlib.pyplot.xlabel', 'plt.xlabel', (['"""x"""'], {}), True, 'import matplotlib.pyplot as plt\n'), (57, 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""y"""'], {}), True, 'import matplotlib.pyplot as plt\n'), (58, 'matplotlib.pyplot.title', 'plt.title', (['title'], {}), True, 'import matplotlib.pyplot as plt\n'), (59, 'matplotlib.pyplot.legend', 'plt.legend', (["['Filter', 'Raw']"], {}), True, 'import matplotlib.pyplot as plt\n'), (60, 'matplotlib.pyplot.show', 'plt.show', ([], {}), True, 'import matplotlib.pyplot as plt\n'), (22, 'numpy.array', 'np.array', (['[0]'], {}), True, 'import numpy as np\n'), (23, 'numpy.array', 'np.array', (['[0]'], {}), True, 'import numpy as np\n')]