-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
701 lines (608 loc) · 21.9 KB
/
app.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# Import required libraries
import pickle
import copy
import pathlib
import dash
import math
import datetime as dt
import pandas as pd
from dash.dependencies import Input, Output, State, ClientsideFunction
import dash_core_components as dcc
import dash_html_components as html
# Multi-dropdown options
from controls import COUNTIES, WELL_STATUSES, WELL_TYPES, WELL_COLORS
# get relative data folder
PATH = pathlib.Path(__file__).parent
DATA_PATH = PATH.joinpath("data").resolve()
app = dash.Dash(
__name__, meta_tags=[{"name": "viewport", "content": "width=device-width"}]
)
server = app.server
# Create controls
county_options = [
{"label": str(COUNTIES[county]), "value": str(county)} for county in COUNTIES
]
well_status_options = [
{"label": str(WELL_STATUSES[well_status]), "value": str(well_status)}
for well_status in WELL_STATUSES
]
well_type_options = [
{"label": str(WELL_TYPES[well_type]), "value": str(well_type)}
for well_type in WELL_TYPES
]
# Load data
df = pd.read_csv(DATA_PATH.joinpath("wellspublic.csv"), low_memory=False)
df["Date_Well_Completed"] = pd.to_datetime(df["Date_Well_Completed"])
df = df[df["Date_Well_Completed"] > dt.datetime(1960, 1, 1)]
trim = df[["API_WellNo", "Well_Type", "Well_Name"]]
trim.index = trim["API_WellNo"]
dataset = trim.to_dict(orient="index")
points = pickle.load(open(DATA_PATH.joinpath("points.pkl"), "rb"))
# Create global chart template
mapbox_access_token = "pk.eyJ1IjoiamFja2x1byIsImEiOiJjajNlcnh3MzEwMHZtMzNueGw3NWw5ZXF5In0.fk8k06T96Ml9CLGgKmk81w"
layout = dict(
autosize=True,
automargin=True,
margin=dict(l=30, r=30, b=20, t=40),
hovermode="closest",
plot_bgcolor="#F9F9F9",
paper_bgcolor="#F9F9F9",
legend=dict(font=dict(size=10), orientation="h"),
title="Satellite Overview",
)
# Create app layout
app.layout = html.Div(
[
dcc.Store(id="aggregate_data"),
# empty Div to trigger javascript file for graph resizing
html.Div(id="output-clientside"),
html.Div(
[
html.Div(
[
html.Img(
src=app.get_asset_url("daml.png"),
id="plotly-image",
style={
"height": "60px",
"width": "auto",
"margin-bottom": "25px",
"margin-right": "10px"
},
),
html.Img(
src=app.get_asset_url("bigspoon.png"),
id="plotly-image2",
style={
"height": "60px",
"width": "auto",
"margin-bottom": "25px",
},
)
],
className="one-third column",
),
html.Div(
[
html.Div(
[
html.H3(
"Big Spoon Roasters",
style={"margin-bottom": "0px"},
),
html.H5(
"Revenue Overview", style={"margin-top": "0px"}
),
]
)
],
className="one-half column",
id="title",
),
],
id="header",
className="row flex-display",
style={"margin-bottom": "25px"},
),
html.Div(
[
html.Div(
[
html.P(
"Filter by date (or select range in histogram):",
className="control_label",
),
dcc.RangeSlider(
id="year_slider",
min=1960,
max=2017,
value=[1990, 2010],
className="dcc_control",
),
html.P("Filter by Product Type", className="control_label"),
dcc.RadioItems(
id="well_status_selector",
options=[
{"label": "All ", "value": "all"},
{"label": "Active only ", "value": "active"},
{"label": "Customize ", "value": "custom"},
],
value="active",
labelStyle={"display": "inline-block"},
className="dcc_control",
),
dcc.Dropdown(
id="well_statuses",
options=well_status_options,
multi=True,
value=list(WELL_STATUSES.keys()),
className="dcc_control",
),
dcc.Checklist(
id="lock_selector",
options=[{"label": "Lock camera", "value": "locked"}],
className="dcc_control",
value=[],
),
html.P("Filter by payment type:", className="control_label"),
dcc.RadioItems(
id="well_type_selector",
options=[
{"label": "All ", "value": "all"},
{"label": "Cash ", "value": "productive"},
{"label": "Credit/Debit ", "value": "custom"},
],
value="productive",
labelStyle={"display": "inline-block"},
className="dcc_control",
),
dcc.Dropdown(
id="well_types",
options=well_type_options,
multi=True,
value=list(WELL_TYPES.keys()),
className="dcc_control",
),
],
className="pretty_container four columns",
id="cross-filter-options",
),
html.Div(
[
html.Div(
[
html.Div(
[html.H6(id="well_text"), html.P("No. of Bar Sales")],
id="wells",
className="mini_container",
),
html.Div(
[html.H6(id="waterText"), html.P("$ from Bar Sales")],
id="water",
className="mini_container",
),
html.Div(
[html.H6(id="gasText"), html.P("No. of Nut Butter Sales")],
id="gas",
className="mini_container",
),
html.Div(
[html.H6(id="oilText"), html.P("$ from Nut Butter Sales")],
id="oil",
className="mini_container",
),
],
id="info-container",
className="row container-display",
),
html.Div(
[dcc.Graph(id="count_graph")],
id="countGraphContainer",
className="pretty_container",
),
],
id="right-column",
className="eight columns",
),
],
className="row flex-display",
),
html.Div(
[
],
className="row flex-display",
),
html.Div(
[
html.Div(
[dcc.Graph(id="pie_graph")],
className="pretty_container seven columns",
),
],
className="row flex-display",
),
],
id="mainContainer",
style={"display": "flex", "flex-direction": "column"},
)
# Helper functions
def human_format(num):
if num == 0:
return "0"
magnitude = int(math.log(num, 1000))
mantissa = str(int(num / (1000 ** magnitude)))
return mantissa + ["", "K", "M", "G", "T", "P"][magnitude]
def filter_dataframe(df, well_statuses, well_types, year_slider):
dff = df[
df["Well_Status"].isin(well_statuses)
& df["Well_Type"].isin(well_types)
& (df["Date_Well_Completed"] > dt.datetime(year_slider[0], 1, 1))
& (df["Date_Well_Completed"] < dt.datetime(year_slider[1], 1, 1))
]
return dff
def produce_individual(api_well_num):
try:
points[api_well_num]
except:
return None, None, None, None
index = list(
range(min(points[api_well_num].keys()), max(points[api_well_num].keys()) + 1)
)
gas = []
oil = []
water = []
for year in index:
try:
gas.append(points[api_well_num][year]["Gas Produced, MCF"])
except:
gas.append(0)
try:
oil.append(points[api_well_num][year]["Oil Produced, bbl"])
except:
oil.append(0)
try:
water.append(points[api_well_num][year]["Water Produced, bbl"])
except:
water.append(0)
return index, gas, oil, water
def produce_aggregate(selected, year_slider):
index = list(range(max(year_slider[0], 1985), 2016))
gas = []
oil = []
water = []
for year in index:
count_gas = 0
count_oil = 0
count_water = 0
for api_well_num in selected:
try:
count_gas += points[api_well_num][year]["Gas Produced, MCF"]
except:
pass
try:
count_oil += points[api_well_num][year]["Oil Produced, bbl"]
except:
pass
try:
count_water += points[api_well_num][year]["Water Produced, bbl"]
except:
pass
gas.append(count_gas)
oil.append(count_oil)
water.append(count_water)
return index, gas, oil, water
# Create callbacks
app.clientside_callback(
ClientsideFunction(namespace="clientside", function_name="resize"),
Output("output-clientside", "children"),
[Input("count_graph", "figure")],
)
@app.callback(
Output("aggregate_data", "data"),
[
Input("well_statuses", "value"),
Input("well_types", "value"),
Input("year_slider", "value"),
],
)
def update_production_text(well_statuses, well_types, year_slider):
dff = filter_dataframe(df, well_statuses, well_types, year_slider)
selected = dff["API_WellNo"].values
index, gas, oil, water = produce_aggregate(selected, year_slider)
return [human_format(sum(gas)), human_format(sum(oil)), human_format(sum(water))]
# Radio -> multi
@app.callback(
Output("well_statuses", "value"), [Input("well_status_selector", "value")]
)
def display_status(selector):
if selector == "all":
return list(WELL_STATUSES.keys())
elif selector == "active":
return ["AC"]
return []
# Radio -> multi
@app.callback(Output("well_types", "value"), [Input("well_type_selector", "value")])
def display_type(selector):
if selector == "all":
return list(WELL_TYPES.keys())
elif selector == "productive":
return ["GD", "GE", "GW", "IG", "IW", "OD", "OE", "OW"]
return []
# Slider -> count graph
@app.callback(Output("year_slider", "value"), [Input("count_graph", "selectedData")])
def update_year_slider(count_graph_selected):
if count_graph_selected is None:
return [1990, 2010]
nums = [int(point["pointNumber"]) for point in count_graph_selected["points"]]
return [min(nums) + 1960, max(nums) + 1961]
# Selectors -> well text
@app.callback(
Output("well_text", "children"),
[
Input("well_statuses", "value"),
Input("well_types", "value"),
Input("year_slider", "value"),
],
)
def update_well_text(well_statuses, well_types, year_slider):
dff = filter_dataframe(df, well_statuses, well_types, year_slider)
return dff.shape[0]
@app.callback(
[
Output("gasText", "children"),
Output("oilText", "children"),
Output("waterText", "children"),
],
[Input("aggregate_data", "data")],
)
def update_text(data):
return data[0] , data[1] , data[2]
# Selectors -> main graph
def make_main_figure(
well_statuses, well_types, year_slider, selector, main_graph_layout
):
dff = filter_dataframe(df, well_statuses, well_types, year_slider)
traces = []
figure = dict(data=traces, layout=layout)
return figure
# Main graph -> individual graph
def make_individual_figure(main_graph_hover):
layout_individual = copy.deepcopy(layout)
if main_graph_hover is None:
main_graph_hover = {
"points": [
{"curveNumber": 4, "pointNumber": 569, "customdata": 31101173130000}
]
}
chosen = [point["customdata"] for point in main_graph_hover["points"]]
index, gas, oil, water = produce_individual(chosen[0])
if index is None:
annotation = dict(
text="No data available",
x=0.5,
y=0.5,
align="center",
showarrow=False,
xref="paper",
yref="paper",
)
layout_individual["annotations"] = [annotation]
data = []
else:
data = [
dict(
type="scatter",
mode="lines+markers",
name="Gas Produced (mcf)",
x=index,
y=gas,
line=dict(shape="spline", smoothing=2, width=1, color="#fac1b7"),
marker=dict(symbol="diamond-open"),
),
dict(
type="scatter",
mode="lines+markers",
name="Oil Produced (bbl)",
x=index,
y=oil,
line=dict(shape="spline", smoothing=2, width=1, color="#a9bb95"),
marker=dict(symbol="diamond-open"),
),
dict(
type="scatter",
mode="lines+markers",
name="Water Produced (bbl)",
x=index,
y=water,
line=dict(shape="spline", smoothing=2, width=1, color="#92d8d8"),
marker=dict(symbol="diamond-open"),
),
]
layout_individual["title"] = dataset[chosen[0]]["Well_Name"]
figure = dict(data=data, layout=layout_individual)
return figure
def make_aggregate_figure(well_statuses, well_types, year_slider, main_graph_hover):
layout_aggregate = copy.deepcopy(layout)
if main_graph_hover is None:
main_graph_hover = {
"points": [
{"curveNumber": 4, "pointNumber": 569, "customdata": 31101173130000}
]
}
chosen = [point["customdata"] for point in main_graph_hover["points"]]
well_type = dataset[chosen[0]]["Well_Type"]
dff = filter_dataframe(df, well_statuses, well_types, year_slider)
selected = dff[dff["Well_Type"] == well_type]["API_WellNo"].values
index, gas, oil, water = produce_aggregate(selected, year_slider)
data = [
dict(
type="scatter",
mode="lines",
name="Gas Produced (mcf)",
x=index,
y=gas,
line=dict(shape="spline", smoothing="2", color="#F9ADA0"),
),
dict(
type="scatter",
mode="lines",
name="Oil Produced (bbl)",
x=index,
y=oil,
line=dict(shape="spline", smoothing="2", color="#849E68"),
),
dict(
type="scatter",
mode="lines",
name="Water Produced (bbl)",
x=index,
y=water,
line=dict(shape="spline", smoothing="2", color="#59C3C3"),
),
]
layout_aggregate["title"] = "Aggregate: " + WELL_TYPES[well_type]
figure = dict(data=data, layout=layout_aggregate)
return figure
def make_pie_figure(well_statuses, well_types, year_slider):
layout_pie = copy.deepcopy(layout)
dff = filter_dataframe(df, well_statuses, well_types, year_slider)
selected = dff["API_WellNo"].values
index, gas, oil, water = produce_aggregate(selected, year_slider)
aggregate = dff.groupby(["Well_Type"]).count()
data = [
dict(
type="pie",
labels=["Gas", "Oil", "Water"],
values=[sum(gas), sum(oil), sum(water)],
name="Production Breakdown",
text=[
"Total Gas Produced (mcf)",
"Total Oil Produced (bbl)",
"Total Water Produced (bbl)",
],
hoverinfo="text+value+percent",
textinfo="label+percent+name",
hole=0.5,
marker=dict(colors=["#fac1b7", "#a9bb95", "#92d8d8"]),
domain={"x": [0, 0.45], "y": [0.2, 0.8]},
),
dict(
type="pie",
labels=[WELL_TYPES[i] for i in aggregate.index],
values=aggregate["API_WellNo"],
name="Well Type Breakdown",
hoverinfo="label+text+value+percent",
textinfo="label+percent+name",
hole=0.5,
marker=dict(colors=[WELL_COLORS[i] for i in aggregate.index]),
domain={"x": [0.55, 1], "y": [0.2, 0.8]},
),
]
layout_pie["title"] = "Production Summary: {} to {}".format(
year_slider[0], year_slider[1]
)
layout_pie["font"] = dict(color="#777777")
layout_pie["legend"] = dict(
font=dict(color="#CCCCCC", size="10"), orientation="h", bgcolor="rgba(0,0,0,0)"
)
figure = dict(data=data, layout=layout_pie)
return figure
# Selectors -> count graph
@app.callback(
Output("count_graph", "figure"),
[
Input("well_statuses", "value"),
Input("well_types", "value"),
Input("year_slider", "value"),
],
)
def make_count_figure(well_statuses, well_types, year_slider):
layout_count = copy.deepcopy(layout)
dff = filter_dataframe(df, well_statuses, well_types, [1960, 2017])
g = dff[["API_WellNo", "Date_Well_Completed"]]
g.index = g["Date_Well_Completed"]
g = g.resample("A").count()
colors = []
for i in range(1960, 2018):
if i >= int(year_slider[0]) and i < int(year_slider[1]):
colors.append("rgb(123, 199, 255)")
else:
colors.append("rgba(123, 199, 255, 0.2)")
data = [
dict(
type="scatter",
mode="markers",
x=g.index,
y=g["API_WellNo"] / 2,
name="All Wells",
opacity=0,
hoverinfo="skip",
),
dict(
type="bar",
x=g.index,
y=g["API_WellNo"],
name="All Wells",
marker=dict(color=colors),
),
]
layout_count["title"] = "Total Sales/Year"
layout_count["dragmode"] = "select"
layout_count["showlegend"] = False
layout_count["autosize"] = True
figure = dict(data=data, layout=layout_count)
return figure
# Selectors, main graph -> pie graph
@app.callback(
Output("pie_graph", "figure"),
[
Input("well_statuses", "value"),
Input("well_types", "value"),
Input("year_slider", "value"),
],
)
def make_pie_figure(well_statuses, well_types, year_slider):
layout_pie = copy.deepcopy(layout)
dff = filter_dataframe(df, well_statuses, well_types, year_slider)
selected = dff["API_WellNo"].values
index, gas, oil, water = produce_aggregate(selected, year_slider)
aggregate = dff.groupby(["Well_Type"]).count()
data = [
dict(
type="pie",
labels=["eTicket", "In-Person", "Resale"],
values=[sum(gas), sum(oil), sum(water)],
name="Production Breakdown",
text=[
"Total $ from eTicket Sales",
"Total $ from In-Person Ticket Sales",
"Total $ from Verified Ticket Resales",
],
hoverinfo="text+value+percent",
textinfo="label+percent+name",
hole=0.5,
marker=dict(colors=["#fac1b7", "#a9bb95", "#92d8d8"]),
domain={"x": [0, 0.45], "y": [0.2, 0.8]},
),
dict(
type="pie",
labels=[WELL_TYPES[i] for i in aggregate.index],
values=aggregate["API_WellNo"],
name="Well Type Breakdown",
hoverinfo="label+text+value+percent",
textinfo="label+percent+name",
hole=0.5,
marker=dict(colors=[WELL_COLORS[i] for i in aggregate.index]),
domain={"x": [0.55, 1], "y": [0.2, 0.8]},
),
]
layout_pie["title"] = "Revenue Summary"
layout_pie["font"] = dict(color="#777777")
layout_pie["legend"] = dict(
font=dict(color="#CCCCCC", size="10"), orientation="h", bgcolor="rgba(0,0,0,0)"
)
figure = dict(data=data, layout=layout_pie)
return figure
# Main
if __name__ == "__main__":
app.run_server(debug=True)