Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
20 changes: 20 additions & 0 deletions shapiq/games/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,26 @@ def precompute(self, coalitions: np.ndarray | None = None) -> None:
self.coalition_lookup = coalitions_dict
self.precompute_flag = True

def compute(
self, coalitions: np.ndarray | None = None
) -> tuple[np.ndarray, dict[tuple[int, ...], int], float]:
"""Compute the game values for all or a given set of coalitions.

Args:
coalitions: The coalitions to evaluate.

Returns:
A tuple containing:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deleted the return types because sphinx will display automatically and then we do not have to maintain two parts of the code. :)

- np.ndarray: The values of the coalitions.
- dict[tuple[int, ...], int]: The lookup of the coalitions
- float: The normalization value (optional, if return_normalization is 'True')

"""
coalitions: np.ndarray = self._check_coalitions(coalitions)
game_values = self.value_function(coalitions)

return game_values, self.coalition_lookup, self.normalization_value

def save_values(self, path: Path | str) -> None:
"""Saves the game values to the given path.

Expand Down
2 changes: 2 additions & 0 deletions shapiq/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .sets import (
count_interactions,
generate_interaction_lookup,
generate_interaction_lookup_from_coalitions,
get_explicit_subsets,
pair_subset_sizes,
powerset,
Expand All @@ -23,6 +24,7 @@
"split_subsets_budget",
"get_explicit_subsets",
"generate_interaction_lookup",
"generate_interaction_lookup_from_coalitions",
"transform_coalitions_to_array",
"transform_array_to_coalitions",
"count_interactions",
Expand Down
25 changes: 25 additions & 0 deletions shapiq/utils/sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,31 @@ def generate_interaction_lookup(
}


def generate_interaction_lookup_from_coalitions(
coalitions: np.ndarray,
) -> dict[tuple[Any, ...], int]:
"""Generates a lookup dictionary for interactions based on an array of coalitions.

Args:
coalitions: An array of player coalitions.

Returns:
A dictionary that maps interactions to their index in the values vector

Example:
>>> coalitions = np.array([
... [1, 0, 1],
... [0, 1, 1],
... [1, 1, 0],
... [0, 0, 1]
... ])
>>> generate_interaction_lookup_from_coalitions(coalitions)
{(0, 2): 0, (1, 2): 1, (0, 1): 2, (2,): 3}

"""
return {tuple(np.where(coalition)[0]): idx for idx, coalition in enumerate(coalitions)}


def transform_coalitions_to_array(
coalitions: Collection[tuple[int, ...]],
n_players: int | None = None,
Expand Down
19 changes: 19 additions & 0 deletions tests/tests_games/test_base_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,22 @@ def test_exact_computer_call():
sv = game.exact_values(index=index, order=order)
assert sv.index == index
assert sv.max_order == order


def test_compute():
"""Tests the compute function with and without returned normalization."""
normalization_value = 1.0 # not zero

n_players = 3
game = DummyGame(n=n_players, interaction=(0, 1))

coalitions = np.array([[1, 0, 0], [0, 1, 1]])

# Make sure normalization value is added
game.normalization_value = normalization_value
assert game.normalize

result = game.compute(coalitions=coalitions)
assert len(result[0]) == len(coalitions)
assert result[2] == 1.0 # normalization_value
assert len(result) == 3 # game_values, normalization_value and coalition_lookup
32 changes: 32 additions & 0 deletions tests/tests_utils/test_utils_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from shapiq.utils import (
count_interactions,
generate_interaction_lookup,
generate_interaction_lookup_from_coalitions,
get_explicit_subsets,
pair_subset_sizes,
powerset,
Expand Down Expand Up @@ -110,6 +111,37 @@ def test_generate_interaction_lookup(n, min_order, max_order, expected):
assert generate_interaction_lookup(n, min_order, max_order) == expected


@pytest.mark.parametrize(
("coalitions", "expected"),
[
(
np.array([[1, 0, 1], [0, 1, 1], [1, 1, 0], [0, 0, 1]]),
{(0, 2): 0, (1, 2): 1, (0, 1): 2, (2,): 3},
),
(
np.array([[1, 1, 1], [0, 1, 0], [1, 0, 0], [0, 0, 1]]),
{(0, 1, 2): 0, (1,): 1, (0,): 2, (2,): 3},
),
(
np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
{(0,): 0, (1,): 1, (2,): 2},
),
(
np.array([[1, 1, 0, 1], [0, 0, 1, 1], [1, 0, 1, 0]]),
{(0, 1, 3): 0, (2, 3): 1, (0, 2): 2},
),
(
np.array([[0, 0, 0], [1, 1, 1]]),
{(): 0, (0, 1, 2): 1},
),
],
)
def test_generate_interaction_lookup_from_coalitions(coalitions, expected):
"""Tests the generate_interaction_lookup_from_coalitions function."""
result = generate_interaction_lookup_from_coalitions(coalitions)
assert result == expected


@pytest.mark.parametrize(
("coalitions", "n_player", "expected"),
[
Expand Down