Skip to content
This repository was archived by the owner on Dec 21, 2023. It is now read-only.

Commit 43fbf0d

Browse files
authored
Unit tests clean up temp files (#3010)
1 parent 7f17481 commit 43fbf0d

11 files changed

+32
-28
lines changed

src/python/turicreate/test/test_activity_classifier.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ def test_export_coreml(self):
476476
import coremltools
477477

478478
# Save the model as a CoreML model file
479-
filename = tempfile.mkstemp("ActivityClassifier.mlmodel")[1]
479+
filename = tempfile.NamedTemporaryFile(suffix=".mlmodel").name
480480
self.model.export_coreml(filename)
481481

482482
# Load the model back from the CoreML model file
@@ -521,7 +521,7 @@ def test_create_single_input_column(self):
521521
session_id=self.session_id,
522522
prediction_window=self.prediction_window,
523523
)
524-
filename = tempfile.mkstemp("ActivityClassifier.mlmodel")[1]
524+
filename = tempfile.NamedTemporaryFile(suffix=".mlmodel").name
525525
model.export_coreml(filename)
526526

527527
# Load the model back from the CoreML model file
@@ -748,7 +748,7 @@ def test_gpu_save_load_export(self):
748748
model.save(filename)
749749
model = tc.load_model(filename)
750750

751-
filename = tempfile.mkstemp("ActivityClassifier.mlmodel")[1]
751+
filename = tempfile.NamedTemporaryFile(suffix=".mlmodel").name
752752
model.export_coreml(filename)
753753

754754
tc.config.set_num_gpus(old_num_gpus)

src/python/turicreate/test/test_drawing_classifier.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import turicreate.toolkits.libtctensorflow
1313
from turicreate.toolkits._main import ToolkitError as _ToolkitError
1414
import numpy as _np
15-
from tempfile import mkstemp as _mkstemp
15+
import tempfile
1616
import coremltools as _coremltools
1717
from copy import copy as _copy
1818
from array import array as _array
@@ -350,7 +350,7 @@ def test_export_coreml(self):
350350
max_iters_ans = [str(self.max_iterations), "1"]
351351
warm_start_ans = "" if self.warm_start is None else self.warm_start
352352
for i, model in enumerate(self.models):
353-
filename = _mkstemp("bingo.mlmodel")[1]
353+
filename = tempfile.NamedTemporaryFile(suffix=".mlmodel").name
354354
model.export_coreml(filename)
355355

356356
# Load the model back from the CoreML model file
@@ -384,7 +384,7 @@ def test_export_coreml_with_predict(self):
384384
prefix = "pretrained" + str(test_number)
385385
else:
386386
prefix = "scratch" + str(test_number)
387-
filename = _mkstemp(prefix + ".mlmodel")[1]
387+
filename = tempfile.NamedTemporaryFile(prefix=prefix, suffix=".mlmodel").name
388388
model.export_coreml(filename)
389389
mlmodel = _coremltools.models.MLModel(filename)
390390
tc_preds = model.predict(sf)

src/python/turicreate/test/test_image_classifier.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def test_export_coreml(self):
212212
"Expected failure until "
213213
+ "https://github.com/apple/turicreate/issues/2744 is fixed"
214214
)
215-
filename = tempfile.mkstemp("bingo.mlmodel")[1]
215+
filename = tempfile.NamedTemporaryFile(suffix=".mlmodel").name
216216
self.model.export_coreml(filename)
217217

218218
coreml_model = coremltools.models.MLModel(filename)
@@ -234,7 +234,7 @@ def test_export_coreml(self):
234234

235235
@unittest.skipIf(sys.platform != "darwin", "Core ML only supported on Mac")
236236
def test_export_coreml_predict(self):
237-
filename = tempfile.mkstemp("bingo.mlmodel")[1]
237+
filename = tempfile.NamedTemporaryFile(suffix=".mlmodel").name
238238
self.model.export_coreml(filename)
239239

240240
coreml_model = coremltools.models.MLModel(filename)

src/python/turicreate/test/test_image_similarity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def get_psnr(x, y):
227227
)
228228

229229
# Save the model as a CoreML model file
230-
filename = tempfile.mkstemp("ImageSimilarity.mlmodel")[1]
230+
filename = tempfile.NamedTemporaryFile(suffix=".mlmodel").name
231231
self.model.export_coreml(filename)
232232

233233
# Load the model back from the CoreML model file

src/python/turicreate/test/test_object_detector.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ def test_export_coreml(self):
449449
import coremltools
450450
import platform
451451

452-
filename = tempfile.mkstemp("bingo.mlmodel")[1]
452+
filename = tempfile.NamedTemporaryFile(suffix=".mlmodel").name
453453
self.model.export_coreml(filename, include_non_maximum_suppression=False)
454454

455455
coreml_model = coremltools.models.MLModel(filename)
@@ -486,7 +486,7 @@ def test_export_coreml(self):
486486
# not that meaningful unless the model has converged
487487

