Aequitas
repository·master·Indexed 20 days ago
https://github.com/dssg/aequitasAn 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.
What's inside aequitas
- 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.
How Datasets and Methods work in Fairflow
masterAequitas Fairflow is built around two customizable components: Datasets and Methods.
Datasets
Datasets follow a standard lifecycle:
instantiation$\rightarrow$load$\rightarrow$split. You can useGenericDatasetto load local files.Methods
Methods are categorized by when they intervene in the ML pipeline:
- Pre-processing: Transforms the dataset before training (e.g.,
PrevalenceSampling). - In-processing: Scores the dataset during training (e.g.,
FairGBM). - 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)- Pre-processing: Transforms the dataset before training (e.g.,
Run the Aequitas Webapp
masterTo launch the Aequitas Bias Audit Toolkit web application locally, use the
servemodule via the Python interpreter.python -m serveRun tests and generate coverage reports
masterUnit tests are executed using
pytest. Because the project uses optional dependencies, you must include thecliandwebappextras 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 resultingcov.xmlcan 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- Run tests: Use
Build the documentation using nbsphinx
masterTo build the project documentation from Python notebooks, ensure you have
pandocinstalled 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 docsTrain Fair ML methods (Pre-, In-, and Post-processing)
masterAequitas 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)Format code using ruff
masterAequitas uses
rufffor 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
srcdirectory:uv run ruff format src
uv run ruff format <filename> # or uv run ruff format src- To format a specific file:
Report bugs and propose features
masterAequitas 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.
Set up the Aequitas development environment
masterThe 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:
- Clone the repository.
- Use
uv run pythonto instantiate a Python shell within the managed virtual environment.
git clone https://github.com/dssg/aequitas.git cd aequitas uv run pythonAdd a new file to the documentation
masterTo include a new notebook or
.rstfile in the documentation build:- Create the notebook (or
.rstfile) inside the/sourcedirectory. - Add the filename to the
toctreedirective in/source/index.rst.
- Create the notebook (or
Install Aequitas Fairflow
masterAequitas Fairflow is bundled with the
aequitaspackage from version 0.43.0 onwards. It requires Python 3.7+. Install it using pip:pip install "aequitas>=0.43.0"Install Aequitas
masterYou can install
aequitasvia PyPI or directly from the GitHub repository usingpip.pip install aequitasOr from source
pip install git+https://github.com/dssg/aequitas.git