Skip to content

Commit

Permalink
ENH: avoid unnecessary double copies in unyt_array conversion (arr.to…
Browse files Browse the repository at this point in the history
…(...).value -> arr.to_value(...))
  • Loading branch information
neutrinoceros committed Jun 11, 2023
1 parent 7cb2915 commit 1914908
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions yt/frontends/adaptahop/data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class AdaptaHOPDataset(Dataset):
_field_info_class = AdaptaHOPFieldInfo

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

Expand Down Expand Up @@ -165,7 +165,7 @@ def _parse_parameter_file(self):

self.domain_left_edge = np.array([0.0, 0.0, 0.0])
self.domain_right_edge = (
self.parent_ds.domain_right_edge.to("Mpc").value * self._code_length_to_Mpc
self.parent_ds.domain_right_edge.to_value("Mpc") * self._code_length_to_Mpc
)

self.parameters.update(params)
Expand Down Expand Up @@ -325,9 +325,9 @@ def _set_halo_member_data(self):

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

members = self.member_ids
ok = False
Expand Down
2 changes: 1 addition & 1 deletion yt/frontends/cf_radial/tests/test_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def test_grid_parameters():
expected_width.append(maxval - minval)
expected_width = np.array(expected_width)

actual_width = ds.domain_width.to("m").value
actual_width = ds.domain_width.to_value("m")
assert all(expected_width == actual_width)
assert all(ds.domain_dimensions == cfkwargs["grid_shape"])

Expand Down
4 changes: 2 additions & 2 deletions yt/utilities/lib/octree_raytracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def __init__(self, data_source):
self.octree = _OctreeRayTracing(LE, RE, depth)
ds = data_source.ds

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

ipos = np.floor(xyz * (1 << depth)).astype("int64")
mylog.debug("Adding cells to volume")
Expand Down
4 changes: 2 additions & 2 deletions yt/visualization/volume_rendering/render_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,8 @@ def render(self, camera, zbuffer=None):

data = self.data_source

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

Expand Down

0 comments on commit 1914908

Please sign in to comment.