Skip to content

Commit f1f1a92

Browse files
committed
BUG: avoid soon-to-be-deprecated direct mutations of ndarray.shape attributes
1 parent e962538 commit f1f1a92

File tree

14 files changed

+66
-63
lines changed

14 files changed

+66
-63
lines changed

CONTRIBUTING.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,7 @@ Source code style guide
720720
* Do not use nested classes unless you have a very good reason to, such as
721721
requiring a namespace or class-definition modification. Classes should live
722722
at the top level. ``__metaclass__`` is exempt from this.
723-
* Avoid copying memory when possible. For example, don't do
724-
``a = a.reshape(3, 4)`` when ``a.shape = (3, 4)`` will do, and ``a = a * 3``
725-
should be ``np.multiply(a, 3, a)``.
723+
* Avoid copying memory when possible.
726724
* In general, avoid all double-underscore method names: ``__something`` is
727725
usually unnecessary.
728726
* When writing a subclass, use the super built-in to access the super class,

yt/data_objects/construction_data_containers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2643,8 +2643,7 @@ def _export_ply(
26432643
f.write(b"end_header\n")
26442644
v.tofile(f)
26452645
arr["ni"][:] = 3
2646-
vi = np.arange(nv, dtype="<i")
2647-
vi.shape = (nv // 3, 3)
2646+
vi = np.arange(nv, dtype="<i").reshape(nv // 3, 3)
26482647
arr["v1"][:] = vi[:, 0]
26492648
arr["v2"][:] = vi[:, 1]
26502649
arr["v3"][:] = vi[:, 2]

yt/fields/field_detector.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -276,18 +276,19 @@ def fcoords(self):
276276
np.mgrid[0 : 1 : self.nd * 1j, 0 : 1 : self.nd * 1j, 0 : 1 : self.nd * 1j]
277277
)
278278
if self.flat:
279-
fc.shape = (self.nd * self.nd * self.nd, 3)
279+
fc = fc.reshape(self.nd * self.nd * self.nd, 3)
280280
else:
281281
fc = fc.transpose()
282282
return self.ds.arr(fc, units="code_length")
283283

284284
@property
285285
def fcoords_vertex(self):
286-
rng = np.random.default_rng()
287-
fc = rng.random((self.nd, self.nd, self.nd, 8, 3))
288286
if self.flat:
289-
fc.shape = (self.nd * self.nd * self.nd, 8, 3)
290-
return self.ds.arr(fc, units="code_length")
287+
shape = (self.nd * self.nd * self.nd, 8, 3)
288+
else:
289+
shape = (self.nd, self.nd, self.nd, 8, 3)
290+
rng = np.random.default_rng()
291+
return self.ds.arr(rng.random(shape), units="code_length")
291292

292293
@property
293294
def icoords(self):
@@ -297,21 +298,23 @@ def icoords(self):
297298
0 : self.nd - 1 : self.nd * 1j,
298299
]
299300
if self.flat:
300-
ic.shape = (self.nd * self.nd * self.nd, 3)
301+
return ic.reshape(self.nd * self.nd * self.nd, 3)
301302
else:
302-
ic = ic.transpose()
303-
return ic
303+
return ic.transpose()
304304

305305
@property
306306
def ires(self):
307-
ir = np.ones(self.nd**3, dtype="int64")
308-
if not self.flat:
309-
ir.shape = (self.nd, self.nd, self.nd)
310-
return ir
307+
if self.flat:
308+
shape = (self.nd**3,)
309+
else:
310+
shape = (self.nd, self.nd, self.nd)
311+
return np.ones(shape, dtype="int64")
311312

312313
@property
313314
def fwidth(self):
314-
fw = np.ones((self.nd**3, 3), dtype="float64") / self.nd
315-
if not self.flat:
316-
fw.shape = (self.nd, self.nd, self.nd, 3)
315+
if self.flat:
316+
shape = (self.nd**3, 3)
317+
else:
318+
shape = (self.nd, self.nd, self.nd, 3)
319+
fw = np.full(shape, 1 / self.nd, dtype="float64")
317320
return self.ds.arr(fw, units="code_length")

yt/fields/geometric_fields.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ def _morton_index(data):
134134
data["index", "z"].ravel(),
135135
LE,
136136
RE,
137-
)
138-
morton.shape = data["index", "x"].shape
137+
).reshape(data["index", "x"].shape)
139138
return morton.view("f8")
140139

