shapiq.utils.modules#

Functions

safe_isinstance(obj, class_path_str)

Acts as a safe version of isinstance without having to explicitly import packages which may not exist in the user's environment.

try_import(name[, package])

Try to import a module and return None if it fails.

shapiq.utils.modules.safe_isinstance(obj, class_path_str)[source]#

Acts as a safe version of isinstance without having to explicitly import packages which may not exist in the user’s environment. Checks if obj is an instance of type specified by class_path_str.

Note

This function was directly taken from the shap repository.

Parameters:
  • obj (Any) – Some object you want to test against

  • class_path_str (Union[str, list[str], tuple[str]]) – A string or list of strings specifying full class paths Example: sklearn.ensemble.RandomForestRegressor

Return type:

bool

Returns:

True if isinstance is true and the package exists, False otherwise

shapiq.utils.modules.try_import(name, package=None)[source]#

Try to import a module and return None if it fails.

Note

Solution adapted from [stack overflow](https://stackoverflow.com/a/53241197).

Parameters:
  • name (str) – The name of the module to import.

  • package (Optional[str]) – The package to import the module from.

Return type:

Optional[ModuleType]

Returns:

The imported module or None if the import fails.