shaprpy 0.3.0__tar.gz
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.
- shaprpy-0.3.0/LICENSE +2 -0
- shaprpy-0.3.0/LICENSE.md +21 -0
- shaprpy-0.3.0/MANIFEST.in +4 -0
- shaprpy-0.3.0/PKG-INFO +168 -0
- shaprpy-0.3.0/README.md +130 -0
- shaprpy-0.3.0/pyproject.toml +74 -0
- shaprpy-0.3.0/setup.cfg +4 -0
- shaprpy-0.3.0/src/shaprpy/__init__.py +51 -0
- shaprpy-0.3.0/src/shaprpy/_explain.py +630 -0
- shaprpy-0.3.0/src/shaprpy/datasets.py +27 -0
- shaprpy-0.3.0/src/shaprpy/utils.py +67 -0
- shaprpy-0.3.0/src/shaprpy.egg-info/PKG-INFO +168 -0
- shaprpy-0.3.0/src/shaprpy.egg-info/SOURCES.txt +14 -0
- shaprpy-0.3.0/src/shaprpy.egg-info/dependency_links.txt +1 -0
- shaprpy-0.3.0/src/shaprpy.egg-info/requires.txt +10 -0
- shaprpy-0.3.0/src/shaprpy.egg-info/top_level.txt +1 -0
shaprpy-0.3.0/LICENSE
ADDED
shaprpy-0.3.0/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Norsk Regnesentral
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
shaprpy-0.3.0/PKG-INFO
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: shaprpy
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Python wrapper for the R package shapr (via rpy2)
|
|
5
|
+
Author: Martin Jullum, Lars Henry Berge Olsen, Didrik Nielsen
|
|
6
|
+
License: YEAR: 2019
|
|
7
|
+
COPYRIGHT HOLDER: Norsk Regnesentral
|
|
8
|
+
|
|
9
|
+
Project-URL: Homepage, https://github.com/NorskRegnesentral/shapr
|
|
10
|
+
Project-URL: Documentation, https://norskregnesentral.github.io/shapr/shaprpy.html
|
|
11
|
+
Project-URL: Issues, https://github.com/NorskRegnesentral/shapr/issues
|
|
12
|
+
Project-URL: Changelog, https://github.com/NorskRegnesentral/shapr/blob/main/python/CHANGELOG.md
|
|
13
|
+
Keywords: explainable-ai,shapley-values,machine-learning,model-interpretability
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
21
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
22
|
+
Classifier: Intended Audience :: Science/Research
|
|
23
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
24
|
+
Requires-Python: >3.10
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
License-File: LICENSE.md
|
|
28
|
+
Requires-Dist: rpy2>=3.5.1
|
|
29
|
+
Requires-Dist: numpy>=1.22.3
|
|
30
|
+
Requires-Dist: pandas>=1.4.2
|
|
31
|
+
Requires-Dist: scikit-learn>=1.0.0
|
|
32
|
+
Requires-Dist: tabulate>=0.8.10
|
|
33
|
+
Provides-Extra: test
|
|
34
|
+
Requires-Dist: pytest>=7.0.0; extra == "test"
|
|
35
|
+
Requires-Dist: syrupy>=4.0.0; extra == "test"
|
|
36
|
+
Requires-Dist: xgboost>=1.5.0; extra == "test"
|
|
37
|
+
Dynamic: license-file
|
|
38
|
+
|
|
39
|
+
# shaprpy
|
|
40
|
+
|
|
41
|
+
Python wrapper for the R package [shapr](https://github.com/NorskRegnesentral/shapr).
|
|
42
|
+
|
|
43
|
+
NOTE: This wrapper is not as comprehensively tested as the `R`-package.
|
|
44
|
+
|
|
45
|
+
`shaprpy` relies heavily on the `rpy2` Python library for accessing R from within Python.
|
|
46
|
+
`rpy2` has limited support on Windows. `shaprpy` has only been tested on Linux.
|
|
47
|
+
The below instructions assumes a Linux environment.
|
|
48
|
+
|
|
49
|
+
# shaprpy
|
|
50
|
+
|
|
51
|
+
`shaprpy` is a Python wrapper for the R package [shapr](https://github.com/NorskRegnesentral/shapr),
|
|
52
|
+
using the [`rpy2`](https://rpy2.github.io/) Python library to access R from within Python.
|
|
53
|
+
|
|
54
|
+
> **Note:** This wrapper is **not** as comprehensively tested as the R package.
|
|
55
|
+
> `rpy2` has limited support on Windows, and the same therefore applies to `shaprpy`.
|
|
56
|
+
> `shaprpy` has only been tested on Linux (and WSL - Windows Subsystem for Linux), and the below instructions assume a Linux environment.
|
|
57
|
+
>
|
|
58
|
+
> **Requirement:** Python 3.10 or later is required to use `shaprpy`.
|
|
59
|
+
|
|
60
|
+
## Changelog
|
|
61
|
+
|
|
62
|
+
For a list of changes and updates to the `shaprpy` package, see the [shaprpy CHANGELOG](https://norskregnesentral.github.io/shapr/py_changelog.html).
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## Installation
|
|
67
|
+
|
|
68
|
+
These instructions assume you already have **pip** and **R** installed and available to the Python environment in which you want to run `shaprpy`.
|
|
69
|
+
|
|
70
|
+
- Official instructions for installing `pip` can be found [here](https://pip.pypa.io/en/stable/installation/).
|
|
71
|
+
- Official instructions for installing R can be found [here](https://cran.r-project.org/).
|
|
72
|
+
|
|
73
|
+
On Debian/Ubuntu-based systems, R can also be installed via:
|
|
74
|
+
```bash
|
|
75
|
+
sudo apt update
|
|
76
|
+
sudo apt install r-base r-base-dev -y
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### 1. Install the R package `shapr`
|
|
80
|
+
|
|
81
|
+
`shaprpy` requires the R package `shapr` (version 1.0.5 or newer).
|
|
82
|
+
In your R environment, install the latest version from CRAN using:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
Rscript -e 'install.packages("shapr", repos="https://cran.rstudio.com")'
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### 2. Ensure R is discoverable (R_HOME and PATH)
|
|
89
|
+
|
|
90
|
+
Sometimes `shaprpy` cannot automatically locate your R installation. To ensure proper detection, verify that:
|
|
91
|
+
- R is available in your system `PATH`, **or**
|
|
92
|
+
- The `R_HOME` environment variable is set to your R installation directory.
|
|
93
|
+
|
|
94
|
+
Example:
|
|
95
|
+
```bash
|
|
96
|
+
export R_HOME=$(R RHOME)
|
|
97
|
+
export PATH=$PATH:$(R RHOME)/bin
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### 3. Install the Python wrapper
|
|
101
|
+
|
|
102
|
+
Install directly from PyPI with:
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
pip install shaprpy
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
#### Local development install (for contributors)
|
|
109
|
+
If you have cloned the repository and want to install in development mode for local changes, navigate to the `./python` directory and run:
|
|
110
|
+
```bash
|
|
111
|
+
pip install -e .
|
|
112
|
+
```
|
|
113
|
+
The `-e` flag installs in editable mode, allowing local code changes to be reflected immediately.
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## Quick Demo
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
from sklearn.ensemble import RandomForestRegressor
|
|
121
|
+
from shaprpy import explain
|
|
122
|
+
from shaprpy.datasets import load_california_housing
|
|
123
|
+
|
|
124
|
+
# Load example data
|
|
125
|
+
dfx_train, dfx_test, dfy_train, dfy_test = load_california_housing()
|
|
126
|
+
|
|
127
|
+
# Fit a model
|
|
128
|
+
model = RandomForestRegressor()
|
|
129
|
+
model.fit(dfx_train, dfy_train.values.flatten())
|
|
130
|
+
|
|
131
|
+
# Explain predictions
|
|
132
|
+
explanation = explain(
|
|
133
|
+
model=model,
|
|
134
|
+
x_train=dfx_train,
|
|
135
|
+
x_explain=dfx_test,
|
|
136
|
+
approach="empirical",
|
|
137
|
+
phi0=dfy_train.mean().item(),
|
|
138
|
+
seed=1
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
print(explanation["shapley_values_est"])
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
## Supported Models
|
|
147
|
+
|
|
148
|
+
`shaprpy` can explain predictions from models built with:
|
|
149
|
+
- [`scikit-learn`](https://scikit-learn.org/)
|
|
150
|
+
- [`keras`](https://keras.io/) (Sequential API)
|
|
151
|
+
- [`xgboost`](https://xgboost.readthedocs.io/)
|
|
152
|
+
|
|
153
|
+
For other model types, you can supply:
|
|
154
|
+
- A custom `predict_model` function
|
|
155
|
+
- (Optionally) a custom `get_model_specs` function
|
|
156
|
+
to `shaprpy.explain`.
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
## Examples
|
|
161
|
+
|
|
162
|
+
See the `/examples` folder for runnable examples, including:
|
|
163
|
+
- A custom PyTorch model
|
|
164
|
+
- The **regression paradigm** described in [Olsen et al. (2024)](https://link.springer.com/article/10.1007/s10618-024-01016-z),
|
|
165
|
+
which shows:
|
|
166
|
+
- How to specify the regression model
|
|
167
|
+
- How to enable automatic cross-validation of hyperparameters
|
|
168
|
+
- How to apply pre-processing steps before fitting regression models
|
shaprpy-0.3.0/README.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# shaprpy
|
|
2
|
+
|
|
3
|
+
Python wrapper for the R package [shapr](https://github.com/NorskRegnesentral/shapr).
|
|
4
|
+
|
|
5
|
+
NOTE: This wrapper is not as comprehensively tested as the `R`-package.
|
|
6
|
+
|
|
7
|
+
`shaprpy` relies heavily on the `rpy2` Python library for accessing R from within Python.
|
|
8
|
+
`rpy2` has limited support on Windows. `shaprpy` has only been tested on Linux.
|
|
9
|
+
The below instructions assumes a Linux environment.
|
|
10
|
+
|
|
11
|
+
# shaprpy
|
|
12
|
+
|
|
13
|
+
`shaprpy` is a Python wrapper for the R package [shapr](https://github.com/NorskRegnesentral/shapr),
|
|
14
|
+
using the [`rpy2`](https://rpy2.github.io/) Python library to access R from within Python.
|
|
15
|
+
|
|
16
|
+
> **Note:** This wrapper is **not** as comprehensively tested as the R package.
|
|
17
|
+
> `rpy2` has limited support on Windows, and the same therefore applies to `shaprpy`.
|
|
18
|
+
> `shaprpy` has only been tested on Linux (and WSL - Windows Subsystem for Linux), and the below instructions assume a Linux environment.
|
|
19
|
+
>
|
|
20
|
+
> **Requirement:** Python 3.10 or later is required to use `shaprpy`.
|
|
21
|
+
|
|
22
|
+
## Changelog
|
|
23
|
+
|
|
24
|
+
For a list of changes and updates to the `shaprpy` package, see the [shaprpy CHANGELOG](https://norskregnesentral.github.io/shapr/py_changelog.html).
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
These instructions assume you already have **pip** and **R** installed and available to the Python environment in which you want to run `shaprpy`.
|
|
31
|
+
|
|
32
|
+
- Official instructions for installing `pip` can be found [here](https://pip.pypa.io/en/stable/installation/).
|
|
33
|
+
- Official instructions for installing R can be found [here](https://cran.r-project.org/).
|
|
34
|
+
|
|
35
|
+
On Debian/Ubuntu-based systems, R can also be installed via:
|
|
36
|
+
```bash
|
|
37
|
+
sudo apt update
|
|
38
|
+
sudo apt install r-base r-base-dev -y
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### 1. Install the R package `shapr`
|
|
42
|
+
|
|
43
|
+
`shaprpy` requires the R package `shapr` (version 1.0.5 or newer).
|
|
44
|
+
In your R environment, install the latest version from CRAN using:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
Rscript -e 'install.packages("shapr", repos="https://cran.rstudio.com")'
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### 2. Ensure R is discoverable (R_HOME and PATH)
|
|
51
|
+
|
|
52
|
+
Sometimes `shaprpy` cannot automatically locate your R installation. To ensure proper detection, verify that:
|
|
53
|
+
- R is available in your system `PATH`, **or**
|
|
54
|
+
- The `R_HOME` environment variable is set to your R installation directory.
|
|
55
|
+
|
|
56
|
+
Example:
|
|
57
|
+
```bash
|
|
58
|
+
export R_HOME=$(R RHOME)
|
|
59
|
+
export PATH=$PATH:$(R RHOME)/bin
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### 3. Install the Python wrapper
|
|
63
|
+
|
|
64
|
+
Install directly from PyPI with:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
pip install shaprpy
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
#### Local development install (for contributors)
|
|
71
|
+
If you have cloned the repository and want to install in development mode for local changes, navigate to the `./python` directory and run:
|
|
72
|
+
```bash
|
|
73
|
+
pip install -e .
|
|
74
|
+
```
|
|
75
|
+
The `-e` flag installs in editable mode, allowing local code changes to be reflected immediately.
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## Quick Demo
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
from sklearn.ensemble import RandomForestRegressor
|
|
83
|
+
from shaprpy import explain
|
|
84
|
+
from shaprpy.datasets import load_california_housing
|
|
85
|
+
|
|
86
|
+
# Load example data
|
|
87
|
+
dfx_train, dfx_test, dfy_train, dfy_test = load_california_housing()
|
|
88
|
+
|
|
89
|
+
# Fit a model
|
|
90
|
+
model = RandomForestRegressor()
|
|
91
|
+
model.fit(dfx_train, dfy_train.values.flatten())
|
|
92
|
+
|
|
93
|
+
# Explain predictions
|
|
94
|
+
explanation = explain(
|
|
95
|
+
model=model,
|
|
96
|
+
x_train=dfx_train,
|
|
97
|
+
x_explain=dfx_test,
|
|
98
|
+
approach="empirical",
|
|
99
|
+
phi0=dfy_train.mean().item(),
|
|
100
|
+
seed=1
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
print(explanation["shapley_values_est"])
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## Supported Models
|
|
109
|
+
|
|
110
|
+
`shaprpy` can explain predictions from models built with:
|
|
111
|
+
- [`scikit-learn`](https://scikit-learn.org/)
|
|
112
|
+
- [`keras`](https://keras.io/) (Sequential API)
|
|
113
|
+
- [`xgboost`](https://xgboost.readthedocs.io/)
|
|
114
|
+
|
|
115
|
+
For other model types, you can supply:
|
|
116
|
+
- A custom `predict_model` function
|
|
117
|
+
- (Optionally) a custom `get_model_specs` function
|
|
118
|
+
to `shaprpy.explain`.
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## Examples
|
|
123
|
+
|
|
124
|
+
See the `/examples` folder for runnable examples, including:
|
|
125
|
+
- A custom PyTorch model
|
|
126
|
+
- The **regression paradigm** described in [Olsen et al. (2024)](https://link.springer.com/article/10.1007/s10618-024-01016-z),
|
|
127
|
+
which shows:
|
|
128
|
+
- How to specify the regression model
|
|
129
|
+
- How to enable automatic cross-validation of hyperparameters
|
|
130
|
+
- How to apply pre-processing steps before fitting regression models
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=69", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "shaprpy"
|
|
7
|
+
version = "0.3.0"
|
|
8
|
+
description = "Python wrapper for the R package shapr (via rpy2)"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { file = "LICENSE" }
|
|
11
|
+
authors = [
|
|
12
|
+
{ name = "Martin Jullum" },
|
|
13
|
+
{ name = "Lars Henry Berge Olsen" },
|
|
14
|
+
{ name = "Didrik Nielsen" }
|
|
15
|
+
]
|
|
16
|
+
requires-python = ">3.10"
|
|
17
|
+
keywords = ["explainable-ai", "shapley-values", "machine-learning", "model-interpretability"]
|
|
18
|
+
classifiers = [
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
21
|
+
"Programming Language :: Python :: 3.10",
|
|
22
|
+
"Programming Language :: Python :: 3.11",
|
|
23
|
+
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"Programming Language :: Python :: 3.13",
|
|
25
|
+
"License :: OSI Approved :: MIT License",
|
|
26
|
+
"Operating System :: POSIX :: Linux",
|
|
27
|
+
"Intended Audience :: Science/Research",
|
|
28
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence"
|
|
29
|
+
]
|
|
30
|
+
dependencies = [
|
|
31
|
+
"rpy2>=3.5.1",
|
|
32
|
+
"numpy>=1.22.3",
|
|
33
|
+
"pandas>=1.4.2",
|
|
34
|
+
"scikit-learn>=1.0.0",
|
|
35
|
+
"tabulate>=0.8.10",
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
[project.optional-dependencies]
|
|
39
|
+
test = [
|
|
40
|
+
"pytest>=7.0.0",
|
|
41
|
+
"syrupy>=4.0.0",
|
|
42
|
+
"xgboost>=1.5.0"
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[project.urls]
|
|
46
|
+
Homepage = "https://github.com/NorskRegnesentral/shapr"
|
|
47
|
+
Documentation = "https://norskregnesentral.github.io/shapr/shaprpy.html"
|
|
48
|
+
Issues = "https://github.com/NorskRegnesentral/shapr/issues"
|
|
49
|
+
Changelog = "https://github.com/NorskRegnesentral/shapr/blob/main/python/CHANGELOG.md"
|
|
50
|
+
|
|
51
|
+
[tool.setuptools]
|
|
52
|
+
package-dir = {"" = "src"}
|
|
53
|
+
|
|
54
|
+
[tool.setuptools.packages.find]
|
|
55
|
+
where = ["src"]
|
|
56
|
+
include = ["shaprpy*" ]
|
|
57
|
+
|
|
58
|
+
[tool.setuptools.dynamic]
|
|
59
|
+
# (none needed now)
|
|
60
|
+
|
|
61
|
+
[tool.pytest.ini_options]
|
|
62
|
+
testpaths = ["tests"]
|
|
63
|
+
python_files = ["test_*.py"]
|
|
64
|
+
python_classes = ["Test*"]
|
|
65
|
+
python_functions = ["test_*"]
|
|
66
|
+
addopts = [
|
|
67
|
+
"--strict-markers",
|
|
68
|
+
"--strict-config",
|
|
69
|
+
"-ra",
|
|
70
|
+
]
|
|
71
|
+
markers = [
|
|
72
|
+
"slow: marks tests as slow (deselect with '-m \"not slow\"')",
|
|
73
|
+
"snapshot: marks tests as snapshot/output tests",
|
|
74
|
+
]
|
shaprpy-0.3.0/setup.cfg
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
from importlib.metadata import version, PackageNotFoundError
|
|
2
|
+
from importlib import import_module
|
|
3
|
+
|
|
4
|
+
# Lightweight public re-export (no R dependency)
|
|
5
|
+
from . import datasets # noqa: F401
|
|
6
|
+
|
|
7
|
+
__all__ = ["explain", "datasets", "ensure_r_ready"]
|
|
8
|
+
|
|
9
|
+
try:
|
|
10
|
+
__version__ = version("shaprpy")
|
|
11
|
+
except PackageNotFoundError:
|
|
12
|
+
__version__ = "0.0.0+local"
|
|
13
|
+
|
|
14
|
+
_r_ready = False
|
|
15
|
+
_explain_impl = None
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def ensure_r_ready() -> bool:
|
|
19
|
+
"""Ensure rpy2 and the R package 'shapr' are available, then bind the real explain() (idempotent)."""
|
|
20
|
+
global _r_ready, _explain_impl
|
|
21
|
+
if _r_ready:
|
|
22
|
+
return True
|
|
23
|
+
|
|
24
|
+
try:
|
|
25
|
+
import rpy2.robjects as _ro # noqa: F401
|
|
26
|
+
from rpy2.robjects.packages import importr
|
|
27
|
+
except Exception as e:
|
|
28
|
+
raise ImportError(
|
|
29
|
+
"shaprpy requires rpy2 and a working R installation.\n"
|
|
30
|
+
"Install R and rpy2, and ensure R is on PATH/R_HOME. See README."
|
|
31
|
+
) from e
|
|
32
|
+
|
|
33
|
+
try:
|
|
34
|
+
importr("shapr")
|
|
35
|
+
except Exception as e:
|
|
36
|
+
raise ImportError(
|
|
37
|
+
"The R package 'shapr' is not installed or not found.\n"
|
|
38
|
+
"In an R session, run: install.packages('shapr')"
|
|
39
|
+
) from e
|
|
40
|
+
|
|
41
|
+
# Import the implementation from a private module to avoid name collision
|
|
42
|
+
_explain_mod = import_module(__name__ + "._explain")
|
|
43
|
+
_explain_impl = _explain_mod.explain
|
|
44
|
+
_r_ready = True
|
|
45
|
+
return True
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def explain(*args, **kwargs):
|
|
49
|
+
"""Lazily initialize R/shapr then call the real explain()."""
|
|
50
|
+
ensure_r_ready()
|
|
51
|
+
return _explain_impl(*args, **kwargs)
|