<jupyter_start><jupyter_text>Cleaning The Data<jupyter_code>df.info()
df.isnull().sum()
dff=df.drop('Event',axis=1)
dff = dff.dropna(axis=0)
dff
dff.isnull().sum()
dff.info()
# What is our baseline model?

dff["Indicator"].value_counts(normalize=True)
dfYear = dff
dfYear['Date'] = pd.to_datetime(dff['Date'])
dfYear['Year'], dfYear['Month'] = dfYear['Date'].dt.year, dfYear['Date'].dt.month
dfYear
dfYear = dfYear.rename(columns={'Daily / Cumulative': 'Daily_Cumulative','Cases (person)':'Cases'})
dfYear.info()
dfYear['Indicator'].value_counts()
dfYear['Region'].value_counts()<jupyter_output><empty_output><jupyter_text>DROP Totals from Region<jupyter_code>dfYear.drop(dfYear[dfYear['Region'] == 'Total'].index, inplace = True)
dfYear['Region'].value_counts()
dfYear['Indicator'].value_counts()<jupyter_output><empty_output><jupyter_text>What is our baseline model?<jupyter_code>dfYear["Indicator"].value_counts(normalize=True)
dfYear['Daily_Cumulative'].value_counts(normalize=True)
dfYear["Daily_Cumulative"].value_counts(normalize=True)
dfYear["Region"].value_counts(normalize=True)<jupyter_output><empty_output><jupyter_text>Splitting the Daily_Cumulative Column<jupyter_code>df_Cumulative = dfYear.query('Daily_Cumulative == "Cumulative"')
df_Cumulative
df_Daily = dfYear.query('Daily_Cumulative == "Daily"')
df_Daily<jupyter_output><empty_output><jupyter_text>Data Frame for Al- Riyadh Region<jupyter_code>df_ruh = df_Daily.query('Region == "Ar Riyad"')
df_ruh
df_ruh["City"].value_counts()
df_ruh['Cases'].value_counts().sum()<jupyter_output><empty_output><jupyter_text>Preprocessing for Data Frame for Al-Riyadh Region<jupyter_code>re_df_final=df_ruh.reset_index()[['Date','Cases','Indicator']].rename({'Date':'ds','Cases':'y','Indicator':'Indicator'}, axis='columns')
re_df_final
re_df_final.y.value_counts()

pivot_table_year = pd.pivot_table(df_ruh, values='Cases', index=['Year'],
                    columns=['Month'], aggfunc=np.sum, fill_value=0)
pivot_table_year
pivot_table_Indicator = pd.pivot_table(df_ruh, values='Cases', index=['Indicator'],
                    columns=['Month'], aggfunc=np.sum, fill_value=0)
pivot_table_Indicator
#by month
plt.figure(figsize=(15,5))
sns.lineplot(x='Month', y='Cases', data=df_ruh, hue='Indicator');
#df_ruh
#by year
plt.figure(figsize=(15,5))
sns.lineplot(x='Month', y='Cases', data=df_ruh, hue='Year');
#df_ruh
avrge = re_df_final['y'].mean()
avrge
df_r_cases = re_df_final.query('Indicator == "Cases"')
df_r_cases
df_r_cases.dtypes
df_r_sum_cases = df_r_cases.groupby('ds', as_index=False)['y'].sum()
df_r_sum_cases
df_r_sum_cases.dtypes
df_r_sum_cases['7 ma'] = df_r_sum_cases['y'].rolling(7).mean()
df_r_sum_cases<jupyter_output><empty_output><jupyter_text>Split the data into the train & test<jupyter_code>train = df_r_sum_cases[(df_r_sum_cases['ds']>= pd.to_datetime('2020-03-09'))& (df_r_sum_cases['ds']<= pd.to_datetime('2021-12-31'))]
test = df_r_sum_cases[(df_r_sum_cases['ds']>= pd.to_datetime('2021-12-31'))]
#& (df_r_sum_cases['ds']<= pd.to_datetime('2022-01-30'))
train
test
train.shape
test.shape<jupyter_output><empty_output><jupyter_text>Create and fit the model<jupyter_code>model = Prophet(interval_width=0.95, daily_seasonality=True)
model
model.fit(train)
train.columns
model.params<jupyter_output><empty_output><jupyter_text>Forcasting<jupyter_code>abs(-6)
future= model.make_future_dataframe(periods=120)
forecast= model.predict(future)
forecast.head()<jupyter_output><empty_output><jupyter_text>forecast = model.predict(test.drop(columns="y"))forecast<jupyter_code>forecast[['ds','yhat','yhat_lower','yhat_upper']].tail()
fig1 = model.plot(forecast)
fig2 = model.plot_components(forecast)
cv_results = cross_validation( model = model, initial = '100 days', horizon = '7 days')
df_p = performance_metrics(cv_results)
df_p
fig3 = plot_cross_validation_metric(cv_results, metric='mse')<jupyter_output><empty_output><jupyter_text>Data Frame for Eastern Region<jupyter_code>df_east = df_Daily.query('Region == "Eastern Region"')
df_east
df_east["City"].value_counts()
df_east['Cases'].value_counts().sum()<jupyter_output><empty_output><jupyter_text>Preprocessing for Data Frame for Eastern Region<jupyter_code>east_df_final=df_east.reset_index()[['Date','Cases','Indicator']].rename({'Date':'ds','Cases':'y','Indicator':'Indicator'}, axis='columns')
east_df_final

