Skip to content

Commit d792beb

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 917212f commit d792beb

3 files changed

Lines changed: 46 additions & 45 deletions

File tree

yt/geometry/coordinates/tests/test_sph_pixelization_pytestonly.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import numpy as np
22
import pytest
33
import unyt
4-
from numpy.typing import NDArray
54

65
import yt
76
from yt.data_objects.selection_objects.region import YTRegion
@@ -167,20 +166,19 @@ def makemasses(i, j, k):
167166
assert_rel_equal(expected_out, img.v, 5)
168167

169168

170-
171169
@pytest.mark.parametrize("axis", [0, 1, 2])
172170
@pytest.mark.parametrize("shiftcenter", [True, False])
173171
@pytest.mark.parametrize("periodic", [True, False])
174172
@pytest.mark.parametrize("depth", [None, (1.0, "cm"), (5.0, "cm")])
175-
@pytest.mark.parametrize("hsmlfac", [0.2, 0.5, 1.0])
173+
@pytest.mark.parametrize("hsmlfac", [0.2, 0.5, 1.0])
176174
def test_sph_proj_pixelave_alongaxes(
177175
axis: int,
178176
shiftcenter: bool,
179177
periodic: bool,
180178
depth: float,
181179
hsmlfac: float,
182180
) -> None:
183-
# weighted and unweighted tested in one round:
181+
# weighted and unweighted tested in one round:
184182
# need weight map to downsample the baseline weighted map
185183
if shiftcenter:
186184
center = unyt.unyt_array(np.array((0.6, 0.5, 0.4)), "cm")
@@ -279,9 +277,8 @@ def makemasses(i, j, k):
279277
baseimg = resreduce_image(_baseimg, buff_size)
280278
_baseimg_wtd = baseprj_wtd.frb.data[("gas", "density")]
281279
_divimg = np.copy(baseimg.v)
282-
_divimg[_divimg == 0.] = -1. # avoid div. by 0 test failures
283-
baseimg_wtd = (resreduce_image(_baseimg * _baseimg_wtd.v, buff_size)
284-
/ _divimg)
280+
_divimg[_divimg == 0.0] = -1.0 # avoid div. by 0 test failures
281+
baseimg_wtd = resreduce_image(_baseimg * _baseimg_wtd.v, buff_size) / _divimg
285282

