Skip to content

pydvl.valuation.types

Sample dataclass

Sample(idx: int | IndexT | None, subset: NDArray[IndexT])

idx instance-attribute

idx: int | IndexT | None

Index of current sample

subset instance-attribute

subset: NDArray[IndexT]

Indices of current sample

__hash__

__hash__()

This type must be hashable for the utility caching to work. We use hashlib.sha256 which is about 4-5x faster than hash(), and returns the same value in all processes, as opposed to hash() which is salted in each process

Source code in src/pydvl/valuation/types.py
def __hash__(self):
    """This type must be hashable for the utility caching to work.
    We use hashlib.sha256 which is about 4-5x faster than hash(), and returns the
    same value in all processes, as opposed to hash() which is salted in each
    process
    """
    sha256_hash = hashlib.sha256(self.subset.tobytes()).hexdigest()
    return int(sha256_hash, base=16)

with_idx_in_subset

with_idx_in_subset() -> Self

Return a copy of sample with idx added to the subset.

Returns the original sample if idx was already part of the subset.

RETURNS DESCRIPTION
Sample

A copy of the sample with idx added to the subset.

TYPE: Self

RAISES DESCRIPTION
ValueError

If idx is None.

Source code in src/pydvl/valuation/types.py
def with_idx_in_subset(self) -> Self:
    """Return a copy of sample with idx added to the subset.

    Returns the original sample if idx was already part of the subset.

    Returns:
        Sample: A copy of the sample with idx added to the subset.

    Raises:
        ValueError: If idx is None.
    """
    if self.idx in self.subset:
        return self

    if self.idx is None:
        raise ValueError("Cannot add idx to subset if idx is None.")

    new_subset = np.array(self.subset.tolist() + [self.idx])
    return replace(self, subset=new_subset)

with_idx

with_idx(idx: int) -> Self

Return a copy of sample with idx changed.

Returns the original sample if idx is the same.

PARAMETER DESCRIPTION
idx

New value for idx.

TYPE: int

RETURNS DESCRIPTION
Sample

A copy of the sample with idx changed.

TYPE: Self

Source code in src/pydvl/valuation/types.py
def with_idx(self, idx: int) -> Self:
    """Return a copy of sample with idx changed.

    Returns the original sample if idx is the same.

    Args:
        idx: New value for idx.

    Returns:
        Sample: A copy of the sample with idx changed.
    """
    if self.idx == idx:
        return self

    return replace(self, idx=idx)

with_subset

with_subset(subset: NDArray[IndexT]) -> Self

Return a copy of sample with subset changed.

Returns the original sample if subset is the same.

PARAMETER DESCRIPTION
subset

New value for subset.

TYPE: NDArray[IndexT]

RETURNS DESCRIPTION
Sample

A copy of the sample with subset changed.

TYPE: Self

Source code in src/pydvl/valuation/types.py
def with_subset(self, subset: NDArray[IndexT]) -> Self:
    """Return a copy of sample with subset changed.

    Returns the original sample if subset is the same.

    Args:
        subset: New value for subset.

    Returns:
        Sample: A copy of the sample with subset changed.
    """
    if np.array_equal(self.subset, subset):
        return self

    return replace(self, subset=subset)

ClasswiseSample dataclass

ClasswiseSample(
    idx: int | IndexT | None,
    subset: NDArray[IndexT],
    label: int,
    ooc_subset: NDArray[IndexT],
)

Bases: Sample

Sample class for classwise shapley valuation

idx instance-attribute

idx: int | IndexT | None

Index of current sample

subset instance-attribute

subset: NDArray[IndexT]

Indices of current sample

label instance-attribute

label: int

Label of the current sample

ooc_subset instance-attribute

ooc_subset: NDArray[IndexT]

Indices of out-of-class elements, i.e., those with a label different from this sample's label

with_idx_in_subset

with_idx_in_subset() -> Self

Return a copy of sample with idx added to the subset.

Returns the original sample if idx was already part of the subset.

RETURNS DESCRIPTION
Sample

A copy of the sample with idx added to the subset.

TYPE: Self

RAISES DESCRIPTION
ValueError

If idx is None.

Source code in src/pydvl/valuation/types.py
def with_idx_in_subset(self) -> Self:
    """Return a copy of sample with idx added to the subset.

    Returns the original sample if idx was already part of the subset.

    Returns:
        Sample: A copy of the sample with idx added to the subset.

    Raises:
        ValueError: If idx is None.
    """
    if self.idx in self.subset:
        return self

    if self.idx is None:
        raise ValueError("Cannot add idx to subset if idx is None.")

    new_subset = np.array(self.subset.tolist() + [self.idx])
    return replace(self, subset=new_subset)

with_idx

with_idx(idx: int) -> Self

Return a copy of sample with idx changed.

Returns the original sample if idx is the same.

PARAMETER DESCRIPTION
idx

New value for idx.

TYPE: int

RETURNS DESCRIPTION
Sample

A copy of the sample with idx changed.

TYPE: Self

Source code in src/pydvl/valuation/types.py
def with_idx(self, idx: int) -> Self:
    """Return a copy of sample with idx changed.

    Returns the original sample if idx is the same.

    Args:
        idx: New value for idx.

    Returns:
        Sample: A copy of the sample with idx changed.
    """
    if self.idx == idx:
        return self

    return replace(self, idx=idx)

with_subset

with_subset(subset: NDArray[IndexT]) -> Self

Return a copy of sample with subset changed.

Returns the original sample if subset is the same.

PARAMETER DESCRIPTION
subset

New value for subset.

TYPE: NDArray[IndexT]

RETURNS DESCRIPTION
Sample

A copy of the sample with subset changed.

TYPE: Self

Source code in src/pydvl/valuation/types.py
def with_subset(self, subset: NDArray[IndexT]) -> Self:
    """Return a copy of sample with subset changed.

    Returns the original sample if subset is the same.

    Args:
        subset: New value for subset.

    Returns:
        Sample: A copy of the sample with subset changed.
    """
    if np.array_equal(self.subset, subset):
        return self

    return replace(self, subset=subset)