Skip to content

Commit

Permalink
MAINT: align daal4py, onedal and sklearnex modules with isort a…
Browse files Browse the repository at this point in the history
…nd black (#1375)

* apply isort and black fixes for daal4py, onedal and sklearnex modules
  • Loading branch information
samir-nasibli authored Jul 26, 2023
1 parent 057fe01 commit 14d3132
Show file tree
Hide file tree
Showing 203 changed files with 9,594 additions and 6,929 deletions.
33 changes: 18 additions & 15 deletions daal4py/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
#===============================================================================
# ===============================================================================
# Copyright 2014 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,44 +13,47 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#===============================================================================
# ===============================================================================

import platform

if "Windows" in platform.system():
import os
import sys
import site
import sys

current_path = os.path.dirname(__file__)
path_to_env = site.getsitepackages()[0]
path_to_libs = os.path.join(path_to_env, "Library", "bin")
path_to_oneapi_backend = os.path.join(current_path, "oneapi")
if sys.version_info.minor >= 8:
if 'DALROOT' in os.environ:
dal_root_redist = os.path.join(os.environ['DALROOT'], "redist", "intel64")
if "DALROOT" in os.environ:
dal_root_redist = os.path.join(os.environ["DALROOT"], "redist", "intel64")
if os.path.exists(dal_root_redist):
os.add_dll_directory(dal_root_redist)
os.environ['PATH'] = dal_root_redist + os.pathsep + os.environ['PATH']
os.environ["PATH"] = dal_root_redist + os.pathsep + os.environ["PATH"]
os.add_dll_directory(path_to_libs)
os.add_dll_directory(path_to_oneapi_backend)
os.environ['PATH'] = path_to_libs + os.pathsep + os.environ['PATH']
os.environ["PATH"] = path_to_libs + os.pathsep + os.environ["PATH"]

try:
from daal4py._daal4py import *
from daal4py._daal4py import (
_get__version__,
__has_dist__,
_get__daal_link_version__,
_get__daal_run_version__,
__has_dist__)
_get__version__,
)
except ImportError as e:
s = str(e)
if 'libfabric' in s:
if "libfabric" in s:
raise ImportError(
s + '\n\nActivating your conda environment or sourcing mpivars.'
'[c]sh/psxevars.[c]sh may solve the issue.\n')
s + "\n\nActivating your conda environment or sourcing mpivars."
"[c]sh/psxevars.[c]sh may solve the issue.\n"
)

raise

from . import mb
from . import sklearn
from . import mb, sklearn

__all__ = ['mb', 'sklearn']
__all__ = ["mb", "sklearn"]
28 changes: 16 additions & 12 deletions daal4py/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
#===============================================================================
# ===============================================================================
# Copyright 2014 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,9 +13,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#===============================================================================
# ===============================================================================

import sys

from .sklearn import patch_sklearn


