Skip to content

Commit 2bd5086

Browse files
alixdammangdementen
authored andcommitted
fix #121 : Update docstring of set_labels.
fix #133 :Rename replace_axes as set_axes. Add inplace flag to set_axes and rename methods
1 parent 7855b79 commit 2bd5086

File tree

2 files changed

+64
-49
lines changed

2 files changed

+64
-49
lines changed

larray/core.py

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3905,7 +3905,7 @@ def __init__(self,
39053905
raise ValueError("length of axes %s does not match "
39063906
"data shape %s" % (axes.shape, data.shape))
39073907

3908-
# Because __getattr__ and __setattr__ have been rewritten
3908+
# Because __getattr__ and __setattr__ have been overridden
39093909
object.__setattr__(self, 'data', data)
39103910
object.__setattr__(self, 'axes', axes)
39113911
object.__setattr__(self, 'title', title)
@@ -3937,31 +3937,28 @@ def nonzero(self):
39373937
# can do a[a.nonzero()]
39383938
return self.data.nonzero()
39393939

3940-
def replace_axes(self, axes_to_replace=None, new_axis=None, **kwargs):
3940+
def set_axes(self, axes_to_replace=None, new_axis=None, inplace=False, **kwargs):
39413941
"""
3942-
Returns an array with one or several axes replaced.
3942+
Replace one, several or all axes of the array.
39433943
39443944
Parameters
39453945
----------
3946-
axes_to_replace : axis ref or dict {axis ref: axis} or
3947-
list of tuple (axis ref, axis) or list of Axis or
3948-
AxisCollection
3949-
Axes to replace. If a single axis reference is given,
3950-
the `new_axes` argument must be used. If a list of
3951-
Axis or an AxisCollection is given, all axes will be
3952-
replaced by the new ones. In that case, the number of
3953-
new axes must match the number of the old ones.
3946+
axes_to_replace : axis ref or dict {axis ref: axis} or list of tuple (axis ref, axis)
3947+
or list of Axis or AxisCollection
3948+
Axes to replace. If a single axis reference is given, the `new_axiss` argument must be provided.
3949+
If a list of Axis or an AxisCollection is given, all axes will be replaced by the new ones.
3950+
In that case, the number of new axes must match the number of the old ones.
39543951
new_axis : Axis
3955-
New axis if `axes_to_replace`
3956-
contains a single axis reference.
3952+
New axis if `axes_to_replace` contains a single axis reference.
3953+
inplace : bool
3954+
Whether or not to modify the original object or return a new array and leave the original intact.
39573955
**kwargs : Axis
3958-
New axis for each axis to replace given
3959-
as a keyword argument.
3956+
New axis for each axis to replace given as a keyword argument.
39603957
39613958
Returns
39623959
-------
39633960
LArray
3964-
Array with some axes replaced.
3961+
Array with axes replaced.
39653962
39663963
See Also
39673964
--------
@@ -3976,28 +3973,37 @@ def replace_axes(self, axes_to_replace=None, new_axis=None, **kwargs):
39763973
a1 | 3 | 4 | 5
39773974
>>> row = Axis('row', ['r0', 'r1'])
39783975
>>> column = Axis('column', ['c0', 'c1', 'c2'])
3979-
>>> arr.replace_axes(x.a, row)
3976+
3977+
Replace one axis (second argument `new_axis` must be provided)
3978+
3979+
>>> arr.set_axes(x.a, row)
39803980
row\\b | b0 | b1 | b2
39813981
r0 | 0 | 1 | 2
39823982
r1 | 3 | 4 | 5
3983-
>>> arr.replace_axes([row, column])
3983+
3984+
Replace several axes (keywords, list of tuple or dictionary)
3985+
3986+
>>> arr.set_axes(a=row, b=column)
39843987
row\\column | c0 | c1 | c2
39853988
r0 | 0 | 1 | 2
39863989
r1 | 3 | 4 | 5
3987-
>>> arr.replace_axes(a=row, b=column)
3990+
>>> arr.set_axes([(x.a, row), (x.b, column)])
39883991
row\\column | c0 | c1 | c2
39893992
r0 | 0 | 1 | 2
39903993
r1 | 3 | 4 | 5
3991-
>>> arr.replace_axes([(x.a, row), (x.b, column)])
3994+
>>> arr.set_axes({x.a: row, x.b: column})
39923995
row\\column | c0 | c1 | c2
39933996
r0 | 0 | 1 | 2
39943997
r1 | 3 | 4 | 5
3995-
>>> arr.replace_axes({x.a: row, x.b: column})
3998+
3999+
Replace all axes (list of axes or AxisCollection)
4000+
4001+
>>> arr.set_axes([row, column])
39964002
row\\column | c0 | c1 | c2
39974003
r0 | 0 | 1 | 2
39984004
r1 | 3 | 4 | 5
39994005
>>> arr2 = ndrange([row, column])
4000-
>>> arr.replace_axes(arr2.axes)
4006+
>>> arr.set_axes(arr2.axes)
40014007
row\\column | c0 | c1 | c2
40024008
r0 | 0 | 1 | 2
40034009
r1 | 3 | 4 | 5
@@ -4021,13 +4027,17 @@ def replace_axes(self, axes_to_replace=None, new_axis=None, **kwargs):
40214027
items += kwargs.items()
40224028
for old, new in items:
40234029
axes = axes.replace(old, new)
4024-
return LArray(self.data, axes, title=self.title)
4030+
if inplace:
4031+
object.__setattr__(self, 'axes', axes)
4032+
return self
4033+
else:
4034+
return LArray(self.data, axes, title=self.title)
40254035

40264036
def with_axes(self, axes):
40274037
warnings.warn("LArray.with_axes is deprecated, "
40284038
"use LArray.replace_axes instead",
40294039
DeprecationWarning)
4030-
return self.replace_axes(axes)
4040+
return self.set_axes(axes)
40314041

40324042
def __getattr__(self, key):
40334043
try:
@@ -4271,28 +4281,27 @@ def __bool__(self):
42714281
# Python 2
42724282
__nonzero__= __bool__
42734283

4274-
def rename(self, renames=None, to=None, **kwargs):
4275-
"""Renames some array axes.
4284+
def rename(self, renames=None, to=None, inplace=False, **kwargs):
4285+
"""Renames axes of the array.
42764286
42774287
Parameters
42784288
----------
42794289
renames : axis ref or dict {axis ref: str} or
42804290
list of tuple (axis ref, str)
4281-
Renames to apply. If a single axis reference
4282-
is given, the `to` argument must be used.
4283-
to : str or Axis
4284-
New name if `renames` contains a single axis reference
4285-
**kwargs : str
4291+
Renames to apply. If a single axis reference is given, the `to` argument must be used.
4292+
to : string or Axis
4293+
New name if `renames` contains a single axis reference.
4294+
**kwargs :
42864295
New name for each axis given as a keyword argument.
42874296
42884297
Returns
42894298
-------
42904299
LArray
4291-
Array with some axes renamed.
4300+
Array with axes renamed.
42924301
42934302
See Also
42944303
--------
4295-
replace_axes : replace one or several axes
4304+
set_axes : replace one or several axes
42964305
42974306
Examples
42984307
--------
@@ -4332,7 +4341,11 @@ def rename(self, renames=None, to=None, **kwargs):
43324341
renames = {self.axes[k]: v for k, v in items}
43334342
axes = [a.rename(renames[a]) if a in renames else a
43344343
for a in self.axes]
4335-
return LArray(self.data, axes)
4344+
if inplace:
4345+
object.__setattr__(self, 'axes', AxisCollection(axes))
4346+
return self
4347+
else:
4348+
return LArray(self.data, axes)
43364349

43374350
def sort_values(self, key):
43384351
"""Sorts values of the array.
@@ -8657,13 +8670,12 @@ def set_labels(self, axis, labels, inplace=False):
86578670
86588671
Parameters
86598672
----------
8660-
axis
8673+
axis : string or Axis
86618674
Axis for which we want to replace the labels.
8662-
labels : list of axis labels
8663-
New labels.
8675+
labels : int or iterable
8676+
Integer or list of values usable as the collection of labels for an Axis.
86648677
inplace : bool
8665-
Whether or not to modify the original object or return a new
8666-
array and leave the original intact.
8678+
Whether or not to modify the original object or return a new array and leave the original intact.
86678679
86688680
Returns
86698681
-------
@@ -8677,6 +8689,10 @@ def set_labels(self, axis, labels, inplace=False):
86778689
nat\\sex | M | F
86788690
BE | 0 | 1
86798691
FO | 2 | 3
8692+
>>> a.set_labels(x.sex, 'Men,Women')
8693+
nat\\sex | Men | Women
8694+
BE | 0 | 1
8695+
FO | 2 | 3
86808696
>>> a.set_labels(x.sex, ['Men', 'Women'])
86818697
nat\\sex | Men | Women
86828698
BE | 0 | 1
@@ -8687,8 +8703,7 @@ def set_labels(self, axis, labels, inplace=False):
86878703
axis.labels = labels
86888704
return self
86898705
else:
8690-
return LArray(self.data,
8691-
self.axes.replace(axis, Axis(axis.name, labels)))
8706+
return LArray(self.data, self.axes.replace(axis, Axis(axis.name, labels)))
86928707

86938708
def astype(self, dtype, order='K', casting='unsafe', subok=True, copy=True):
86948709
return LArray(self.data.astype(dtype, order, casting, subok, copy),

larray/tests/test_la.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2847,7 +2847,7 @@ def test_replace_axes(self):
28472847
la = LArray(self.small_data, axes=(self.sex, lipro2),
28482848
title=self.small_title)
28492849
# replace one axis
2850-
la2 = self.small.replace_axes(x.lipro, lipro2)
2850+
la2 = self.small.set_axes(x.lipro, lipro2)
28512851
assert_array_equal(la, la2)
28522852
self.assertEqual(la.title, la2.title, "title of array returned by "
28532853
"replace_axes should be the same as the original one. "
@@ -2856,16 +2856,16 @@ def test_replace_axes(self):
28562856
la = LArray(self.small_data, axes=(sex2, lipro2),
28572857
title=self.small_title)
28582858
# all at once
2859-
la2 = self.small.replace_axes([sex2, lipro2])
2859+
la2 = self.small.set_axes([sex2, lipro2])
28602860
assert_array_equal(la, la2)
28612861
# using keywrods args
2862-
la2 = self.small.replace_axes(sex=sex2, lipro=lipro2)
2862+
la2 = self.small.set_axes(sex=sex2, lipro=lipro2)
28632863
assert_array_equal(la, la2)
28642864
# using dict
2865-
la2 = self.small.replace_axes({x.sex: sex2, x.lipro: lipro2})
2865+
la2 = self.small.set_axes({x.sex: sex2, x.lipro: lipro2})
28662866
assert_array_equal(la, la2)
28672867
# using list of pairs (axis_to_replace, new_axis)
2868-
la2 = self.small.replace_axes([(x.sex, sex2), (x.lipro, lipro2)])
2868+
la2 = self.small.set_axes([(x.sex, sex2), (x.lipro, lipro2)])
28692869
assert_array_equal(la, la2)
28702870

28712871
def test_append(self):
@@ -3618,7 +3618,7 @@ def test_matmul(self):
36183618
f = Axis('f', 'f0,f1')
36193619

36203620
# 4D(a, b, c, d) @ 3D(e, d, f) -> 5D(a, b, e, c, f)
3621-
arr3d = arr3d.replace_axes([e, d, f])
3621+
arr3d = arr3d.set_axes([e, d, f])
36223622
res = from_lists([['a', 'b', 'e', 'c\\f', 'f0', 'f1'],
36233623
['a0', 'b0', 'e0', 'c0', 2, 3],
36243624
['a0', 'b0', 'e0', 'c1', 6, 11],
@@ -3659,7 +3659,7 @@ def test_matmul(self):
36593659
assert_array_equal(arr3d.__matmul__(arr4d), res)
36603660

36613661
# 4D(a, b, c, d) @ 3D(b, d, f) -> 4D(a, b, c, f)
3662-
arr3d = arr3d.replace_axes([b, d, f])
3662+
arr3d = arr3d.set_axes([b, d, f])
36633663
res = from_lists([['a', 'b', 'c\\f', 'f0', 'f1'],
36643664
['a0', 'b0', 'c0', 2, 3],
36653665
['a0', 'b0', 'c1', 6, 11],
@@ -3684,7 +3684,7 @@ def test_matmul(self):
36843684
assert_array_equal(arr3d.__matmul__(arr4d), res)
36853685

36863686
# 4D(a, b, c, d) @ 2D(d, f) -> 5D(a, b, c, f)
3687-
arr2d = arr2d.replace_axes([d, f])
3687+
arr2d = arr2d.set_axes([d, f])
36883688
res = from_lists([['a', 'b', 'c\\f', 'f0', 'f1'],
36893689
['a0', 'b0', 'c0', 2, 3],
36903690
['a0', 'b0', 'c1', 6, 11],

0 commit comments

Comments
 (0)