pyshapr 0.5.0__py3-none-any.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.
- pyshapr/__init__.py +70 -0
- pyshapr/_explain.py +680 -0
- pyshapr/_rutils.py +58 -0
- pyshapr/datasets.py +35 -0
- pyshapr/explanation.py +311 -0
- pyshapr/utils.py +133 -0
- pyshapr-0.5.0.dist-info/METADATA +233 -0
- pyshapr-0.5.0.dist-info/RECORD +12 -0
- pyshapr-0.5.0.dist-info/WHEEL +5 -0
- pyshapr-0.5.0.dist-info/licenses/LICENSE +2 -0
- pyshapr-0.5.0.dist-info/licenses/LICENSE.md +21 -0
- pyshapr-0.5.0.dist-info/top_level.txt +1 -0
pyshapr/__init__.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import contextlib
|
|
2
|
+
from importlib import import_module
|
|
3
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
4
|
+
|
|
5
|
+
# Lightweight public re-export (no R dependency)
|
|
6
|
+
from . import datasets
|
|
7
|
+
from ._rutils import get_package_lib_loc
|
|
8
|
+
|
|
9
|
+
__all__ = ["Shapr", "datasets", "ensure_r_ready", "explain"]
|
|
10
|
+
|
|
11
|
+
try:
|
|
12
|
+
__version__ = version("pyshapr")
|
|
13
|
+
except PackageNotFoundError:
|
|
14
|
+
__version__ = "0.0.0+local"
|
|
15
|
+
|
|
16
|
+
_r_ready = False
|
|
17
|
+
_explain_impl = None
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def ensure_r_ready() -> bool:
|
|
21
|
+
"""Ensure rpy2 and the R package 'shapr' are available, then bind the real explain() (idempotent)."""
|
|
22
|
+
global _r_ready, _explain_impl
|
|
23
|
+
if _r_ready:
|
|
24
|
+
return True
|
|
25
|
+
|
|
26
|
+
try:
|
|
27
|
+
import rpy2.robjects as _ro
|
|
28
|
+
from rpy2.robjects.packages import importr
|
|
29
|
+
except Exception as e:
|
|
30
|
+
raise ImportError(
|
|
31
|
+
"pyshapr requires rpy2 and a working R installation.\n"
|
|
32
|
+
"Install R and rpy2, and ensure R is on PATH/R_HOME. See README."
|
|
33
|
+
) from e
|
|
34
|
+
|
|
35
|
+
try:
|
|
36
|
+
lib_loc = get_package_lib_loc(_ro, "shapr")
|
|
37
|
+
if lib_loc:
|
|
38
|
+
importr("shapr", lib_loc=lib_loc)
|
|
39
|
+
else:
|
|
40
|
+
importr("shapr")
|
|
41
|
+
except Exception as e:
|
|
42
|
+
raise ImportError(
|
|
43
|
+
"The R package 'shapr' is not installed or not found.\n"
|
|
44
|
+
"In an R session, run: install.packages('shapr')"
|
|
45
|
+
) from e
|
|
46
|
+
|
|
47
|
+
# Import the implementation from a private module to avoid name collision
|
|
48
|
+
_explain_mod = import_module(__name__ + "._explain")
|
|
49
|
+
_explain_impl = _explain_mod.explain
|
|
50
|
+
_r_ready = True
|
|
51
|
+
return True
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def explain(*args, **kwargs):
|
|
55
|
+
"""Lazily initialize R/shapr then call the real explain()."""
|
|
56
|
+
ensure_r_ready()
|
|
57
|
+
return _explain_impl(*args, **kwargs)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
# Import the Shapr class (lazy import to avoid R dependency issues)
|
|
61
|
+
def _import_shapr():
|
|
62
|
+
from .explanation import Shapr
|
|
63
|
+
|
|
64
|
+
return Shapr
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
# Make Shapr available when the module is imported
|
|
68
|
+
Shapr = None
|
|
69
|
+
with contextlib.suppress(ImportError):
|
|
70
|
+
Shapr = _import_shapr()
|