Expand All @@ -29,27 +30,30 @@ def _main():
Python* patches of scikit-learn, optimizing solvers of
scikit-learn with Intel(R) oneAPI Data Analytics Library.
""",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)

parser.add_argument('-m', action='store_true', dest='module',
help="Executes following as a module")
parser.add_argument('name', help="Script or module name")
parser.add_argument('args', nargs=argparse.REMAINDER,
help="Command line arguments")
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)

parser.add_argument(
"-m", action="store_true", dest="module", help="Executes following as a module"
)
parser.add_argument("name", help="Script or module name")
parser.add_argument("args", nargs=argparse.REMAINDER, help="Command line arguments")
args = parser.parse_args()

try:
import sklearn

patch_sklearn()
except ImportError:
print("Scikit-learn could not be imported. Nothing to patch")

sys.argv = [args.name] + args.args
if '_' + args.name in globals():
return globals()['_' + args.name](*args.args)
if "_" + args.name in globals():
return globals()["_" + args.name](*args.args)
import runpy

runf = runpy.run_module if args.module else runpy.run_path
runf(args.name, run_name='__main__')
runf(args.name, run_name="__main__")


sys.exit(_main())
6 changes: 3 additions & 3 deletions daal4py/mb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
#===============================================================================
# ===============================================================================
# Copyright 2023 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,8 +13,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#===============================================================================
# ===============================================================================

from .model_builders import GBTDAALBaseModel, convert_model

__all__ = ['GBTDAALBaseModel', 'convert_model']
__all__ = ["GBTDAALBaseModel", "convert_model"]
109 changes: 67 additions & 42 deletions daal4py/mb/model_builders.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#===============================================================================
# ===============================================================================
# Copyright 2023 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -12,16 +12,18 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#===============================================================================
# ===============================================================================

# daal4py Model builders API

import numpy as np

import daal4py as d4p

try:
from pandas import DataFrame
from pandas.core.dtypes.cast import find_common_type

pandas_is_imported = True
except (ImportError, ModuleNotFoundError):
pandas_is_imported = False
Expand All @@ -41,7 +43,7 @@ def getFPType(X):
dt = find_common_type(X.dtypes.tolist())
return parse_dtype(dt)

dt = getattr(X, 'dtype', None)
dt = getattr(X, "dtype", None)
return parse_dtype(dt)


Expand All @@ -65,9 +67,9 @@ def _get_params_from_xgboost(self, params):
self.n_features_in_ = int(params["learner"]["learner_model_param"]["num_feature"])

def _get_params_from_catboost(self, params):
if 'class_params' in params['model_info']:
self.n_classes_ = len(params['model_info']['class_params']['class_to_label'])
self.n_features_in_ = len(params['features_info']['float_features'])
if "class_params" in params["model_info"]:
self.n_classes_ = len(params["model_info"]["class_params"]["class_to_label"])
self.n_features_in_ = len(params["features_info"]["float_features"])

def _convert_model_from_lightgbm(self, booster):
lgbm_params = d4p.get_lightgbm_params(booster)
Expand All @@ -85,91 +87,112 @@ def _convert_model_from_catboost(self, booster):
self._get_params_from_catboost(catboost_params)

def _convert_model(self, model):
(submodule_name, class_name) = (model.__class__.__module__,
model.__class__.__name__)
(submodule_name, class_name) = (
model.__class__.__module__,
model.__class__.__name__,
)
self_class_name = self.__class__.__name__

# Build GBTDAALClassifier from LightGBM
if (submodule_name, class_name) == ("lightgbm.sklearn", "LGBMClassifier"):
if self_class_name == "GBTDAALClassifier":
self._convert_model_from_lightgbm(model.booster_)
else:
raise TypeError(f"Only GBTDAALClassifier can be created from\
{submodule_name}.{class_name} (got {self_class_name})")
raise TypeError(
f"Only GBTDAALClassifier can be created from\
{submodule_name}.{class_name} (got {self_class_name})"
)
# Build GBTDAALClassifier from XGBoost
elif (submodule_name, class_name) == ("xgboost.sklearn", "XGBClassifier"):
if self_class_name == "GBTDAALClassifier":
self._convert_model_from_xgboost(model.get_booster())
else:
raise TypeError(f"Only GBTDAALClassifier can be created from\
{submodule_name}.{class_name} (got {self_class_name})")
raise TypeError(
f"Only GBTDAALClassifier can be created from\
{submodule_name}.{class_name} (got {self_class_name})"
)
# Build GBTDAALClassifier from CatBoost
elif (submodule_name, class_name) == ("catboost.core", "CatBoostClassifier"):
if self_class_name == "GBTDAALClassifier":
self._convert_model_from_catboost(model)
else:
raise TypeError(f"Only GBTDAALClassifier can be created from\
{submodule_name}.{class_name} (got {self_class_name})")
raise TypeError(
f"Only GBTDAALClassifier can be created from\
{submodule_name}.{class_name} (got {self_class_name})"
)
# Build GBTDAALRegressor from LightGBM
elif (submodule_name, class_name) == ("lightgbm.sklearn", "LGBMRegressor"):
if self_class_name == "GBTDAALRegressor":
self._convert_model_from_lightgbm(model.booster_)
else:
raise TypeError(f"Only GBTDAALRegressor can be created from\
{submodule_name}.{class_name} (got {self_class_name})")
raise TypeError(
f"Only GBTDAALRegressor can be created from\
{submodule_name}.{class_name} (got {self_class_name})"
)
# Build GBTDAALRegressor from XGBoost
elif (submodule_name, class_name) == ("xgboost.sklearn", "XGBRegressor"):
if self_class_name == "GBTDAALRegressor":
self._convert_model_from_xgboost(model.get_booster())
else:
raise TypeError(f"Only GBTDAALRegressor can be created from\
{submodule_name}.{class_name} (got {self_class_name})")
raise TypeError(
f"Only GBTDAALRegressor can be created from\
{submodule_name}.{class_name} (got {self_class_name})"
)
# Build GBTDAALRegressor from CatBoost
elif (submodule_name, class_name) == ("catboost.core", "CatBoostRegressor"):
if self_class_name == "GBTDAALRegressor":
self._convert_model_from_catboost(model)
else:
raise TypeError(f"Only GBTDAALRegressor can be created from\
{submodule_name}.{class_name} (got {self_class_name})")
raise TypeError(
f"Only GBTDAALRegressor can be created from\
{submodule_name}.{class_name} (got {self_class_name})"
)
# Build GBTDAALModel from LightGBM
elif (submodule_name, class_name) == ("lightgbm.basic", "Booster"):
if self_class_name == "GBTDAALModel":
self._convert_model_from_lightgbm(model)
else:
raise TypeError(f"Only GBTDAALModel can be created from\
{submodule_name}.{class_name} (got {self_class_name})")
raise TypeError(
f"Only GBTDAALModel can be created from\
{submodule_name}.{class_name} (got {self_class_name})"
)
# Build GBTDAALModel from XGBoost
elif (submodule_name, class_name) == ("xgboost.core", "Booster"):
if self_class_name == "GBTDAALModel":
self._convert_model_from_xgboost(model)
else:
raise TypeError(f"Only GBTDAALModel can be created from\
{submodule_name}.{class_name} (got {self_class_name})")
raise TypeError(
f"Only GBTDAALModel can be created from\
{submodule_name}.{class_name} (got {self_class_name})"
)
# Build GBTDAALModel from CatBoost
elif (submodule_name, class_name) == ("catboost.core", "CatBoost"):
if self_class_name == "GBTDAALModel":
self._convert_model_from_catboost(model)
else:
raise TypeError(f"Only GBTDAALModel can be created from\
{submodule_name}.{class_name} (got {self_class_name})")
raise TypeError(
f"Only GBTDAALModel can be created from\
{submodule_name}.{class_name} (got {self_class_name})"
)
else:
raise TypeError(f"Unknown model format {submodule_name}.{class_name}")

def _predict_classification(self, X, fptype, resultsToEvaluate):
if X.shape[1] != self.n_features_in_:
raise ValueError('Shape of input is different from what was seen in `fit`')
raise ValueError("Shape of input is different from what was seen in `fit`")

if not hasattr(self, 'daal_model_'):
raise ValueError((
"The class {} instance does not have 'daal_model_' attribute set. "
"Call 'fit' with appropriate arguments before using this method.")
.format(type(self).__name__))
if not hasattr(self, "daal_model_"):
raise ValueError(
(
"The class {} instance does not have 'daal_model_' attribute set. "
"Call 'fit' with appropriate arguments before using this method."
).format(type(self).__name__)
)

# Prediction
predict_algo = d4p.gbt_classification_prediction(
fptype=fptype,
nClasses=self.n_classes_,
resultsToEvaluate=resultsToEvaluate)
fptype=fptype, nClasses=self.n_classes_, resultsToEvaluate=resultsToEvaluate
)
predict_result = predict_algo.compute(X, self.daal_model_)

if resultsToEvaluate == "computeClassLabels":
Expand All @@ -179,13 +202,15 @@ def _predict_classification(self, X, fptype, resultsToEvaluate):

def _predict_regression(self, X, fptype):
if X.shape[1] != self.n_features_in_:
raise ValueError('Shape of input is different from what was seen in `fit`')

if not hasattr(self, 'daal_model_'):
raise ValueError((
"The class {} instance does not have 'daal_model_' attribute set. "
"Call 'fit' with appropriate arguments before using this method.").format(
type(self).__name__))
raise ValueError("Shape of input is different from what was seen in `fit`")

if not hasattr(self, "daal_model_"):
raise ValueError(
(
"The class {} instance does not have 'daal_model_' attribute set. "
"Call 'fit' with appropriate arguments before using this method."
).format(type(self).__name__)
)

# Prediction
predict_algo = d4p.gbt_regression_prediction(fptype=fptype)
Expand Down
Loading

0 comments on commit 14d3132

Please sign in to comment.