-
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SapientML to automl benchmark (#630)
* Add SapientML to automl benchmark Signed-off-by: Kosaku Kimura <kimura.kosaku@fujitsu.com> * fix setup.sh (#2) * WIP Signed-off-by: Kosaku Kimura <kimura.kosaku@fujitsu.com> * Issue fix in setup.sh for SapientML * Updation of config file as Sapientml supports Python version 3.9 Signed-off-by: HimanshuRRai <himanshu.rai@fujitsu.com> --------- Signed-off-by: Kosaku Kimura <kimura.kosaku@fujitsu.com> Signed-off-by: HimanshuRRai <himanshu.rai@fujitsu.com> Co-authored-by: muhammed-nafi-k-a <muhammednafi.a@fujitsu.com> Co-authored-by: HimanshuRRai <himanshu.rai@fujitsu.com> * Remove requirement.txt file from master branch Signed-off-by: HimanshuRRai <himanshu.rai@fujitsu.com> --------- Signed-off-by: Kosaku Kimura <kimura.kosaku@fujitsu.com> Signed-off-by: HimanshuRRai <himanshu.rai@fujitsu.com> Co-authored-by: muhammed-nafi-k-a <muhammednafi.a@fujitsu.com> Co-authored-by: HimanshuRRai <himanshu.rai@fujitsu.com>
- Loading branch information
1 parent
7e6b665
commit ac09497
Showing
5 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from amlb.benchmark import TaskConfig | ||
from amlb.data import Dataset | ||
from amlb.utils import call_script_in_same_dir | ||
|
||
|
||
def setup(*args, **kwargs): | ||
call_script_in_same_dir(__file__, "setup.sh", *args, **kwargs) | ||
|
||
|
||
def run(dataset: Dataset, config: TaskConfig): | ||
from frameworks.shared.caller import run_in_venv | ||
|
||
data = dict( | ||
train=dict(path=dataset.train.data_path("csv")), | ||
test=dict(path=dataset.test.data_path("csv")), | ||
target=dict(name=dataset.target.name, classes=dataset.target.values), | ||
problem_type=dataset.type.name, | ||
) | ||
return run_in_venv( | ||
__file__, | ||
"exec.py", | ||
input_data=data, | ||
dataset=dataset, | ||
config=config, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import logging | ||
import os | ||
import tempfile as tmp | ||
|
||
from frameworks.shared.callee import call_run, result | ||
from frameworks.shared.utils import Timer | ||
from sapientml import SapientML | ||
from sapientml.util.logging import setup_logger | ||
from sklearn.preprocessing import OneHotEncoder | ||
|
||
os.environ["JOBLIB_TEMP_FOLDER"] = tmp.gettempdir() | ||
os.environ["OMP_NUM_THREADS"] = "1" | ||
os.environ["OPENBLAS_NUM_THREADS"] = "1" | ||
os.environ["MKL_NUM_THREADS"] = "1" | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def run(dataset, config): | ||
import re | ||
|
||
import pandas as pd | ||
|
||
log.info(f"\n**** Sapientml ****\n") | ||
|
||
is_classification = config.type == "classification" | ||
is_multiclass = dataset.problem_type = "multiclass" | ||
training_params = {k: v for k, v in config.framework_params.items() if not k.startswith("_")} | ||
|
||
train_path, test_path = dataset.train.path, dataset.test.path | ||
target_col = dataset.target.name | ||
|
||
# Read parquet using pandas | ||
X_train = pd.read_csv(train_path) | ||
X_test = pd.read_csv(test_path) | ||
|
||
# Removing unwanted sybols from column names (exception case) | ||
X_train.columns = [re.sub("[^A-Za-z0-9_.]+", "", col) for col in X_train.columns] | ||
X_test.columns = [re.sub("[^A-Za-z0-9_.]+", "", col) for col in X_test.columns] | ||
target_col = re.sub("[^A-Za-z0-9_.]+", "", target_col) | ||
|
||
# y_train and y_test | ||
y_train = X_train[target_col].reset_index(drop=True) | ||
y_test = X_test[target_col].reset_index(drop=True) | ||
|
||
# Drop target col from X_test | ||
X_test.drop([target_col], axis=1, inplace=True) | ||
|
||
# Sapientml | ||
output_dir = config.output_dir + "/" + "outputs" + "/" + config.name + "/" + str(config.fold) | ||
predictor = SapientML([target_col], task_type="classification" if is_classification else "regression") | ||
|
||
# Fit the model | ||
with Timer() as training: | ||
predictor.fit(X_train, output_dir=output_dir) | ||
log.info(f"Finished fit in {training.duration}s.") | ||
|
||
# predict | ||
with Timer() as predict: | ||
predictions = predictor.predict(X_test) | ||
log.info(f"Finished predict in {predict.duration}s.") | ||
|
||
if is_classification: | ||
|
||
predictions[target_col] = predictions[target_col].astype(str) | ||
predictions[target_col] = predictions[target_col].str.lower() | ||
predictions[target_col] = predictions[target_col].str.strip() | ||
y_test = y_test.to_frame() | ||
y_test[target_col] = y_test[target_col].astype(str) | ||
y_test[target_col] = y_test[target_col].str.lower() | ||
y_test[target_col] = y_test[target_col].str.strip() | ||
|
||
if is_classification: | ||
probabilities = OneHotEncoder(handle_unknown="ignore").fit_transform(predictions.to_numpy()) | ||
probabilities = pd.DataFrame(probabilities.toarray(), columns=dataset.target.classes) | ||
|
||
return result( | ||
output_file=config.output_predictions_file, | ||
predictions=predictions, | ||
truth=y_test, | ||
probabilities=probabilities, | ||
training_duration=training.duration, | ||
predict_duration=predict.duration, | ||
) | ||
else: | ||
return result( | ||
output_file=config.output_predictions_file, | ||
predictions=predictions, | ||
truth=y_test, | ||
training_duration=training.duration, | ||
predict_duration=predict.duration, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
call_run(run) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/env bash | ||
HERE=$(dirname "$0") | ||
VERSION=${1:-"stable"} | ||
REPO=${2:-"https://github.com/sapientml/sapientml"} | ||
PKG=${3:-"sapientml"} | ||
if [[ "$VERSION" == "latest" ]]; then | ||
VERSION="main" | ||
fi | ||
|
||
#create local venv | ||
. ${HERE}/../shared/setup.sh ${HERE} true | ||
|
||
# PIP install -r ${HERE}/requirements.txt | ||
if [[ "$VERSION" == "stable" ]]; then | ||
PIP install --no-cache-dir -U ${PKG} | ||
elif [[ "$VERSION" =~ ^[0-9] ]]; then | ||
PIP install --no-cache-dir -U ${PKG}==${VERSION} | ||
else | ||
# PIP install --no-cache-dir -e git+${REPO}@${VERSION}#egg=${PKG} | ||
TARGET_DIR="${HERE}/lib/${PKG}" | ||
rm -Rf ${TARGET_DIR} | ||
git clone --depth 1 --single-branch --branch ${VERSION} --recurse-submodules ${REPO} ${TARGET_DIR} | ||
PIP install -U -e ${TARGET_DIR} | ||
fi | ||
|
||
PY -c "import pkg_resources; print(pkg_resources.get_distribution('sapientml').version)" >> "${HERE}/.setup/installed" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters