import vincent
import pandas as pd
from vincent import AxisProperties, PropertySet, ValueRef
from vincent import Map

vincent.core.initialize_notebook()

"""
Explanation: San - Francisco Crime Analysis
Loading data with pandas and graphing using vincent
End of explanation
"""


incidents = pd.read_csv('sanfrancisco_incidents_summer_2014.csv')

"""
Explanation: Reading the file
End of explanation
"""


incidents.columns = ['Id'
                    ,'Category'
                    ,'Description'
                    ,'DayOfWeek'
                    ,'Date'
                    ,'Time'
                    ,'District'
                    ,'Resolution'
                    ,'Address'
                    ,'Longitude'
                    ,'Latitude'
                    ,'Location'
                    ,'PdId']

"""
Explanation: Changing the column labels of the data set
End of explanation
"""


# the date and time of incident are in two separate columns
# combining them into a date_time column
incidents['DateTime'] = pd.to_datetime(incidents['Date'] + ' ' + incidents['Time'])
date_idx = pd.DatetimeIndex(incidents['DateTime'])
incidents['Date'] = date_idx.date.astype('datetime64')
incidents['Hour'] = date_idx.hour
incidents['Year'] = date_idx.year
incidents['Month'] = date_idx.month
incidents['Weekday'] = date_idx.weekday

"""
Explanation: The date and time of incident are in two separate columns. Combining them into a DateTime column
End of explanation
"""


count_by_category = pd.DataFrame({'count' : incidents.groupby( ['Category'] ).size()}).reset_index()
count_by_category.sort_values(by='count',ascending= True,inplace=True)
index = count_by_category['Category']

graph = vincent.Bar(count_by_category,columns=['count'], key_on='Category')
graph.legend(title='Category')
graph.axis_titles(x='Category', y='Incident Count')
ax = AxisProperties(
    labels=PropertySet(
        angle=ValueRef(value=270),
        align=ValueRef(value='right')
        )
    )
graph.axes[0].properties = ax
graph.display()
#ax = count_by_category.plot(kind="barh",x='Category', y='count',sort_columns=True)

by_year = incidents.pivot_table('Id'
                                , aggfunc='count'
                                , index='Weekday'
                                , columns='District')



graph = vincent.Line(by_year)
graph.legend(title='District')
graph.axis_titles(x='Weekday', y='Incident Count')

graph.display()

"""
Explanation: Histogram plot of the incidents by category
The plot shows that Larceny/Theft is the most reported crime across all districts, second being Assault and 9th being Missing Person.
End of explanation
"""


filtered = incidents[incidents['District'] == 'MISSION']
filtered = filtered[~filtered['Category'].isin(['LARCENY/THEFT'
                                               ,'NON-CRIMINAL'
                                               ,'OTHER OFFENSES'
                                               ,'WARRANTS'
                                               ,'DISORDERLY CONDUCT'])]

count_by_category = pd.DataFrame({'count' : filtered.groupby( ['Category'] ).size()}).reset_index()
count_by_category.sort_values(by='count',ascending= True,inplace=True)
index = count_by_category['Category']

graph = vincent.Bar(count_by_category,columns=['count'], key_on='Category')
graph.legend(title='Category')
graph.axis_titles(x='Category', y='Incident Count')
ax = AxisProperties(
    labels=PropertySet(
        angle=ValueRef(value=270),
        align=ValueRef(value='right')
        )
    )
graph.axes[0].properties = ax
graph.display()

"""
Explanation: Top incident category in Mission district
End of explanation
"""



by_hour = filtered.pivot_table('Id'
                              , aggfunc='count'
                              , index='Hour'
                              , columns='Category')

graph = vincent.Line(by_hour) 
graph.legend(title='Category')
graph.axis_titles(x='Hour', y='Incident Count')

graph.display()

"""
Explanation: The graph result is surprising. "Missing Person" incidents are fourth highest compared to overall SFO
Plotting the incidents by category across different hours
The plot shows another surprising peak in the Missing Person incidents at 11 am.
End of explanation
"""


filter_by_category = 'MISSING PERSON'

