Skip to content

Collection

The Collection classes provide a way to organize and iterate through groups of related fairness scenarios, making it easier to run batch analyses across multiple datasets with different sensitive attribute configurations.

Class Documentation

fairml_datasets.collection.Collection

A collection of fairness scenarios that can be iterated through.

This class provides a way to group multiple related scenarios and perform batch operations or analyses across them.

Source code in fairml_datasets/collection.py
class Collection:
    """
    A collection of fairness scenarios that can be iterated through.

    This class provides a way to group multiple related scenarios
    and perform batch operations or analyses across them.
    """

    scenarios: List[Scenario]

    def __init__(self, scenarios: List[Scenario]):
        """
        Initialize a Collection with a list of scenarios.

        Args:
            scenarios: List of Scenario objects
        """
        self.scenarios = scenarios

    def __iter__(self):
        """
        Make the Collection iterable, yielding each scenario in turn.

        Returns:
            Iterator over scenarios in the collection
        """
        return iter(self.scenarios)

Functions

__init__(scenarios)

Initialize a Collection with a list of scenarios.

Parameters:

Name Type Description Default
scenarios List[Scenario]

List of Scenario objects

required
Source code in fairml_datasets/collection.py
def __init__(self, scenarios: List[Scenario]):
    """
    Initialize a Collection with a list of scenarios.

    Args:
        scenarios: List of Scenario objects
    """
    self.scenarios = scenarios

__iter__()

Make the Collection iterable, yielding each scenario in turn.

Returns:

Type Description

Iterator over scenarios in the collection

Source code in fairml_datasets/collection.py
def __iter__(self):
    """
    Make the Collection iterable, yielding each scenario in turn.

    Returns:
        Iterator over scenarios in the collection
    """
    return iter(self.scenarios)

fairml_datasets.collection.PrespecifiedCollection

Bases: Collection

A collection of scenarios with predefined dataset and sensitive column configurations.

This class derives from Collection and is designed to be subclassed with a specific 'info' attribute that defines which datasets and sensitive columns to use.

Source code in fairml_datasets/collection.py
class PrespecifiedCollection(Collection):
    """
    A collection of scenarios with predefined dataset and sensitive column configurations.

    This class derives from Collection and is designed to be subclassed with
    a specific 'info' attribute that defines which datasets and sensitive columns to use.
    """

    scenario_ids: List[str]

    def __init__(self):
        """
        Initialize a PrespecifiedCollection based on the predefined scenario ids.
        """
        # Generate scenarios based on info
        scenarios = [Scenario.from_id(scenario_id) for scenario_id in self.scenario_ids]
        super().__init__(scenarios)

Functions

__init__()

Initialize a PrespecifiedCollection based on the predefined scenario ids.

Source code in fairml_datasets/collection.py
def __init__(self):
    """
    Initialize a PrespecifiedCollection based on the predefined scenario ids.
    """
    # Generate scenarios based on info
    scenarios = [Scenario.from_id(scenario_id) for scenario_id in self.scenario_ids]
    super().__init__(scenarios)

Usage Examples

Basic Usage

from fairml_datasets.scenario import Scenario
from fairml_datasets.collection import Collection

# Create scenarios
scenario1 = Scenario("adult", ["sex"])
scenario2 = Scenario("compas", ["race"])

# Create a collection
collection = Collection([scenario1, scenario2])

# Iterate through scenarios
for scenario in collection:
    print(f"Dataset: {scenario.dataset_id}, Sensitive columns: {scenario.sensitive_columns}")

    df = scenario.load(stage="prepared")