Skip to content

Commit febf394

Browse files
authored
Copy over code from Ramon's PR(5938) and write a readme on how to run the notebook (#7030)
1 parent 3453b51 commit febf394

File tree

11 files changed

+372
-132
lines changed

11 files changed

+372
-132
lines changed

servers/xgboostserver/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test/model.bst
2+
test/model.json

servers/xgboostserver/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# How to run the Jupyter notebook
2+
3+
## Create a virtual environment and use it in Jupyter Lab
4+
5+
1. Create a virtual environment
6+
```bash
7+
python -m venv xgboost-env
8+
```
9+
10+
2. Activate it
11+
```bash
12+
source xgboost-env/bin/activate
13+
```
14+
15+
3. Install the Jupyter kernel using the Python interpreter in this environment
16+
```bash
17+
python -m ipykernel install --user \
18+
--name xgboost-env \
19+
--display-name "Python (xgboost-env)"
20+
```
21+
22+
4. Install development dependencies
23+
```bash
24+
pip install -r requirements-dev.txt
25+
```
26+
27+
5. Additionally, install the `seldon_core` package
28+
* You can either install it with pip from pypi with `pip install seldon-core`
29+
* Or you can install it from source by:
30+
* Navigating to the <root>/python folder and `make install`
31+
32+
6. Install the package dependencies
33+
```bash
34+
pip install -r ./xgboostserver/requirements.txt
35+
```
36+
37+
## Open Jupyter Lab and run the cells
38+
39+
1. Open Jupyter Lab
40+
```bash
41+
jupyter lab
42+
```
43+
44+
2. Navigate to the browser, then to the `test` folder and select the notebook
45+
46+
3. From the top right, choose the Kernel called `Python (xgboost-env)`
47+
48+
4. Run the cells one after the other
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pytest >= 6.2.4
2+
jupyterlab == 4.3.4
3+
ipython_genutils == 0.2.0
4+
ipykernel == 6.29.5

servers/xgboostserver/test/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

servers/xgboostserver/test/xgboost_irisv0uczf2p_nbqa_ipynb.py renamed to servers/xgboostserver/test/__init__.py

File renamed without changes.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
import tempfile
3+
import numpy as np
4+
import pytest
5+
import yaml
6+
import xgboost as xgb
7+
from xgboostserver.XGBoostServer import XGBoostServer
8+
9+
10+
@pytest.fixture
11+
def model_uri():
12+
with tempfile.TemporaryDirectory() as temp_dir:
13+
X = np.random.rand(100, 5)
14+
y = np.random.randint(2, size=100)
15+
dtrain = xgb.DMatrix(X, label=y)
16+
params = {'objective': 'binary:logistic', 'eval_metric': 'error'}
17+
booster = xgb.train(params, dtrain, num_boost_round=10)
18+
model_path = os.path.join(temp_dir, "model.json")
19+
booster.save_model(model_path)
20+
yield temp_dir
21+
22+
23+
def test_init_metadata(model_uri):
24+
metadata = {"key": "value"}
25+
metadata_path = os.path.join(model_uri, "metadata.yaml")
26+
with open(metadata_path, "w") as f:
27+
yaml.dump(metadata, f)
28+
29+
server = XGBoostServer(model_uri)
30+
31+
loaded_metadata = server.init_metadata()
32+
33+
# Assert that the loaded metadata matches the original metadata
34+
assert loaded_metadata == metadata
35+
36+
37+
def test_predict_invalid_input(model_uri):
38+
# Create an instance of XGBoostServer with the model URI
39+
server = XGBoostServer(model_uri)
40+
server.load()
41+
42+
X_test = np.random.rand(10, 3) # Incorrect number of features
43+
with pytest.raises(ValueError):
44+
server.predict(X_test, names=[])
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import os
2+
import tempfile
3+
import numpy as np
4+
import pytest
5+
import yaml
6+
import xgboost as xgb
7+
from xgboostserver.XGBoostServer import XGBoostServer
8+
9+
10+
@pytest.fixture
11+
def model_uri():
12+
with tempfile.TemporaryDirectory() as temp_dir:
13+
X = np.random.rand(100, 5)
14+
y = np.random.randint(2, size=100)
15+
dtrain = xgb.DMatrix(X, label=y)
16+
params = {'objective': 'binary:logistic', 'eval_metric': 'error'}
17+
booster = xgb.train(params, dtrain, num_boost_round=10)
18+
model_path = os.path.join(temp_dir, "model.json")
19+
booster.save_model(model_path)
20+
yield temp_dir
21+
22+
23+
def test_init_metadata(model_uri):
24+
metadata = {"key": "value"}
25+
metadata_path = os.path.join(model_uri, "metadata.yaml")
26+
with open(metadata_path, "w") as f:
27+
yaml.dump(metadata, f)
28+
29+
server = XGBoostServer(model_uri)
30+
31+
loaded_metadata = server.init_metadata()
32+
33+
# Assert that the loaded metadata matches the original metadata
34+
assert loaded_metadata == metadata
35+
36+
37+
def test_predict_invalid_input(model_uri):
38+
# Create an instance of XGBoostServer with the model URI
39+
server = XGBoostServer(model_uri)
40+
server.load()
41+
X_test = np.random.rand(10, 3) # Incorrect number of features
42+
with pytest.raises(ValueError):
43+
server.predict(X_test, names=[])

0 commit comments

Comments
 (0)