filtered = incidents[incidents['Category'] == filter_by_category]
by_hour = filtered.pivot_table('Id'
                                , aggfunc='count'
                                , index='Hour'
                                , columns='District')

graph = vincent.Line(by_hour) #,columns=['count'],key_on='District')
graph.legend(title='District')
graph.axis_titles(x='Hour', y='Incident Count')

graph.display()

"""
Explanation: Comparing this trend across other districts
The comparison shows that the trend is clearly something that is unique to Mission District and the number of missing person incidents is rather high compared to the other districts
End of explanation
"""


count_by_category = pd.DataFrame({'count' : filtered.groupby( ['District'] ).size()}).reset_index()
count_by_category.sort_values(by='count',ascending= True,inplace=True)
index = count_by_category['District']

graph = vincent.Bar(count_by_category,columns=['count'], key_on='District')
graph.legend(title='District')
graph.axis_titles(x='District', y='Incident Count')
ax = AxisProperties(
    labels=PropertySet(
        angle=ValueRef(value=270),
        align=ValueRef(value='right')
        )
    )
graph.axes[0].properties = ax
graph.display()

"""
Explanation: Histogram plot of the "Missing Person" incidents across districts
The plot clearly shows that the Missing Person incidents are almost double of the same incidents in Southern district, which has the next highest "Missing Person" rate.
End of explanation
"""


filter_by_districts = ['MISSION','SOUTHERN','PARK','BAYVIEW']

filtered = incidents[incidents['Category'] == filter_by_category]
filtered = filtered[filtered['District'].isin(filter_by_districts)]
#filtered = filtered[filtered['Weekday'] == 2]

by_hour = filtered.pivot_table('Id'
                                , aggfunc='count'
                                , index='Hour'
                                , columns='District')

graph = vincent.Line(by_hour) #,columns=['count'],key_on='District')
graph.legend(title='District')
graph.axis_titles(x='Hour', y='Incident Count')

graph.display()

filter_by_district = 'MISSION'
filter_by_category = 'MISSING PERSON'
filtered = incidents[incidents['Category'] == filter_by_category]
filtered = filtered[filtered['District'] == filter_by_district]

"""
Explanation: Top 4 districts where "Missing Person" cases are relatively highest
In the 4 districts where "assaults" are high, Southern District seems to be the highest. There is a peak post 5pm.
End of explanation
"""


by_hour = filtered.pivot_table('Id'
                                , aggfunc='count'
                                , index='Hour'
                                , columns='District')

graph = vincent.Bar(by_hour) #,columns=['count'],key_on='District')
graph.legend(title='District')
graph.axis_titles(x='Hour', y='Incident Count')

graph.display()

"""
Explanation: Missing Person incidents in Mission peak at 11am
Most missing person cases seem to be around 10 to 12 am with the peak being at 11am
End of explanation
"""


by_weekday = filtered.pivot_table('Id'
                                , aggfunc='count'
                                , index='Weekday'
                                , columns='District')

graph = vincent.Bar(by_weekday) 
graph.legend(title='District')
graph.axis_titles(x='Weekday', y='Incident Count')

graph.display()

def getExtent(data):
    extentMin = [data['Latitude'].min(),data['Longitude'].min()]
    extentMax = [data['Latitude'].max(),data['Longitude'].max()]
    center = [data['Latitude'].mean(),data['Longitude'].mean()]
    return (center,extentMin,extentMax)

center,extentMin,extentMax = getExtent(incidents)
center

#filter_by_district = 'CENTRAL'
#filter_by_category = 'VEHICLE THEFT'

#filtered = filtered[filtered['Hour'] == 11]
#filtered = filtered[filtered['Hour'] < 19]
#filtered = filtered[filtered['Weekday'] == 4]
#filtered = incidents[incidents['Category'] == filter_by_category]
#filtered = filtered[filtered['District'] == filter_by_district]



import folium
dots = filtered.as_matrix(columns=['Latitude','Longitude'])
map_osm = folium.Map(location=center, zoom_start=13, max_zoom=16)
map_osm.create_map(path='osm.html')

for dot in dots:
    map_osm.simple_marker(dot)
map_osm

incidents.head()

"""
Explanation: Missing Persons in Mission district peak on Fridays
End of explanation
"""


