Separate explainers, predict and explorers in Units - #795
Open
Felipedino wants to merge 1 commit into
Open
Conversation
- Introduced `test_exploration_units.py` to validate the functionality of exploration units, ensuring proper handling of datasets, explorers, and saving results. - Added `test_prediction_units.py` to test prediction units, focusing on model loading, dataset handling, and prediction saving. - Enhanced `test_unit_contracts.py` with a new test to ensure units do not require keys they do not read, preventing potential composability issues.
Contributor
There was a problem hiding this comment.
🟡 Human review recommended
It performs a wide refactor across multiple core job execution paths (predict/explore/explain), so it warrants final human review despite the added regression and contract tests.
Pull request overview
This PR refactors DashAI’s prediction, exploration, and explainability job flows to be composed from reusable “units” wired through an ExecutionContext, and hardens SHAP integration by avoiding passing bound model methods to SHAP.
Changes:
- Refactors
PredictJob,ExplorerJob, andExplainerJobto orchestrate unit pipelines (load → run → save) viaExecutionContext. - Adds new unit implementations for exploration, prediction, and explanation building/generation, and registers them in
initial_components.py. - Improves SHAP compatibility by introducing
as_shap_predictor()and switching SHAP explainers to use it, plus adds targeted contract and end-to-end regression tests.
File summaries
| File | Description |
|---|---|
| tests/back/units/test_unit_contracts.py | Adds a “reverse” contract test to ensure REQUIRES doesn’t include unread context keys. |
| tests/back/units/test_prediction_units.py | New unit-level contract tests for prediction units (model/dataset load, predict, save). |
| tests/back/units/test_exploration_units.py | New unit-level contract tests for exploration run/save units. |
| tests/back/units/test_explanation_units.py | New unit-level contract tests for explanation units (load/build/prepare/generate). |
| tests/back/explainers/test_shap_predictor_handover.py | New regression tests ensuring SHAP is not handed bound model.predict. |
| tests/back/api/test_units_api.py | Extends units API expectations to include newly registered units and their schemas. |
| tests/back/api/test_predict_job.py | New end-to-end regression net for PredictJob (status transitions, outputs, error cases). |
| tests/back/api/test_explorer_job.py | New end-to-end regression net for ExplorerJob (artifact paths, error cases). |
| tests/back/api/test_explainer_job.py | New end-to-end regression net for ExplainerJob (global/local artifacts, error cases). |
| DashAI/back/units/save_prediction_unit.py | New unit to persist prediction results to a unique results folder and publish results_path. |
| DashAI/back/units/save_exploration_unit.py | New unit to persist exploration artifacts under the notebook folder and publish exploration_path. |
| DashAI/back/units/run_exploration_unit.py | New unit to resolve/instantiate an explorer, prepare the dataset, run exploration, and publish outputs. |
| DashAI/back/units/prepare_explanation_data_unit.py | New unit to replay a run’s recorded split indexes and prepare data_x/data_y for explainers. |
| DashAI/back/units/predict_unit.py | New unit to run model prediction over selected input columns and decode via the task. |
| DashAI/back/units/load_training_dataset_unit.py | New unit to load the training dataset and publish both dataset and JSON-serializable types. |
| DashAI/back/units/load_trained_model_unit.py | New unit to restore a trained model from the Run row’s recorded artifact path. |
| DashAI/back/units/load_run_model_unit.py | New unit matching explainer-flow semantics: instantiate model with parameters then load artifact. |
| DashAI/back/units/generate_local_explanation_unit.py | New unit to select instances (split/rows/manual), fit explainer, generate plots, and publish paths. |
| DashAI/back/units/generate_global_explanation_unit.py | New unit to generate global explanation + plot artifacts and publish their paths. |
| DashAI/back/units/explanation_artifacts.py | New helper module for explainer instantiation and writing explanation artifacts (shared by units). |
| DashAI/back/units/build_manual_input_unit.py | New unit to build an in-memory dataset from user-typed manual input via the task. |
| DashAI/back/units/build_local_explainer_unit.py | New unit to instantiate a local explainer bound to the model in context. |
| DashAI/back/units/build_global_explainer_unit.py | New unit to instantiate a global explainer bound to the model in context. |
| DashAI/back/job/predict_job.py | Refactors prediction execution to a unit-based pipeline and improves error-path status marking. |
| DashAI/back/job/explorer_job.py | Refactors exploration execution to units and ensures failure paths mark the row as ERROR. |
| DashAI/back/job/explainer_job.py | Refactors explanation execution to units; centralizes artifact path publishing and row updates. |
| DashAI/back/initial_components.py | Registers new units so they are discoverable by the registry and exposed via units API. |
| DashAI/back/explainability/model_input.py | Adds as_shap_predictor() to provide SHAP a non-bound callable. |
| DashAI/back/explainability/explainers/regression_kernel_shap.py | Switches SHAP model handover to as_shap_predictor(self.model). |
| DashAI/back/explainability/explainers/kernel_shap.py | Switches SHAP model handover to as_shap_predictor(self.model). |
| DashAI/back/explainability/explainers/contrastive_shap.py | Switches SHAP model handover to as_shap_predictor(self.model). |
Review details
- Files reviewed: 31/31 changed files
- Comments generated: 0
- Review effort level: Lite
We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces significant refactoring and improvements to the explainability and job execution pipeline in DashAI, focusing on modularizing job logic using units, improving SHAP model compatibility, and enhancing error handling and maintainability. The changes replace custom job logic with a unit-based approach, introduce a utility to wrap model predictors for SHAP, and register new units for explainability and prediction workflows.
Refactoring and Modularization of Job Logic:
The job execution logic for exploration and prediction (
explorer_job.py,predict_job.py) is refactored to use a unit-based workflow (ExecutionContextand units likeLoadDatasetUnit,RunExplorationUnit,SaveExplorationUnit, etc.), replacing custom code for dataset loading, explorer/model instantiation, and result saving. This greatly improves maintainability and extensibility. [1] [2] [3] [4] [5] [6] [7]New units are registered in
initial_components.pyto support the new workflow, including units for building explainers, loading/saving models and datasets, running explorations, and generating explanations. [1] [2]Explainability Improvements (SHAP Integration):
Introduces
as_shap_predictorinmodel_input.py, a utility to wrap model predictors as plain functions for SHAP, avoiding issues with read-onlyfeature_names_in_attributes in LightGBM/XGBoost wrappers. This ensures SHAP explainers work reliably across all supported models.Updates all SHAP-based explainers (
contrastive_shap.py,kernel_shap.py,regression_kernel_shap.py) to useas_shap_predictor(self.model)instead ofself.model.predictdirectly, ensuring compatibility and preventing attribute errors. [1] [2] [3] [4] [5] [6]Error Handling and Cleanup:
These changes collectively make the codebase more robust, modular, and maintainable, and ensure that explainability features work reliably with a wider range of models.