-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcommon.py
More file actions
326 lines (264 loc) · 11.3 KB
/
common.py
File metadata and controls
326 lines (264 loc) · 11.3 KB
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
import datetime
import json
import logging
import pickle
import re
import nltk
UNK = "<UNK>"
class Label(object):
HR = "HR"
K = "K"
OTHER = "OTHER"
@classmethod
def get_all(cls):
return [ k for k in dir(cls) if not k.startswith("_") and k != "get_all" ]
class GameState(object):
__slots__ = [
"visteam",
"hometeam",
"visteam_pitcherid",
"hometeam_pitcherid"
]
def __str__(self):
return "<GameState: %s>" % ", ".join([ "%s=%s" % (slot, getattr(self, slot, None)) for slot in sorted(self.__slots__) ])
class PlayerState(object):
lahman_stats = ["birthYear",
"birthMonth",
"birthDay",
"birthCountry",
"birthState",
"birthCity",
"weight",
"height",
"bats",
"throws",
"debut",
"finalGame",
"college"]
player_stats = ["retrosheetid",
"visorhome",
"team",
"name",
"fieldpos",
"batpos"]
__slots__ = player_stats \
+ [ "lahman_%s" % stat for stat in lahman_stats ]
def __str__(self):
return "<PlayerState: %s>" % ", ".join([ "%s=%s" % (slot, getattr(self, slot, None)) for slot in sorted(self.__slots__) ])
def __repr__(self):
return self.__str__()
class FeatureSet(object):
__slots__ = [
# general, per-game
"game_daynight",
"game_date",
"game_number", # 0 is the first game of the day, 1 is the second of a doubleheader
"game_site", # ballpark
"game_temp",
"game_winddir",
# at-bat
"ab_inning",
"ab_numballs",
"ab_numstrikes",
# label
"label" ] \
+ [ "batter_%s" % stat for stat in (PlayerState.player_stats + PlayerState.lahman_stats) ] \
+ [ "pitcher_%s" % stat for stat in (PlayerState.player_stats + PlayerState.lahman_stats) ]
def __str__(self):
return self.to_json()
def to_json(self):
d = {}
for slot in self.__slots__:
# all values are strings!
d[slot] = getattr(self, slot, "").strip() or UNK
return json.dumps(d)
@classmethod
def get_parse_map(cls):
if not hasattr(cls, "parse_map"):
player_ints = ["birthYear",
"birthMonth",
"birthDay",
"weight",
"height",
"batpos",
"fieldpos"]
int_fields = ["game_number",
"game_temp",
"ab_inning",
"ab_numballs",
"ab_numstrikes",
"birthYear"] + \
[ "batter_%s" % k for k in player_ints ] + \
[ "pitcher_%s" % k for k in player_ints ]
parse_map = {"game_date": (lambda x: datetime.datetime.strptime(x, "%Y/%m/%d")),
"batter_debut": (lambda x: datetime.datetime.strptime(x, "%m/%d/%Y")),
"pitcher_debut": (lambda x: datetime.datetime.strptime(x, "%m/%d/%Y"))}
for field in int_fields:
parse_map[field] = int
cls.parse_map = parse_map
return cls.parse_map
def copy(self):
# this is very puzzling; why does a new object created in the context of a given object have all the fields of the object in which the new object is created...?
new_obj = FeatureSet()
assert(new_obj.to_json() == self.to_json())
return new_obj
def add_batter_info(self, batter_state):
self.add_player_info(batter_state, "batter")
def add_pitcher_info(self, pitcher_state):
self.add_player_info(pitcher_state, "pitcher")
def add_player_info(self, player_state, prefix):
for field in ["fieldpos", "batpos", "team", "visorhome", "name", "retrosheetid"]:
setattr(self, "%s_%s" % (prefix, field), getattr(player_state, field))
for lahman_field in ["birthYear",
"birthMonth",
"birthDay",
"birthCountry",
"birthState",
"birthCity",
"weight",
"height",
"bats",
"throws",
"debut",
"college"]:
if hasattr(player_state, "lahman_%s" % lahman_field):
setattr(self, "%s_%s" % (prefix, lahman_field), getattr(player_state, "lahman_%s" % lahman_field))
class TrainingDatum(object):
player_stats = ["age", # bucketized by 3
"weight", # bucketized by 10
"height", # bucketized by 3
"team",
"experience", # game date - debut, bucketized by 3
"birthCountry",
"throws"]
batter_stats = ["fieldpos",
"batpos", # bucketized 1-2,3-5,6-7,8-9,10,11 (10 is DH, 11 is PH)
"bats",
"visorhome" ]
__slots__ = ["game_daynight",
"game_month", # bucketized by 2
"game_year", # bucketized by 5
"game_number", # 0 or 1+
"game_site",
"game_temp", # bucketized by 5
"game_winddir", # in or out
"ab_inning", # bucketized: 1-3, 4-6, 7-9, 9+
"ab_numballs",
"ab_numstrikes",
"ab_lrmatchup", # "same" for L vs. R, "opposite" for R vs. R
"label"] \
+ [ "batter_%s" % stat for stat in (player_stats + batter_stats) ] \
+ [ "pitcher_%s" % stat for stat in player_stats ]
@classmethod
def bucketized(cls, val, buckets=None, granularity=None):
if granularity:
lower = (val / granularity) * granularity
upper = ((val / granularity) + 1) * granularity
return "[%d-%d)" % (lower, upper)
if buckets:
for i in reversed(xrange(len(buckets))):
bkt = buckets[i]
if val >= bkt:
if i == (len(buckets) - 1):
# this is the last bucket
return "%d+" % bkt
else:
return "[%d-%d)" % (bkt, buckets[i+1])
raise Exception, "Bucket value %s doesn't fit into buckets: %s" % (val, buckets)
@classmethod
def get_winddir(cls, d):
if d["game_winddir"].startswith("from"):
return "in"
elif d["game_winddir"].startswith("to"):
return "out"
return UNK
@classmethod
def get_lrmatchup(cls, d):
if d["batter_bats"] is UNK:
return UNK
if d["batter_bats"] in "B":
return "same"
if d["pitcher_throws"] is UNK:
return None
return "same" if d["batter_bats"] == d["pitcher_throws"] else "opposite"
@classmethod
def from_featureset_json(cls, json_str):
d = json.loads(json_str)
parse_map = FeatureSet.get_parse_map()
for k in d:
# all keys are strings in the .features format
if d[k] != UNK and k in parse_map:
try:
d[k] = parse_map[k](d[k])
except ValueError:
d[k] = UNK
obj = cls()
def unk_check(obj_key, keys=None):
""" If all specified keys are known, return True. Otherwise,
set UNK on the specified object key. """
if keys is None:
keys = [ obj_key ]
if all( (d[k] != UNK) for k in keys):
# proceed with calculation
return True
else:
setattr(obj, obj_key, UNK)
return False
obj.game_daynight = d["game_daynight"]
if unk_check("game_date"):
obj.game_month = cls.bucketized(d["game_date"].month, granularity=2)
if unk_check("game_date"):
obj.game_year = cls.bucketized(d["game_date"].year, granularity=5)
if unk_check("game_number"):
obj.game_number = cls.bucketized(d["game_number"], buckets=[0,1])
if unk_check("game_temp"):
obj.game_temp = cls.bucketized(d["game_temp"], granularity=5)
obj.game_site = d["game_site"]
obj.game_winddir = cls.get_winddir(d)
if unk_check("ab_inning"):
obj.ab_inning = cls.bucketized(d["ab_inning"], buckets=[1,4,7,10])
obj.ab_numballs = d["ab_numballs"]
obj.ab_numstrikes = d["ab_numstrikes"]
obj.ab_lrmatchup = cls.get_lrmatchup(d)
obj.batter_bats = d["batter_bats"]
obj.batter_fieldpos = d["batter_fieldpos"]
obj.batter_visorhome = d["batter_visorhome"]
if unk_check("batter_batpos"):
obj.batter_batpos = cls.bucketized(d["batter_batpos"], buckets=[1,3,5,8,10,11,12])
obj.label = d["label"]
for prefix in ["batter_", "pitcher_"]:
def key(k):
return prefix + k
def year_from_td(td):
return td.days / 365
if unk_check(key("age"), keys=[key("birthYear"), key("birthMonth"), key("birthDay"), "game_date"]):
birthday = datetime.datetime(d[key("birthYear")], d[key("birthMonth")], d[key("birthDay")])
setattr(obj, key("age"), cls.bucketized(year_from_td(d["game_date"] - birthday), granularity=3))
if unk_check(key("weight")):
setattr(obj, key("weight"), cls.bucketized(d[key("weight")], granularity=10))
if unk_check(key("height")):
setattr(obj, key("height"), cls.bucketized(d[key("height")], granularity=3))
setattr(obj, key("team"), d[key("team")])
setattr(obj, key("throws"), d[key("throws")])
setattr(obj, key("birthCountry"), d[key("birthCountry")])
if unk_check(key("experience"), keys=[key("debut"), "game_date"]):
setattr(obj, key("experience"), cls.bucketized(year_from_td(d["game_date"] - d[key("debut")]), granularity=3))
return obj
def __str__(self):
return "<TrainingDatum: %s>" % ", ".join([ "%s=%s" % (k, getattr(self, k)) for k in sorted(self.__slots__) if hasattr(self, k)])
def to_json(self):
return json.dumps(dict([ (slot, getattr(self,slot)) for slot in self.__slots__ ]))
line_regex = re.compile(r'((?:".*?")|.*?)(?:,|$)')
def csv_split(line):
# there's gotta be a better way to do this
values = line_regex.findall(line)
if not line.endswith(","):
return values[:-1]
return values
def dump_bayes_to_file(bayes_classifier, filename):
pickle.dump({"_label_probdist": bayes_classifier._label_probdist,
"_feature_probdist": bayes_classifier._feature_probdist},
open(filename, "w"))
def load_bayes_from_file(filename):
d = pickle.load(open(filename))
return nltk.NaiveBayesClassifier(d["_label_probdist"], d["_feature_probdist"])