Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions servers/xgboostserver/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test/model.bst
test/model.json
48 changes: 48 additions & 0 deletions servers/xgboostserver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# How to run the Jupyter notebook

## Create a virtual environment and use it in Jupyter Lab

1. Create a virtual environment
```bash
python -m venv xgboost-env
```

2. Activate it
```bash
source xgboost-env/bin/activate
```

3. Install the Jupyter kernel using the Python interpreter in this environment
```bash
python -m ipykernel install --user \
--name xgboost-env \
--display-name "Python (xgboost-env)"
```

4. Install development dependencies
```bash
pip install -r requirements-dev.txt
```

5. Additionally, install the `seldon_core` package
* You can either install it with pip from pypi with `pip install seldon-core`
* Or you can install it from source by:
* Navigating to the <root>/python folder and `make install`

6. Install the package dependencies
```bash
pip install -r ./xgboostserver/requirements.txt
```

## Open Jupyter Lab and run the cells

1. Open Jupyter Lab
```bash
jupyter lab
```

2. Navigate to the browser, then to the `test` folder and select the notebook

3. From the top right, choose the Kernel called `Python (xgboost-env)`

4. Run the cells one after the other
4 changes: 4 additions & 0 deletions servers/xgboostserver/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pytest >= 6.2.4
jupyterlab == 4.3.4
ipython_genutils == 0.2.0
ipykernel == 6.29.5
1 change: 0 additions & 1 deletion servers/xgboostserver/test/.gitignore

This file was deleted.

44 changes: 44 additions & 0 deletions servers/xgboostserver/test/test_metadata_and_input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os
import tempfile
import numpy as np
import pytest
import yaml
import xgboost as xgb
from xgboostserver.XGBoostServer import XGBoostServer


@pytest.fixture
def model_uri():
with tempfile.TemporaryDirectory() as temp_dir:
X = np.random.rand(100, 5)
y = np.random.randint(2, size=100)
dtrain = xgb.DMatrix(X, label=y)
params = {'objective': 'binary:logistic', 'eval_metric': 'error'}
booster = xgb.train(params, dtrain, num_boost_round=10)
model_path = os.path.join(temp_dir, "model.json")
booster.save_model(model_path)
yield temp_dir


def test_init_metadata(model_uri):
metadata = {"key": "value"}
metadata_path = os.path.join(model_uri, "metadata.yaml")
with open(metadata_path, "w") as f:
yaml.dump(metadata, f)

server = XGBoostServer(model_uri)

loaded_metadata = server.init_metadata()

# Assert that the loaded metadata matches the original metadata
assert loaded_metadata == metadata


def test_predict_invalid_input(model_uri):
# Create an instance of XGBoostServer with the model URI
server = XGBoostServer(model_uri)
server.load()

X_test = np.random.rand(10, 3) # Incorrect number of features
with pytest.raises(ValueError):
server.predict(X_test, names=[])
43 changes: 43 additions & 0 deletions servers/xgboostserver/test/test_metadata_and_predict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os
import tempfile
import numpy as np
import pytest
import yaml
import xgboost as xgb
from xgboostserver.XGBoostServer import XGBoostServer


@pytest.fixture
def model_uri():
with tempfile.TemporaryDirectory() as temp_dir:
X = np.random.rand(100, 5)
y = np.random.randint(2, size=100)
dtrain = xgb.DMatrix(X, label=y)
params = {'objective': 'binary:logistic', 'eval_metric': 'error'}
booster = xgb.train(params, dtrain, num_boost_round=10)
model_path = os.path.join(temp_dir, "model.json")
booster.save_model(model_path)
yield temp_dir


def test_init_metadata(model_uri):
metadata = {"key": "value"}
metadata_path = os.path.join(model_uri, "metadata.yaml")
with open(metadata_path, "w") as f:
yaml.dump(metadata, f)

server = XGBoostServer(model_uri)

loaded_metadata = server.init_metadata()

# Assert that the loaded metadata matches the original metadata
assert loaded_metadata == metadata


def test_predict_invalid_input(model_uri):
# Create an instance of XGBoostServer with the model URI
server = XGBoostServer(model_uri)
server.load()
X_test = np.random.rand(10, 3) # Incorrect number of features
with pytest.raises(ValueError):
server.predict(X_test, names=[])
Loading
Loading