Skip to content

Commit 1914908

Browse files
committed
ENH: avoid unnecessary double copies in unyt_array conversion (arr.to(...).value -> arr.to_value(...))
1 parent 7cb2915 commit 1914908

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

yt/frontends/adaptahop/data_structures.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class AdaptaHOPDataset(Dataset):
5656
_field_info_class = AdaptaHOPFieldInfo
5757

5858
# AdaptaHOP internally assumes 1Mpc == 3.0824cm
59-
_code_length_to_Mpc = (1.0 * Mpc).to("cm").value / 3.08e24
59+
_code_length_to_Mpc = (1.0 * Mpc).to_value("cm") / 3.08e24
6060
_header_attributes: Optional[ATTR_T] = None
6161
_halo_attributes: Optional[ATTR_T] = None
6262

@@ -165,7 +165,7 @@ def _parse_parameter_file(self):
165165

166166
self.domain_left_edge = np.array([0.0, 0.0, 0.0])
167167
self.domain_right_edge = (
168-
self.parent_ds.domain_right_edge.to("Mpc").value * self._code_length_to_Mpc
168+
self.parent_ds.domain_right_edge.to_value("Mpc") * self._code_length_to_Mpc
169169
)
170170

171171
self.parameters.update(params)
@@ -325,9 +325,9 @@ def _set_halo_member_data(self):
325325

326326
# Note: convert to physical units to prevent errors when jumping
327327
# from halo_ds to parent_ds
328-
halo_pos = halo_ds.r["halos", "particle_position"][ihalo, :].to("Mpc").value
329-
halo_vel = halo_ds.r["halos", "particle_velocity"][ihalo, :].to("km/s").value
330-
halo_radius = halo_ds.r["halos", "r"][ihalo].to("Mpc").value
328+
halo_pos = halo_ds.r["halos", "particle_position"][ihalo, :].to_value("Mpc")
329+
halo_vel = halo_ds.r["halos", "particle_velocity"][ihalo, :].to_value("km/s")
330+
halo_radius = halo_ds.r["halos", "r"][ihalo].to_value("Mpc")
331331

332332
members = self.member_ids
333333
ok = False

yt/frontends/cf_radial/tests/test_outputs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def test_grid_parameters():
146146
expected_width.append(maxval - minval)
147147
expected_width = np.array(expected_width)
148148

149-
actual_width = ds.domain_width.to("m").value
149+
actual_width = ds.domain_width.to_value("m")
150150
assert all(expected_width == actual_width)
151151
assert all(ds.domain_dimensions == cfkwargs["grid_shape"])
152152

yt/utilities/lib/octree_raytracing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ def __init__(self, data_source):
2929
self.octree = _OctreeRayTracing(LE, RE, depth)
3030
ds = data_source.ds
3131

32-
xyz = np.stack([data_source[key].to("unitary").value for key in "xyz"], axis=-1)
33-
lvl = data_source["grid_level"].astype("int64").value + lvl_min
32+
xyz = np.stack([data_source[key].to_value("unitary") for key in "xyz"], axis=-1)
33+
lvl = data_source["grid_level"].value.astype("int64", copy=False) + lvl_min
3434

3535
ipos = np.floor(xyz * (1 << depth)).astype("int64")
3636
mylog.debug("Adding cells to volume")

yt/visualization/volume_rendering/render_source.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,8 @@ def render(self, camera, zbuffer=None):
662662

663663
data = self.data_source
664664

665-
dx = data["dx"].to("unitary").value[:, None]
666-
xyz = np.stack([data[_].to("unitary").value for _ in "x y z".split()], axis=-1)
665+
dx = data["dx"].to_value("unitary")[:, None]
666+
xyz = np.stack([data[_].to_value("unitary") for _ in "xyz"], axis=-1)
667667
LE = xyz - dx / 2
668668
RE = xyz + dx / 2
669669

0 commit comments

Comments
 (0)