I have the following code, which is called test_build, and it has a test case to save a scikit-learn model along with x_train, y_train and score data, in a tuple object to a ".pkl" file.
from build import *
import os
import pandas as pd
import sklearn
from sklearn import *
import unittest
from sklearn.model_selection import train_test_split
import numpy as np
import tempfile
class TestMachineLearningUtils(unittest.TestCase):
def test_save_model(self):
X, y = np.arange(10).reshape((5,2)), range(5)
model = RandomForestClassifier(n_estimators = 300,
oob_score = True,
n_jobs = -1,
random_state = 123)
X_train, X_test, y_train, y_test = train_test_split(\
X, y, test_size=0.33, random_state=42)
clf = model.fit(X_train, y_train)
score = model.score(X_test, y_test)
dir_path = os.path.dirname(os.path.realpath(__file__))
f = tempfile.TemporaryDirectory(dir = dir_path)
pkl_file_name = f.name + "/" + "pickle_model.pkl"
tuple_objects = (clf, X_train, y_train, score)
path_model = save_model(tuple_objects, pkl_file_name)
exists_model = os.path.exists(path_model)
self.assertExists(exists_model, True)
if __name__ == "__main__":
unittest.main()
This is the content of the save_model function found in the build module I imported in my test file.
def save_model(tuple_objects, model_path):
pickle.dump(tuple_objects, open(model_path), 'wb')
return model_path
The problem I am running to, is that I cannot test if the file is created within a temporal directory. It is apparently created, but it is cleaned after it has been created, from the error message I receive.
C:\Users\User\AppData\Local\Continuum\miniconda3\envs\geoenv\lib\tempfile.py:798: ResourceWarning: Implicitly cleaning up <TemporaryDirectory>
Does anyone knows a solution to this problem? How could one supress the cleaning up of a temporary directory created using the tempfile module in python?