Skip to content

Commit

Permalink
final commit from lynn
Browse files Browse the repository at this point in the history
  • Loading branch information
gaolingshan committed Dec 16, 2016
1 parent 338d0b4 commit 7f9aebb
Show file tree
Hide file tree
Showing 47 changed files with 145 additions and 93 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified Final_Project_1007/.DS_Store
Binary file not shown.
1 change: 0 additions & 1 deletion Final_Project_1007/BoroughBound

This file was deleted.

Binary file modified Final_Project_1007/Methods/.DS_Store
Binary file not shown.
168 changes: 125 additions & 43 deletions Final_Project_1007/Methods/MethodClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
This module contain all classes of methods could be applied on the information
Copyright:
@ Nan Wu
@ [email protected]
@ [email protected]
@ Nan Wu, Lingshan Gao, Shucheng Yan
@ [email protected]; [email protected]; [email protected]
'''
import datetime
import pandas as pd
Expand Down Expand Up @@ -315,7 +314,7 @@ def PrecinctTableAll(self,precinct_List,Indicator):
Args:
precint_List: a dictionary contains one type of objects
precinct_List: a dictionary contains one type of objects
Indicator: one possible indicator of the severity of the collision
Return:
table: Data Frame
Expand Down Expand Up @@ -504,9 +503,6 @@ def PlotbyMonth(self,Indicator,level,name='null'):
ax.set_xlabel('Time')
ax.set_ylabel(self.IndicatorPrint[Indicator])
figure.subplots_adjust(bottom=0.20)
#figure.title('Time Series analysis on ' + level + ' level')
#figure.ylabel(self.IndicatorPrint[Indicator])
#figure.xlabel('Time')
figure.show()
self.CloseFigure()

Expand All @@ -533,24 +529,107 @@ def PlotbyMonth(self,Indicator,level,name='null'):
print("Figure has been saved.")
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
self.ContinueALL()
def InjuryKillPIE(self,level,name='null'):
pass
def Map(self,Indicator,level,name='null', noPlot=False):
def GetSum(self,Indicator,level,name):
'''
this method calculates the total sum of collision statistics returned from the dataframe
'''
df=self.Table_Dict[level](Indicator,name)
if name == 'null':
rowSum = df.sum(axis = 1)
totalSum = rowSum.sum(axis = 0)
else:
totalSum = df.sum(axis = 0)
totalSum = totalSum.ix[0]
return totalSum

def Pieconponent(self,level,name='null'):
'''
This medhod retuens a dataframe in which the total number of injury and killed combined
are calculated for different people on the road.
Example:
InjuredandKill
Cyclists 1.0
Motorists 514.0
Passengers 381.0
Pedestrians 4.0
'''
dfFORpie=dict.fromkeys(['MotoristsInjured', 'MotoristsKilled', 'PassengInjured', 'PassengKilled',
'CyclistsInjured', 'CyclistsKilled', 'PedestrInjured', 'PedestrKilled'])

self.TableDICT_Init()
for indicator in range(6,14):
sum = self.GetSum(indicator, level, name)
dfFORpie[self.Indicator[indicator]]=sum
dfpie={'Motorists':dfFORpie['MotoristsInjured']+dfFORpie['MotoristsKilled'],
'Passengers':dfFORpie['PassengInjured']+dfFORpie['PassengKilled'],
'Cyclists':dfFORpie['CyclistsInjured']+dfFORpie['CyclistsKilled'],
'Pedestrians':dfFORpie['PedestrInjured']+dfFORpie['PedestrKilled']}
df=pd.DataFrame(dfpie,index=['InjuredandKill'])
df = df.transpose()
return df
def InjuryKillPIE(self,level,name='null',Indicator = 'null'):
'''
This method draws the pie chart to campare the number of injury and killed combined for
different kinds of people.
'''

dfPie = self.Pieconponent(level, name)
print(dfPie)
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
ax = dfPie.plot(kind = 'pie', y= 'InjuredandKill', title = 'pie chart comparison for injury and killed combined',
colors=colors, shadow = True, startangle = 90, autopct = '%1.1f%%')
figure = ax.get_figure()
figure.show()
self.CloseFigure()
figure.savefig(self.savepath + '/PieChart_for_' + level + '_level')
print("Figure has been saved.")
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
self.ContinueALL()

def RankTop10(self, Indicator, level, name='null'):
'''
This method returns the the number of top 10 of a Collision statistic demanded by user.
'''
self.TableDICT_Init()
df = self.Table_Dict[level](Indicator,name)
if name == 'null':
rowSum = df.sum(axis = 1)
rowSumFrame = pd.DataFrame(rowSum, columns = [self.Indicator[Indicator]])
sortedFrame = rowSumFrame.sort(columns = self.Indicator[Indicator], ascending =False)
if int(len(sortedFrame.index)) >= 10 :
print(sortedFrame.head(n = 10))
else:
print(sortedFrame)

else:
sortedFrame = df.sort(columns = name, ascending = False)
if int(len(df.index)) >= 10:
print(sortedFrame.head(n = 10))
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
else:
print(sortedFrame)
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
self.ContinueALL()

def Map(self,Indicator,level,name='null'):

# References:
# http://stackoverflow.com/questions/6028675/setting-color-range-in-matplotlib-patchcollection
# http://basemaptutorial.readthedocs.io/en/latest/shapefile.html
# Shapefile source : https://nycopendata.socrata.com/Public-Safety/Police-Precincts/78dh-3ptz/data
# Precinct shapefile source : https://nycopendata.socrata.com/Public-Safety/Police-Precincts/78dh-3ptz/data
# Borough boundaries shapefile source: https://data.cityofnewyork.us/City-Government/Borough-Boundaries/tqmj-j8zm/data
'''
This function plots a heatmap for numbers of collision happen in each area.
This function plots a heatmap for number of collisions happen in each area.
The function takes three inputs:
level: string type. 'borough' or 'precinct'
name: string type. Borough name. 'mn' for Manhattan, 'bk' for Brooklyn, 'bn' for Bronx, 'si' for 'Staten Island', 'qn' for Queens
indicator: string type. The metrics that the user would like to use, such as 'number of people injured' or 'collision'
indicator: string type. The metrics that the user would like to use, such as 'number of people injured' or 'number of collisions happened'
'''
# Create map related variables
patches = []
color_list = []
fig, ax = plt.subplots()
# Draw a basemap according to the geographic level
mapbase = Basemap(projection='mill',
llcrnrlat = 40.492,
Expand All @@ -560,57 +639,62 @@ def Map(self,Indicator,level,name='null', noPlot=False):
resolution='c')
mapbase.fillcontinents(color='white')

# Call TableDICT method to extract data
# Call TableDICT to extract data
self.TableDICT_Init()

# if 'borough' level and name of the borough are not specified, then plots a heatmap for NYC by boroughs.
if (level == 'Borough') and (name == 'null'):
df = self.Table_Dict[level](Indicator)
plot_series = df.sum()
# Draw borough bounds
mapbase.readshapefile('BoroughBound/boroughshape', 'boroughmaps', drawbounds=True)
# Color the map base on their value of the indicator

# Color the boroughs based on their value of the indicator
for i in range(len(mapbase.boroughmaps)):
info = mapbase.boroughmaps_info[i]
shape = mapbase.boroughmaps[i]
area = str(info['boro_name'])
plot_series = df.sum()
if area in plot_series.index:
color_list.append(plot_series.loc[area])
polygons = Polygon(np.array(shape), True)
patches.append(polygons)

# if 'precinct' level, then load the precinct map
elif level == 'Precinct':
mapbase.readshapefile('PrecinctBound/precinctshape', 'precinctmaps', drawbounds=True)
# if plot precincts for the whole city
if name in ['Manhattan', 'Brooklyn', 'Bronx', 'Queens', 'Staten Island']:
df = self.Table_Dict[level](Indicator, name)
# if plot precincts for only one borough
elif name == 'null':
else:
# if 'borough' level and name of the borough are listed, then plots a heatmap for NYC these boroughs by precinct
if (level == 'Borough') and (name in ['mn', 'bk', 'bx', 'qn', 'si']):
df = self.PrecinctTableAll(self.data.Borough_Dict[name].precinctList, Indicator)
# if 'precinct' level, then plot a heatmap by precinct for the entire city.
elif level == 'Precinct':
df = self.Table_Dict[level](Indicator)

# Add dataframe by column
plot_series = df.sum()
# Draw precinct bounds
mapbase.readshapefile('PrecinctBound/precinctshape', 'precinctmaps', drawbounds=True)

# Sum the data frame by column
plot_series = df.sum()

# Color the precincts based on their value of the indicator
for i in range(len(mapbase.precinctmaps)):
info = mapbase.precinctmaps_info[i]
shape = mapbase.precinctmaps[i]
area = str(int(info['precinct']))

area = "{0:0=3d}".format(int(info['precinct']))
if area in plot_series.index:
color_list.append(plot_series.loc[area])
polygons = Polygon(np.array(shape), True)
patches.append(polygons)

self.color_list = color_list
self.patches = patches
if not noPlot:
colors = np.array(color_list)
polygons = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.5)
polygons.set_array(colors)
fig, ax = plt.subplots()
ax.add_collection(polygons)
plt.colorbar(polygons)
plt.show()

colors = np.array(color_list)
polygons = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.5)
polygons.set_array(colors)
ax.add_collection(polygons)
plt.colorbar(polygons)
fig.show()
self.CloseFigure()
fig.savefig(self.savepath +'/Heatmap '+ level + name)
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
print("Figure has been saved.")
self.ContinueALL()

def BoroughCompare(self,Indicator,level,name='null'):
'''
This method is designed specfically for 5 boroughs. Users can see a comparision
Expand Down Expand Up @@ -644,14 +728,14 @@ def RankTop10(self, Indicator, level, name='null'):
rowSum = df.sum(axis = 1)
rowSumFrame = pd.DataFrame(rowSum, columns = [self.Indicator[Indicator]])
sortedFrame = rowSumFrame.sort(columns = self.Indicator[Indicator], ascending =False)
if len(sortedFrame.index >= 10) :
if int(len(sortedFrame.index)) >= 10:
print(sortedFrame.head(n = 10))
else:
print(sortedFrame)