pivot_table_year_east = pd.pivot_table(df_east, values='Cases', index=['Year'],
                    columns=['Month'], aggfunc=np.sum, fill_value=0)
pivot_table_year_east
pivot_table_Indicator_east = pd.pivot_table(df_east, values='Cases', index=['Indicator'],
                    columns=['Month'], aggfunc=np.sum, fill_value=0)
pivot_table_Indicator_east
#by month
plt.figure(figsize=(15,5))
sns.lineplot(x='Month', y='Cases', data=df_east, hue='Indicator');
#df_ruh
#by year
plt.figure(figsize=(15,5))
sns.lineplot(x='Month', y='Cases', data=df_east, hue='Year');
#df_ruh
avrge = east_df_final['y'].mean()
df_e_cases = df_east.query('Indicator == "Cases"')
east_df_final['7 ma'] = east_df_final['y'].rolling(7).mean()
east_df_final<jupyter_output><empty_output><jupyter_text>Split the datafram for Eastern Region into the train & test as out_simple forcasting<jupyter_code>train_e = east_df_final[(east_df_final['ds']>= pd.to_datetime('2020-03-09'))& (east_df_final['ds']<= pd.to_datetime('2021-12-31'))]
test_e = east_df_final[(east_df_final['ds']>= pd.to_datetime('2021-12-31'))]

print(train_e.shape)
print(test_e.shape)
train_e<jupyter_output><empty_output><jupyter_text>Create and fit the model for Eastern Region<jupyter_code>model_e = Prophet(interval_width=0.95, daily_seasonality=True)
model_e
model_e.fit(train_e)
model_e.params<jupyter_output><empty_output><jupyter_text>forecast for Eastern Region<jupyter_code>future_e= model_e.make_future_dataframe(periods=120)
forecast_e= model_e.predict(future_e)
forecast_e.head()
forecast_e[['ds','yhat','yhat_lower','yhat_upper']].tail()
fig1_e = model_e.plot(forecast_e)
fig2_e = model_e.plot_components(forecast_e)
cv_e_results = cross_validation( model = model_e, initial = '120 days', horizon = '7 days')
df_p_e = performance_metrics(cv_e_results)
df_p_e
fig3_e = plot_cross_validation_metric(cv_e_results, metric='mse')<jupyter_output><empty_output><jupyter_text>Data Frame for Makkah Almukaramh Region<jupyter_code>df_mk = df_Daily.query('Region == "Makkah Al Mukarramah"')
df
df_mk["City"].value_counts()
df_mk['Cases'].value_counts().sum()<jupyter_output><empty_output><jupyter_text>Preprocessing for Data Frame for Makkah Almukaramah Region<jupyter_code>mk_df_final=df_mk.reset_index()[['Date','Cases','Indicator']].rename({'Date':'ds','Cases':'y','Indicator':'Indicator'}, axis='columns')
mk_df_final

