Skip to content

Commit

Permalink
Optional tempeh, conditional imports (#338)
Browse files Browse the repository at this point in the history
* Make tempeh optional + remove scipy upper bound
* Raise warnings if importing uninstalled packages
* Change import for deprecation class from sklearn
  • Loading branch information
DanielRyszkaIBM authored Aug 26, 2022
1 parent fe93677 commit ee3ca19
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 19 deletions.
6 changes: 5 additions & 1 deletion aif360/algorithms/inprocessing/grid_search_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
"""
from logging import warning

import fairlearn.reductions as red
try:
import fairlearn.reductions as red
except ImportError as error:
warning("{}: GridSearchReduction will be unavailable. To install, run:\n"
"pip install 'aif360[Reductions]'".format(error))
import pandas as pd

from aif360.algorithms import Transformer
Expand Down
2 changes: 1 addition & 1 deletion aif360/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
from aif360.datasets.meps_dataset_panel20_fy2015 import MEPSDataset20
from aif360.datasets.meps_dataset_panel21_fy2016 import MEPSDataset21
from aif360.datasets.regression_dataset import RegressionDataset
from aif360.datasets.law_school_gpa_dataset import LawSchoolGPADataset
from aif360.datasets.law_school_gpa_dataset import LawSchoolGPADataset
8 changes: 6 additions & 2 deletions aif360/datasets/law_school_gpa_dataset.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import os
import pandas as pd
from aif360.datasets import RegressionDataset
import tempeh.configurations as tc

try:
import tempeh.configurations as tc
except ImportError as error:
from logging import warning
warning("{}: LawSchoolGPADataset will be unavailable. To install, run:\n"
"pip install 'aif360[LawSchoolGPA]'".format(error))

class LawSchoolGPADataset(RegressionDataset):
"""Law School GPA dataset.
Expand Down
2 changes: 1 addition & 1 deletion aif360/metrics/mdss_classification_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from aif360.detectors.mdss.MDSS import MDSS

import pandas as pd
from sklearn.exceptions import deprecated
from sklearn.utils.deprecation import deprecated


class MDSSClassificationMetric(ClassificationMetric):
Expand Down
7 changes: 6 additions & 1 deletion aif360/sklearn/datasets/tempeh_datasets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import pandas as pd
import tempeh.configurations as tc
try:
import tempeh.configurations as tc
except ImportError as error:
from logging import warning
warning("{}: fetch_lawschool_gpa will be unavailable. To install, run:\n"
"pip install 'aif360[LawSchoolGPA]'".format(error))

from aif360.sklearn.datasets.utils import standardize_dataset

Expand Down
7 changes: 6 additions & 1 deletion aif360/sklearn/inprocessing/adversarial_debiasing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
from sklearn.preprocessing import LabelEncoder
from sklearn.utils import check_random_state
from sklearn.utils.validation import check_is_fitted
import tensorflow.compat.v1 as tf
try:
import tensorflow.compat.v1 as tf
except ImportError as error:
from logging import warning
warning("{}: AdversarialDebiasing will be unavailable. To install, run:\n"
"pip install 'aif360[AdversarialDebiasing]'".format(error))

from aif360.sklearn.utils import check_inputs, check_groups

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
available in the https://github.com/fairlearn/fairlearn library
licensed under the MIT Licencse, Copyright Microsoft Corporation
"""
import fairlearn.reductions as red
try:
import fairlearn.reductions as red
except ImportError as error:
from logging import warning
warning("{}: ExponentiatedGradientReduction will be unavailable. To install, run:\n"
"pip install 'aif360[Reductions]'".format(error))
from sklearn.base import BaseEstimator, ClassifierMixin, clone
from sklearn.preprocessing import LabelEncoder

Expand Down
7 changes: 6 additions & 1 deletion aif360/sklearn/inprocessing/grid_search_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
available in the https://github.com/fairlearn/fairlearn library
licensed under the MIT Licencse, Copyright Microsoft Corporation
"""
import fairlearn.reductions as red
try:
import fairlearn.reductions as red
except ImportError as error:
from logging import warning
warning("{}: GridSearchReduction will be unavailable. To install, run:\n"
"pip install 'aif360[Reductions]'".format(error))
from sklearn.base import BaseEstimator, ClassifierMixin, clone
from sklearn.preprocessing import LabelEncoder

Expand Down
2 changes: 1 addition & 1 deletion aif360/sklearn/metrics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sklearn.neighbors import NearestNeighbors
from sklearn.utils import check_X_y
from sklearn.utils.validation import column_or_1d
from sklearn.exceptions import deprecated
from sklearn.utils.deprecation import deprecated

from aif360.sklearn.utils import check_groups
from aif360.detectors.mdss.ScoringFunctions import BerkJones, Bernoulli
Expand Down
15 changes: 10 additions & 5 deletions aif360/sklearn/preprocessing/fairadapt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

import pandas as pd
import numpy as np
from rpy2 import robjects
from rpy2.robjects.vectors import StrVector
from rpy2.robjects.packages import importr
from rpy2.robjects import pandas2ri
from rpy2.robjects.conversion import localconverter
try:
from rpy2 import robjects
from rpy2.robjects.vectors import StrVector
from rpy2.robjects.packages import importr
from rpy2.robjects import pandas2ri
from rpy2.robjects.conversion import localconverter
except ImportError as error:
from logging import warning
warning("{}: FairAdapt will be unavailable. To install, run:\n"
"pip install 'aif360[FairAdapt]'".format(error))
from sklearn.base import BaseEstimator

from aif360.sklearn.utils import check_inputs, check_groups
Expand Down
9 changes: 7 additions & 2 deletions aif360/sklearn/preprocessing/learning_fair_representations.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
from sklearn.exceptions import ConvergenceWarning
from sklearn.preprocessing import LabelEncoder
from sklearn.utils import check_random_state
import torch
import torch.nn.functional as F
try:
import torch
import torch.nn.functional as F
except ImportError as error:
from logging import warning
warning("{}: LearnedFairRepresentations will be unavailable. To install, run:\n"
"pip install 'aif360[LFR]'".format(error))

from aif360.sklearn.utils import check_inputs, check_groups

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'Reductions': ['fairlearn==0.4.6'],
'FairAdapt':['rpy2'],
'notebooks': ['jupyter', 'tqdm', 'igraph[plotting]', 'lightgbm', 'seaborn', 'ipympl'],
'LawSchoolGPA': ['tempeh'],
}
extras['tests'] = reduce(lambda l1, l2: l1+l2, extras.values(), ['pytest>=3.5'])
extras['docs'] = ['sphinx<2', 'sphinx_rtd_theme']
Expand All @@ -40,11 +41,10 @@
python_requires='>=3.7',
install_requires=[
'numpy>=1.16',
'scipy>=1.2.0,<1.6.0',
'scipy>=1.2.0',
'pandas>=0.24.0',
'scikit-learn>=1.0',
'matplotlib',
'tempeh',
],
extras_require=extras,
package_data={'aif360': ['data/*', 'data/*/*', 'data/*/*/*']},
Expand Down

0 comments on commit ee3ca19

Please sign in to comment.