286283
print(
287284
f"axis: {axis}, shiftcenter: {shiftcenter}, "
@@ -301,46 +298,48 @@ def makemasses(i, j, k):
301298
# subsampling with multiple particles is not quite at 4.
302299
# pixel values seem to converge more slowly though, so we test
303300
# mass conservation explicitly, and pixel agreement at low
304-
# precision
301+
# precision
305302
assert_rel_equal(baseimg.v, testimg.v, 1)
306303
assert_rel_equal(np.sum(baseimg.v), np.sum(testimg.v), 2)
307304
assert_rel_equal(baseimg_wtd.v, testimg_wtd.v, 1)
308305

309306

310307
def test_massconservation_pixave():
311-
bbox = np.array([[0., 3.], [0., 3.], [0., 3.]])
308+
bbox = np.array([[0.0, 3.0], [0.0, 3.0], [0.0, 3.0]])
312309
pz = 1.5
313310
periodic = True
314-
periods = 3.
311+
periods = 3.0
315312
nrandom = 50
316-
313+
317314
# random centers, three pixel size + hsml sets
318315
# test three cases in the backend routine
319316
rng = np.random.default_rng()
320-
pxs = rng.uniform(0., 3., nrandom)
321-
pys = rng.uniform(0., 3., nrandom)
317+
pxs = rng.uniform(0.0, 3.0, nrandom)
318+
pys = rng.uniform(0.0, 3.0, nrandom)
322319
# particles < pixels: overlap 1, 2x1, or 2x2 pixels
323320
hsmls1 = rng.uniform(0.01, 0.05, nrandom)
324321
outsize1 = (7, 7)
325322
# particles ~ pixels: typical 2x2 overlaps
326323
hsmls2 = rng.uniform(0.1, 0.25, nrandom)
327324
outsize2 = (7, 7)
328-
# particles > pixels:
325+
# particles > pixels:
329326
hsmls3 = rng.uniform(0.07, 0.8, nrandom)
330327
outsize3 = (50, 50)
331-
332-
for i, (hsmls, outsize) in enumerate([(hsmls1, outsize1),
333-
(hsmls2, outsize2),
334-
(hsmls3, outsize3),
335-
]):
336328

329+
for i, (hsmls, outsize) in enumerate(
330+
[
331+
(hsmls1, outsize1),
332+
(hsmls2, outsize2),
333+
(hsmls3, outsize3),
334+
]
335+
):
337336
masses_rel = []
338337
for i in range(nrandom):
339338
data = {
340339
"particle_position_x": (np.array([pxs[i]]), "cm"),
341340
"particle_position_y": (np.array([pys[i]]), "cm"),
342341
"particle_position_z": (np.array([pz]), "cm"),
343-
"particle_mass": (np.array([1.]), "g"),
342+
"particle_mass": (np.array([1.0]), "g"),
344343
"particle_velocity_x": (np.zeros(1), "cm/s"),
345344
"particle_velocity_y": (np.zeros(1), "cm/s"),
346345
"particle_velocity_z": (np.zeros(1), "cm/s"),
@@ -360,25 +359,28 @@ def test_massconservation_pixave():
360359

361360
proj = ds.proj(("gas", "density"), 2)
362361
frb = proj.to_frb(
363-
width=(3., "cm"),
362+
width=(3.0, "cm"),
364363
resolution=outsize,
365-
height=(3., "cm"),
364+
height=(3.0, "cm"),
366365
center=np.array([1.5, 1.5, 1.5]),
367366
periodic=True,
368367
pixelmeaning="pixelave",
369368
)
370369
out = frb.get_image(("gas", "density"))
371-
mass_in = 1.
370+
mass_in = 1.0
372371
mass_out = np.sum(out.v) * periods**2 / np.prod(outsize)
373-
masses_rel.append(mass_out / mass_in - 1.)
372+
masses_rel.append(mass_out / mass_in - 1.0)
374373
masses_rel = np.array(masses_rel)
375374
minrel = np.min(masses_rel)
376375
maxrel = np.max(masses_rel)
377-
print(f"Mass conservation test pixel/hsml set {i}:"
378-
" mass conservation deviations fractions"
379-
f" {minrel:.2e} -- {maxrel:.2e} ")
376+
print(
377+
f"Mass conservation test pixel/hsml set {i}:"
378+
" mass conservation deviations fractions"
379+
f" {minrel:.2e} -- {maxrel:.2e} "
380+
)
380381
assert np.all(np.abs(masses_rel) < 1e-4)
381382

383+
382384
@pytest.mark.parametrize("periodic", [True, False])
383385
@pytest.mark.parametrize("shiftcenter", [False, True])
384386
@pytest.mark.parametrize("zoff", [0.0, 0.1, 0.5, 1.0])

yt/testing.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,9 @@ def integrate_kernel(
158158

159159

160160
def resreduce_image(
161-
arr: NDArray[np.float32],
162-
outshape: tuple[int, int]
161+
arr: NDArray[np.float32], outshape: tuple[int, int]
163162
) -> NDArray[np.float32]:
164-
'''
163+
"""
165164
gets an image array of size outshape by averaging the pixel values
166165
in arr over contiguous blocks in each array dimension.
167166
@@ -181,19 +180,22 @@ def resreduce_image(
181180
values in `arr` are averaged over contiguous blocks of size
182181
(Nsx, Nsy), where Nsx = Nx // outshape[0],
183182
and Nsy = Ny // outshape[1].
184-
'''
183+
"""
185184
insize = arr.shape
186185
if insize[0] % outshape[0] != 0 or insize[1] % outshape[1] != 0:
187-
raise ValueError("desired output array shape must use integer"
188-
"divisors of the input array shape. "
189-
f"desired {outshape} doesn't divide "
190-
f"input array shape {arr.shape()}")
186+
raise ValueError(
187+
"desired output array shape must use integer"
188+
"divisors of the input array shape. "
189+
f"desired {outshape} doesn't divide "
190+
f"input array shape {arr.shape()}"
191+
)
191192
tempi0 = insize[0] // outshape[0]
192193
tempi1 = insize[1] // outshape[1]
193194
temp = arr.reshape(outshape[0], tempi0, outshape[1], tempi1)
194195
out = np.sum(temp, axis=(1, 3)) / (tempi0 * tempi1)
195196
return out
196197

198+
197199
_zeroperiods = np.array([0.0, 0.0, 0.0])
198200

199201

yt/visualization/tests/test_offaxisprojection_pytestonly.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import unyt
44
from numpy.testing import assert_allclose
55

6-
from yt.data_objects.selection_objects.region import YTRegion
76
from yt.testing import (
87
assert_rel_equal,
98
cubicspline_python,
@@ -174,12 +173,12 @@ def makemasses(i, j, k):
174173
assert_rel_equal(expected_out, img.v, 4)
175174

176175

177-
@pytest.mark.parametrize("axis", [(0., 1e-4, 1.), (0.2, 0., -0.8)])
176+
@pytest.mark.parametrize("axis", [(0.0, 1e-4, 1.0), (0.2, 0.0, -0.8)])
178177
@pytest.mark.parametrize("north_vector", [None, (1.0e-4, 1.0, 0.0)])
179178
@pytest.mark.parametrize("shiftcenter", [True, False])
180179
@pytest.mark.parametrize("periodic", [True, False])
181180
@pytest.mark.parametrize("depth", [None, (1.0, "cm"), (5.0, "cm")])
182-
@pytest.mark.parametrize("hsmlfac", [0.2, 0.5, 1.0])
181+
@pytest.mark.parametrize("hsmlfac", [0.2, 0.5, 1.0])
183182
def test_sph_proj_pixelave_offaxes(
184183
axis: tuple[float, float, float],
185184
north_vector: tuple[float, float, float] | None,
@@ -188,14 +187,13 @@ def test_sph_proj_pixelave_offaxes(
188187
depth: float,
189188
hsmlfac: float,
190189
) -> None:
191-
# weighted and unweighted tested in one round:
190+
# weighted and unweighted tested in one round:
192191
# need weight map to downsample the baseline weighted map
193192
if shiftcenter:
194193
center = unyt.unyt_array(np.array((0.6, 0.5, 0.4)), "cm")
195194
else:
196195
center = unyt.unyt_array(np.array((1.5, 1.5, 1.5)), "cm")
197-
bbox = unyt.unyt_array(np.array([[0.0, 3.0], [0.0, 3.0], [0.0, 3.0]]),
198-
"cm")
196+
bbox = unyt.unyt_array(np.array([[0.0, 3.0], [0.0, 3.0], [0.0, 3.0]]), "cm")
199197
hsml_factor = hsmlfac
200198
unitrho = 1.5
201199
buff_size = (4, 4)
@@ -283,9 +281,8 @@ def makemasses(i, j, k):
283281
baseimg = resreduce_image(_baseimg, buff_size)
284282
_baseimg_wtd = baseprj_wtd.frb.data[("gas", "density")]
285283
_divimg = np.copy(baseimg.v)
286-
_divimg[_divimg == 0.] = -1. # avoid div. by 0 test failures
287-
baseimg_wtd = (resreduce_image(_baseimg * _baseimg_wtd.v, buff_size)
288-
/ _divimg)
284+
_divimg[_divimg == 0.0] = -1.0 # avoid div. by 0 test failures
285+
baseimg_wtd = resreduce_image(_baseimg * _baseimg_wtd.v, buff_size) / _divimg
289286

290287
print(
291288
f"axis: {axis}, shiftcenter: {shiftcenter}, "
@@ -305,7 +302,7 @@ def makemasses(i, j, k):
305302
# subsampling with multiple particles is not quite at 4.
306303
# pixel values seem to converge more slowly though, so we test
307304
# mass conservation explicitly, and pixel agreement at low
308-
# precision
305+
# precision
309306
assert_rel_equal(baseimg.v, testimg.v, 1)
310307
assert_rel_equal(np.sum(baseimg.v), np.sum(testimg.v), 2)
311308
assert_rel_equal(baseimg_wtd.v, testimg_wtd.v, 1)

0 commit comments

Comments
 (0)