avrge = mk_df_final['y'].mean()
df_m_cases = df_mk.query('Indicator == "Cases"')
df_m_cases<jupyter_output><empty_output><jupyter_text>Split the datafram for Makkah Almukaramah Region into the train & test as out_simple forcasting<jupyter_code>mk_df_final['7 ma'] = mk_df_final['y'].rolling(7).mean()
mk_df_final
train_m = mk_df_final[(mk_df_final['ds']>= pd.to_datetime('2020-03-09'))& (mk_df_final['ds']<= pd.to_datetime('2021-12-31'))]
test_m = mk_df_final[(mk_df_final['ds']>= pd.to_datetime('2021-12-31'))]

print(train_m.shape)
print(test_m.shape)<jupyter_output>(12729, 4)
(1330, 4)<jupyter_text>Create and fit the model for Makkah Almukaramh Region<jupyter_code>model_m = Prophet(interval_width=0.95, daily_seasonality=True)
model_m
model_m.fit(train_m)<jupyter_output>INFO:prophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this.<jupyter_text>forecast for Makkah Almukaramh Region<jupyter_code>future_m= model_m.make_future_dataframe(periods=120)
forecast_m= model_m.predict(future_m)
forecast_m.head()
forecast_m.to_csv('makkah1.csv',index=False)
forecast_m[['ds','yhat','yhat_lower','yhat_upper']].tail()
df_mk.to_csv('makkah.csv',index=False)
fig1_m = model_m.plot(forecast_m)
fig2_m = model_m.plot_components(forecast_m)
cv_m_results = cross_validation( model = model_m, initial = '120 days', horizon = '7 days')
df_p_m = performance_metrics(cv_m_results)
df_p_m
fig3_m = plot_cross_validation_metric(cv_m_results, metric='mse')

import pandas as pd
r_df = pd.read_csv('Riyadh12.csv')
e_df = pd.read_csv('Eastren12.csv')
m_df = pd.read_csv('makkah12.csv')
forecast_r = r_df[['ds','yhat','yhat_lower','yhat_upper']]
forecast_e = e_df[['ds','yhat','yhat_lower','yhat_upper']]
forecast_m = m_df[['ds','yhat','yhat_lower','yhat_upper']]
forecast_r
px.line(forecast, x="ds", y="yhat_upper", title='Future cases for Riyadh', height=500, template="plotly_dark")
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns 

import plotly.express as px
import plotly.graph_objects as go
from jupyter_dash import JupyterDash
from dash import dcc 
from dash import html
from dash.dependencies import Input, Output
import dash
import dash_bootstrap_components as dbc
Region_names = df_Daily.Region.unique()
Region_names.sort()
Region_names
external_stylesheets = [
    # Bootswatch theme
    dbc.themes.SLATE,
    # for social media icons
    "https://use.fontawesome.com/releases/v5.9.0/css/all.css",
]


app = dash.Dash(__name__,
    external_stylesheets=external_stylesheets,
    routes_pathname_prefix='/',
)

row_1 = dbc.Row(
    [
        dbc.Col(
                dbc.Card(
                    dbc.CardBody(
                    dcc.Dropdown(
                    id='Dropdown2', 
                    clearable=False,
                    options=[{"label": x, "value": x} for x in Region_names],
                    value=Region_names[0],  
                    style={'font-size':20, 'width': '2500px', 'height': '60px',"background-color":"gray",
                            'font-color':'black','color':'black',
                             'verticalAlign':"middle"}
                    ),
                        style={'height':'40px',
                               'margin-left':  '0px',
                               'font-size': '10px'}
                    )),
                width=3           
        ),
        
        dbc.Col(
                dbc.Card(
                    dbc.CardBody(
                        [
                            html.H1(id="item-cases", className="mt-3", style={'color':'white', 'font-size':35}),
                            html.P(
                                f"{'Cases'}",
                                className="card-text",
                                style={'color':'white', 'font-size':15}
                            ),
                        ],
                    ),
                    className=f"top-bar-card-{'Cases'}",
                ),
                className="top-bar-card-body",
                width=3,
                style={'color':'yellow'}
            ),
        dbc.Col(
                dbc.Card(
                    dbc.CardBody(
                        [
                            html.H1(id="item-recoveries", className=f"top-bar-value-{'Recoveries'}" , 
                                    style={'color':'yellow', 'font-size':35}),
                            html.P(
                                f"{'Recoveries'}",
                                className="card-text",
                                style={'color':'yellow', 'font-size':15}
                            ),
                        ]
                    ),
                    className=f"top-bar-card-{'Recoveries'}",
                ),
                width=3,
                className="top-bar-card-body",
            ),
        dbc.Col(
                dbc.Card(
                    dbc.CardBody(
                        [   html.H1(id="item-mortalities", className=f"top-bar-value-{'Mortalities'}" , 
                                    style={'color':'red', 'font-size':35}),
                            html.P('Mortalities', className="card-text", style={'color':'red', 'font-size':15}),
                            
                        ]
                    ),
                    className=f"top-bar-card-{'Mortalities'}",
                ),
                width=3,
                className="top-bar-card-body",
            )
    ],
    className="mb-4",
)