141140
registry.add_field(

yt/frontends/amrvac/datfile_utils.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,10 @@ def get_single_block_data(istream, byte_offset, block_shape):
152152
def get_single_block_field_data(istream, byte_offset, block_shape, field_idx):
153153
"""retrieve a specific block (ONE field) from a datfile"""
154154
# compute byte size of a single field
155-
field_shape = block_shape[:-1]
156-
fmt = ALIGN + np.prod(field_shape) * "d"
155+
field_shape = block_shape[:-1][::-1]
156+
count = np.prod(field_shape)
157+
fmt = ALIGN + count * "d"
157158
byte_size_field = struct.calcsize(fmt)
158159

159160
istream.seek(byte_offset + byte_size_field * field_idx)
160-
data = np.fromfile(istream, "=f8", count=np.prod(field_shape))
161-
data.shape = field_shape[::-1]
162-
return data.T
161+
return np.fromfile(istream, dtype="=f8", count=count).reshape(field_shape).T

yt/frontends/amrvac/io.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,12 @@ def _read_data(self, fid, grid, field):
103103
offset = grid._index.block_offsets[ileaf]
104104
field_idx = self.ds.parameters["w_names"].index(field)
105105

106-
field_shape = self.block_shape[:-1]
106+
field_shape = self.block_shape[:-1][::-1]
107107
count = np.prod(field_shape)
108108
byte_size_field = count * 8 # size of a double
109109

110110
fid.seek(offset + byte_size_field * field_idx)
111-
data = np.fromfile(fid, "=f8", count=count)
112-
data.shape = field_shape[::-1]
113-
data = data.T
111+
data = np.fromfile(fid, dtype="=f8", count=count).reshape(field_shape).T
114112
# Always convert data to 3D, as grid.ActiveDimensions is always 3D
115113
while len(data.shape) < 3:
116114
data = data[..., np.newaxis]

yt/frontends/chimera/data_structures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def _initialize_mesh(self):
103103
mylog.warning(
104104
"Yin-Yang File Detected; This data is not currently supported."
105105
)
106-
coords.shape = (nxd * nyd * nzd, 3)
106+
coords = coords.reshape(nxd * nyd * nzd, 3)
107107
# Connectivity is an array of rows, each of which corresponds to a grid cell.
108108
# The 8 elements of each row are integers representing the cell vertices.
109109
# These integers reference the numerical index of the element of the

yt/frontends/exodus_ii/data_structures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def _read_connectivity(self):
361361
raise NotImplementedError("only equal-size polyhedra supported")
362362
q, r = np.divmod(len(conn), npe)
363363
assert r == 0
364-
conn.shape = (q, npe)
364+
conn = conn.reshape(q, npe)
365365
connectivity.append(conn)
366366
return connectivity
367367

yt/frontends/gadget/io.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,10 +462,11 @@ def _yield_coordinates(self, data_file, needed_ptype=None):
462462
if needed_ptype is not None and ptype != needed_ptype:
463463
continue
464464
# The first total_particles * 3 values are positions
465-
pp = np.fromfile(f, dtype=dt, count=count * 3).astype(
466-
dt_native, copy=False
465+
pp = (
466+
np.fromfile(f, dtype=dt, count=count * 3)
467+
.reshape(count, 3)
468+
.astype(dt_native, copy=False)
467469
)
468-
pp.shape = (count, 3)
469470
yield ptype, pp
470471

471472
def _get_smoothing_length(self, data_file, position_dtype, position_shape):

yt/frontends/http_stream/io.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def _read_particle_coords(self, chunks, ptf):
3737
for ptype in ptf:
3838
s = self._open_stream(data_file, (ptype, "Coordinates"))
3939
c = np.frombuffer(s, dtype="float64")
40-
c.shape = (c.shape[0] / 3.0, 3)
40+
c = c.reshape(c.size // 3, 3)
4141
yield ptype, (c[:, 0], c[:, 1], c[:, 2]), 0.0
4242

4343
def _read_particle_fields(self, chunks, ptf, selector):
@@ -46,7 +46,7 @@ def _read_particle_fields(self, chunks, ptf, selector):
4646
for ptype, field_list in sorted(ptf.items()):
4747
s = self._open_stream(data_file, (ptype, "Coordinates"))
4848
c = np.frombuffer(s, dtype="float64")
49-
c.shape = (c.shape[0] / 3.0, 3)
49+
c = c.reshape(c.size // 3, 3)
5050
mask = selector.select_points(c[:, 0], c[:, 1], c[:, 2], 0.0)
5151
del c
5252
if mask is None:
@@ -55,7 +55,7 @@ def _read_particle_fields(self, chunks, ptf, selector):
5555
s = self._open_stream(data_file, (ptype, field))
5656
c = np.frombuffer(s, dtype="float64")
5757
if field in self._vector_fields:
58-
c.shape = (c.shape[0] / 3.0, 3)
58+
c = c.reshape(c.size // 3, 3)
5959
data = c[mask, ...]
6060
yield (ptype, field), data
6161

0 commit comments

Comments
 (0)