Skip to content

Commit

Permalink
minor optimizations (#13)
Browse files Browse the repository at this point in the history
* makes roster warning debug. adds dataframe methods

- removes relative imports

* bumps version

* fix
  • Loading branch information
bdilday committed Jun 12, 2020
1 parent 893b893 commit 5257c35
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
3 changes: 2 additions & 1 deletion pychadwick/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from pandas import Int32Dtype
from pychadwick.libutils import ChadwickLibrary

_version = "0.2.1"

_version = "0.2.2"

__all__ = ["ChadwickLibrary"]

Expand Down
46 changes: 38 additions & 8 deletions pychadwick/chadwick.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

import pandas as pd

from .league import CWLeague
from .game import CWGame
from .gameiter import CWGameIterator
from .roster import CWRoster
from .utils import CWEventFieldStruct
from . import EVENT_DATA_TYPES, ChadwickLibrary
from pychadwick.league import CWLeague
from pychadwick.game import CWGame
from pychadwick.gameiter import CWGameIterator
from pychadwick.roster import CWRoster
from pychadwick.utils import CWEventFieldStruct
from pychadwick import EVENT_DATA_TYPES, ChadwickLibrary


class Chadwick:
Expand Down Expand Up @@ -193,11 +193,11 @@ def process_game(self, game_ptr, roster_visitor=None, roster_home=None):
gameiter = self.cw_gameiter_create(game_ptr)

if not roster_visitor:
logging.warning("roster for %s is undefined.", "visitor")
logging.debug("roster for %s is undefined.", "visitor")
roster_visitor = pointer(CWRoster())

if not roster_home:
logging.warning("roster for %s is undefined.", "home")
logging.debug("roster for %s is undefined.", "home")
roster_home = pointer(CWRoster())

event_str = create_string_buffer(b" ", 4096)
Expand Down Expand Up @@ -229,6 +229,17 @@ def convert_data_frame_types(df, data_type_mapping):
raise TypeError
return df

def games_to_dataframe(self, games, data_type_mapping=None):
if data_type_mapping is None:
data_type_mapping = EVENT_DATA_TYPES
dfs = [
pd.DataFrame(list(self.process_game(game_ptr)), dtype="f8")
for game_ptr in games
]
return self.convert_data_frame_types(
pd.concat(dfs, axis=0, ignore_index=True), data_type_mapping
)

def game_to_dataframe(self, game_ptr, data_type_mapping=None):
if data_type_mapping is None:
data_type_mapping = EVENT_DATA_TYPES
Expand All @@ -237,6 +248,25 @@ def game_to_dataframe(self, game_ptr, data_type_mapping=None):
data_type_mapping,
)

def event_file_to_dataframe(self, event_file, data_type_mapping=None):
if data_type_mapping is None:
data_type_mapping = EVENT_DATA_TYPES
return self.games_to_dataframe(self.games(event_file), data_type_mapping)

def event_files_to_dataframe(self, event_files, data_type_mapping=None):
if data_type_mapping is None:
data_type_mapping = EVENT_DATA_TYPES
data = []
for event_file in event_files:
games = self.games(event_file)
for game in games:
data += list(self.process_game(game))
return self.convert_data_frame_types(
pd.DataFrame(data, dtype="f8"),
data_type_mapping,
)


def register_function(self, func_name, func_arg_types, func_res_type):
func = self.libchadwick.__getattr__(func_name)
func.argtypes = func_arg_types
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.2.1
current_version = 0.2.2

[flake8]
max-line-length = 90
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
long_description = fh.read()
setup(
name="pychadwick",
version="0.2.1",
version="0.2.2",
author="Ben Dilday",
author_email="ben.dilday.phd@gmail.com",
description="Python bindings to the Chadwick library",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def test_load_games_to_df(chadwick, team_events):
f"https://raw.githubusercontent.com/chadwickbureau/retrosheet/master/event/regular/{team_event}"
)
games = chadwick.games(event_path)
dfs = [chadwick.game_to_dataframe(game) for game in games]
df = chadwick.games_to_dataframe(games)

games = chadwick.games(event_path)
df = chadwick.game_to_dataframe(next(games))


def test_game_to_csv(chadwick, team_events):
Expand Down

0 comments on commit 5257c35

Please sign in to comment.