forked from datarobot/datarobot-user-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
artifacts.py
49 lines (41 loc) · 1.72 KB
/
artifacts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Copyright 2021 DataRobot, Inc. and its affiliates.
All rights reserved.
This is proprietary source code of DataRobot, Inc. and its affiliates.
Released under the terms of DataRobot Tool and Utility Agreement.
"""
import os
import glob
import shutil
import subprocess
from tempfile import mkdtemp
# fixtures dir
TESTS_ROOT_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TESTS_FIXTURES_PATH = os.path.join(TESTS_ROOT_PATH, "fixtures")
TESTS_DATASET_DIR = os.path.join(TESTS_ROOT_PATH, "testdata")
SOURCE_ARTIFACTS_PATH = os.path.join(TESTS_FIXTURES_PATH, "drop_in_model_artifacts")
def generate_artifacts_dir():
"""
This method may be used in conftest.py in a pytest_sessionstart hook,
to generate a folder with model artifacts.
Generated folder will contain some copied artifacts, some re-trained with current dependencies.
Returns
-------
path to generated directory
"""
test_artifacts_dir = mkdtemp(prefix="custom_model_tests_artifacts", dir="/tmp")
# copy files required to generate model artifacts
for filename in glob.glob(r"{}/*.csv".format(TESTS_DATASET_DIR)):
shutil.copy2(filename, test_artifacts_dir)
for file_wildcard in ["*.ipynb", "PyTorch.*", "*.rds", "*.jar", "*.pmml"]:
for filename in glob.glob(r"{}/{}".format(SOURCE_ARTIFACTS_PATH, file_wildcard)):
shutil.copy2(filename, test_artifacts_dir)
cur_dir = os.getcwd()
try:
os.chdir(test_artifacts_dir)
for filename in glob.glob(r"{}/*ipynb".format(test_artifacts_dir)):
subprocess.call(["jupyter", "nbconvert", "--execute", filename])
subprocess.call(["python3", "PyTorch.py"])
finally:
os.chdir(cur_dir)
return test_artifacts_dir