else:
sortedFrame = df.sort(columns = name, ascending = False)
if len(df.index >= 10):
if int(len(df.index)) >= 10:
print(sortedFrame.head(n = 10))
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
else:
Expand Down Expand Up @@ -1189,5 +1273,3 @@ def InfluenceONSeverity(self,Influencer,Indicator,level,name='null'):
except TypeError:
print("No information.")
raise


Binary file not shown.
Binary file not shown.
20 changes: 0 additions & 20 deletions Final_Project_1007/Methods/unittest_Methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,8 @@ def setUp(self):
self.savepath = ''.join([self.dirname[:-8],'/test_results/'])
self.NYC = StructureBuilding(self.TimeBegin,self.TimeEnd,self.DataPath)
self.Situation = SituationMethods(self.NYC, self.savepath, self.TimeBegin,self.TimeEnd)
'''
def test_CityTable(self):
values=dict(zip(range(1,15),[18375, 4643.0, 27.0, 4643.0, 27.0,1587.0,5.0,1832.0,4.0,464.0,1.0,760.0,17.0,3450.0]))
for indicator in self.Situation.Indicator.keys():
self.assertEqual(list(self.Situation.CityTable(indicator, 'null').ix[0])[0],values[indicator])
'''

def test_BoroughTable(self):
#error
value_1=dict(zip([ 'bk', 'mn', 'bx', 'qn', 'si'],[5294, 4295, 2531, 5219, 1036]))
value_2=dict(zip([ 'bk', 'mn', 'bx', 'qn', 'si'],[1522.0, 686.0, 757.0, 1411.0, 267.0]))
value_3=dict(zip([ 'bk', 'mn', 'bx', 'qn', 'si'],[9.0, 7.0, 4.0, 5.0, 2.0]))
Expand All @@ -51,11 +43,8 @@ def test_BoroughTable(self):
for indicator in self.Situation.Indicator.keys():
for name in [ 'bk', 'mn', 'bx', 'qn', 'si']:
self.assertEqual(list(self.Situation.BoroughTable(indicator,name).ix[0])[0], values[indicator][name])



