From 4f0ce27b911eb54976c1945ffa7f7f998dd7d119 Mon Sep 17 00:00:00 2001 From: daniel-ryszka Date: Wed, 24 Aug 2022 17:54:37 +0200 Subject: [PATCH 1/5] Make tempeh optional + remove scipy upper bound Signed-off-by: DanielRyszkaIBM --- aif360/datasets/__init__.py | 5 ++++- aif360/sklearn/datasets/__init__.py | 5 ++++- setup.py | 6 +++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/aif360/datasets/__init__.py b/aif360/datasets/__init__.py index 8505e77f..9c1f1919 100644 --- a/aif360/datasets/__init__.py +++ b/aif360/datasets/__init__.py @@ -10,4 +10,7 @@ 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 + +import pkgutil +if pkgutil.find_loader("tempeh"): + from aif360.datasets.law_school_gpa_dataset import LawSchoolGPADataset diff --git a/aif360/sklearn/datasets/__init__.py b/aif360/sklearn/datasets/__init__.py index cd475d14..187c5aed 100644 --- a/aif360/sklearn/datasets/__init__.py +++ b/aif360/sklearn/datasets/__init__.py @@ -11,4 +11,7 @@ from aif360.sklearn.datasets.utils import * from aif360.sklearn.datasets.openml_datasets import * from aif360.sklearn.datasets.compas_dataset import fetch_compas -from aif360.sklearn.datasets.tempeh_datasets import * + +import pkgutil +if pkgutil.find_loader("tempeh"): + from aif360.sklearn.datasets.tempeh_datasets import * diff --git a/setup.py b/setup.py index 40a05bdd..6f89282f 100644 --- a/setup.py +++ b/setup.py @@ -22,6 +22,7 @@ 'Reductions': ['fairlearn==0.4.6'], 'FairAdapt':['rpy2'], 'notebooks': ['jupyter', 'tqdm', 'igraph[plotting]', 'lightgbm', 'seaborn', 'ipympl'], + 'tempeh-datasets': ['tempeh'], } extras['tests'] = reduce(lambda l1, l2: l1+l2, extras.values(), ['pytest>=3.5']) extras['docs'] = ['sphinx<2', 'sphinx_rtd_theme'] @@ -40,11 +41,10 @@ python_requires='>=3.6', install_requires=[ 'numpy>=1.16', - 'scipy>=1.2.0,<1.6.0', + 'scipy>=1.2.0', 'pandas>=0.24.0', 'scikit-learn>=0.22.1', - 'matplotlib', - 'tempeh', + 'matplotlib' ], extras_require=extras, package_data={'aif360': ['data/*', 'data/*/*', 'data/*/*/*']}, From e947a842bbcd452dfbab9e03cfe8ef24ca3561ba Mon Sep 17 00:00:00 2001 From: daniel-ryszka Date: Wed, 24 Aug 2022 20:57:00 +0200 Subject: [PATCH 2/5] Change import for deprecation class from sklearn Signed-off-by: DanielRyszkaIBM --- aif360/metrics/mdss_classification_metric.py | 2 +- aif360/sklearn/metrics/metrics.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/aif360/metrics/mdss_classification_metric.py b/aif360/metrics/mdss_classification_metric.py index 2897d0ac..4211199b 100644 --- a/aif360/metrics/mdss_classification_metric.py +++ b/aif360/metrics/mdss_classification_metric.py @@ -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): diff --git a/aif360/sklearn/metrics/metrics.py b/aif360/sklearn/metrics/metrics.py index 824b681b..02e24cf1 100644 --- a/aif360/sklearn/metrics/metrics.py +++ b/aif360/sklearn/metrics/metrics.py @@ -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 From 375e7faa2fc04238303820864e33200b097751fb Mon Sep 17 00:00:00 2001 From: daniel-ryszka Date: Wed, 24 Aug 2022 21:09:34 +0200 Subject: [PATCH 3/5] Make conditional tf import for sklearn.AdversarialDebiasing Signed-off-by: DanielRyszkaIBM --- aif360/sklearn/inprocessing/adversarial_debiasing.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/aif360/sklearn/inprocessing/adversarial_debiasing.py b/aif360/sklearn/inprocessing/adversarial_debiasing.py index 0df80af5..d5206cb0 100644 --- a/aif360/sklearn/inprocessing/adversarial_debiasing.py +++ b/aif360/sklearn/inprocessing/adversarial_debiasing.py @@ -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 From 0bbfa2f41912576abe2823097d5ef6991a6dc2d3 Mon Sep 17 00:00:00 2001 From: daniel-ryszka Date: Wed, 24 Aug 2022 22:18:45 +0200 Subject: [PATCH 4/5] More safe imports Signed-off-by: DanielRyszkaIBM --- aif360/algorithms/inprocessing/grid_search_reduction.py | 6 +++++- .../inprocessing/exponentiated_gradient_reduction.py | 7 ++++++- aif360/sklearn/inprocessing/grid_search_reduction.py | 7 ++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/aif360/algorithms/inprocessing/grid_search_reduction.py b/aif360/algorithms/inprocessing/grid_search_reduction.py index aa0ada81..df1b7ebf 100644 --- a/aif360/algorithms/inprocessing/grid_search_reduction.py +++ b/aif360/algorithms/inprocessing/grid_search_reduction.py @@ -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 diff --git a/aif360/sklearn/inprocessing/exponentiated_gradient_reduction.py b/aif360/sklearn/inprocessing/exponentiated_gradient_reduction.py index 2a1da1d5..1eae93d1 100644 --- a/aif360/sklearn/inprocessing/exponentiated_gradient_reduction.py +++ b/aif360/sklearn/inprocessing/exponentiated_gradient_reduction.py @@ -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 diff --git a/aif360/sklearn/inprocessing/grid_search_reduction.py b/aif360/sklearn/inprocessing/grid_search_reduction.py index 4af7762d..35e37b57 100644 --- a/aif360/sklearn/inprocessing/grid_search_reduction.py +++ b/aif360/sklearn/inprocessing/grid_search_reduction.py @@ -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 From cf4bb425f2971621b3373b5d5b0c67cdc8bffc26 Mon Sep 17 00:00:00 2001 From: DanielRyszkaIBM Date: Fri, 26 Aug 2022 21:24:09 +0200 Subject: [PATCH 5/5] Refactor + more safe imports Signed-off-by: DanielRyszkaIBM --- aif360/datasets/__init__.py | 5 +---- aif360/datasets/law_school_gpa_dataset.py | 8 ++++++-- aif360/sklearn/datasets/__init__.py | 5 +---- aif360/sklearn/datasets/tempeh_datasets.py | 7 ++++++- aif360/sklearn/preprocessing/fairadapt.py | 15 ++++++++++----- .../learning_fair_representations.py | 9 +++++++-- setup.py | 2 +- 7 files changed, 32 insertions(+), 19 deletions(-) diff --git a/aif360/datasets/__init__.py b/aif360/datasets/__init__.py index 9c1f1919..a9d17caf 100644 --- a/aif360/datasets/__init__.py +++ b/aif360/datasets/__init__.py @@ -10,7 +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 - -import pkgutil -if pkgutil.find_loader("tempeh"): - from aif360.datasets.law_school_gpa_dataset import LawSchoolGPADataset +from aif360.datasets.law_school_gpa_dataset import LawSchoolGPADataset diff --git a/aif360/datasets/law_school_gpa_dataset.py b/aif360/datasets/law_school_gpa_dataset.py index cbfdf8ba..eac3656c 100644 --- a/aif360/datasets/law_school_gpa_dataset.py +++ b/aif360/datasets/law_school_gpa_dataset.py @@ -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. diff --git a/aif360/sklearn/datasets/__init__.py b/aif360/sklearn/datasets/__init__.py index 2843104d..525b1431 100644 --- a/aif360/sklearn/datasets/__init__.py +++ b/aif360/sklearn/datasets/__init__.py @@ -12,7 +12,4 @@ from aif360.sklearn.datasets.openml_datasets import fetch_adult, fetch_german, fetch_bank from aif360.sklearn.datasets.compas_dataset import fetch_compas from aif360.sklearn.datasets.meps_datasets import fetch_meps - -import pkgutil -if pkgutil.find_loader("tempeh"): - from aif360.sklearn.datasets.tempeh_datasets import fetch_lawschool_gpa +from aif360.sklearn.datasets.tempeh_datasets import fetch_lawschool_gpa diff --git a/aif360/sklearn/datasets/tempeh_datasets.py b/aif360/sklearn/datasets/tempeh_datasets.py index cc44e1a3..7adae048 100644 --- a/aif360/sklearn/datasets/tempeh_datasets.py +++ b/aif360/sklearn/datasets/tempeh_datasets.py @@ -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 diff --git a/aif360/sklearn/preprocessing/fairadapt.py b/aif360/sklearn/preprocessing/fairadapt.py index 96d2150d..96569464 100644 --- a/aif360/sklearn/preprocessing/fairadapt.py +++ b/aif360/sklearn/preprocessing/fairadapt.py @@ -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 diff --git a/aif360/sklearn/preprocessing/learning_fair_representations.py b/aif360/sklearn/preprocessing/learning_fair_representations.py index 34f0a6f5..52f62873 100644 --- a/aif360/sklearn/preprocessing/learning_fair_representations.py +++ b/aif360/sklearn/preprocessing/learning_fair_representations.py @@ -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 diff --git a/setup.py b/setup.py index c9623b92..a6ad5e54 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ 'Reductions': ['fairlearn==0.4.6'], 'FairAdapt':['rpy2'], 'notebooks': ['jupyter', 'tqdm', 'igraph[plotting]', 'lightgbm', 'seaborn', 'ipympl'], - 'tempeh-datasets': ['tempeh'], + 'LawSchoolGPA': ['tempeh'], } extras['tests'] = reduce(lambda l1, l2: l1+l2, extras.values(), ['pytest>=3.5']) extras['docs'] = ['sphinx<2', 'sphinx_rtd_theme']