Skip to content

Commit f4864a1

Browse files
committed
STY: Reduce array().astype() and similar constructs
[git-blame-ignore-rev]
1 parent 8f77bf1 commit f4864a1

File tree

7 files changed

+12
-12
lines changed

7 files changed

+12
-12
lines changed

Diff for: nibabel/freesurfer/io.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ def _read_volume_info(fobj):
7070
if key in ('valid', 'filename'):
7171
volume_info[key] = pair[1].strip()
7272
elif key == 'volume':
73-
volume_info[key] = np.array(pair[1].split()).astype(int)
73+
volume_info[key] = np.array(pair[1].split(), int)
7474
else:
75-
volume_info[key] = np.array(pair[1].split()).astype(float)
75+
volume_info[key] = np.array(pair[1].split(), float)
7676
# Ignore the rest
7777
return volume_info
7878

@@ -521,7 +521,7 @@ def write_annot(filepath, labels, ctab, names, fill_ctab=True):
521521
vnum = len(labels)
522522

523523
def write(num, dtype=dt):
524-
np.array([num]).astype(dtype).tofile(fobj)
524+
np.array([num], dtype).tofile(fobj)
525525

526526
def write_string(s):
527527
s = (s if isinstance(s, bytes) else s.encode()) + b'\x00'

Diff for: nibabel/freesurfer/tests/test_mghformat.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def test_write_mgh():
123123
def test_write_noaffine_mgh():
124124
# now just save the image without the vox2ras transform
125125
# and see if it uses the default values to save
126-
v = np.ones((7, 13, 3, 22)).astype(np.uint8)
126+
v = np.ones((7, 13, 3, 22), np.uint8)
127127
# form a MGHImage object using data
128128
# and the default affine matrix (Note the "None")
129129
img = MGHImage(v, None)
@@ -175,7 +175,7 @@ def bad_dtype_mgh():
175175
"""
176176
# try to write an unsigned short and make sure it
177177
# raises MGHError
178-
v = np.ones((7, 13, 3, 22)).astype(np.uint16)
178+
v = np.ones((7, 13, 3, 22), np.uint16)
179179
# form a MGHImage object using data
180180
# and the default affine matrix (Note the "None")
181181
MGHImage(v, None)
@@ -189,7 +189,7 @@ def test_bad_dtype_mgh():
189189

190190
def test_filename_exts():
191191
# Test acceptable filename extensions
192-
v = np.ones((7, 13, 3, 22)).astype(np.uint8)
192+
v = np.ones((7, 13, 3, 22), np.uint8)
193193
# form a MGHImage object using data
194194
# and the default affine matrix (Note the "None")
195195
img = MGHImage(v, None)
@@ -251,7 +251,7 @@ def test_header_updating():
251251

252252
def test_cosine_order():
253253
# Test we are interpreting the cosine order right
254-
data = np.arange(60).reshape((3, 4, 5)).astype(np.int32)
254+
data = np.arange(60, dtype=np.int32).reshape((3, 4, 5))
255255
aff = np.diag([2.0, 3, 4, 1])
256256
aff[0] = [2, 1, 0, 10]
257257
img = MGHImage(data, aff)

Diff for: nibabel/gifti/tests/test_gifti.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ def test_darray_dtype_coercion_failures():
465465
encodings = ('ASCII', 'B64BIN', 'B64GZ')
466466
for data_dtype, darray_dtype, encoding in itertools.product(dtypes, dtypes, encodings):
467467
da = GiftiDataArray(
468-
np.arange(10).astype(data_dtype),
468+
np.arange(10, dtype=data_dtype),
469469
encoding=encoding,
470470
intent='NIFTI_INTENT_NODE_INDEX',
471471
datatype=darray_dtype,

Diff for: nibabel/tests/test_funcs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def test_concat():
127127

128128
def test_closest_canonical():
129129
# Use 32-bit data so that the AnalyzeImage class doesn't complain
130-
arr = np.arange(24).reshape((2, 3, 4, 1)).astype(np.int32)
130+
arr = np.arange(24, dtype=np.int32).reshape((2, 3, 4, 1))
131131

132132
# Test with an AnalyzeImage first
133133
img = AnalyzeImage(arr, np.eye(4))

Diff for: nibabel/tests/test_image_api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def validate_data_interface(self, imaker, params):
255255
with maybe_deprecated(meth_name), pytest.raises(ValueError):
256256
method(caching='something')
257257
# dataobj is read only
258-
fake_data = np.zeros(img.shape).astype(img.get_data_dtype())
258+
fake_data = np.zeros(img.shape, dtype=img.get_data_dtype())
259259
with pytest.raises(AttributeError):
260260
img.dataobj = fake_data
261261
# So is in_memory

Diff for: nibabel/tests/test_proxy_api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161

6262
def _some_slicers(shape):
6363
ndim = len(shape)
64-
slicers = np.eye(ndim).astype(int).astype(object)
64+
slicers = np.eye(ndim, dtype=int).astype(object)
6565
slicers[slicers == 0] = slice(None)
6666
for i in range(ndim):
6767
if i % 2:

Diff for: nibabel/tests/test_volumeutils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,7 @@ def assert_rt(
11411141
for in_cast, pre_clips, inter, slope, post_clips, nan_fill in itp(
11421142
(None, np.float32), (None, (-1, 25)), (0.0, 1.0), (1.0, 0.5), (None, (-2, 49)), (None, 1)
11431143
):
1144-
data = np.arange(24).astype(np.float32)
1144+
data = np.arange(24, dtype=np.float32)
11451145
assert_rt(
11461146
data,
11471147
shape,

0 commit comments

Comments
 (0)