def test_PrecinctTable(self):
#error
value_1=dict(zip([ '001', '100', '060'],[295, 50, 186]))
value_2=dict(zip([ '001', '100', '060'],[49.0, 14.0, 35.0]))
values=dict(zip(range(1,3),[value_1,value_2]))
Expand All @@ -68,7 +57,6 @@ def test_PrecinctTable(self):


def test_PrecinctCalculate(self):
#error
df1 = self.NYC.Borough_Dict['mn'].precinctList['001'].Collisions_intersection['2015']['05']
self.assertEqual(self.Situation.PrecinctCalculate(1,df1), 290)
self.assertEqual(self.Situation.PrecinctCalculate(4,df1), 18)
Expand Down Expand Up @@ -127,14 +115,12 @@ def test_BoroughTableAll(self):
self.assertEqual(df['Manhattan']['201505'], 136)
pass
def test_BTHRTableAll(self):
#error
df=self.Situation.BTHRtableAll(self.NYC.Bridge_Dict,1)
self.assertEqual(df['VZ BR UPPER']['201505'], 19)
df=self.Situation.BTHRTableAll(self.NYC.Tunnel_Dict,13)
self.assertEqual(df['BBT W TUBE']['201505'], 0)
pass
def test_TunnelTable(self):
#error
df=self.Situation.TunnelTable(1,'null')
self.assertEqual(df['BBT W TUBE']['201505'], 2)
df=self.Situation.TunnelTable(13,'null')
Expand Down Expand Up @@ -176,11 +162,6 @@ def test_RoadTable(self):
df=self.Situation.RoadTable(8,'86 STREET TRANSVERSE')
self.assertEqual(df['86 STREET TRANSVERSE']['201505'], 0)
pass

