shapiq 1.8.0__cp315-cp315-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lazy_dispatch/__init__.py +27 -0
- lazy_dispatch/isinstance.py +92 -0
- lazy_dispatch/load.py +73 -0
- lazy_dispatch/singledispatch.py +210 -0
- shapiq/__init__.py +164 -0
- shapiq/_version.py +24 -0
- shapiq/approximator/__init__.py +154 -0
- shapiq/approximator/base.py +357 -0
- shapiq/approximator/marginals/__init__.py +6 -0
- shapiq/approximator/marginals/owen.py +173 -0
- shapiq/approximator/marginals/stratified.py +155 -0
- shapiq/approximator/montecarlo/__init__.py +7 -0
- shapiq/approximator/montecarlo/base.py +508 -0
- shapiq/approximator/montecarlo/shapiq.py +167 -0
- shapiq/approximator/montecarlo/svarmiq.py +125 -0
- shapiq/approximator/permutation/__init__.py +7 -0
- shapiq/approximator/permutation/sii.py +195 -0
- shapiq/approximator/permutation/stii.py +292 -0
- shapiq/approximator/permutation/sv.py +182 -0
- shapiq/approximator/proxy/__init__.py +7 -0
- shapiq/approximator/proxy/_models.py +421 -0
- shapiq/approximator/proxy/_routes.py +280 -0
- shapiq/approximator/proxy/proxyshap.py +590 -0
- shapiq/approximator/proxy/proxyspex.py +306 -0
- shapiq/approximator/proxy/regressionmsr.py +120 -0
- shapiq/approximator/regression/__init__.py +23 -0
- shapiq/approximator/regression/base.py +653 -0
- shapiq/approximator/regression/faithful.py +132 -0
- shapiq/approximator/regression/kadd_shap.py +74 -0
- shapiq/approximator/regression/kernelshap.py +84 -0
- shapiq/approximator/regression/kernelshapiq.py +151 -0
- shapiq/approximator/regression/leverageshap.py +508 -0
- shapiq/approximator/regression/oddshap.py +769 -0
- shapiq/approximator/regression/polyshap.py +376 -0
- shapiq/approximator/sampling.py +634 -0
- shapiq/approximator/shapleig/__init__.py +8 -0
- shapiq/approximator/shapleig/_error.py +10 -0
- shapiq/approximator/shapleig/_shapley_math.py +546 -0
- shapiq/approximator/shapleig/_surrogate.py +198 -0
- shapiq/approximator/shapleig/shapleig.py +443 -0
- shapiq/approximator/sparse/__init__.py +9 -0
- shapiq/approximator/sparse/base.py +312 -0
- shapiq/approximator/sparse/spex.py +63 -0
- shapiq/explainer/__init__.py +41 -0
- shapiq/explainer/agnostic.py +132 -0
- shapiq/explainer/base.py +323 -0
- shapiq/explainer/configuration.py +249 -0
- shapiq/explainer/custom_types.py +15 -0
- shapiq/explainer/nn/__init__.py +11 -0
- shapiq/explainer/nn/_util.py +69 -0
- shapiq/explainer/nn/base.py +100 -0
- shapiq/explainer/nn/knn.py +86 -0
- shapiq/explainer/nn/threshold_nn.py +106 -0
- shapiq/explainer/nn/weighted_knn.py +359 -0
- shapiq/explainer/product_kernel/__init__.py +7 -0
- shapiq/explainer/product_kernel/base.py +31 -0
- shapiq/explainer/product_kernel/conversion.py +88 -0
- shapiq/explainer/product_kernel/explainer.py +136 -0
- shapiq/explainer/product_kernel/product_kernel.py +160 -0
- shapiq/explainer/product_kernel/validation.py +55 -0
- shapiq/explainer/tabpfn.py +158 -0
- shapiq/explainer/tabular.py +225 -0
- shapiq/explainer/utils.py +362 -0
- shapiq/explainer/validation.py +141 -0
- shapiq/game.py +763 -0
- shapiq/game_theory/__init__.py +33 -0
- shapiq/game_theory/aggregation.py +392 -0
- shapiq/game_theory/core.py +183 -0
- shapiq/game_theory/exact.py +1065 -0
- shapiq/game_theory/indices.py +308 -0
- shapiq/game_theory/moebius_converter.py +461 -0
- shapiq/imputer/__init__.py +23 -0
- shapiq/imputer/base.py +168 -0
- shapiq/imputer/baseline_imputer.py +174 -0
- shapiq/imputer/gaussian_copula_imputer.py +232 -0
- shapiq/imputer/gaussian_imputer.py +256 -0
- shapiq/imputer/gaussian_imputer_exceptions.py +21 -0
- shapiq/imputer/generative_conditional_imputer.py +207 -0
- shapiq/imputer/marginal_imputer.py +239 -0
- shapiq/imputer/tabpfn_imputer.py +142 -0
- shapiq/interaction_values.py +1417 -0
- shapiq/plot/__init__.py +32 -0
- shapiq/plot/_config.py +41 -0
- shapiq/plot/bar.py +264 -0
- shapiq/plot/beeswarm.py +431 -0
- shapiq/plot/force.py +669 -0
- shapiq/plot/network.py +72 -0
- shapiq/plot/scatter.py +316 -0
- shapiq/plot/sentence.py +203 -0
- shapiq/plot/si_graph.py +778 -0
- shapiq/plot/stacked_bar.py +159 -0
- shapiq/plot/upset.py +161 -0
- shapiq/plot/utils.py +150 -0
- shapiq/plot/waterfall.py +408 -0
- shapiq/py.typed +0 -0
- shapiq/tree/__init__.py +39 -0
- shapiq/tree/base.py +545 -0
- shapiq/tree/cext/subset_tables.hpp +882 -0
- shapiq/tree/conversion/__init__.py +49 -0
- shapiq/tree/conversion/catboost.py +90 -0
- shapiq/tree/conversion/cext/catboost_json.cc +582 -0
- shapiq/tree/conversion/cext/cext.cc +903 -0
- shapiq/tree/conversion/cext/converter.hpp +95 -0
- shapiq/tree/conversion/cext/lightgbm_text.cc +364 -0
- shapiq/tree/conversion/cext/xgboost_ubjson.cc +1190 -0
- shapiq/tree/conversion/cext.cp315-win_amd64.pyd +0 -0
- shapiq/tree/conversion/common.py +96 -0
- shapiq/tree/conversion/edges.py +80 -0
- shapiq/tree/conversion/lightgbm.py +75 -0
- shapiq/tree/conversion/sklearn.py +608 -0
- shapiq/tree/conversion/xgboost.py +90 -0
- shapiq/tree/explainer.py +721 -0
- shapiq/tree/interventional/__init__.py +5 -0
- shapiq/tree/interventional/cext/cext.cc +586 -0
- shapiq/tree/interventional/cext/cohort_fill.hpp +138 -0
- shapiq/tree/interventional/cext/leaf_update.hpp +266 -0
- shapiq/tree/interventional/cext/single_reference_fill.hpp +135 -0
- shapiq/tree/interventional/cext/utils.cpp +106 -0
- shapiq/tree/interventional/cext/weights.cpp +227 -0
- shapiq/tree/interventional/cext.cp315-win_amd64.pyd +0 -0
- shapiq/tree/interventional/computer.py +375 -0
- shapiq/tree/linear/__init__.py +5 -0
- shapiq/tree/linear/cext/cext.cc +230 -0
- shapiq/tree/linear/cext/linear_tree_shap.cc +352 -0
- shapiq/tree/linear/cext.cp315-win_amd64.pyd +0 -0
- shapiq/tree/linear/computer.py +307 -0
- shapiq/tree/precision.py +69 -0
- shapiq/tree/quadrature/__init__.py +5 -0
- shapiq/tree/quadrature/cext/cext.cc +304 -0
- shapiq/tree/quadrature/cext/quadrature_tree_shap.cc +444 -0
- shapiq/tree/quadrature/cext.cp315-win_amd64.pyd +0 -0
- shapiq/tree/quadrature/computer.py +375 -0
- shapiq/tree/subset_layout.py +84 -0
- shapiq/tree/treeshapiq.py +762 -0
- shapiq/tree/utils.py +65 -0
- shapiq/tree/validation.py +95 -0
- shapiq/typing.py +101 -0
- shapiq/utils/__init__.py +38 -0
- shapiq/utils/datasets.py +27 -0
- shapiq/utils/errors.py +25 -0
- shapiq/utils/modules.py +86 -0
- shapiq/utils/saving.py +116 -0
- shapiq/utils/sets.py +383 -0
- shapiq-1.8.0.dist-info/DELVEWHEEL +2 -0
- shapiq-1.8.0.dist-info/METADATA +825 -0
- shapiq-1.8.0.dist-info/RECORD +220 -0
- shapiq-1.8.0.dist-info/WHEEL +5 -0
- shapiq-1.8.0.dist-info/licenses/LICENSE +21 -0
- shapiq-1.8.0.dist-info/top_level.txt +4 -0
- shapiq.libs/msvcp140-a4c2229bdc2a2a630acdc095b4d86008.dll +0 -0
- shapiq.libs/vcomp140-f96f3a14d88d8846f31f3ab38a490304.dll +0 -0
- shapiq_benchmark/__init__.py +68 -0
- shapiq_benchmark/benchmark.py +150 -0
- shapiq_benchmark/computers.py +437 -0
- shapiq_benchmark/datasets/__init__.py +57 -0
- shapiq_benchmark/datasets/_cache.py +166 -0
- shapiq_benchmark/datasets/_imagenette.py +177 -0
- shapiq_benchmark/datasets/_preprocess.py +34 -0
- shapiq_benchmark/datasets/_registry.py +280 -0
- shapiq_benchmark/datasets/_synthetic.py +298 -0
- shapiq_benchmark/datasets/_tabarena.py +135 -0
- shapiq_benchmark/datasets/_tabular.py +510 -0
- shapiq_benchmark/metrics.py +276 -0
- shapiq_benchmark/models.py +312 -0
- shapiq_benchmark/optimization/__init__.py +1 -0
- shapiq_benchmark/optimization/optuna_optimization.py +151 -0
- shapiq_benchmark/runner.py +203 -0
- shapiq_benchmark/setups/__init__.py +105 -0
- shapiq_benchmark/setups/_base.py +422 -0
- shapiq_benchmark/setups/_causal.py +114 -0
- shapiq_benchmark/setups/_language.py +45 -0
- shapiq_benchmark/setups/_ml_games.py +565 -0
- shapiq_benchmark/setups/_structured.py +260 -0
- shapiq_benchmark/setups/_vision.py +169 -0
- shapiq_games/__init__.py +95 -0
- shapiq_games/_base.py +224 -0
- shapiq_games/_optional.py +35 -0
- shapiq_games/_tabpfn.py +115 -0
- shapiq_games/_training.py +213 -0
- shapiq_games/causal.py +272 -0
- shapiq_games/clustering.py +108 -0
- shapiq_games/ensemble_selection.py +235 -0
- shapiq_games/feature_selection.py +114 -0
- shapiq_games/global_xai.py +131 -0
- shapiq_games/kernel/__init__.py +5 -0
- shapiq_games/kernel/product_kernel.py +111 -0
- shapiq_games/language.py +138 -0
- shapiq_games/local_xai.py +265 -0
- shapiq_games/nn/__init__.py +7 -0
- shapiq_games/nn/_base.py +130 -0
- shapiq_games/nn/knn.py +44 -0
- shapiq_games/nn/threshold_nn.py +64 -0
- shapiq_games/nn/weighted_knn.py +112 -0
- shapiq_games/synthetic/__init__.py +7 -0
- shapiq_games/synthetic/_moebius.py +32 -0
- shapiq_games/synthetic/dummy.py +73 -0
- shapiq_games/synthetic/random_table.py +67 -0
- shapiq_games/synthetic/soum.py +171 -0
- shapiq_games/tree/__init__.py +6 -0
- shapiq_games/tree/_output.py +95 -0
- shapiq_games/tree/interventional.py +103 -0
- shapiq_games/tree/path_dependent.py +218 -0
- shapiq_games/typing.py +93 -0
- shapiq_games/uncertainty.py +109 -0
- shapiq_games/unsupervised.py +133 -0
- shapiq_games/valuation.py +285 -0
- shapiq_games/vision/__init__.py +8 -0
- shapiq_games/vision/_batching.py +27 -0
- shapiq_games/vision/_dinov2.py +103 -0
- shapiq_games/vision/_fill.py +77 -0
- shapiq_games/vision/_image_text_model.py +216 -0
- shapiq_games/vision/_preprocess.py +81 -0
- shapiq_games/vision/_region_game.py +170 -0
- shapiq_games/vision/_regions.py +99 -0
- shapiq_games/vision/_resnet.py +76 -0
- shapiq_games/vision/_superpixels.py +47 -0
- shapiq_games/vision/_token_removal.py +192 -0
- shapiq_games/vision/_vit.py +127 -0
- shapiq_games/vision/image_classifier.py +240 -0
- shapiq_games/vision/image_text.py +146 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""Lazy alternatives to singledispatch and isinstance."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
# start delvewheel patch
|
|
5
|
+
def _delvewheel_patch_1_13_2():
|
|
6
|
+
import os
|
|
7
|
+
if os.path.isdir(libs_dir := os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, 'shapiq.libs'))):
|
|
8
|
+
os.add_dll_directory(libs_dir)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
_delvewheel_patch_1_13_2()
|
|
12
|
+
del _delvewheel_patch_1_13_2
|
|
13
|
+
# end delvewheel patch
|
|
14
|
+
|
|
15
|
+
from lazy_dispatch.isinstance import LazyType, lazy_isinstance, lazy_issubclass
|
|
16
|
+
from lazy_dispatch.load import lazy_callable, lazy_import
|
|
17
|
+
from lazy_dispatch.singledispatch import is_valid_dispatch_type, lazydispatch
|
|
18
|
+
|
|
19
|
+
__all__ = [
|
|
20
|
+
"LazyType",
|
|
21
|
+
"is_valid_dispatch_type",
|
|
22
|
+
"lazy_callable",
|
|
23
|
+
"lazy_import",
|
|
24
|
+
"lazy_isinstance",
|
|
25
|
+
"lazy_issubclass",
|
|
26
|
+
"lazydispatch",
|
|
27
|
+
]
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"""A lazy version of isinstance."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from types import UnionType
|
|
6
|
+
from typing import Any, Union, get_args, get_origin
|
|
7
|
+
|
|
8
|
+
LazyType = type | str | UnionType | tuple # type: ignore[assignment]
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _is_union_type(cls: Any) -> bool: # noqa: ANN401
|
|
12
|
+
return get_origin(cls) in {Union, UnionType}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _split_lazy_type(lazy_type: LazyType) -> tuple[set[type], set[str]]:
|
|
16
|
+
"""Split classinfo into a set of types and a set of strings."""
|
|
17
|
+
if isinstance(lazy_type, str):
|
|
18
|
+
return set(), {t.strip() for t in lazy_type.split("|")}
|
|
19
|
+
if isinstance(lazy_type, type):
|
|
20
|
+
return {lazy_type}, set()
|
|
21
|
+
if isinstance(lazy_type, tuple):
|
|
22
|
+
types: set[type] = set()
|
|
23
|
+
strings: set[str] = set()
|
|
24
|
+
for item in lazy_type:
|
|
25
|
+
t, s = _split_lazy_type(item)
|
|
26
|
+
types.update(t)
|
|
27
|
+
strings.update(s)
|
|
28
|
+
return types, strings
|
|
29
|
+
if _is_union_type(lazy_type):
|
|
30
|
+
types = set()
|
|
31
|
+
strings = set()
|
|
32
|
+
for arg in get_args(lazy_type):
|
|
33
|
+
t, s = _split_lazy_type(arg)
|
|
34
|
+
types.update(t)
|
|
35
|
+
strings.update(s)
|
|
36
|
+
return types, strings
|
|
37
|
+
|
|
38
|
+
msg = f"Invalid classinfo: {lazy_type!r}"
|
|
39
|
+
raise TypeError(msg)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _find_matching_string_type(cls: type, string_types: set[str] | dict[str, Any]) -> str | None:
|
|
43
|
+
"""Check if the type's name matches any of the strings."""
|
|
44
|
+
module = cls.__module__
|
|
45
|
+
qualname = cls.__qualname__
|
|
46
|
+
|
|
47
|
+
if module == "builtins":
|
|
48
|
+
for s in string_types:
|
|
49
|
+
if qualname == s:
|
|
50
|
+
return s
|
|
51
|
+
|
|
52
|
+
for s in string_types:
|
|
53
|
+
if f"{module}.{qualname}" == s:
|
|
54
|
+
return s
|
|
55
|
+
|
|
56
|
+
return None
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _find_closest_string_type(
|
|
60
|
+
cls: type, string_types: set[str] | dict[str, Any]
|
|
61
|
+
) -> tuple[type, str] | None:
|
|
62
|
+
"""Check if any type in the MRO matches any of the strings."""
|
|
63
|
+
mro = cls.__mro__
|
|
64
|
+
for super_cls in mro:
|
|
65
|
+
matching_type = _find_matching_string_type(super_cls, string_types)
|
|
66
|
+
if matching_type is not None:
|
|
67
|
+
return super_cls, matching_type
|
|
68
|
+
return None
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def lazy_isinstance(obj: object, class_or_tuple: LazyType, /) -> bool:
|
|
72
|
+
"""A lazy version of isinstance."""
|
|
73
|
+
types, strings = _split_lazy_type(class_or_tuple)
|
|
74
|
+
if len(types) > 0 and isinstance(obj, tuple(types)):
|
|
75
|
+
return True
|
|
76
|
+
|
|
77
|
+
if len(strings) > 0:
|
|
78
|
+
return _find_closest_string_type(type(obj), strings) is not None
|
|
79
|
+
|
|
80
|
+
return False
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def lazy_issubclass(cls: type, class_or_tuple: LazyType, /) -> bool:
|
|
84
|
+
"""A lazy version of issubclass."""
|
|
85
|
+
types, strings = _split_lazy_type(class_or_tuple)
|
|
86
|
+
if len(types) > 0 and issubclass(cls, tuple(types)):
|
|
87
|
+
return True
|
|
88
|
+
|
|
89
|
+
if len(strings) > 0:
|
|
90
|
+
return _find_closest_string_type(cls, strings) is not None
|
|
91
|
+
|
|
92
|
+
return False
|
lazy_dispatch/load.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""Lazy import utilities."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import importlib
|
|
6
|
+
import importlib.util
|
|
7
|
+
import sys
|
|
8
|
+
from typing import TYPE_CHECKING, Any, overload
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from collections.abc import Callable, Iterable
|
|
12
|
+
from types import ModuleType
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def lazy_import(name: str, package: str | None = None, *, register: bool = False) -> ModuleType:
|
|
16
|
+
"""Lazily import a module."""
|
|
17
|
+
if name in sys.modules:
|
|
18
|
+
return sys.modules[name]
|
|
19
|
+
|
|
20
|
+
spec = importlib.util.find_spec(name, package=package)
|
|
21
|
+
|
|
22
|
+
if spec is None or spec.loader is None:
|
|
23
|
+
msg = f"Module {name} not found"
|
|
24
|
+
raise ImportError(msg)
|
|
25
|
+
|
|
26
|
+
loader: importlib.util.LazyLoader = importlib.util.LazyLoader(spec.loader)
|
|
27
|
+
spec.loader = loader
|
|
28
|
+
module = importlib.util.module_from_spec(spec)
|
|
29
|
+
if register:
|
|
30
|
+
sys.modules[name] = module
|
|
31
|
+
loader.exec_module(module)
|
|
32
|
+
return module
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@overload
|
|
36
|
+
def lazy_callable(
|
|
37
|
+
module: ModuleType | str,
|
|
38
|
+
attrs: str,
|
|
39
|
+
package: str | None = None,
|
|
40
|
+
*,
|
|
41
|
+
register: bool = False,
|
|
42
|
+
) -> Callable: ...
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@overload
|
|
46
|
+
def lazy_callable(
|
|
47
|
+
module: ModuleType | str,
|
|
48
|
+
attrs: Iterable[str],
|
|
49
|
+
package: str | None = None,
|
|
50
|
+
*,
|
|
51
|
+
register: bool = False,
|
|
52
|
+
) -> list[Callable]: ...
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def lazy_callable(
|
|
56
|
+
module: ModuleType | str,
|
|
57
|
+
attrs: str | Iterable[str],
|
|
58
|
+
package: str | None = None,
|
|
59
|
+
*,
|
|
60
|
+
register: bool = False,
|
|
61
|
+
) -> Callable | list[Callable]:
|
|
62
|
+
"""Lazily get a callable attribute from a module or module name."""
|
|
63
|
+
if isinstance(module, str):
|
|
64
|
+
module = lazy_import(module, package=package, register=register)
|
|
65
|
+
|
|
66
|
+
if isinstance(attrs, str):
|
|
67
|
+
|
|
68
|
+
def fn(*args: Any, **kwargs: Any) -> Any: # noqa: ANN401
|
|
69
|
+
return getattr(module, attrs)(*args, **kwargs)
|
|
70
|
+
|
|
71
|
+
return fn
|
|
72
|
+
|
|
73
|
+
return [lazy_callable(module, attr) for attr in attrs]
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"""A lazy version of functools.singledispatch."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import operator
|
|
6
|
+
from collections.abc import Callable
|
|
7
|
+
from functools import reduce, singledispatch, update_wrapper
|
|
8
|
+
from typing import TYPE_CHECKING, Any, get_args, overload
|
|
9
|
+
|
|
10
|
+
from lazy_dispatch.isinstance import (
|
|
11
|
+
LazyType,
|
|
12
|
+
_find_closest_string_type,
|
|
13
|
+
_is_union_type,
|
|
14
|
+
_split_lazy_type,
|
|
15
|
+
lazy_issubclass,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
if TYPE_CHECKING:
|
|
19
|
+
from types import UnionType
|
|
20
|
+
|
|
21
|
+
RegistrationFunction = Callable[[type], Any]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def is_valid_dispatch_type(cls: LazyType) -> bool:
|
|
25
|
+
"""Check if cls is a valid dispatch type."""
|
|
26
|
+
if isinstance(cls, type | str):
|
|
27
|
+
return True
|
|
28
|
+
if isinstance(cls, tuple):
|
|
29
|
+
return all(is_valid_dispatch_type(c) for c in cls)
|
|
30
|
+
if _is_union_type(cls):
|
|
31
|
+
return all(is_valid_dispatch_type(c) for c in get_args(cls))
|
|
32
|
+
return False
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def first_argument(x: Any, *args: Any, **kwargs: Any) -> Any: # noqa: ARG001, ANN401
|
|
36
|
+
"""Return the first argument."""
|
|
37
|
+
return x
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class lazydispatch: # noqa: N801
|
|
41
|
+
"""A lazy version of functools.singledispatch that also works with string types."""
|
|
42
|
+
|
|
43
|
+
def __new__(
|
|
44
|
+
cls,
|
|
45
|
+
func: Callable | None = None,
|
|
46
|
+
*,
|
|
47
|
+
dispatch_on: Callable = first_argument,
|
|
48
|
+
) -> lazydispatch | Callable[[Callable], lazydispatch]:
|
|
49
|
+
"""Create a new lazy_singledispatch or return a decorator."""
|
|
50
|
+
if func is None:
|
|
51
|
+
|
|
52
|
+
def decorator(func: Callable) -> lazydispatch:
|
|
53
|
+
return cls(func, dispatch_on=dispatch_on)
|
|
54
|
+
|
|
55
|
+
return decorator
|
|
56
|
+
|
|
57
|
+
return super().__new__(cls)
|
|
58
|
+
|
|
59
|
+
def __init__(
|
|
60
|
+
self,
|
|
61
|
+
func: Callable | None = None,
|
|
62
|
+
*,
|
|
63
|
+
dispatch_on: Callable = first_argument,
|
|
64
|
+
) -> None:
|
|
65
|
+
"""Initialize the lazy_singledispatch instance."""
|
|
66
|
+
if func is None:
|
|
67
|
+
msg = "func must be provided"
|
|
68
|
+
raise ValueError(msg)
|
|
69
|
+
update_wrapper(self, func, updated=())
|
|
70
|
+
|
|
71
|
+
self._singledispatcher = singledispatch(func)
|
|
72
|
+
self.funcname = getattr(func, "__name__", "singledispatch function")
|
|
73
|
+
self.string_registry: dict[str, Callable] = {}
|
|
74
|
+
self.delayed_registration_registry: dict[str | type, RegistrationFunction] = {}
|
|
75
|
+
self.dispatch_on = dispatch_on
|
|
76
|
+
|
|
77
|
+
def dispatch(self, cls: type, *, delayed_register: bool = True) -> Callable[..., Any]:
|
|
78
|
+
"""Find the best available function for the given type or string."""
|
|
79
|
+
delayed_registration_registry = self.delayed_registration_registry
|
|
80
|
+
string_registry = self.string_registry
|
|
81
|
+
if delayed_register and len(delayed_registration_registry) > 0:
|
|
82
|
+
active_registrations: dict[str | type, RegistrationFunction] = {
|
|
83
|
+
t: registration_func
|
|
84
|
+
for t, registration_func in delayed_registration_registry.items()
|
|
85
|
+
if lazy_issubclass(cls, t)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
for t, registration_func in active_registrations.items():
|
|
89
|
+
registration_func(cls)
|
|
90
|
+
del delayed_registration_registry[t]
|
|
91
|
+
|
|
92
|
+
if len(string_registry) > 0:
|
|
93
|
+
closest = _find_closest_string_type(cls, self.string_registry)
|
|
94
|
+
if closest is not None:
|
|
95
|
+
real_type, string_type = closest
|
|
96
|
+
registration_func = string_registry.pop(string_type)
|
|
97
|
+
self.eager_register(real_type, registration_func)
|
|
98
|
+
|
|
99
|
+
return self._singledispatcher.dispatch(cls)
|
|
100
|
+
|
|
101
|
+
def eager_register(
|
|
102
|
+
self, cls: type | UnionType | Callable, func: Callable | None = None
|
|
103
|
+
) -> Callable:
|
|
104
|
+
"""Eagerly register a new implementation for the given type or union type."""
|
|
105
|
+
return self._singledispatcher.register(cls, func) # type: ignore[arg-type]
|
|
106
|
+
|
|
107
|
+
def register(self, cls: LazyType | Callable, func: Callable | None = None) -> Callable:
|
|
108
|
+
"""Register a new implementation for the given type or string."""
|
|
109
|
+
if is_valid_dispatch_type(cls): # type: ignore[arg-type]
|
|
110
|
+
if func is None:
|
|
111
|
+
return lambda f: self.register(cls, f)
|
|
112
|
+
else:
|
|
113
|
+
if func is not None:
|
|
114
|
+
msg = f"Invalid first argument to `register()`. {cls!r} is not a class, string, tuple or union type."
|
|
115
|
+
raise TypeError(msg)
|
|
116
|
+
ann = getattr(cls, "__annotations__", {})
|
|
117
|
+
if not ann:
|
|
118
|
+
msg = (
|
|
119
|
+
f"Invalid first argument to `register()`: {cls!r}. "
|
|
120
|
+
f"Use either `@register(some_class)` or plain `@register` "
|
|
121
|
+
f"on an annotated function."
|
|
122
|
+
)
|
|
123
|
+
raise TypeError(msg)
|
|
124
|
+
func = cls # type: ignore[assignment]
|
|
125
|
+
|
|
126
|
+
argname, cls = next(iter(func.__annotations__.items()))
|
|
127
|
+
if not is_valid_dispatch_type(cls):
|
|
128
|
+
if _is_union_type(cls) or isinstance(cls, tuple):
|
|
129
|
+
msg = f"Invalid annotation for {argname!r}. {cls!r} not all arguments are classes or strings."
|
|
130
|
+
raise TypeError(msg)
|
|
131
|
+
msg = f"Invalid annotation for {argname!r}. {cls!r} is not a class or string."
|
|
132
|
+
raise TypeError(msg)
|
|
133
|
+
|
|
134
|
+
types, strings = _split_lazy_type(cls) # type: ignore[arg-type]
|
|
135
|
+
|
|
136
|
+
if len(types) > 0:
|
|
137
|
+
# Use reduce with operator.or_ to dynamically create a Union (PEP 604 style) and avoid private API usage.
|
|
138
|
+
union_type = reduce(operator.or_, types)
|
|
139
|
+
|
|
140
|
+
self.eager_register(union_type, func)
|
|
141
|
+
|
|
142
|
+
if len(strings) > 0:
|
|
143
|
+
for s in strings:
|
|
144
|
+
self.string_registry[s] = func # type: ignore[assignment]
|
|
145
|
+
|
|
146
|
+
return func # type: ignore[return-value]
|
|
147
|
+
|
|
148
|
+
@overload
|
|
149
|
+
def delayed_register(
|
|
150
|
+
self, cls: LazyType
|
|
151
|
+
) -> Callable[[RegistrationFunction], RegistrationFunction]: ...
|
|
152
|
+
|
|
153
|
+
@overload
|
|
154
|
+
def delayed_register(self, cls: RegistrationFunction) -> RegistrationFunction: ...
|
|
155
|
+
|
|
156
|
+
@overload
|
|
157
|
+
def delayed_register(
|
|
158
|
+
self, cls: LazyType, func: RegistrationFunction
|
|
159
|
+
) -> RegistrationFunction: ...
|
|
160
|
+
|
|
161
|
+
def delayed_register(
|
|
162
|
+
self,
|
|
163
|
+
cls: LazyType | RegistrationFunction,
|
|
164
|
+
func: RegistrationFunction | None = None,
|
|
165
|
+
) -> RegistrationFunction | Callable[[RegistrationFunction], RegistrationFunction]:
|
|
166
|
+
"""Register a delayed registration function."""
|
|
167
|
+
if is_valid_dispatch_type(cls): # type: ignore[arg-type]
|
|
168
|
+
if func is None:
|
|
169
|
+
return lambda f: self.delayed_register(cls, f) # type: ignore[arg-type]
|
|
170
|
+
else:
|
|
171
|
+
if func is not None:
|
|
172
|
+
msg = (
|
|
173
|
+
f"Invalid first argument to `delayed_register()`. "
|
|
174
|
+
f"{cls!r} is not a class, string, tuple or union type."
|
|
175
|
+
)
|
|
176
|
+
raise TypeError(msg)
|
|
177
|
+
ann = getattr(cls, "__annotations__", {})
|
|
178
|
+
if not ann:
|
|
179
|
+
msg = (
|
|
180
|
+
f"Invalid first argument to `delayed_register()`: {cls!r}. "
|
|
181
|
+
f"Use either `@delayed_register(some_class)` or plain `@delayed_register` "
|
|
182
|
+
f"on an annotated function."
|
|
183
|
+
)
|
|
184
|
+
raise TypeError(msg)
|
|
185
|
+
func = cls # type: ignore[assignment]
|
|
186
|
+
|
|
187
|
+
argname, cls = next(iter(func.__annotations__.items()))
|
|
188
|
+
if not is_valid_dispatch_type(cls):
|
|
189
|
+
if _is_union_type(cls) or isinstance(cls, tuple):
|
|
190
|
+
msg = f"Invalid annotation for {argname!r}. {cls!r} not all arguments are classes or strings."
|
|
191
|
+
raise TypeError(msg)
|
|
192
|
+
msg = f"Invalid annotation for {argname!r}. {cls!r} is not a class or string."
|
|
193
|
+
raise TypeError(msg)
|
|
194
|
+
|
|
195
|
+
types, strings = _split_lazy_type(cls) # type: ignore[arg-type]
|
|
196
|
+
|
|
197
|
+
for t in types:
|
|
198
|
+
self.delayed_registration_registry[t] = func # type: ignore[assignment]
|
|
199
|
+
|
|
200
|
+
for s in strings:
|
|
201
|
+
self.delayed_registration_registry[s] = func # type: ignore[assignment]
|
|
202
|
+
|
|
203
|
+
return func # type: ignore[return-value]
|
|
204
|
+
|
|
205
|
+
def __call__(self, *args: Any, **kwargs: Any) -> Any: # noqa: ANN401
|
|
206
|
+
"""Call the appropriate registered function based on the type of the first argument."""
|
|
207
|
+
if not args:
|
|
208
|
+
msg = f"{self.funcname} requires at least 1 positional argument"
|
|
209
|
+
raise TypeError(msg)
|
|
210
|
+
return self.dispatch(self.dispatch_on(*args, **kwargs).__class__)(*args, **kwargs)
|
shapiq/__init__.py
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"""shapiq: Shapley Interactions for Machine Learning.
|
|
2
|
+
|
|
3
|
+
shapiq is a library creating explanations for machine learning models based on
|
|
4
|
+
the well established Shapley value and its generalization to interaction.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# start delvewheel patch
|
|
11
|
+
def _delvewheel_patch_1_13_2():
|
|
12
|
+
import os
|
|
13
|
+
if os.path.isdir(libs_dir := os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, 'shapiq.libs'))):
|
|
14
|
+
os.add_dll_directory(libs_dir)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
_delvewheel_patch_1_13_2()
|
|
18
|
+
del _delvewheel_patch_1_13_2
|
|
19
|
+
# end delvewheel patch
|
|
20
|
+
|
|
21
|
+
try:
|
|
22
|
+
from ._version import __version__
|
|
23
|
+
except ImportError: # pragma: no cover - _version.py is generated at build time
|
|
24
|
+
__version__ = "0.0.0"
|
|
25
|
+
|
|
26
|
+
# approximator classes
|
|
27
|
+
from .approximator import (
|
|
28
|
+
SHAPIQ,
|
|
29
|
+
SPEX,
|
|
30
|
+
SVARM,
|
|
31
|
+
SVARMIQ,
|
|
32
|
+
InconsistentKernelSHAPIQ,
|
|
33
|
+
KernelSHAP,
|
|
34
|
+
KernelSHAPIQ,
|
|
35
|
+
LeverageSHAP,
|
|
36
|
+
OddSHAP,
|
|
37
|
+
OwenSamplingSV,
|
|
38
|
+
PermutationSamplingSII,
|
|
39
|
+
PermutationSamplingSTII,
|
|
40
|
+
PermutationSamplingSV,
|
|
41
|
+
PolySHAP,
|
|
42
|
+
ProxySHAP,
|
|
43
|
+
ProxySPEX,
|
|
44
|
+
RegressionFBII,
|
|
45
|
+
RegressionFSII,
|
|
46
|
+
RegressionMSR,
|
|
47
|
+
ShaplEIG,
|
|
48
|
+
StratifiedSamplingSV,
|
|
49
|
+
UnbiasedKernelSHAP,
|
|
50
|
+
kADDSHAP,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
# explainer classes
|
|
54
|
+
from .explainer import (
|
|
55
|
+
AgnosticExplainer,
|
|
56
|
+
Explainer,
|
|
57
|
+
TabPFNExplainer,
|
|
58
|
+
TabularExplainer,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
# game classes
|
|
62
|
+
from .game import Game
|
|
63
|
+
|
|
64
|
+
# exact computer classes
|
|
65
|
+
from .game_theory.exact import ExactComputer
|
|
66
|
+
|
|
67
|
+
# imputer classes
|
|
68
|
+
from .imputer import (
|
|
69
|
+
BaselineImputer,
|
|
70
|
+
GaussianCopulaImputer,
|
|
71
|
+
GaussianImputer,
|
|
72
|
+
GenerativeConditionalImputer,
|
|
73
|
+
MarginalImputer,
|
|
74
|
+
TabPFNImputer,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
# base classes
|
|
78
|
+
from .interaction_values import InteractionValues, InteractionValuesBatch
|
|
79
|
+
|
|
80
|
+
# plotting functions
|
|
81
|
+
from .plot import (
|
|
82
|
+
bar_plot,
|
|
83
|
+
beeswarm_plot,
|
|
84
|
+
force_plot,
|
|
85
|
+
network_plot,
|
|
86
|
+
scatter_plot,
|
|
87
|
+
sentence_plot,
|
|
88
|
+
si_graph_plot,
|
|
89
|
+
stacked_bar_plot,
|
|
90
|
+
upset_plot,
|
|
91
|
+
waterfall_plot,
|
|
92
|
+
)
|
|
93
|
+
from .tree import TreeExplainer
|
|
94
|
+
|
|
95
|
+
# public utils functions
|
|
96
|
+
from .utils import ( # sets.py # tree.py
|
|
97
|
+
get_explicit_subsets,
|
|
98
|
+
powerset,
|
|
99
|
+
safe_isinstance,
|
|
100
|
+
split_subsets_budget,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
__all__ = [
|
|
104
|
+
# version
|
|
105
|
+
"__version__",
|
|
106
|
+
# base
|
|
107
|
+
"InteractionValues",
|
|
108
|
+
"InteractionValuesBatch",
|
|
109
|
+
"ExactComputer",
|
|
110
|
+
"Game",
|
|
111
|
+
# approximators
|
|
112
|
+
"PermutationSamplingSII",
|
|
113
|
+
"PermutationSamplingSTII",
|
|
114
|
+
"PermutationSamplingSV",
|
|
115
|
+
"StratifiedSamplingSV",
|
|
116
|
+
"OwenSamplingSV",
|
|
117
|
+
"KernelSHAP",
|
|
118
|
+
"LeverageSHAP",
|
|
119
|
+
"RegressionFSII",
|
|
120
|
+
"RegressionFBII",
|
|
121
|
+
"KernelSHAPIQ",
|
|
122
|
+
"InconsistentKernelSHAPIQ",
|
|
123
|
+
"SHAPIQ",
|
|
124
|
+
"SVARM",
|
|
125
|
+
"SVARMIQ",
|
|
126
|
+
"kADDSHAP",
|
|
127
|
+
"UnbiasedKernelSHAP",
|
|
128
|
+
"SPEX",
|
|
129
|
+
"ProxySHAP",
|
|
130
|
+
"ShaplEIG",
|
|
131
|
+
"ProxySPEX",
|
|
132
|
+
"RegressionMSR",
|
|
133
|
+
"OddSHAP",
|
|
134
|
+
"PolySHAP",
|
|
135
|
+
# explainers
|
|
136
|
+
"Explainer",
|
|
137
|
+
"TabularExplainer",
|
|
138
|
+
"TreeExplainer",
|
|
139
|
+
"TabPFNExplainer",
|
|
140
|
+
"AgnosticExplainer",
|
|
141
|
+
# imputers
|
|
142
|
+
"MarginalImputer",
|
|
143
|
+
"BaselineImputer",
|
|
144
|
+
"GenerativeConditionalImputer",
|
|
145
|
+
"TabPFNImputer",
|
|
146
|
+
"GaussianImputer",
|
|
147
|
+
"GaussianCopulaImputer",
|
|
148
|
+
# plots
|
|
149
|
+
"network_plot",
|
|
150
|
+
"stacked_bar_plot",
|
|
151
|
+
"force_plot",
|
|
152
|
+
"bar_plot",
|
|
153
|
+
"si_graph_plot",
|
|
154
|
+
"waterfall_plot",
|
|
155
|
+
"sentence_plot",
|
|
156
|
+
"upset_plot",
|
|
157
|
+
"beeswarm_plot",
|
|
158
|
+
"scatter_plot",
|
|
159
|
+
# public utils
|
|
160
|
+
"powerset",
|
|
161
|
+
"get_explicit_subsets",
|
|
162
|
+
"split_subsets_budget",
|
|
163
|
+
"safe_isinstance",
|
|
164
|
+
]
|
shapiq/_version.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# file generated by vcs-versioning
|
|
2
|
+
# don't change, don't track in version control
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"__version__",
|
|
7
|
+
"__version_tuple__",
|
|
8
|
+
"version",
|
|
9
|
+
"version_tuple",
|
|
10
|
+
"__commit_id__",
|
|
11
|
+
"commit_id",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
version: str
|
|
15
|
+
__version__: str
|
|
16
|
+
__version_tuple__: tuple[int | str, ...]
|
|
17
|
+
version_tuple: tuple[int | str, ...]
|
|
18
|
+
commit_id: str | None
|
|
19
|
+
__commit_id__: str | None
|
|
20
|
+
|
|
21
|
+
__version__ = version = '1.8.0'
|
|
22
|
+
__version_tuple__ = version_tuple = (1, 8, 0)
|
|
23
|
+
|
|
24
|
+
__commit_id__ = commit_id = 'g91f05b879'
|