Backend refactor to allow custom functions #29
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds:
Refactor Feature Calculations
A refactor for
compute_edge_features_pl,compute_node_features_plandcompute_adjacency_matrix_pl. They are renamed tocompute_edge_features,compute_node_featuresandcompute_adjacency_matrix.They now look like something like this under the hood:
Edge / Node Feature Functions
self.node_feature_funcsandself.edge_feature_funcsare now both a list of functions that should take in**kwargs. For example:Passing this
x_normedfunction inSoccerGraphConverterPolars(..., node_feature_funcs=[x_normed])now computes the normalized x value as defined in the above function.The default functions that were initially the edge and node features in this package have now been declared as the default features properties (see below).
Custom Functions
We can also pass custom edge and node feature functions. Any of these functions needs to be decorated with
@graph_featuredecorator. This decorator takes 2 parametersis_customandfeature_type("node" or "edge").This will simply make sure the user consciously adding the correct functions to the correct list (node or edge features).
Additionally we introduce
additional_feature_colstoSoccerGraphConverterPolarswhich allows us to pass columns we've added to ourKloppyPolarsData.datadataframe which we can subsequently access throughkwargsin our custom functions.For example:
This will now add an extra edge feature to our edge feature matrix with value "0.90" for all players.
Custom node feature functions need to return a (N, ) or a (N, k) numpy array. Custom edge features need to return a (N, N) or tuple of multiple (N, N) numpy arrays.
Gobal Features
We also show above how to add
global_feature_colswhich. These values are joined to thenode_featuresto the "ball" or to "all" by settingglobal_feature_type(defaults to "ball").Feature Options
We have also added
feature_optstoDefaultGraphConverter. This is a dictionary with additional information we might want to use inside our custom functions. For example, if we want to normalize height in centimeters between 0 and 1, we can addfeature_opts={"max_height": 200}and pass it to theSoccerGraphConverterPolars. Subsequently we can access this maximum height in our custom node feature function via kwargs:For this to work we also need to pass
additional_feature_cols=["height_cm"]toSoccerGraphConverterPolarsand we would need to join a "height_cm" column to ourKloppyPolarsDataset.dataotherwise "height_cm" would not be in kwargs.