-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
355 lines (305 loc) · 12.1 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
from includes.about import about
from includes.navbar import navbar
from includes.home import home
import pandas as pd
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
import plotly.graph_objs as go
from dash.dependencies import Input, Output, State
import dash_table
import io
import base64
import datetime
import plotly.express as px
import xlrd
import seaborn as sns
import geopandas as gpd
import matplotlib.pyplot as plt
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.config['suppress_callback_exceptions'] = True
app.title = 'Crimes Against Women'
PLOTLY_LOGO = "https://images.plot.ly/logo/new-branding/plotly-logomark.png"
# ========================== Data Processing ===============================================
df = pd.read_excel('Data/crime2014.xlsx')
tf = df
# print(df.head())
cols = df.columns
display_data = df.drop(columns='Total Crimes Against Women')
year_data = display_data.groupby("Year").sum().reset_index()
classy = year_data.melt(id_vars="Year", var_name="Cases")
# Year list:
year_options = []
for year in df['Year'].unique():
year_options.append({'label': str(year), 'value': year})
state_options = []
for state in df['State/UT'].unique():
state_options.append({'label': str(state), 'value': state})
minYear = df['Year'].min()
maxYear = df['Year'].max()
# Crime head dictionary-LIST:
# With Sum aggr:
crimeDictS = {'Rape': sum, 'Kidnapping & Abduction': sum, 'Dowry Deaths': sum, 'Assault on women with intent to outrage her modesty ': sum, 'Insult to modesty of women': sum, 'Cruelty by Husband or his Relatives': sum,
'Importation of Girls from foreign country': sum, 'Immoral Traffic (P) Act': sum, 'Dowry Prohibition Act': sum, 'Indecent Representation of Women(P) Act': sum, 'Commission of Sati Prevention Act': sum}
# Without aggr list:
crimeDict = ['Rape', 'Kidnapping & Abduction', 'Dowry Deaths', 'Assault on women with intent to outrage her modesty ', 'Insult to modesty of women', 'Cruelty by Husband or his Relatives',
'Importation of Girls from foreign country', 'Immoral Traffic (P) Act', 'Dowry Prohibition Act', 'Indecent Representation of Women(P) Act', 'Commission of Sati Prevention Act']
# crime list:
crimes = ['Rape', 'Kidnapping & Abduction', 'Dowry Deaths', 'Assault on women with intent to outrage her modesty ', 'Insult to modesty of women', 'Cruelty by Husband or his Relatives',
'Importation of Girls from foreign country', 'Immoral Traffic (P) Act', 'Dowry Prohibition Act', 'Indecent Representation of Women(P) Act', 'Commission of Sati Prevention Act']
# New df: removing year:
totalc = df.groupby(["State/UT"], as_index=False).agg(crimeDictS)
totalc['Total'] = totalc[crimeDict].sum(axis=1)
print(totalc.columns)
card_question = dbc.Card(
[
dbc.CardBody([
html.H4("Question 1", className="card-title"),
html.P("What was India's life expectancy in 1952?",
className="card-text"),
dbc.ListGroup(
[
dbc.ListGroupItem("A. 55 years"),
dbc.ListGroupItem("B. 37 years"),
dbc.ListGroupItem("C. 49 years"),
], flush=True)
]),
], color="warning",
)
card_graph = dbc.Card(
dcc.Graph(id='treemap',
figure=px.treemap(classy,
path=["Year", "Cases"], values="value",
height=600, width=1450).update_layout(margin=dict(t=25, r=0, l=5, b=20))), body=True, color="secondary",
)
app.layout = html.Div([
dbc.Row([dbc.Col(card_question, width=1),
dbc.Col(card_graph, width=10)], justify="around"), # justify="start", "center", "end", "between", "around"
# dbc.CardGroup([card_main, card_question, card_graph]) # attaches cards with equal width and height columns
# dbc.CardDeck([card_main, card_question, card_graph]) # same as CardGroup but with gutter in between cards
# dbc.CardColumns([ # Cards organised into Masonry-like columns
# card_main,
# card_question,
# card_graph,
# card_question,
# card_question,
# ])
])
@app.callback(Output(component_id='perYear', component_property='figure'),
[Input(component_id='year-picker', component_property='value')])
def update_figure(selected_year):
filtered_df = df[df['Year'] == selected_year]
traces = [go.Bar(
x=filtered_df['State/UT'],
y=filtered_df['Total Crimes Against Women']
)]
return {
'data': traces,
'layout': go.Layout(title='Total crime in the year '+str(selected_year), xaxis={'title': 'States/UT'})
}
# TAB 1 ENDS
# TAB 2 : STARTS
@app.callback(Output('stateCrime', 'figure'),
[Input('selectState', 'value'),
Input('selectCrime', 'value')])
def state_crime_graph(sstate, scrime):
filter_state = df[df['State/UT'] == sstate]
traces = [go.Scatter(
x=filter_state['Year'],
y=filter_state[scrime],
name=scrime,
fill='tonexty',
mode='lines+markers'
)]
return {
'data': traces,
'layout': go.Layout(title='{} cases in {}'.format(scrime, sstate),
xaxis={'title': 'Year'},
yaxis={'title': 'cases of '+scrime},
hovermode='closest')
}
# Forecast:
def pattern(a, b, c):
if(a == 1):
if(b == 1):
if(c == 1):
return "Higher chances of an increase"
else:
return "Medium chances of an decrease"
elif(b == 0):
if(c == 1):
return "Medium chances of an increase"
else:
return "High chances of an decrease"
elif(a == 0):
if(b == 0):
if(c == 0):
return "Higher chances of decrease"
else:
return "Lower chances of increase"
elif(b == 1):
if(c == 0):
return "Lower chances of decrease"
else:
return "Higher chances of increase"
@app.callback(Output('forecast', 'children'),
[Input('selectState', 'value'),
Input('selectCrime', 'value')])
def forecast_update(sstate, scrime):
y1 = df['Year'].max()
y2 = y1-1
y3 = y2-1
y4 = y3-1
x1 = list(df[(df['State/UT'] == sstate) & (df['Year'] == y1)][scrime])
x2 = list(df[(df['State/UT'] == sstate) & (df['Year'] == y2)][scrime])
x3 = list(df[(df['State/UT'] == sstate) & (df['Year'] == y3)][scrime])
x4 = list(df[(df['State/UT'] == sstate) & (df['Year'] == y4)][scrime])
if((sstate == 'Telangana') & (y1 == 2015)):
s1 = 0
s2 = 0
if((x1[0] - x2[0]) > 0):
s3 = 1
else:
s3 = 0
else:
if((x3[0] - x4[0]) > 0):
s1 = 1
else:
s1 = 0
if((x2[0] - x3[0]) > 0):
s2 = 1
else:
s2 = 0
if((x1[0] - x2[0]) > 0):
s3 = 1
else:
s3 = 0
res = pattern(s1, s2, s3)
cast = "> {} has {} in {} in the year {} ,considering constant current policies.".format(
sstate, res, scrime, str(y1+1))
return cast
# TAB 2 ENDS
# TAB 3 STARTS
@app.callback(Output('corr-graphic', 'figure'),
[Input('crimex', 'value'),
Input('crimey', 'value'),
Input('statez', 'value')])
def update_graph(xaxis_name, yaxis_name, state_name):
filter_tf = tf[tf['State/UT'] == state_name]
total = filter_tf['Total Crimes Against Women'].sum()
return {'data': [go.Scatter(x=filter_tf[xaxis_name],
y=filter_tf[yaxis_name],
text=filter_tf['Year'],
mode='markers',
marker=dict(size=(filter_tf['Total Crimes Against Women']/total)*1000,
color=filter_tf['Total Crimes Against Women'], showscale=True)
)],
'layout': go.Layout(title='Crime correlation in '+state_name,
xaxis={'title': xaxis_name},
yaxis={'title': yaxis_name},
hovermode='closest')
}
@app.callback(Output('corrRes', 'children'),
[Input('crimex', 'value'),
Input('crimey', 'value'),
Input('statez', 'value')])
def corr_result(xvalue, yvalue, zvalue):
filter_tf = tf[tf['State/UT'] == zvalue]
Correlation = filter_tf[xvalue].corr(filter_tf[yvalue])
strcorr = str(round(Correlation, 1))
if(strcorr != 'nan'):
r = strcorr
else:
r = '0'
return r
def corr_check(corr):
if(corr > 0.0):
if(corr >= 0.5 and corr < 2.0):
return 'Highly Positive'
elif(corr >= 0.3 and corr < 0.5):
return 'Moderately Positive'
elif(corr < 0.3):
return'Low positive'
elif(corr == 0):
return 'No correlation'
else:
return 'Negative'
@app.callback(Output('corrType', 'children'),
[Input('crimex', 'value'),
Input('crimey', 'value'),
Input('statez', 'value')])
def corr_type(xvalue, yvalue, zvalue):
filter_tf = tf[tf['State/UT'] == zvalue]
Correlation = filter_tf[xvalue].corr(filter_tf[yvalue])
corri = round(Correlation, 1)
strcorr = str(corri)
if(strcorr == 'nan'):
rtype = 'No correlation'
else:
rtype = corr_check(corri)
return rtype
# TAB 3 ENDS
# TAB 4 STARTS:
def parse_contents(contents, filename, date):
content_type, content_string = contents.split(',')
decoded = base64.b64decode(content_string)
try:
if 'csv' in filename:
# Assume that the user uploaded a CSV file
newdf = pd.read_csv(
io.StringIO(decoded.decode('utf-8')))
updatedf = df.append(newdf)
updatedf.to_excel('Data/crime2014.xlsx', index=False)
elif 'xls' in filename:
# Assume that the user uploaded an excel file
newdf = pd.read_excel(io.BytesIO(decoded))
updatedf = df.append(newdf)
updatedf.to_excel('Data/crime2014.xlsx', index=False)
except Exception as e:
print(e)
return html.Div([
'There was an error processing this file.'
])
return html.Div([
html.H5(filename),
html.H6(datetime.datetime.fromtimestamp(date)),
html.H4("Dataframe added!"),
# dash_table.DataTable(
# data=updatedf.to_dict('rows'),
# columns=[{'name': i, 'id': i} for i in updatedf.columns]
# ),
html.Hr(), # horizontal line
# For debugging, display the raw contents provided by the web browser
html.Div('Raw Content'),
html.Pre(contents[0:200] + '...', style={
'whiteSpace': 'pre-wrap',
'wordBreak': 'break-all'
})
])
@app.callback(Output('output-data-upload', 'children'),
[Input('upload-data', 'contents')],
[State('upload-data', 'filename'),
State('upload-data', 'last_modified')])
def update_output(list_of_contents, list_of_names, list_of_dates):
if list_of_contents is not None:
children = [
parse_contents(c, n, d) for c, n, d in
zip(list_of_contents, list_of_names, list_of_dates)]
return children
# TAB 4 ENDS:
# ========================== Route Controller ==================================
@app.callback(Output("content", "children"), [Input("url", "pathname")])
def display_page(pathname):
if pathname == "/home":
return home
if pathname == "/about":
return about
if pathname == "/caw":
return caw
# if not recognised, return 404 message
return html.P("Adding Soon")
# ========================== Server =============================================
if __name__ == "__main__":
app.run_server(debug=True)