Skip to content

Commit

Permalink
Merge pull request #4677 from yut23/restore_global_state_in_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros authored Sep 28, 2023
2 parents 96f8594 + ed3bdac commit 0f84cc2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
8 changes: 8 additions & 0 deletions yt/data_objects/tests/test_image_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@
from yt.data_objects.image_array import ImageArray
from yt.testing import requires_module

old_settings = None


def setup():
global old_settings
from yt.config import ytcfg

ytcfg["yt", "internals", "within_testing"] = True
old_settings = np.geterr()
np.seterr(all="ignore")


def teardown():
np.seterr(**old_settings)


def dummy_image(kstep, nlayers):
im = np.zeros([64, 128, nlayers])
for i in range(im.shape[0]):
Expand Down
8 changes: 7 additions & 1 deletion yt/utilities/lib/tests/test_alt_ray_tracers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
from yt.utilities.lib.alt_ray_tracers import _cyl2cart, cylindrical_ray_trace

left_grid = right_grid = amr_levels = center_grid = data = None
old_settings = None


def setup():
# set up some sample cylindrical grid data, radiating out from center
global left_grid, right_grid, amr_levels, center_grid, data
global left_grid, right_grid, amr_levels, center_grid, data, old_settings
old_settings = np.geterr()
np.seterr(all="ignore")
l1, r1, lvl1 = amrspace([0.0, 1.0, 0.0, -1.0, 0.0, 2 * np.pi], levels=(7, 7, 0))
l2, r2, lvl2 = amrspace([0.0, 1.0, 0.0, 1.0, 0.0, 2 * np.pi], levels=(7, 7, 0))
Expand All @@ -21,6 +23,10 @@ def setup():
data = np.cos(np.sqrt(np.sum(center_grid[:, :2] ** 2, axis=1))) ** 2 # cos^2


def teardown():
np.seterr(**old_settings)


point_pairs = np.array(
[
# p1 p2
Expand Down
13 changes: 13 additions & 0 deletions yt/utilities/tests/test_set_log_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

from yt.utilities.logger import set_log_level

old_level = None


def setup():
global old_level
from yt.utilities.logger import ytLogger

old_level = ytLogger.level


def teardown():
set_log_level(old_level)


def test_valid_level():
# test a subset of valid entries to cover
Expand Down
3 changes: 3 additions & 0 deletions yt/visualization/image_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ def write_bitmap(bitmap_array, filename, max_val=None, transpose=False):
alpha_channel.shape = s1, s2, 1
if max_val is None:
max_val = bitmap_array[:, :, :3].max()
if max_val == 0.0:
# avoid dividing by zero for blank images
max_val = 1.0
bitmap_array = np.clip(bitmap_array[:, :, :3] / max_val, 0.0, 1.0) * 255
bitmap_array = np.concatenate(
[bitmap_array.astype("uint8"), alpha_channel], axis=-1
Expand Down

0 comments on commit 0f84cc2

Please sign in to comment.