def verify_serialization(explanation, extra_path=None, exist_ok=False): path = 'brand/new/path' if extra_path is not None: path = os.path.join(path, extra_path) save_explanation(explanation, path, exist_ok=exist_ok) loaded_explanation = load_explanation(path) _assert_explanation_equivalence(explanation, loaded_explanation)
def _save(self, path): """Save the ExplainerManager to the given path. :param path: The directory path to save the ExplainerManager to. :type path: str """ top_dir = Path(path) top_dir.mkdir(parents=True, exist_ok=True) # save the explanation if self._explanation: save_explanation(self._explanation, top_dir / ManagerNames.EXPLAINER) meta = {IS_RUN: self._is_run, IS_ADDED: self._is_added} with open(Path(path) / META_JSON, 'w') as file: json.dump(meta, file)
def verify_serialization(explanation): paramkeys = ['MODEL_TYPE', 'MODEL_TASK', 'METHOD', 'FEATURES', 'CLASSES'] log_items = dict() for paramkey in paramkeys: param = getattr(ExplainParams, paramkey) value = getattr(explanation, param, None) if value is not None: if isinstance(value, np.ndarray): log_items[param] = value.tolist() else: log_items[param] = value comment = json.dumps(log_items) test_logger.setLevel(logging.INFO) test_logger.info("validating serialization of explanation:\n%s", comment) expljson = save_explanation(explanation) deserialized_explanation = load_explanation(expljson) _assert_explanation_equivalence(deserialized_explanation, explanation)
def test_save_explanation(self, iris, tabular_explainer, iris_svm_model): explainer = tabular_explainer(iris_svm_model, iris[DatasetConstants.X_TRAIN], features=iris[DatasetConstants.FEATURES]) explanation = explainer.explain_local(iris[DatasetConstants.X_TEST]) save_explanation(explanation, 'brand/new/path')