attribench.result.SensitivityNResult

class attribench.result.SensitivityNResult(method_names, maskers, activation_fns, num_samples, num_steps)[source]

Bases: GroupedMetricResult

Represents results from running the Sensitivity-N metric.

Parameters:
method_namesList[str]

Names of attribution methods tested by Sensitivity-N.

maskersList[str]

Names of maskers used by Sensitivity-N.

activation_fnsList[str]

Names of activation functions used by Sensitivity-N.

num_samplesint

Number of samples on which Sensitivity-N was run.

num_stepsint

Number of steps used by Sensitivity-N.

Methods

add

Add a batch to the result (abstract method).

get_df

Retrieves a dataframe from the result for a given masker and activation function.

load

Load a result from an HDF5 file or a directory of CSV files.

save

Save the result to an HDF5 file or a nested directory of CSV files.

abstract add(batch_result)

Add a batch to the result (abstract method).

get_df(masker, activation_fn, methods=None, columns=None)[source]

Retrieves a dataframe from the result for a given masker and activation function. The dataframe contains a row for each sample and a column for each method. Each value is the average Sensitivity-N value for the given method on the given sample, over the specified columns.

Parameters:
maskerstr

the masker to use

activation_fnstr

the activation function to use

methodsOptional[List[str, …]]

the methods to include. If None, includes all methods.

columnsOptional[npt.NDArray]

the columns used in the aggregation. If None, uses all columns.

Returns:
Tuple[pd.DataFrame, bool]

dataframe containing results, and boolean indicating if higher is better (always True for Sensitivity-N)

Return type:

Tuple[DataFrame, bool]

classmethod load(path)

Load a result from an HDF5 file or a directory of CSV files. The format is inferred from the path: if the path is a directory, the result is loaded from a directory of CSV files, otherwise the result is loaded from an HDF5 file.

The specific subclass of MetricResult is inferred from the metadata stored in the file or directory, and the appropriate load method is called.

Parameters:
pathstr

Path to the file or directory.

Returns:
MetricResult

The loaded result.

Return type:

MetricResult

save(path, format)

Save the result to an HDF5 file or a nested directory of CSV files.

Parameters:
pathstr

Path to the file.

formatstr

Format to save the result in. Options: hdf5, dir. If hdf5, the full result is stored in a single HDF5 file. If csv, the result is stored in a nested directory of CSV files.

Return type:

None