-
Notifications
You must be signed in to change notification settings - Fork 44
/
baseball.py
79 lines (57 loc) · 2.31 KB
/
baseball.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
import os
from collections import OrderedDict
from flask import Flask
from wtforms import fields
from ggplot import (aes, stat_smooth, geom_point, geom_text, ggtitle, ggplot,
xlab, ylab)
import numpy as np
import pandas as pd
from gleam import Page, panels
# setup
stats = ['At-Bats (AB)', 'Runs (R)', 'Hits (H)', 'Doubles (2B)',
'Triples (3B)', 'Home Runs (HR)', 'Runs Batted In (RBI)',
'Stolen Bases (SB)', 'Caught Stealing (CS)', 'Walks (BB)',
'Intentional Walk (IBB)', 'Salary', 'Attendance']
statchoices = [(s, s) for s in stats]
dir = os.path.split(__file__)[0]
players = pd.read_csv(os.path.join(dir, "baseball_data", "players.csv"))
teams = pd.read_csv(os.path.join(dir, "baseball_data", "teams.csv"))
class BaseballInput(panels.InputPanel):
xvar = fields.SelectField(label="X axis", choices=statchoices,
default="Hits (H)")
yvar = fields.SelectField(label="Y axis", choices=statchoices,
default="Runs (R)")
year = fields.IntegerField(label="Year", default=2013)
linear = fields.BooleanField(label="Linear Fit")
shownames = fields.BooleanField(label="Show Names")
class DataScatter(panels.PlotPanel):
height = 500
width = 700
def __init__(self, name, dat, ID_col):
self.name = name
self.dat = dat
self.ID_col = ID_col
panels.PlotPanel.__init__(self)
def plot(self, inputs):
"""Plot the given X and Y axes on a scatter plot"""
if inputs.year not in self.dat.Year.values:
return
if inputs.xvar not in self.dat or inputs.yvar not in self.dat:
return
subdat = self.dat[self.dat.Year == inputs.year]
p = ggplot(subdat, aes(x=inputs.xvar, y=inputs.yvar))
p = p + geom_point()
if inputs.shownames:
p = p + geom_text(aes(label=self.ID_col), vjust=1, hjust=1)
if inputs.linear:
p = p + stat_smooth(color="red", method="lm")
return p
class BaseballGleam(Page):
title = "Baseball Statistics"
input = BaseballInput()
output = panels.TabPanel([DataScatter("Teams", teams, "teamID"),
DataScatter("Players", players, "name")])
app = Flask("BaseballGleam")
BaseballGleam.add_flask(app)
app.debug = True
app.run()