import numpy as np
from scipy.interpolate import interp1d


def interpolate(data, new_x, xlog=False, ylog=False, kind='cubic', extrapolate=True):
    _x = data.x
    _y = data.y

    if xlog:
        _x = np.log10(_x)

    if ylog:
        _y = np.log10(_y)

    fill = 'extrapolate' if extrapolate else np.nan

    f = interp1d(_x, _y, kind=kind, fill_value=fill)

    ret_val = data.copy()

    if xlog:
        new_y = f(np.log10(new_x))
    else:
        new_y = f(new_x)

    if ylog:
        ret_val.set_data(x=new_x, y=10**new_y, y_err=None)
    else:
        ret_val.set_data(x=new_x, y=new_y, y_err=None)

    return ret_val
