Aequitas

repository·master·Indexed 20 days ago

https://github.com/dssg/aequitas

An open-source bias and fairness audit toolkit designed for auditing and mitigating unfairness in machine learning models. It supports binary classification tasks and provides tools for auditing predictions, calculating fairness metrics, and applying pre-processing, in-processing, and post-processing corrections. Version 1.1.0 includes Aequitas Fairflow for running Fair ML experiments via the Orchestrator and Hydra configuration management.

Tokens
16.1K
Snippets
49
Records
79
Agent score
73%

What's inside aequitas

  1. Overview of Fair ML methods in Aequitas

    master
    Aequitas provides a variety of methods to address bias and discrimination at different stages of the Machine Learning pipeline. These methods are categorized into Pre-processing, In-processing, and Post-processing.
  2. How Datasets and Methods work in Fairflow

    master

    Aequitas Fairflow is built around two customizable components: Datasets and Methods.

    Datasets

    Datasets follow a standard lifecycle: instantiation $\rightarrow$ load $\rightarrow$ split. You can use GenericDataset to load local files.

    Methods

    Methods are categorized by when they intervene in the ML pipeline:

    1. Pre-processing: Transforms the dataset before training (e.g., PrevalenceSampling).
    2. In-processing: Scores the dataset during training (e.g., FairGBM).
    3. Post-processing: Transforms the scores after prediction (e.g., GroupThreshold).

    Example of a sequential pipeline using all three stages:

    from aequitas.fairflow.datasets import GenericDataset
    from aequitas.fairflow.methods.preprocessing import PrevalenceSampling
    from aequitas.fairflow.methods.inprocessing import FairGBM
    from aequitas.fairflow.methods.postprocessing import GroupThreshold
    
    # 1. Dataset Setup
    dataset = GenericDataset(path="dataset.csv")
    dataset.load_data()
    splits = dataset.create_splits()
    
    # 2. Pre-processing
    sampling = PrevalenceSampling()
    sampling.fit(*splits["train"])
    sampled_data = sampling.transform(*splits["train"])
    
    # 3. In-processing
    model = FairGBM()
    model.fit(*sampled_data)
    preds = model.predict_proba(*splits["validation"])
    
    # 4. Post-processing
    threshold = GroupThreshold()
    threshold.fit(*splits["validation"], preds)
    final_scores = threshold.transform(*splits["validation"], preds)
  3. Run tests and generate coverage reports

    master

    Unit tests are executed using pytest. Because the project uses optional dependencies, you must include the cli and webapp extras to run the full test suite.

    • Run tests: Use uv run --extra cli --extra webapp pytest.
    • Generate coverage report: Use uv run --extra cli --extra webapp pytest --cov-report xml:cov.xml. The resulting cov.xml can be used by IDEs like VSCode to display test coverage.

    Note: Submissions should maintain at least 80% test coverage.

    uv run --extra cli --extra webapp pytest
    # For coverage
    uv run --extra cli --extra webapp pytest --cov-report xml:cov.xml
  4. Build the documentation using nbsphinx

    master

    To build the project documentation from Python notebooks, ensure you have pandoc installed on your system. You can then install the necessary Sphinx extensions and build the docs using the following commands:

    python3 -m pip install nbsphinx
    python3 -m pip install sphinx_rtd_theme
    cd aequitas
    python3 -m sphinx docs/source docs
  5. Train Fair ML methods (Pre-, In-, and Post-processing)

    master

    Aequitas provides interfaces for different types of fairness intervention algorithms. Depending on the algorithm type, the workflow varies:

    Pre-processing

    Used to transform the training data before model training (e.g., PrevalenceSampling).

    In-processing

    Used to train a model that incorporates fairness constraints directly during training (e.g., FairGBM).

    Post-processing

    Used to adjust model scores or thresholds after training to achieve fairness targets (e.g., BalancedGroupThreshold).

    # --- Pre-processing example ---
    from aequitas.flow.methods.preprocessing import PrevalenceSampling
    
    sampler = PrevalenceSampling()
    sampler.fit(dataset.train.X, dataset.train.y, dataset.train.s)
    X_sample, y_sample, s_sample = sampler.transform(dataset.train.X, dataset.train.y, dataset.train.s)
    
    # --- In-processing example ---
    from aequitas.flow.methods.inprocessing import FairGBM
    
    model = FairGBM()
    model.fit(X_sample, y_sample, s_sample)
    scores_val = model.predict_proba(dataset.validation.X, dataset.validation.y, dataset.validation.s)
    scores_test = model.predict_proba(dataset.test.X, dataset.test.y, dataset.test.s)
    
    # --- Post-processing example ---
    from aequitas.flow.methods.postprocessing import BalancedGroupThreshold
    
    # Example: Adjusting thresholds to achieve equal FPR
    threshold = BalancedGroupThreshold("top_pct", 0.1, "fpr")
    threshold.fit(dataset.validation.X, scores_val, dataset.validation.y, dataset.validation.s)
    corrected_scores = threshold.transform(dataset.test.X, scores_test, dataset.test.s)
  6. Format code using ruff

    master

    Aequitas uses ruff for import organization, code formatting, and linting. Use the following commands to format your changes:

    • To format a specific file: uv run ruff format <filename>
    • To format all files in the src directory: uv run ruff format src
    uv run ruff format <filename>
    # or
    uv run ruff format src
  7. Report bugs and propose features

    master

    Aequitas uses GitHub Issues for bug reporting and feature requests.

    When reporting a bug, include:

    • Operating system name and version.
    • Details about your local setup.
    • Detailed steps to reproduce the bug.

    When proposing a feature:

    • Explain in detail how it would work.
    • Keep the scope as narrow as possible to facilitate implementation.
  8. Set up the Aequitas development environment

    master

    The project recommends using Astral's uv for project management. Ensure you have Python 3.8 or higher installed and managed by uv.

    To set up the environment:

    1. Clone the repository.
    2. Use uv run python to instantiate a Python shell within the managed virtual environment.
    git clone https://github.com/dssg/aequitas.git
    cd aequitas
    uv run python