Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] Replace make_temp_file with tempfile #4111

Merged
merged 3 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 0 additions & 10 deletions python/taichi/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import functools
import itertools
import os
from tempfile import mkstemp

from taichi._lib import core as _ti_core
from taichi.lang import (cc, cpu, cuda, gpu, is_arch_supported, metal, opengl,
Expand Down Expand Up @@ -50,14 +49,6 @@ def allclose(x, y, **kwargs):
return x == approx(y, **kwargs)


def make_temp_file(*args, **kwargs):
'''Create a temporary file'''

fd, name = mkstemp(*args, **kwargs)
os.close(fd)
return name


class TestParam:
def __init__(self, value, required_extensions):
self._value = value
Expand Down Expand Up @@ -205,6 +196,5 @@ def wrapped(*args, **kwargs):
'get_rel_eps',
'approx',
'allclose',
'make_temp_file',
'test',
]
15 changes: 8 additions & 7 deletions tests/python/test_gui.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import tempfile

import numpy as np
import pytest

import taichi as ti
from taichi import make_temp_file


@pytest.mark.parametrize('dtype', [ti.u8, ti.f32])
Expand All @@ -23,9 +24,9 @@ def paint(c: dtype):
else:
paint(i * 1.0 / n)
gui.set_image(pixels)
image_path = make_temp_file(suffix='.png')
gui.show(image_path)
image = ti.imread(image_path)
delta = (image - i).sum()
assert delta == 0, "Expected image difference to be 0 but got {} instead.".format(
delta)
with tempfile.NamedTemporaryFile(suffix='.png') as image_path:
gui.show(image_path.name)
image = ti.imread(image_path.name)
delta = (image - i).sum()
assert delta == 0, "Expected image difference to be 0 but got {} instead.".format(
delta)
42 changes: 20 additions & 22 deletions tests/python/test_image_io.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import os
import tempfile

import numpy as np
import pytest

import taichi as ti
from taichi import make_temp_file


# jpg is also supported but hard to test here since it's lossy:
Expand All @@ -24,17 +24,16 @@ def test_image_io(resx, resy, comp, ext, is_field, dt):
pixel = np.random.randint(256, size=shape, dtype=ti.to_numpy_type(dt))
if is_field:
pixel_t.from_numpy(pixel)
fn = make_temp_file(suffix='.' + ext)
if is_field:
ti.imwrite(pixel_t, fn)
else:
ti.imwrite(pixel, fn)
pixel_r = ti.imread(fn)
if comp == 1:
# from (resx, resy, 1) to (resx, resy)
pixel_r = pixel_r.reshape((resx, resy))
assert (pixel_r == pixel).all()
os.remove(fn)
with tempfile.NamedTemporaryFile(suffix='.' + ext) as fn:
if is_field:
ti.imwrite(pixel_t, fn.name)
else:
ti.imwrite(pixel, fn.name)
pixel_r = ti.imread(fn.name)
if comp == 1:
# from (resx, resy, 1) to (resx, resy)
pixel_r = pixel_r.reshape((resx, resy))
assert (pixel_r == pixel).all()


@pytest.mark.parametrize('comp,ext', [(3, 'png'), (4, 'png')])
Expand All @@ -46,11 +45,11 @@ def test_image_io_vector(resx, resy, comp, ext, dt):
pixel = np.random.rand(*shape, comp).astype(ti.to_numpy_type(dt))
pixel_t = ti.Vector.field(comp, dt, shape)
pixel_t.from_numpy(pixel)
fn = make_temp_file(suffix='.' + ext)
ti.imwrite(pixel_t, fn)
pixel_r = (ti.imread(fn).astype(ti.to_numpy_type(dt)) + 0.5) / 256.0
assert np.allclose(pixel_r, pixel, atol=2e-2)
os.remove(fn)
with tempfile.NamedTemporaryFile(suffix='.' + ext) as fn:
ti.imwrite(pixel_t, fn.name)
pixel_r = (ti.imread(fn.name).astype(ti.to_numpy_type(dt)) +
0.5) / 256.0
assert np.allclose(pixel_r, pixel, atol=2e-2)


@pytest.mark.parametrize('comp,ext', [(3, 'png')])
Expand All @@ -66,11 +65,10 @@ def test_image_io_uint(resx, resy, comp, ext, dt):
pixel = np.random.randint(256, size=(*shape, comp), dtype=np_type) * np_max
pixel_t = ti.Vector.field(comp, dt, shape)
pixel_t.from_numpy(pixel)
fn = make_temp_file(suffix='.' + ext)
ti.imwrite(pixel_t, fn)
pixel_r = ti.imread(fn).astype(np_type) * np_max
assert (pixel_r == pixel).all()
os.remove(fn)
with tempfile.NamedTemporaryFile(suffix='.' + ext) as fn:
ti.imwrite(pixel_t, fn.name)
pixel_r = ti.imread(fn.name).astype(np_type) * np_max
assert (pixel_r == pixel).all()


@pytest.mark.parametrize('comp', [1, 3])
Expand Down