Skip to content

Commit c947aa8

Browse files
authored
fixed #653 : added argument wide and value_name to LArray.dump and LArray.as_table
1 parent e6976c5 commit c947aa8

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

doc/source/changes/version_0_29.rst.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ Miscellaneous improvements
184184
185185
* updated documentation of argument 'keepaxes' of aggregation functions (closes :issue:`607`).
186186
187+
* added missing arguments `wide` and `value_name` to methods `LArray.as_table` and `LArray.dump`
188+
(closes :issue:`653`)
189+
187190
188191
Fixes
189192
-----

larray/core/array.py

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2589,7 +2589,7 @@ def __iter__(self):
25892589
def __contains__(self, key):
25902590
return any(key in axis for axis in self.axes)
25912591

2592-
def as_table(self, maxlines=None, edgeitems=5, light=False):
2592+
def as_table(self, maxlines=None, edgeitems=5, light=False, wide=True, value_name='value'):
25932593
"""
25942594
Generator. Returns next line of the table representing an array.
25952595
@@ -2602,6 +2602,13 @@ def as_table(self, maxlines=None, edgeitems=5, light=False):
26022602
only the first and last `edgeitems` lines are displayed.
26032603
Only active if `maxlines` is not None.
26042604
Equals to 5 by default.
2605+
wide : boolean, optional
2606+
Whether or not to write arrays in "wide" format. If True, arrays are exported with the last axis
2607+
represented horizontally. If False, arrays are exported in "narrow" format: one column per axis plus one
2608+
value column. Defaults to True.
2609+
value_name : str, optional
2610+
Name of the column containing the values (last column) when `wide=False` (see above).
2611+
Defaults to 'value'.
26052612
26062613
Returns
26072614
-------
@@ -2623,25 +2630,44 @@ def as_table(self, maxlines=None, edgeitems=5, light=False):
26232630
['', 'b1', 3, 4, 5],
26242631
['a1', 'b0', 6, 7, 8],
26252632
['', 'b1', 9, 10, 11]]
2633+
>>> list(arr.as_table(wide=False, value_name='data')) # doctest: +NORMALIZE_WHITESPACE
2634+
[['a', 'b', 'c', 'data'],
2635+
['a0', 'b0', 'c0', 0],
2636+
['a0', 'b0', 'c1', 1],
2637+
['a0', 'b0', 'c2', 2],
2638+
['a0', 'b1', 'c0', 3],
2639+
['a0', 'b1', 'c1', 4],
2640+
['a0', 'b1', 'c2', 5],
2641+
['a1', 'b0', 'c0', 6],
2642+
['a1', 'b0', 'c1', 7],
2643+
['a1', 'b0', 'c2', 8],
2644+
['a1', 'b1', 'c0', 9],
2645+
['a1', 'b1', 'c1', 10],
2646+
['a1', 'b1', 'c2', 11]]
26262647
"""
26272648
if not self.ndim:
26282649
return
26292650

26302651
# ert unit geo\time 2012 2011 2010
26312652
# NEER27 I05 AT 101.41 101.63 101.63
26322653
# NEER27 I05 AU 134.86 125.29 117.08
2633-
width = self.shape[-1]
2634-
height = int(np.prod(self.shape[:-1]))
2654+
if wide:
2655+
width = self.shape[-1]
2656+
height = int(np.prod(self.shape[:-1]))
2657+
else:
2658+
width = 1
2659+
height = int(np.prod(self.shape))
26352660
data = np.asarray(self).reshape(height, width)
26362661

26372662
# get list of names of axes
26382663
axes_names = self.axes.display_names[:]
26392664
# transforms ['a', 'b', 'c', 'd'] into ['a', 'b', 'c\\d']
2640-
if len(axes_names) > 1:
2665+
if wide and len(axes_names) > 1:
26412666
axes_names[-2] = '\\'.join(axes_names[-2:])
26422667
axes_names.pop()
2643-
# get list of labels for each axis except the last one.
2644-
labels = [axis.labels.tolist() for axis in self.axes[:-1]]
2668+
axes = self.axes[:-1] if wide else self.axes
2669+
# get list of labels for each axis (except the last one if wide=True)
2670+
labels = [axis.labels.tolist() for axis in axes]
26452671
# creates vertical lines (ticks is a list of list)
26462672
if self.ndim == 1:
26472673
# There is no vertical axis, so the axis name should not have
@@ -2651,8 +2677,9 @@ def as_table(self, maxlines=None, edgeitems=5, light=False):
26512677
ticks = light_product(*labels)
26522678
else:
26532679
ticks = product(*labels)
2654-
# returns the first line (axes names + labels of last axis)
2655-
yield axes_names + self.axes[-1].labels.tolist()
2680+
# returns the first line
2681+
other_colnames = self.axes[-1].labels.tolist() if wide else [value_name]
2682+
yield axes_names + other_colnames
26562683
# summary if needed
26572684
if maxlines is not None and height > maxlines:
26582685
# replace middle lines of the table by '...'.
@@ -2672,13 +2699,20 @@ def as_table(self, maxlines=None, edgeitems=5, light=False):
26722699
# returns next line (labels of N-1 first axes + data)
26732700
yield list(tick) + dataline.tolist()
26742701

2675-
def dump(self, header=True):
2702+
def dump(self, header=True, wide=True, value_name='value'):
26762703
"""Dump array as a 2D nested list
26772704
26782705
Parameters
26792706
----------
26802707
header : bool
26812708
Whether or not to output axes names and labels.
2709+
wide : boolean, optional
2710+
Whether or not to write arrays in "wide" format. If True, arrays are exported with the last axis
2711+
represented horizontally. If False, arrays are exported in "narrow" format: one column per axis plus one
2712+
value column. Not used if header=False. Defaults to True.
2713+
value_name : str, optional
2714+
Name of the column containing the values (last column) when `wide=False` (see above).
2715+
Not used if header=False. Defaults to 'value'.
26822716
26832717
Returns
26842718
-------
@@ -2688,7 +2722,7 @@ def dump(self, header=True):
26882722
# flatten all dimensions except the last one
26892723
return self.data.reshape(-1, self.shape[-1]).tolist()
26902724
else:
2691-
return list(self.as_table())
2725+
return list(self.as_table(wide=wide, value_name=value_name))
26922726

26932727
# XXX: should filter(geo=['W']) return a view by default? (collapse=True)
26942728
# I think it would be dangerous to make it the default

0 commit comments

Comments
 (0)