Skip to content

Commit 54dde97

Browse files
Migrate Data.transpose method from LAMA to Dask
1 parent b137867 commit 54dde97

File tree

2 files changed

+21
-32
lines changed

2 files changed

+21
-32
lines changed

cf/data/data.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12079,6 +12079,7 @@ def tolist(self):
1207912079
"""
1208012080
return self.array.tolist()
1208112081

12082+
@daskified(1)
1208212083
@_deprecated_kwarg_check("i")
1208312084
@_inplace_enabled(default=False)
1208412085
def transpose(self, axes=None, inplace=False, i=False):
@@ -12119,9 +12120,8 @@ def transpose(self, axes=None, inplace=False, i=False):
1211912120
"""
1212012121
d = _inplace_enabled_define_and_cleanup(self)
1212112122

12122-
ndim = d._ndim
12123-
1212412123
# Parse the axes. By default, reverse the order of the axes.
12124+
ndim = d.ndim
1212512125
if axes is None:
1212612126
if ndim <= 1:
1212712127
return d
@@ -12142,21 +12142,14 @@ def transpose(self, axes=None, inplace=False, i=False):
1214212142
)
1214312143
# --- End: if
1214412144

12145-
# Permute the axes.
12146-
data_axes = d._axes
12147-
d._axes = [data_axes[i] for i in iaxes]
12148-
12149-
# Permute the shape
12150-
shape = d._shape
12151-
d._shape = tuple([shape[i] for i in iaxes])
12145+
# TODODASK Q) is d._axes still relevant? Or can the following go?
12146+
# Test passes with it commented out, but it could still be important?
12147+
# data_axes = d._axes
12148+
# d._axes = [data_axes[i] for i in iaxes]
1215212149

12153-
# Permute the locations map
12154-
for partition in d.partitions.matrix.flat:
12155-
location = partition.location
12156-
shape = partition.shape
12157-
12158-
partition.location = [location[i] for i in iaxes]
12159-
partition.shape = [shape[i] for i in iaxes]
12150+
dx = d._get_dask()
12151+
dx = da.transpose(dx, axes=axes)
12152+
d._set_dask(dx)
1216012153

1216112154
return d
1216212155

cf/test/test_Data.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,7 @@ def test_Data_squeeze_insert_dimension(self):
859859
def test_Data___getitem__(self):
860860
if self.test_only and inspect.stack()[0][3] not in self.test_only:
861861
return
862+
# TODODASK - why is there no testing here?
862863

863864
def test_Data___setitem__(self):
864865
if self.test_only and inspect.stack()[0][3] not in self.test_only:
@@ -1522,29 +1523,24 @@ def test_Data_swapaxes(self):
15221523
self.assertEqual(b.shape, e.shape, message)
15231524
self.assertTrue((b == e.array).all(), message)
15241525

1525-
@unittest.skipIf(TEST_DASKIFIED_ONLY, "no attribute 'chunk_sizes'")
15261526
def test_Data_transpose(self):
15271527
if self.test_only and inspect.stack()[0][3] not in self.test_only:
15281528
return
15291529

15301530
a = np.arange(10 * 15 * 19).reshape(10, 1, 15, 19)
15311531

1532-
for chunksize in self.chunk_sizes:
1533-
with cf.chunksize(chunksize):
1534-
d = cf.Data(a.copy())
1532+
d = cf.Data(a.copy())
15351533

1536-
for indices in (range(a.ndim), range(-a.ndim, 0)):
1537-
for axes in itertools.permutations(indices):
1538-
a = np.transpose(a, axes)
1539-
d.transpose(axes, inplace=True)
1540-
message = (
1541-
"cf.Data.transpose({}) failed: "
1542-
"d.shape={}, a.shape={}".format(
1543-
axes, d.shape, a.shape
1544-
)
1545-
)
1546-
self.assertEqual(d.shape, a.shape, message)
1547-
self.assertTrue((d.array == a).all(), message)
1534+
for indices in (range(a.ndim), range(-a.ndim, 0)):
1535+
for axes in itertools.permutations(indices):
1536+
a = np.transpose(a, axes)
1537+
d.transpose(axes, inplace=True)
1538+
message = (
1539+
"cf.Data.transpose({}) failed: "
1540+
"d.shape={}, a.shape={}".format(axes, d.shape, a.shape)
1541+
)
1542+
self.assertEqual(d.shape, a.shape, message)
1543+
self.assertTrue((d.array == a).all(), message)
15481544

15491545
@unittest.skipIf(TEST_DASKIFIED_ONLY, "no attribute 'chunk_sizes'")
15501546
def test_Data_unique(self):

0 commit comments

Comments
 (0)