<jupyter_start><jupyter_text>PROPHET - Modell<jupyter_code>from fbprophet import Prophet
from prophet.plot import plot_plotly, plot_components_plotly
import pandas as pd
import numpy as np
import plotly
import plotly.express as px
import plotly.graph_objects as go
from statsmodels.tsa.statespace.sarimax import SARIMAX
from sklearn.metrics import mean_squared_error
from math import sqrt
from scipy import integrate
from scipy.signal import butter, lfilter, sosfiltfilt, filtfilt
import warnings
warnings.filterwarnings("ignore")<jupyter_output><empty_output><jupyter_text>Custom plotly template.<jupyter_code>custom_template = {
    "layout": go.Layout(
        font={
            "family": "Arial",
            "size": 14,
            "color": "#707070",
        },
        title={
            "font": {
                "family": "Arial",
                "size": 18,
                "color": "#1f1f1f",
            },
        },
        plot_bgcolor="#ffffff",
        paper_bgcolor="#ffffff",
        colorway=px.colors.qualitative.G10,
    )
}

def format_title(title, subtitle=None, subtitle_font_size=14):
    title = f'<b>{title}</b>'
    if not subtitle:
        return title
    subtitle = f'<span style="font-size: {subtitle_font_size}px;">{subtitle}</span>'
    return f'{title}<br>{subtitle}'<jupyter_output><empty_output><jupyter_text>Please fill in the path to the preprocessed data.<jupyter_code>pre_path = "../data/preprocessed/measurement_windio_msb-0002-a_2021-10-21.csv"<jupyter_output><empty_output><jupyter_text>The following cell shows your preprocessed input data.<jupyter_code>df = pd.read_csv(pre_path, parse_dates=["date_time"])

# Naming convention for prophet modell
df.rename(columns={'date_time': 'ds'}, inplace=True, errors='raise')
df['ds'] = pd.to_datetime(df['ds'])

# Set index_column
df.set_index('ds', inplace=True)

df.head()<jupyter_output><empty_output><jupyter_text>Removes the columns that are not needed and all empty rows.<jupyter_code>df.drop(['uptime','acc_z', 'rot_x', 'rot_y', 'rot_z', 'mag_x', 'mag_y', 'mag_z', 'temp'], axis=1, inplace=True)
df.dropna(inplace = True)
df.head()<jupyter_output><empty_output><jupyter_text>Convert acceleration unit and calculate the velocity / position data.<jupyter_code># Convert acceleration unit from g to m/s^2
df["acc_x"] = df["acc_x"]*9.81
df["acc_y"] = df["acc_y"]*9.81

# Dataframe with same freq. as timeseries
t = df.index.astype('int64')/1e9

# Deinition of the used highpass-filter
def butter_highpass_sosfiltfilt(data, lowcut=0.1, fs=50, pad='even', padlen=500, order=9):
    """
    applies a symmetric filter (no phase offset)
    """
    if pad not in ('even', 'odd', 'constant', None):
        raise Exception('please provide a valid padding')
    # check if len(data > padlen)
    if len(data) < padlen:
        print(
            f'padlen exceeds the number of available data points. Setting padlen to {len(data)}')
        padlen = len(data) - 1
    nyq = fs*0.5
    low = lowcut/nyq
    sos = butter(order, low, btype='highpass', output='sos')
    y = sosfiltfilt(sos, data, padtype=pad, padlen=padlen)
    return y

# Apply highpass-filter for acceleration-data
for c in ['acc_x', 'acc_y']:
    df[c] = butter_highpass_sosfiltfilt(df[c])

# Calculate the velocity-data
for component in ['x', 'y']:
    df.insert(loc=len(df.columns),
        value=integrate.cumtrapz(
            df[f'acc_{component}'], t, initial=0),
        column=f'vel_{component}',)

# Apply highpass-filter for velocity-data
for c in ['vel_x', 'vel_y']:
    df[c] = butter_highpass_sosfiltfilt(df[c])

# Calculate the position-data
for component in ['x', 'y']:
        df.insert(loc=len(df.columns),
            value=integrate.cumtrapz(
                df[f'vel_{component}'], t, initial=0),
            column=f'pos_{component}',)

# Apply highpass-filter for position-data
for c in ['pos_x', 'pos_y']:
    df[c] = butter_highpass_sosfiltfilt(df[c])

# Delete not needed columns
df.drop(['acc_x', 'acc_y', 'vel_x', 'vel_y'], axis=1, inplace=True)

# Rename columns
df.rename(columns={'pos_x': 'pos_x ($m$)', 'pos_y': 'pos_y ($m$)'}, inplace=True, errors='raise')
df.head()<jupyter_output><empty_output><jupyter_text>Please fill in the column, that should be forecasted.<jupyter_code>var_forecast = 'pos_x ($m$)'<jupyter_output><empty_output><jupyter_text>Prepare Dataframe for prophet forecasting.<jupyter_code>df = df.resample("S").mean()
df.dropna(inplace = True)
df.reset_index(drop=False, inplace=True)

# Naming convention for prophet modell
df.rename(columns={var_forecast: 'y'}, inplace=True, errors='raise')<jupyter_output><empty_output><jupyter_text>Devide the dataset into train and test data<jupyter_code>train = df.iloc[:-(round(0.02*len(df)))]
test=df.iloc[-(round(0.02*len(df))):]<jupyter_output><empty_output><jupyter_text>Train Prophet Modell<jupyter_code>m = Prophet()
m.fit(train)
future = m.make_future_dataframe(periods=300, freq='S')
forecast = m.predict(future)<jupyter_output><empty_output><jupyter_text>Plot the modell results.<jupyter_code>plot_plotly(m, forecast)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14532261227691703, 'fraction_numerical': 0.012551480682486762, 'mean_word_length': 4.65410199556541, 'pattern_counts': {'":': 8, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '328051', 'license': 'MIT', 'n_tokens_mistral': 1883, 'n_tokens_neox': 1825, 'n_words': 380, 'path': 'models/PROPHET.ipynb', 'repo_name': 'WindIO-Bachelorthesis/Shortterm_Forecast', 'size': '5099'}