Skip to content
Open
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
bb7d854
added BasketballDataset class
not-heavychevy Apr 10, 2025
2abeeff
added BasketballPitchDimensions class
not-heavychevy Apr 10, 2025
bd59522
added graph settings
not-heavychevy Apr 10, 2025
8a83938
added optimized graph converter
not-heavychevy Apr 10, 2025
f5071c6
added ball handling
not-heavychevy Apr 10, 2025
26d6d85
added init files
not-heavychevy Apr 10, 2025
f2d164b
bugfix dataset load() bug
not-heavychevy Apr 10, 2025
d86c0af
added tests
not-heavychevy Apr 10, 2025
d1c0c73
added additional fields computation
not-heavychevy Apr 10, 2025
64f5ee3
BasketballDataset inherits from DefaultDataset
not-heavychevy Apr 12, 2025
835cd59
bugfix
not-heavychevy Apr 12, 2025
98f09ae
files read with kloppy.io
not-heavychevy Apr 19, 2025
0502aa7
added norm parameters
not-heavychevy Apr 19, 2025
d2f6b52
refactor: move get_dataframe to DefaultDataset
not-heavychevy Apr 20, 2025
53ea444
created post_init
not-heavychevy Apr 20, 2025
3482bf9
added self.settings to BasketballDataset
not-heavychevy Apr 20, 2025
51a6657
added add_dummy_labels и add_graph_ids
not-heavychevy Apr 20, 2025
1352f80
rewritten tests for dataset.py
not-heavychevy Apr 21, 2025
b0fc5c1
Refactor BasketballPitchDimensions
not-heavychevy Apr 25, 2025
1e04bfd
added tests for BasketballPitchDimensions
not-heavychevy Apr 25, 2025
627fae8
Refactor BasketballGraphSettings
not-heavychevy Apr 25, 2025
1bdd740
added tests for BasketballGraphSettings
not-heavychevy Apr 25, 2025
7c64156
Merge PitchDimensions and GraphSettings
not-heavychevy Apr 25, 2025
a70739c
graph_settings test update
not-heavychevy Apr 25, 2025
ebe0914
import bugs fix
not-heavychevy Apr 25, 2025
2dcd3fb
graph_converter refactoring
not-heavychevy Apr 26, 2025
4b96024
dataset separator bugfix
not-heavychevy Apr 26, 2025
af3a02a
added tests for graph_converter
not-heavychevy Apr 26, 2025
8a47337
moved the functionality to “features”
not-heavychevy Apr 26, 2025
633afca
tests update
not-heavychevy Apr 26, 2025
7463b1e
tests fix
not-heavychevy Apr 26, 2025
dcfa8e4
Deprecate speed/acceleration thresholds
not-heavychevy Apr 26, 2025
1b5bc3b
unify data/settings access on DefaultDataset
not-heavychevy Apr 26, 2025
7eb2081
Refactor _convert to use polars methods
not-heavychevy Apr 26, 2025
b0b9d72
Add unified graph-export API to GraphConverter
not-heavychevy Apr 26, 2025
e55d30e
added new tests for public export API
not-heavychevy Apr 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added tests for graph_converter
  • Loading branch information
not-heavychevy committed Apr 26, 2025
commit af3a02a7c6f93a8aa23cf4a41c7963b6ee91c198
111 changes: 111 additions & 0 deletions tests/test_basketball.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,114 @@ def test_graph_settings_custom_values_and_inheritance():
assert hasattr(settings,"self_loop_ball")
assert hasattr(settings,"adjacency_matrix_type")

# New tests for updated BasketballGraphConverter

def test_graph_converter_internal_methods(sample_basketball_json):
"""
Test internal methods of BasketballGraphConverter using real data.
"""
ds = BasketballDataset(tracking_data=str(sample_basketball_json))
ds.add_graph_ids(by=["game_id", "event_id", "frame_id"], column_name="graph_id")
ds.add_dummy_labels(by=["game_id", "event_id", "frame_id"], column_name="label")

settings = BasketballGraphSettings()
settings.normalize_coordinates = True

converter = BasketballGraphConverter(dataset=ds, settings=settings)

frame0 = ds.get_dataframe().filter(pl.col("frame_id") == 0)
records = frame0.to_dicts()

