Skip to content

Commit

Permalink
Merge pull request yt-project#4733 from neutrinoceros/ruff-npy
Browse files Browse the repository at this point in the history
RFC: refactor usage of numpy.random legacy API
  • Loading branch information
matthewturk authored Nov 10, 2023
2 parents a381a29 + 69ca0c7 commit 9983bf2
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 20 deletions.
17 changes: 9 additions & 8 deletions doc/source/examining/Loading_Generic_Particle_Data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
"source": [
"import numpy as np\n",
"\n",
"n_particles = 5000000\n",
"rng = np.random.default_rng()\n",
"n_particles = 5_000_000\n",
"\n",
"ppx, ppy, ppz = 1e6 * np.random.normal(size=[3, n_particles])\n",
"ppx, ppy, ppz = 1e6 * rng.normal(size=(3, n_particles))\n",
"\n",
"ppm = np.ones(n_particles)"
]
Expand Down Expand Up @@ -146,19 +147,19 @@
"metadata": {},
"outputs": [],
"source": [
"n_gas_particles = 1000000\n",
"n_star_particles = 1000000\n",
"n_dm_particles = 2000000\n",
"n_gas_particles = 1_000_000\n",
"n_star_particles = 1_000_000\n",
"n_dm_particles = 2_000_000\n",
"\n",
"ppxg, ppyg, ppzg = 1e6 * np.random.normal(size=[3, n_gas_particles])\n",
"ppxg, ppyg, ppzg = 1e6 * rng.normal(size=(3, n_gas_particles))\n",
"ppmg = np.ones(n_gas_particles)\n",
"hsml = 10000 * np.ones(n_gas_particles)\n",
"dens = 2.0e-4 * np.ones(n_gas_particles)\n",
"\n",
"ppxd, ppyd, ppzd = 1e6 * np.random.normal(size=[3, n_dm_particles])\n",
"ppxd, ppyd, ppzd = 1e6 * rng.normal(size=(3, n_dm_particles))\n",
"ppmd = np.ones(n_dm_particles)\n",
"\n",
"ppxs, ppys, ppzs = 5e5 * np.random.normal(size=[3, n_star_particles])\n",
"ppxs, ppys, ppzs = 5e5 * rng.normal(size=(3, n_star_particles))\n",
"ppms = 0.1 * np.ones(n_star_particles)\n",
"\n",
"bbox = 1.1 * np.array(\n",
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,16 @@ select = [
"YTT", # flake8-2020
"UP", # pyupgrade
"I", # isort
"NPY", # numpy specific rules
]
ignore = [
"E501", # line too long
"E741", # Do not use variables named 'I', 'O', or 'l'
"B018", # Found useless expression. # disabled because ds.index is idiomatic
]

[tool.ruff.per-file-ignores]
"test_*" = ["NPY002"]

[tool.ruff.isort]
combine-as-imports = true
Expand Down
3 changes: 2 additions & 1 deletion yt/fields/field_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ def deposit(self, *args, **kwargs):
def mesh_sampling_particle_field(self, *args, **kwargs):
pos = args[0]
npart = len(pos)
return np.random.rand(npart)
rng = np.random.default_rng()
return rng.random(npart)

def smooth(self, *args, **kwargs):
tr = np.random.random((self.nd, self.nd, self.nd))
Expand Down
6 changes: 4 additions & 2 deletions yt/frontends/enzo/data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,8 @@ def _detect_output_fields(self):

def _generate_random_grids(self):
if self.num_grids > 40:
starter = np.random.randint(0, 20)
rng = np.random.default_rng()
starter = rng.integers(0, 20)
random_sample = np.mgrid[starter : len(self.grids) - 1 : 20j].astype(
"int32"
)
Expand Down Expand Up @@ -608,7 +609,8 @@ def _generate_random_grids(self):
my_rank = self.comm.rank
my_grids = self.grids[self.grid_procs.ravel() == my_rank]
if len(my_grids) > 40:
starter = np.random.randint(0, 20)
rng = np.random.default_rng()
starter = rng.integers(0, 20)
random_sample = np.mgrid[starter : len(my_grids) - 1 : 20j].astype("int32")
mylog.debug("Checking grids: %s", random_sample.tolist())
else:
Expand Down
3 changes: 2 additions & 1 deletion yt/frontends/gadget/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ def fake_gadget_binary(
dtype = endian + dtype
# Generate and write field block
data = []
rng = np.random.default_rng()
for pt in ptype:
data += [np.random.rand(npart[pt], dim)]
data += [rng.random((npart[pt], dim))]
data = np.concatenate(data).astype(dtype)
if field in block_ids:
block_id = block_ids[field]
Expand Down
2 changes: 1 addition & 1 deletion yt/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ def small_fake_hexahedral_ds():
def fake_stretched_ds(N=16):
from yt.loaders import load_uniform_grid

np.random.seed(0x4D3D3D3)
np.random.RandomState().seed(0x4D3D3D3)
data = {"density": np.random.random((N, N, N))}

cell_widths = []
Expand Down
11 changes: 6 additions & 5 deletions yt/utilities/particle_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,11 @@ def __init__(

pbar = get_pbar("Generating Particles", num_particles)
tot_num_accepted = 0
rng = np.random.default_rng()

while num_particles_left > 0:
m = np.random.uniform(high=1.01 * max_mass, size=num_particles_left)
idxs = np.random.randint(low=0, high=num_cells, size=num_particles_left)
m = rng.uniform(high=1.01 * max_mass, size=num_particles_left)
idxs = rng.integers(low=0, high=num_cells, size=num_particles_left)
m_true = (
data_source[density_field] * data_source[("gas", "cell_volume")]
).flat[idxs]
Expand All @@ -418,17 +419,17 @@ def __init__(

xpos = (
data_source[("index", "x")].flat[accepted_idxs]
+ np.random.uniform(low=-0.5, high=0.5, size=num_accepted)
+ rng.uniform(low=-0.5, high=0.5, size=num_accepted)
* data_source[("index", "dx")].flat[accepted_idxs]
)
ypos = (
data_source[("index", "y")].flat[accepted_idxs]
+ np.random.uniform(low=-0.5, high=0.5, size=num_accepted)
+ rng.uniform(low=-0.5, high=0.5, size=num_accepted)
* data_source[("index", "dy")].flat[accepted_idxs]
)
zpos = (
data_source[("index", "z")].flat[accepted_idxs]
+ np.random.uniform(low=-0.5, high=0.5, size=num_accepted)
+ rng.uniform(low=-0.5, high=0.5, size=num_accepted)
* data_source[("index", "dz")].flat[accepted_idxs]
)

Expand Down
3 changes: 2 additions & 1 deletion yt/visualization/fixed_resolution_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,5 @@ def apply(self, buff):
else:
amp = self.bg_lvl
npm, nqm = np.shape(buff)
return buff + np.random.randn(npm, nqm) * amp
rng = np.random.default_rng()
return buff + rng.standard_normal((npm, nqm)) * amp
3 changes: 2 additions & 1 deletion yt/visualization/volume_rendering/camera_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ def setup_tsp(self, niter=50000, init_temp=10.0, alpha=0.999, fixed_start=False)
"""
# randomize tour
self.tour = list(range(self.nframes))
np.random.shuffle(self.tour)
rng = np.random.default_rng()
rng.shuffle(self.tour)
if fixed_start:
first = self.tour.index(0)
self.tour[0], self.tour[first] = self.tour[first], self.tour[0]
Expand Down

0 comments on commit 9983bf2

Please sign in to comment.