tfma.slicer.SingleSliceSpec

Class SingleSliceSpec

Specification for a single slice.

This is intended to be an immutable class that specifies a single slice. Use this in conjunction with get_slices_for_features_dict to generate slices for a dictionary of features.

Examples: - columns = ['age'], features = [] This means to slice by the 'age' column. - columns = ['age'], features = [('gender', 'female')] This means to slice by the 'age' column if the 'gender' is 'female'. - For more examples, refer to the tests in slicer_test.py.

__init__

__init__(
    columns=(),
    features=()
)

Initialises a SingleSliceSpec.

Args:

  • columns: an iterable of column names to slice on.
  • features: a iterable of features to slice on. Each feature is a (key, value) tuple. Note that the value can be either a string or an int, and the type is taken into account when comparing values, so SingleSliceSpec(features=[('age', '5')]) will not match a slice with age=5.

Raises:

  • ValueError: There was overlap between the columns specified in columns and those in features.
  • ValueError: columns or features was a string: they should probably be a singleton list containing that string.

Methods

__eq__

__eq__(other)

Return self==value.

__ne__

__ne__(other)

Return self!=value.

generate_slices

generate_slices(accessor)

Generates all slices that match this specification from the data.

Should only be called within this file.

Examples: - columns = [], features = [] slice accessor has features age=[5], gender=['f'], interest=['knitting'] returns [[]] - columns = ['age'], features = [('gender', 'f')] slice accessor has features age=[5], gender=['f'], interest=['knitting'] returns [[('age', 5), ('gender, 'f')]] - columns = ['interest'], features = [('gender', 'f')] slice accessor has features age=[5], gender=['f'], interest=['knitting', 'games'] returns [[('gender', 'f'), ('interest, 'knitting')], [('gender', 'f'), ('interest, 'games')]]

Args:

  • accessor: slice accessor.

Yields:

A SliceKeyType for each slice that matches this specification. Nothing will be yielded if there no slices matched this specification. The entries in the yielded SliceKeyTypes are guaranteed to be sorted by key names (and then values, if necessary), ascending.

is_overall

is_overall()

Returns True if this specification represents the overall slice.

is_slice_applicable

is_slice_applicable(slice_key)

Determines if this slice spec is applicable to a slice of data.

Args:

  • slice_key: The slice as a SliceKeyType

Returns:

True if the slice_spec is applicable to the given slice, False otherwise.