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 _load(path, model_analysis): """Load the ExplainerManager from the given path. :param path: The directory path to load the ExplainerManager from. :type path: str :param model_analysis: The loaded parent ModelAnalysis. :type model_analysis: ModelAnalysis """ # create the ExplainerManager without any properties using the __new__ # function, similar to pickle inst = ExplainerManager.__new__(ExplainerManager) top_dir = Path(path) explanation_path = top_dir / ManagerNames.EXPLAINER if explanation_path.exists(): explanation = load_explanation(explanation_path) inst.__dict__[EXPLANATION] = explanation inst.__dict__['_' + MODEL] = model_analysis.model with open(top_dir / META_JSON, 'r') as meta_file: meta = meta_file.read() meta = json.loads(meta) inst.__dict__['_' + IS_RUN] = meta[IS_RUN] inst.__dict__['_' + CLASSES] = model_analysis._classes target_column = model_analysis.target_column train = model_analysis.train.drop(columns=[target_column]) test = model_analysis.test.drop(columns=[target_column]) inst.__dict__[U_INITIALIZATION_EXAMPLES] = train inst.__dict__[U_EVALUATION_EXAMPLES] = test inst.__dict__['_' + FEATURES] = list(train.columns) inst.__dict__['_' + IS_ADDED] = False # reset self._surrogate_model if meta[IS_ADDED]: inst.add() return inst
def _load(path, rai_insights): """Load the ExplainerManager from the given path. :param path: The directory path to load the ExplainerManager from. :type path: str :param rai_insights: The loaded parent RAIInsights. :type rai_insights: RAIInsights :return: The ExplainerManager manager after loading. :rtype: ExplainerManager """ # create the ExplainerManager without any properties using the __new__ # function, similar to pickle inst = ExplainerManager.__new__(ExplainerManager) all_cf_dirs = DirectoryManager.list_sub_directories(path) if len(all_cf_dirs) != 0: directory_manager = DirectoryManager( parent_directory_path=path, sub_directory_name=all_cf_dirs[0]) data_directory = directory_manager.get_data_directory() with open(data_directory / META_JSON, 'r') as meta_file: meta = meta_file.read() meta = json.loads(meta) inst.__dict__['_' + IS_RUN] = meta[IS_RUN] inst.__dict__['_' + IS_ADDED] = meta[IS_ADDED] inst.__dict__[EXPLANATION] = None explanation_path = data_directory / ManagerNames.EXPLAINER if explanation_path.exists(): explanation = load_explanation(explanation_path) inst.__dict__[EXPLANATION] = explanation else: inst.__dict__['_' + IS_RUN] = False inst.__dict__['_' + IS_ADDED] = False inst.__dict__[EXPLANATION] = None inst.__dict__['_' + MODEL] = rai_insights.model inst.__dict__['_' + CLASSES] = rai_insights._classes inst.__dict__['_' + CATEGORICAL_FEATURES] = \ rai_insights.categorical_features target_column = rai_insights.target_column train = rai_insights.train.drop(columns=[target_column]) test = rai_insights.test.drop(columns=[target_column]) inst.__dict__[U_INITIALIZATION_EXAMPLES] = train inst.__dict__[U_EVALUATION_EXAMPLES] = test inst.__dict__['_' + FEATURES] = list(train.columns) # reset the surrogate model inst._initialize_surrogate_model() return inst
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_basic_upload(self, iris, tabular_explainer): x_train = iris[DatasetConstants.X_TRAIN] x_test = iris[DatasetConstants.X_TEST] y_train = iris[DatasetConstants.Y_TRAIN] model = create_sklearn_random_forest_classifier(x_train, y_train) explainer = tabular_explainer(model, x_train) global_explanation = explainer.explain_global(x_test) mlflow.set_experiment(TEST_EXPERIMENT) client = mlflow.tracking.MlflowClient() with mlflow.start_run() as run: _log_explanation(TEST_EXPLANATION, global_explanation) os.mkdir(TEST_EXPLANATION) download_path = client.download_artifacts( run.info.run_uuid, '', dst_path=TEST_EXPLANATION) downloaded_explanation = load_explanation(download_path) _assert_explanation_equivalence(global_explanation, downloaded_explanation)