@@ -2589,7 +2589,7 @@ def __iter__(self):
2589
2589
def __contains__ (self , key ):
2590
2590
return any (key in axis for axis in self .axes )
2591
2591
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' ):
2593
2593
"""
2594
2594
Generator. Returns next line of the table representing an array.
2595
2595
@@ -2602,6 +2602,13 @@ def as_table(self, maxlines=None, edgeitems=5, light=False):
2602
2602
only the first and last `edgeitems` lines are displayed.
2603
2603
Only active if `maxlines` is not None.
2604
2604
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'.
2605
2612
2606
2613
Returns
2607
2614
-------
@@ -2623,25 +2630,44 @@ def as_table(self, maxlines=None, edgeitems=5, light=False):
2623
2630
['', 'b1', 3, 4, 5],
2624
2631
['a1', 'b0', 6, 7, 8],
2625
2632
['', '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]]
2626
2647
"""
2627
2648
if not self .ndim :
2628
2649
return
2629
2650
2630
2651
# ert unit geo\time 2012 2011 2010
2631
2652
# NEER27 I05 AT 101.41 101.63 101.63
2632
2653
# 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 ))
2635
2660
data = np .asarray (self ).reshape (height , width )
2636
2661
2637
2662
# get list of names of axes
2638
2663
axes_names = self .axes .display_names [:]
2639
2664
# transforms ['a', 'b', 'c', 'd'] into ['a', 'b', 'c\\d']
2640
- if len (axes_names ) > 1 :
2665
+ if wide and len (axes_names ) > 1 :
2641
2666
axes_names [- 2 ] = '\\ ' .join (axes_names [- 2 :])
2642
2667
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 ]
2645
2671
# creates vertical lines (ticks is a list of list)
2646
2672
if self .ndim == 1 :
2647
2673
# 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):
2651
2677
ticks = light_product (* labels )
2652
2678
else :
2653
2679
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
2656
2683
# summary if needed
2657
2684
if maxlines is not None and height > maxlines :
2658
2685
# replace middle lines of the table by '...'.
@@ -2672,13 +2699,20 @@ def as_table(self, maxlines=None, edgeitems=5, light=False):
2672
2699
# returns next line (labels of N-1 first axes + data)
2673
2700
yield list (tick ) + dataline .tolist ()
2674
2701
2675
- def dump (self , header = True ):
2702
+ def dump (self , header = True , wide = True , value_name = 'value' ):
2676
2703
"""Dump array as a 2D nested list
2677
2704
2678
2705
Parameters
2679
2706
----------
2680
2707
header : bool
2681
2708
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'.
2682
2716
2683
2717
Returns
2684
2718
-------
@@ -2688,7 +2722,7 @@ def dump(self, header=True):
2688
2722
# flatten all dimensions except the last one
2689
2723
return self .data .reshape (- 1 , self .shape [- 1 ]).tolist ()
2690
2724
else :
2691
- return list (self .as_table ())
2725
+ return list (self .as_table (wide = wide , value_name = value_name ))
2692
2726
2693
2727
# XXX: should filter(geo=['W']) return a view by default? (collapse=True)
2694
2728
# I think it would be dangerous to make it the default
0 commit comments