From 3d8c4fe24b2ca87642c65b50044978b76376374f Mon Sep 17 00:00:00 2001 From: Noel Dawe Date: Wed, 8 Apr 2015 12:56:24 +1000 Subject: [PATCH 1/2] stretch: make fields argument optional, and stretch all fields if fields=None --- root_numpy/_utils.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/root_numpy/_utils.py b/root_numpy/_utils.py index 0d92544..6e37b1b 100644 --- a/root_numpy/_utils.py +++ b/root_numpy/_utils.py @@ -72,7 +72,7 @@ def stack(recs, fields=None): return np.hstack([rec[fields] for rec in recs]) -def stretch(arr, fields): +def stretch(arr, fields=None): """Stretch an array. Stretch an array by ``hstack()``-ing multiple array fields while @@ -83,8 +83,8 @@ def stretch(arr, fields): ---------- arr : NumPy structured or record array The array to be stretched. - fields : list of strings - A list of column names to stretch. + fields : list of strings, optional (default=None) + A list of column names to stretch. If None, then stretch all fields. Returns ------- @@ -105,19 +105,21 @@ def stretch(arr, fields): """ dt = [] has_array_field = False - has_scalar_filed = False first_array = None + if fields is None: + fields = arr.dtype.names + # Construct dtype - for c in fields: - if _is_object_field(arr, c): - dt.append((c, arr[c][0].dtype)) + for field in fields: + if _is_object_field(arr, field): + dt.append((field, arr[field][0].dtype)) has_array_field = True - first_array = c if first_array is None else first_array + if first_array is None: + first_array = field else: # Assume scalar - dt.append((c, arr[c].dtype)) - has_scalar_filed = True + dt.append((field, arr[field].dtype)) if not has_array_field: raise RuntimeError("No array column specified") @@ -126,21 +128,21 @@ def stretch(arr, fields): numrec = np.sum(len_array) ret = np.empty(numrec, dtype=dt) - for c in fields: - if _is_object_field(arr, c): + for field in fields: + if _is_object_field(arr, field): # FIXME: this is rather inefficient since the stack # is copied over to the return value - stack = np.hstack(arr[c]) + stack = np.hstack(arr[field]) if len(stack) != numrec: raise ValueError( "Array lengths do not match: " "expected %d but found %d in %s" % - (numrec, len(stack), c)) - ret[c] = stack + (numrec, len(stack), field)) + ret[field] = stack else: # FIXME: this is rather inefficient since the repeat result # is copied over to the return value - ret[c] = np.repeat(arr[c], len_array) + ret[field] = np.repeat(arr[field], len_array) return ret From cc2f178d62aaaa79ff989ff260cd3db5f261e451 Mon Sep 17 00:00:00 2001 From: Noel Dawe Date: Sun, 24 May 2015 16:33:54 +1000 Subject: [PATCH 2/2] stretch: handle fixed-length array columns --- root_numpy/_utils.py | 67 +++++++++++++++++----------------- root_numpy/tests.py | 85 +++++++++++++++++++++----------------------- 2 files changed, 75 insertions(+), 77 deletions(-) diff --git a/root_numpy/_utils.py b/root_numpy/_utils.py index 6e37b1b..e18fcc5 100644 --- a/root_numpy/_utils.py +++ b/root_numpy/_utils.py @@ -17,10 +17,6 @@ VLEN = np.vectorize(len) -def _is_object_field(arr, col): - return arr.dtype[col] == 'O' - - def rec2array(rec, fields=None): """Convert a record array into a ndarray with a homogeneous data type. @@ -103,45 +99,50 @@ def stretch(arr, fields=None): dtype=[('scalar', '