fig_r = px.line(forecast, x="ds", y="yhat_upper", title='Future cases for Riyadh', height=300, template='plotly_dark')
fig_r.update_layout(font=dict(family="Courier New, monospace",size=15,color="white"))
fig_e = px.line(forecast_e, x="ds", y="yhat_upper", title='Future cases for Eastern Region', height=300, template='plotly_dark')
fig_e.update_layout(font=dict(family="Courier New, monospace",size=15,color="white"))
fig_m = px.line(forecast_m, x="ds", y="yhat_upper", title='Future cases for Makkah', height=300, template='plotly_dark')
fig_m.update_layout(font=dict(family="Courier New, monospace",size=15,color="white"))

row_2 = dbc.Row(
    [
        dbc.Col(
                dbc.Card(
                    dbc.CardBody(
                    dcc.Graph(figure=fig_r)
                    )),
                width=4           
        ),
        
        dbc.Col(
                dbc.Card(
                    dbc.CardBody(
                    dcc.Graph(figure=fig_e)
                    ),
                ),
                width=4,
            ),
        dbc.Col(
                dbc.Card(
                    dbc.CardBody(
                    dcc.Graph(figure=fig_m)
                    ),
                ),
                width=4,
            )
        
    ]
)

app.layout = html.Div([dbc.Alert(
    html.Div([
        html.H1("Covid-19 Tracker fo Saudi Arabia", style={'color':'white', 'font-size':40})
    ]), color='black', style={'height':30}
    ),
                                               html.Br(),
          
    html.Div(row_1),
    
    html.Div([                  
    html.Br([]),
       ]),
    dcc.Graph(id="LineGraph", style={'color':'#F4F4F4'}), 
    html.Br([]),
    html.Br([]),
    html.Div(row_2),
    html.Br([]),                   
                      ])

@app.callback(
    Output("item-cases", "children"), [Input("Dropdown2", "value")]
   )
def count_cases(region):
    n = df_Daily[(df_Daily['Indicator']=='Cases') & (df_Daily['Region']==region)]['Cases'].sum()
    return str(n)

@app.callback(
    Output("item-recoveries", "children"), [Input("Dropdown2", "value")]
   )
def count_cases(region):
    n = df_Daily[(df_Daily['Indicator']=='Recoveries') & (df_Daily['Region']==region)]['Cases'].sum()
    return str(n)

@app.callback(
    Output("item-mortalities", "children"), [Input("Dropdown2", "value")]
   )
def count_cases(region):
    n = df_Daily[(df_Daily['Indicator']=='Mortalities') & (df_Daily['Region']==region)]['Cases'].sum()
    return str(n)

@app.callback(
    Output("LineGraph", "figure"), 
    [Input("Dropdown2", "value")])
def update_line_chart(regionss):
    df2 = df_Daily[df_Daily['Region']==regionss]
    df2 = df2.groupby('Date')['Cases'].sum().reset_index()
    fig = px.line(df2, x="Date", y="Cases", title='Daily cases for ' + regionss, height=350, template='plotly_dark')
    
    fig.update_layout(
    font=dict(
        family="Courier New, monospace",
        size=20,
        color="white"))
    return fig

if __name__ == "__main__":
    app.run_server()

df_Daily[df_Daily['Region']=='Ar Riyad']<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15813498532768178, 'fraction_numerical': 0.01623736550374959, 'mean_word_length': 3.396788990825688, 'pattern_counts': {'":': 4, '<': 83, '<?xml version=': 0, '>': 85, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '483080', 'license': 'MIT', 'n_tokens_mistral': 5605, 'n_tokens_neox': 5327, 'n_words': 728, 'path': 'DN_Capston_PROJECT - THU1.ipynb', 'repo_name': 'ElafSHajjar/ML-Capstone-Project', 'size': '15335'}