def matrix_filter(self, bp_matrix): bp_matrix.matrix_filter = self.name file_name = "%s_%s_%s.pickle" % (bp_matrix.matrix_filter, bp_matrix.algorithm, bp_matrix.orientation) file_name = os.path.join(constants.raw_matrix_folder, file_name) if not self.force_recalculate and pickleutil.has_cache(file_name): return pickleutil.get_cache(file_name) matrix = bp_matrix.matrix for i in xrange(len(matrix)): row = matrix[i] entries = 0 total = 0 for j in xrange(len(row)): val = matrix[i][j] if val != -1: total += val entries += 1 average = self.get_average(total, entries) for j in xrange(len(row)): val = matrix[i][j] if val == -1: matrix[i][j] = average pickleutil.cache_matrix(bp_matrix, file_name) return bp_matrix
def get_batter_matrix(self): if not self.force_recalculate and pickleutil.has_cache(self.batter_file_name): return pickleutil.get_cache(self.batter_file_name) global event_data if event_data is None: event_data = session.query(Event).all() batters = session.query(Batter).all() pitchers = session.query(Pitcher).all() matrix_data = numpy.zeros((len(batters), len(pitchers))) for i in xrange(0, len(batters)): batter = batters[i] batter_events = filter(lambda x: x.batter == batter.mlb_id, event_data) for j in xrange(0, len(pitchers)): pitcher = pitchers[j] sub_score = self.score_batter(batter_events, pitcher.mlb_id) matrix_data[i][j] = sub_score raw_matrix = BatterPitcherMatrix(matrix_data, batters, pitchers, self.base_name, BatterPitcherMatrix.BATTER_ORIENTATION) pickleutil.cache_matrix(raw_matrix, self.batter_file_name) return raw_matrix
def __get_pickles(self): folder = constants.raw_matrix_folder pickle_files = [f for f in os.listdir(folder) if os.path.isfile(os.path.join(folder, f))] pickle_files = filter(lambda x: "average" in x, pickle_files) pickle_files = [os.path.join(folder, f) for f in pickle_files] return [pickleutil.get_cache(x) for x in pickle_files]