x, teams = converter._compute_node_features(records)
assert isinstance(x, np.ndarray)
assert x.ndim == 2 and x.shape[0] == len(records)
assert x[0, 0] == pytest.approx(records[0]["x"] / settings.pitch_dimensions.court_length)
assert teams == [rec["team"] for rec in records]

A = converter._compute_adjacency(teams)
assert isinstance(A, np.ndarray)
assert A.shape == (len(records), len(records))
for i in range(len(records)):
for j in range(len(records)):
if teams[i] != teams[j]:
assert A[i, j] == 0.0

E = converter._compute_edge_features(x)
assert isinstance(E, np.ndarray)
assert E.shape == (len(records), len(records))
assert all(E[i, i] == pytest.approx(0.0) for i in range(len(records)))
dist = np.linalg.norm(x[0] - x[1])
assert E[0, 1] == pytest.approx(dist)
assert E[1, 0] == pytest.approx(dist)

def test_apply_settings_mapping(sample_basketball_json):
ds = BasketballDataset(tracking_data=str(sample_basketball_json))
ds.add_graph_ids(by=["game_id", "event_id", "frame_id"], column_name="graph_id")
ds.add_dummy_labels(by=["game_id", "event_id", "frame_id"], column_name="label")

settings = BasketballGraphSettings()
settings.pad = True
settings.random_seed = 123

converter = BasketballGraphConverter(dataset=ds, settings=settings)
mapping = converter._apply_settings()

assert mapping == {
"ball_carrier_threshold": 5.0,
"self_loop_ball": True,
"adjacency_matrix_type": AdjacencyMatrixType.SPLIT_BY_TEAM,
"adjacency_matrix_connect_type": AdjacenyMatrixConnectType.BALL,
"pad": True,
"random_seed": 123,
"label_type": PredictionLabelType.BINARY,
}

def test_graph_converter_init_raises_on_non_dataframe_dataset():
settings = BasketballGraphSettings()
bad_ds = BasketballDataset.__new__(BasketballDataset)
bad_ds.data = "not a dataframe"
from unravel.basketball.graphs.graph_converter import BasketballGraphConverter
with pytest.raises(AttributeError):
_ = BasketballGraphConverter(dataset=bad_ds, settings=settings)

def test_post_init_exprs_variables(sample_basketball_json):
ds = BasketballDataset(tracking_data=str(sample_basketball_json))
ds.add_graph_ids(by=["game_id", "event_id", "frame_id"], column_name="graph_id")
settings = BasketballGraphSettings()
conv = BasketballGraphConverter(dataset=ds, settings=settings)
assert set(conv._exprs_variables["node_feature_cols"]) == {"x", "y", "vx", "vy", "speed", "acceleration"}
assert conv._exprs_variables["label_col"] == "label"
assert conv._exprs_variables["graph_id_col"] == "graph_id"

def test_custom_graph_feature_cols(sample_basketball_json):
ds = BasketballDataset(tracking_data=str(sample_basketball_json))
ds.add_graph_ids(by=["game_id","event_id","frame_id"],column_name="graph_id")
ds.add_dummy_labels(by=["game_id","event_id","frame_id"],column_name="label")
settings = BasketballGraphSettings()
cols = ["x","y"]
conv = BasketballGraphConverter(dataset=ds, settings=settings, graph_feature_cols=cols)
assert conv._exprs_variables["node_feature_cols"] == cols

def test_compute_smoke(sample_basketball_json):
ds = BasketballDataset(tracking_data=str(sample_basketball_json))
ds.add_graph_ids(by=["game_id","event_id","frame_id"], column_name="graph_id")
ds.add_dummy_labels(by=["game_id","event_id","frame_id"], column_name="label")

settings = BasketballGraphSettings()
settings.normalize_coordinates = True

converter = BasketballGraphConverter(dataset=ds, settings=settings)
df = converter.compute()

assert isinstance(df, pl.DataFrame)
assert set(df.columns) == {"id", "x", "a", "e", "y"}

unique_frames = (
ds.get_dataframe()
.select("frame_id")
.unique()
.to_series()
.to_list()
)
assert df.height == len(unique_frames)