def test_Map_Borough(self):
self.Situation.Map(1, 'Borough', noPlot=True)
self.assertEqual(self.Situation.color_list, [1921, 1921, 1921, 1921, 5130, 5130, 5130, 5130, 5130, 5130, 5130, 5130, 5130, 5130, 5130, 5130, 5130, 5130, 5130, 5130, 5130, 5130, 5130, 5130, 5130, 5130, 5130, 5130, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 8584, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10622, 10710, 10710, 10710, 10710, 10710, 10710, 10710, 10710, 10710, 10710, 10710, 10710, 10710, 10710, 10710, 10710, 10710, 10710])
self.assertEqual(len(self.Situation.patches), 104)

class ContributingMethods_Test(unittest.TestCase):

Expand All @@ -198,7 +179,6 @@ def setUp(self):


def test_InfCalculate(self):
#error
Collisions = self.NYC.Borough_Dict['bk'].precinctList['060'].Collisions_intersection['2015']['05']
Factors = self.NYC.Borough_Dict['bk'].precinctList['060'].Factors_intersection['2015']['05']
df=self.Contributing.InfCalculate(1,1,Collisions,Factors)
Expand Down
1 change: 0 additions & 1 deletion Final_Project_1007/NYCcollision.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
if __name__ == '__main__':
try:
dirname, filename = os.path.split(os.path.abspath(__file__))
#print(dirname)
WN_User_Interact.User_Interaction(dirname)

except ExitALLProgram:
Expand Down
1 change: 0 additions & 1 deletion Final_Project_1007/PrecinctBound

This file was deleted.

Binary file modified Final_Project_1007/WN_User_Interact/.DS_Store
Binary file not shown.
Loading

0 comments on commit 7f9aebb

Please sign in to comment.