API Reference¶
Core¶
Bases: DebugMixin, ABC
Base class providing a unified explanation workflow in LUNA XAI.
Notes
Subclasses are expected to implement generate_explanation_data() to
construct domain-specific neighborhood or sensitivity representations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model |
Any
|
Predictive model implementing at least a |
required |
domain |
Any
|
Domain object responsible for preparing data and providing model access. |
required |
method |
Any
|
Explanation/sensitivity method. |
required |
metric |
Any
|
Metric evaluating the method's output. |
required |
renderer |
Any
|
Optional visualization component. |
None
|
output_dir |
str
|
Output directory for artifacts generated by renderers. |
"results"
|
normalizer |
Any
|
Optional importance-normalization function. |
None
|
debug |
bool
|
If True, enables debug-level logging for this explainer instance. |
False
|
Source code in src\luna_xai\core\luna_explainer.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
explain_from_data(explanation_data, render=True, context=None, **kwargs)
¶
Compute feature importances and optionally render results using precomputed explanation data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
explanation_data |
dict
|
Output from |
required |
render |
bool
|
Whether to produce visual output. |
True
|
context |
dict
|
Metadata for the renderer (e.g., instance ID). |
None
|
Returns:
| Type | Description |
|---|---|
MetricResult
|
Final evaluated explanation result. |
Source code in src\luna_xai\core\luna_explainer.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
explain_instance(instance, **kwargs)
¶
One-step pipeline: generate explanation data → compute importances → (optional) render.
This preserves backward compatibility with previous demos.
Source code in src\luna_xai\core\luna_explainer.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
generate_explanation_data(instance, **kwargs)
¶
Construct raw explanation data (predictions, neighborhood, sensitivities).
Notes
This method must be implemented by each concrete explainer variant. The method should not compute feature importances nor perform rendering.
Returns:
| Type | Description |
|---|---|
dict
|
{ "sensitivity_data": {...}, "predictions": array-like (optional), "metadata": {...} } |
Source code in src\luna_xai\core\luna_explainer.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
LUNA XAI Global Component Registry¶
Provides a unified, extensible registry for registering, discovering, and retrieving core components such as: - Domains - Methods - Metrics - Renderers - Explainers
This registry acts as the central lookup table for all component implementations across the framework (Strategy / Factory pattern).
auto_discover_core_components(verbose=False)
¶
Import all submodules under known core namespaces to trigger auto-registration via decorator pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
verbose |
bool
|
Whether to log warnings during discovery. |
False
|
Source code in src\luna_xai\core\registry.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
get_component(full_name)
¶
Retrieve a component by its fully qualified name: 'namespace.name'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
full_name |
str
|
Identifier of the component ('method.dsa', 'metric.ch', etc.) |
required |
Returns:
| Type | Description |
|---|---|
type
|
The class registered under the requested name. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the component or namespace is missing. |
ValueError
|
If the format is invalid. |
Source code in src\luna_xai\core\registry.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
list_registered(namespace=None)
¶
Return dictionary of registered components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace |
str
|
If provided, return only entries under that namespace. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
Mapping of registered classes. |
Source code in src\luna_xai\core\registry.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
register_component(namespace, name, cls=None, *, overwrite=False)
¶
Register a component under a given namespace and name.
Supports both decorator and direct invocation usage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace |
str
|
Logical group of components (e.g., 'metric', 'method', 'renderer'). |
required |
name |
str
|
Component identifier within the namespace. |
required |
cls |
type
|
If provided, the registration happens immediately. If omitted, the function returns a decorator. |
None
|
overwrite |
bool
|
Whether existing entries can be replaced. Useful in rapid prototyping but should be False in production for safety. |
False
|
Returns:
| Type | Description |
|---|---|
Callable
|
A decorator if |
Raises:
| Type | Description |
|---|---|
ValueError
|
If attempting to register a duplicate name without overwrite=True. |
Source code in src\luna_xai\core\registry.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
Domains¶
Bases: BaseDomain
Minimal domain implementation for tabular data.
Responsibilities
- Prepare instances in model-space using DataInterface
- Run predictions with safe numeric handling
- Build a hybrid-space (CANE + scaling) representation
- Construct and expose an HNSW-based neighbor finder
Source code in src\luna_xai\domains\tabular_domain.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
build_index(X_train)
¶
Build the hybrid representation and construct the HNSW index.
This is the default path when the user does not use TabularDomainManager and wants everything built on the fly.
Source code in src\luna_xai\domains\tabular_domain.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
get_neighbor_finder()
¶
Expose the internal neighbor manager (HybridNeighborManager).
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the neighbor_finder has not been initialized yet. |
Source code in src\luna_xai\domains\tabular_domain.py
228 229 230 231 232 233 234 235 236 237 238 239 | |
predict(X)
¶
Run prediction in model-space with strict numeric enforcement.
Steps
- Transform X via DataInterface
- Convert sparse → dense (if needed)
- Ensure numeric dtype
- Cast to float32 and predict
- Optionally inverse-transform labels via DataInterface
Source code in src\luna_xai\domains\tabular_domain.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
prepare(instance)
¶
Convert a raw instance to model-space representation using DataInterface.
Source code in src\luna_xai\domains\tabular_domain.py
65 66 67 68 69 70 | |
prepare_neighborhood(instance, n_neighbors=7, n_synthetic=1000, **kwargs)
¶
Prepare a neighborhood around a given instance for sensitivity analysis.
This delegates to the underlying HybridNeighborManager, if available.
Returns:
| Type | Description |
|---|---|
Tuple[Any, Dict]
|
(neighborhood_data, context_dict) |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the neighbor manager has not been initialized. |
NotImplementedError
|
If the HybridNeighborManager does not implement a compatible API. |
Source code in src\luna_xai\domains\tabular_domain.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
Methods¶
Bases: BaseMethod
Deterministic Sensitivity Analysis (DSA).
Generates PDP/ICE-style local sensitivity curves by: 1. Extracting the hybrid-space neighborhood around an instance 2. Generating synthetic samples (empirical) 3. For each feature: - Enumerating the real neighbor feature levels - Overwriting the feature column with each level - Predicting the model response over the full neighborhood
For classification, when a target_class index is provided and the
underlying model implements predict_proba, DSA operates on the
probability of that class instead of raw predictions.
Source code in src\luna_xai\methods\dsa_method.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
__init__(n_neighbors=7, n_samples=1000, sampler='lhs', seed=None, debug=False)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neighbors |
int
|
Number of real neighbors used to define the local region. |
7
|
n_samples |
int
|
Number of synthetic samples drawn from the local neighborhood. |
1000
|
sampler |
('lhs', ...)
|
Sampling strategy used in |
"lhs"
|
seed |
int
|
Random seed for deterministic sampling. If None, sampling may be non-deterministic unless a seed is provided via kwargs. |
None
|
debug |
bool
|
If True, enables debug-level logging via BaseMethod/DebugMixin. |
False
|
Source code in src\luna_xai\methods\dsa_method.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
generate(domain, instance, data=None, **kwargs)
¶
Generate local PDP/ICE-style sensitivity mappings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
domain |
BaseDomain
|
Provides model prediction ( |
required |
instance |
Series or DataFrame
|
Query instance. Will be normalized into a single-row DataFrame. |
required |
data |
Any
|
Unused placeholder, kept only for backward compatibility. |
None
|
**kwargs |
Any
|
Optional overrides such as:
- sampler="lhs" | ...
- seed=123
- n_neighbors, n_samples (if desired in the future)
- target_class= |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
Nested sensitivity structure: {feature -> {level -> [predictions...]}} |
Source code in src\luna_xai\methods\dsa_method.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
Metrics¶
Bases: BaseMetric
Calinski–Harabasz feature-importance metric with directional sign correction based on prediction trends.
Source code in src\luna_xai\metrics\ch_metric.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
__init__(debug=False)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
debug |
bool
|
If True, enables debug-level logging for this metric instance. |
False
|
Source code in src\luna_xai\metrics\ch_metric.py
50 51 52 53 54 55 56 57 | |
compute(sensitivity_data, **kwargs)
¶
Compute CH-based feature importances with sign adjustment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sensitivity_data |
dict
|
Mapping of the form: { feature_A: { value_1: [preds...], value_2: [...], ... }, feature_B: { ... }, ... } Typically generated by a BaseMethod (e.g. DSAMethod). |
required |
Returns:
| Type | Description |
|---|---|
MetricResult
|
Includes: - importances: dict[str, float] - sensitivity_data: raw data used to compute importances |
Source code in src\luna_xai\metrics\ch_metric.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
Renderers¶
Bases: BaseRenderer
Renderer for LUNA XAI plots.
This renderer delegates the actual plotting to utility functions in
luna_xai.utils.plotting and uses a PlotExportStrategy to
persist the generated figures (Matplotlib-based by default).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plot_type |
('importance', 'line', 'boxplot')
|
Default type of plot to render when calling :meth: |
"importance"
|
output_dir |
str
|
Base directory where plots will be saved. Subdirectories per
instance ( |
"results"
|
exporter |
PlotExportStrategy
|
Strategy object responsible for saving Matplotlib figures.
If None, :class: |
None
|
features |
list of str
|
Subset of feature names to be plotted in line/boxplot modes. If None, all available features in the sensitivity data are used. |
None
|
top_k_importances |
int
|
Number of top features to highlight in the importance plot. |
5
|
debug |
bool
|
If True, enables lightweight debug messages via |
False
|
Notes
Logging for this renderer is integrated with the global LUNA logging
infrastructure. By default, handlers are configured to log to a file
(see configure_luna_logging). If you prefer console logging, attach
a logging.StreamHandler to the "luna_xai" logger instead.
Source code in src\luna_xai\renderers\plot_renderer.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
render(result, context=None)
¶
Render a plot from a :class:MetricResult object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result |
MetricResult
|
Result object containing importances and/or sensitivity data. |
required |
context |
dict
|
Extra metadata used to customize the plot, typically including:
|
None
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If the configured |
Source code in src\luna_xai\renderers\plot_renderer.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
render_from_data(sensitivity_data, plot_type=None, instance=None, idx='instance', instance_prediction=None)
¶
Render plots directly from raw sensitivity data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sensitivity_data |
dict
|
Nested dictionary of sensitivity values, typically in the same format produced by the sensitivity methods (e.g. DSA). |
required |
plot_type |
('line', 'boxplot')
|
Plot mode to use. If None, falls back to the renderer's
default |
"line"
|
instance |
object
|
Instance associated with the sensitivity data. |
None
|
idx |
str
|
Identifier used to name files/directories for this instance. |
"instance"
|
instance_prediction |
float
|
Model prediction for the instance, used in boxplots. |
None
|
Notes
- This method does not support
"importance"; that mode is only available via :meth:renderusing a :class:MetricResult.
Source code in src\luna_xai\renderers\plot_renderer.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
Utils¶
Apply arctangent-based normalization with a scaling factor.
This transformation smoothly compresses large-magnitude importance values into the interval [-1, 1], preserving sign and improving stability/interpretability. Values close to zero remain almost unchanged; very large magnitudes asymptotically approach -1 or 1.
The transformation is defined as:
norm(x) = arctan(x / scale_factor) / (π / 2)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
importances |
dict of str to float
|
Raw importance scores from the metric. Values must be numeric
(castable to |
required |
scale_factor |
float
|
Controls curvature — larger values result in gentler compression (i.e., the arctan transition becomes slower). |
1000.0
|
Returns:
| Type | Description |
|---|---|
dict of str to float
|
Normalized importance scores in the range [-1, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in src\luna_xai\utils\normalization.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |