rex_xai.explanation

main logical entrypoint for ReX.

Functions

try_preprocess(args, model_shape, device)

Makes an attempt to preprocess input data as required for the model.

load_and_preprocess_data(model_shape, device, args)

Loads input data from filepath and does preprocessing.

predict_target(data, prediction_func)

Predicts classification of input data, using given prediction function.

calculate_responsibility(data, args, prediction_func)

Calculates ResponsibilityMaps for input data using given args.

analyze(exp, data_mode)

Analyzes an Explanation.

_explanation(args, model_shape, prediction_func, device)

Takes a CausalArgs object and model information and returns a Explanation.

get_prediction_func_from_args(args)

Takes a CausalArgs object and gets the prediction function and model shape.

explanation(args, device[, db])

Takes a CausalArgs object and returns a Explanation.

Module Contents

rex_xai.explanation.try_preprocess(args, model_shape, device)

Makes an attempt to preprocess input data as required for the model.

Data preprocessing is based on file extension and (possibly) user-defined mode. File extensions in [".jpg", ".jpeg", ".png", ".tif", ".tiff"] are treated as images, “.npy” and “.mat” are treated as Numpy arrays, and “.nii” are treated as nifti files. For any other file extension, we create a Data object without pre-processing.

Parameters:
Returns:

the processed input data

Return type:

Data

rex_xai.explanation.load_and_preprocess_data(model_shape, device, args)

Loads input data from filepath and does preprocessing.

Uses a custom preprocesssing function if this is defined in args.script.preprocess, otherwise try_preprocess().

Parameters:
Returns:

the processed input data

Return type:

Data

rex_xai.explanation.predict_target(data, prediction_func)

Predicts classification of input data, using given prediction function.

Uses prediction_func to identify the classification of the input data and return this as the target classification for ReX.

Parameters:
  • data (rex_xai.input_data.Data) – processed input data object

  • args – configuration values for ReX

  • prediction_func – prediction function for the model

Returns:

the predicted target classification and confidence

Return type:

Prediction

rex_xai.explanation.calculate_responsibility(data, args, prediction_func, keep_all_maps=False)

Calculates ResponsibilityMaps for input data using given args.

Runs causal_explanation() for args.iters iterations, and returns a ResponsibilityMaps object and a dictionary containing some statistics about the calculation process. The ResponsibilityMaps object by default only includes the responsibility map that matches the classification of the input data. Set keep_all_maps to True to retain all maps.

Parameters:
Returns:

tuple containing

  • ResponsibilityMaps: ResponsibilityMaps for the given data, prediction function, and args.

  • dict: statistics for the call of this function that generated the ResponsibilityMaps object

Return type:

tuple[rex_xai.resp_maps.ResponsibilityMaps, dict]

rex_xai.explanation.analyze(exp, data_mode)

Analyzes an Explanation.

Analyzes the area ratio, entropy difference, insertion and deletion curves for an Explanation object, prints them, and returns them.

Parameters:
Returns:

tuple containing

  • area (float)

  • entropy_diff (float)

  • insertion_curve (float)

  • deletion_curve (float)

rex_xai.explanation._explanation(args, model_shape, prediction_func, device, db=None, path=None)

Takes a CausalArgs object and model information and returns a Explanation.

Takes a CausalArgs object, model shape and prediction function and returns an Explanation. Depending on the input args, optionally produces output plots, analyses the output explanation, and/or writes results to a database.

Parameters:
Returns:

An Explanation object containing the causal reponsibility explanation calculated using the given args.

Return type:

Explanation

rex_xai.explanation.get_prediction_func_from_args(args)

Takes a CausalArgs object and gets the prediction function and model shape.

If args.script specifies a prediction function and model shape, returns these. Otherwise gets the prediction function and model shape from the provided model file.

Parameters:

args (rex_xai.config.CausalArgs) – configuration values for ReX

Returns:

tuple containing

  • prediction_func

  • model_shape

Raises:

RuntimeError – if an onnx inference instance cannot be created from the provided model file.

rex_xai.explanation.explanation(args, device, db=None)

Takes a CausalArgs object and returns a Explanation.

Takes a CausalArgs object and returns either an Explanation, or a list of Explanations if the input args.path is a directory rather than a path to a single file.

Parameters:
Returns:

An Explanation object containing the causal reponsibility explanation calculated using the given args.

Return type:

Explanation