488488
# Also check if we can train a second model and export it.
489-
filename2 = tempfile.mkstemp("bingo2.mlmodel")[1]
489+
filename2 = tempfile.NamedTemporaryFile(suffix=".mlmodel").name
490490
# We also test at the same time if we can export a model with a single
491491
# class
492492
sf = tc.SFrame(
@@ -505,7 +505,7 @@ def test_export_coreml(self):
505505
def test_export_coreml_with_non_maximum_suppression(self):
506506
from PIL import Image
507507

508-
filename = tempfile.mkstemp("bingo.mlmodel")[1]
508+
filename = tempfile.NamedTemporaryFile(suffix=".mlmodel").name
509509
self.model.export_coreml(filename, include_non_maximum_suppression=True)
510510

511511
coreml_model = coremltools.models.MLModel(filename)
@@ -521,7 +521,7 @@ def test_export_coreml_with_non_maximum_suppression(self):
521521
# not that meaningful unless the model has converged
522522

523523
# Also check if we can train a second model and export it.
524-
filename2 = tempfile.mkstemp("bingo2.mlmodel")[1]
524+
filename2 = tempfile.NamedTemporaryFile(suffix=".mlmodel").name
525525
# We also test at the same time if we can export a model with a single
526526
# class
527527
sf = tc.SFrame(

src/python/turicreate/test/test_one_shot_object_detector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def test_export_coreml(self):
215215
import coremltools
216216
import platform
217217

218-
filename = tempfile.mkstemp("bingo.mlmodel")[1]
218+
filename = tempfile.NamedTemporaryFile(suffix=".mlmodel").name
219219
self.model.export_coreml(filename, include_non_maximum_suppression=False)
220220

221221
## Test metadata
@@ -254,7 +254,7 @@ def test_export_coreml(self):
254254
self.assertEqual(ret["coordinates"].shape[0], ret["confidence"].shape[0])
255255

256256
# Test export without non max supression
257-
filename2 = tempfile.mkstemp("bingo2.mlmodel")[1]
257+
filename2 = tempfile.NamedTemporaryFile(suffix=".mlmodel").name
258258
self.model.export_coreml(filename2, include_non_maximum_suppression=True)
259259
coreml_model = coremltools.models.MLModel(filename)
260260
self.assertTrue(

src/python/turicreate/test/test_recommender.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import array
3737
from turicreate.util import _assert_sframe_equal as assert_sframe_equal
3838
from turicreate.toolkits._internal_utils import _mac_ver
39-
from tempfile import mkstemp as _mkstemp
39+
import tempfile
4040
from copy import copy
4141
from subprocess import Popen as _Popen
4242
from subprocess import PIPE as _PIPE
@@ -75,7 +75,7 @@ def _coreml_to_tc(preds):
7575

7676
class RecommenderTestBase(unittest.TestCase):
7777
def _test_coreml_export(self, m, item_ids, ratings=None):
78-
temp_file_path = _mkstemp()[1]
78+
temp_file_path = tempfile.NamedTemporaryFile().name
7979
if m.target and ratings:
8080
obs_data_sf = tc.SFrame(
8181
{m.item_id: tc.SArray(item_ids), m.target: tc.SArray(ratings)}
@@ -3053,8 +3053,8 @@ def test_export_model_size(self):
30533053
self.assertEqual(m1.num_users, 10)
30543054
self.assertEqual(m2.num_users, 20 * 10)
30553055

3056-
temp_file_path_1 = _mkstemp()[1]
3057-
temp_file_path_2 = _mkstemp()[1]
3056+
temp_file_path_1 = tempfile.NamedTemporaryFile(suffix=".mlmodel").name
3057+
temp_file_path_2 = tempfile.NamedTemporaryFile(suffix=".mlmodel").name
30583058

30593059
m1.export_coreml(temp_file_path_1)
30603060
m2.export_coreml(temp_file_path_2)

src/python/turicreate/test/test_sarray.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,9 +2306,11 @@ def test_save_sarray(self):
23062306
data = SArray(range(1000))
23072307
data = data[data > 50]
23082308
# lazy and good
2309-
tmp_dir = tempfile.mkdtemp()
2310-
data.save(tmp_dir)
2311-
shutil.rmtree(tmp_dir)
2309+
try:
2310+
tmp_dir = tempfile.mkdtemp()
2311+
data.save(tmp_dir)
2312+
finally:
2313+
shutil.rmtree(tmp_dir)
23122314
print(data)
23132315

23142316
def test_to_numpy(self):

src/python/turicreate/test/test_sframe.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4128,9 +4128,11 @@ def test_save_sframe(self):
41284128
data["x"] = range(100)
41294129
data["x"] = data["x"] > 50
41304130
# lazy and good
4131-
tmp_dir = tempfile.mkdtemp()
4132-
data.save(tmp_dir)
4133-
shutil.rmtree(tmp_dir)
4131+
try:
4132+
tmp_dir = tempfile.mkdtemp()
4133+
data.save(tmp_dir)
4134+
finally:
4135+
shutil.rmtree(tmp_dir)
41344136
print(data)
41354137

41364138
def test_empty_argmax_does_not_fail(self):

src/python/turicreate/test/test_style_transfer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def test_export_coreml(self):
308308

309309
model = self.model
310310
for flexible_shape_on in [True, False]:
311-
filename = tempfile.mkstemp("my_style_transfer.mlmodel")[1]
311+
filename = tempfile.NamedTemporaryFile(suffix=".mlmodel").name
312312
model.export_coreml(filename, include_flexible_shape=flexible_shape_on)
313313

314314
## Metadata test

0 commit comments

Comments
 (0)