import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
%matplotlib notebook

# use the 'seaborn-colorblind' style
plt.style.use('seaborn-colorblind')

"""
Explanation: Holiday Crimes in Michigan 2018
Questions to resolve: 
* How the number of crimes is affected on holidays?
* Is is possible the number of incidents increase in those kind of days?
End of explanation
"""


# SOURCE: 2018: https://www.officeholidays.com/countries/usa/michigan/2018
# scrap tool: https://www.convertcsv.com/html-table-to-csv.htm
holidays = pd.read_csv('michigan-holidays_2018.csv')
holidays.set_index('Day',inplace=True)
holidays.head()

#SOURCE: https://crime-data-explorer.fr.cloud.gov/downloads-and-docs
#MI-2018

incidents = pd.read_csv('nibrs_incident_2018.csv')
incidents.columns = map(str.lower, incidents.columns)
incidents.head(3)

"""
Explanation: Getting Dataset
End of explanation
"""


incidents.incident_date =  pd.to_datetime(incidents.incident_date)
incidents.set_index('incident_date', inplace=True)
incidents.head()

incidents = (incidents.resample('D')
                      .incident_id.count()
                      .to_frame()
                      .rename(columns={'incident_id':'incident_count'}) )

incidents[incidents.index.month==1].head(2)

incidents_mean = (incidents.groupby(pd.Grouper(freq='M'))
                                      .mean()
                                      .rename(columns={'incident_count':'incident_mean'}))

incidents_mean[incidents_mean.index.month==1].head(2)

incidents['incident_mean'] = 0
for i in range(1,13):
    incidents.at[incidents.index.month==i, 'incident_mean'] = incidents_mean[incidents_mean.index.month==i].incident_mean.mean()

incidents[incidents.index.month==1].head(2)

"""
Explanation: Processing Data
End of explanation
"""


fig, ax = plt.subplots()#figsize=(9,5))

incidents.incident_count.plot(kind='line', color='skyblue', ax=ax, label='Daily incidents')
incidents.incident_mean.plot(ax=ax, label='Monthly incidents average')

#holidays
holiday_dates = holidays.index.values
holi = incidents.loc[incidents.index.isin(holiday_dates)]
ax.scatter(holiday_dates, holi.incident_count, zorder=10, color='black', marker='.', label='Incidents on holiday')

#styles
ax.legend(loc=4, fontsize='x-small', frameon=False)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.set_title('Crimes on Holidays, Michigan 2018', fontsize='medium')
ax.xaxis.set_minor_locator(mdates.MonthLocator())
ax.xaxis.set_major_locator(mdates.MonthLocator(bymonthday=15))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b'))
ax.tick_params(axis='x', which='major', bottom=False)
ax.tick_params(axis='y', which='major', left=False)
ax.set_xlabel('Month')
ax.set_ylabel('Number of Incidents');
ax.axhline(y=1750, linestyle='--', color='gray', linewidth=0.5, alpha=0.4)
ax.axhline(y=1500, linestyle='--', color='gray', linewidth=0.5, alpha=0.7)
ax.axhline(y=1000, linestyle='--', color='gray', linewidth=0.5, alpha=0.7)
ax.axhline(y=750, linestyle='--', color='gray', linewidth=0.5, alpha=0.4)
ax.set_ylim(0,2000)
ax.set_xlim(pd.Timestamp('2017-12-30'), pd.Timestamp('2019-01-2'))

"""
Explanation: Plotting
End of explanation
"""


