From e28cd9e215d3cd8976c37f97c12d2aea971adb27 Mon Sep 17 00:00:00 2001 From: Colin Snyder <8csnyder@gmail.com> Date: Wed, 10 Jul 2019 15:32:36 -0700 Subject: add new recfunctions to __all__ --- numpy/lib/recfunctions.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'numpy/lib/recfunctions.py') diff --git a/numpy/lib/recfunctions.py b/numpy/lib/recfunctions.py index fabb509ab..40c687937 100644 --- a/numpy/lib/recfunctions.py +++ b/numpy/lib/recfunctions.py @@ -26,10 +26,12 @@ _check_fill_value = np.ma.core._check_fill_value __all__ = [ - 'append_fields', 'drop_fields', 'find_duplicates', - 'get_fieldstructure', 'join_by', 'merge_arrays', - 'rec_append_fields', 'rec_drop_fields', 'rec_join', - 'recursive_fill_fields', 'rename_fields', 'stack_arrays', + 'append_fields', 'apply_along_fields', 'assign_fields_by_name', + 'drop_fields', 'find_duplicates', 'get_fieldstructure', + 'join_by', 'merge_arrays', 'rec_append_fields', + 'rec_drop_fields', 'rec_join', 'recursive_fill_fields', + 'rename_fields', 'require_fields', 'stack_arrays', + 'structured_to_unstructured', ] @@ -932,12 +934,13 @@ def structured_to_unstructured(arr, dtype=None, copy=False, casting='unsafe'): Examples -------- + >>> from numpy.lib import recfunctions as rfn >>> a = np.zeros(4, dtype=[('a', 'i4'), ('b', 'f4,u2'), ('c', 'f4', 2)]) >>> a array([(0, (0., 0), [0., 0.]), (0, (0., 0), [0., 0.]), (0, (0., 0), [0., 0.]), (0, (0., 0), [0., 0.])], dtype=[('a', '>> structured_to_unstructured(arr) + >>> rfn.structured_to_unstructured(a) array([[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], @@ -945,7 +948,7 @@ def structured_to_unstructured(arr, dtype=None, copy=False, casting='unsafe'): >>> b = np.array([(1, 2, 5), (4, 5, 7), (7, 8 ,11), (10, 11, 12)], ... dtype=[('x', 'i4'), ('y', 'f4'), ('z', 'f8')]) - >>> np.mean(structured_to_unstructured(b[['x', 'z']]), axis=-1) + >>> np.mean(rfn.structured_to_unstructured(b[['x', 'z']]), axis=-1) array([ 3. , 5.5, 9. , 11. ]) """ @@ -1111,11 +1114,12 @@ def apply_along_fields(func, arr): Examples -------- + >>> from numpy.lib import recfunctions as rfn >>> b = np.array([(1, 2, 5), (4, 5, 7), (7, 8 ,11), (10, 11, 12)], ... dtype=[('x', 'i4'), ('y', 'f4'), ('z', 'f8')]) - >>> apply_along_fields(np.mean, b) + >>> rfn.apply_along_fields(np.mean, b) array([ 2.66666667, 5.33333333, 8.66666667, 11. ]) - >>> apply_along_fields(np.mean, b[['x', 'z']]) + >>> rfn.apply_along_fields(np.mean, b[['x', 'z']]) array([ 3. , 5.5, 9. , 11. ]) """ @@ -1200,14 +1204,15 @@ def require_fields(array, required_dtype): Examples -------- + >>> from numpy.lib import recfunctions as rfn >>> a = np.ones(4, dtype=[('a', 'i4'), ('b', 'f8'), ('c', 'u1')]) - >>> require_fields(a, [('b', 'f4'), ('c', 'u1')]) + >>> rfn.require_fields(a, [('b', 'f4'), ('c', 'u1')]) array([(1., 1), (1., 1), (1., 1), (1., 1)], dtype=[('b', '>> require_fields(a, [('b', 'f4'), ('newf', 'u1')]) + >>> rfn.require_fields(a, [('b', 'f4'), ('newf', 'u1')]) array([(1., 0), (1., 0), (1., 0), (1., 0)], dtype=[('b', ' Date: Sun, 14 Jul 2019 22:43:13 -0700 Subject: exported correct functions and made private the rest --- numpy/lib/recfunctions.py | 52 ++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 25 deletions(-) (limited to 'numpy/lib/recfunctions.py') diff --git a/numpy/lib/recfunctions.py b/numpy/lib/recfunctions.py index 40c687937..a2ee19c6b 100644 --- a/numpy/lib/recfunctions.py +++ b/numpy/lib/recfunctions.py @@ -27,11 +27,12 @@ _check_fill_value = np.ma.core._check_fill_value __all__ = [ 'append_fields', 'apply_along_fields', 'assign_fields_by_name', - 'drop_fields', 'find_duplicates', 'get_fieldstructure', + 'drop_fields', 'find_duplicates', 'flatten_descr', + 'get_fieldstructure', 'get_names', 'get_names_flat', 'join_by', 'merge_arrays', 'rec_append_fields', 'rec_drop_fields', 'rec_join', 'recursive_fill_fields', - 'rename_fields', 'require_fields', 'stack_arrays', - 'structured_to_unstructured', + 'rename_fields', 'repack_fields', 'require_fields', + 'stack_arrays', 'structured_to_unstructured', ] @@ -78,7 +79,7 @@ def recursive_fill_fields(input, output): return output -def get_fieldspec(dtype): +def _get_fieldspec(dtype): """ Produce a list of name/dtype pairs corresponding to the dtype fields @@ -93,7 +94,7 @@ def get_fieldspec(dtype): >>> dt = np.dtype([(('a', 'A'), np.int64), ('b', np.double, 3)]) >>> dt.descr [(('a', 'A'), '>> get_fieldspec(dt) + >>> _get_fieldspec(dt) [(('a', 'A'), dtype('int64')), ('b', dtype(('>> from numpy.lib import recfunctions as rfn >>> def print_offsets(d): ... print("offsets:", [d.fields[name][1] for name in d.names]) ... print("itemsize:", d.itemsize) ... - >>> dt = np.dtype('u1,>> dt = np.dtype('u1, >> dt dtype({'names':['f0','f1','f2'], 'formats':['u1','>> print_offsets(dt) offsets: [0, 8, 16] itemsize: 24 - >>> packed_dt = repack_fields(dt) + >>> packed_dt = rfn.repack_fields(dt) >>> packed_dt dtype([('f0', 'u1'), ('f1', '>> print_offsets(packed_dt) @@ -990,7 +992,7 @@ def _unstructured_to_structured_dispatcher(arr, dtype=None, names=None, return (arr,) @array_function_dispatch(_unstructured_to_structured_dispatcher) -def unstructured_to_structured(arr, dtype=None, names=None, align=False, +def _unstructured_to_structured(arr, dtype=None, names=None, align=False, copy=False, casting='unsafe'): """ Converts and n-D unstructured array into an (n-1)-D structured array. @@ -1037,7 +1039,7 @@ def unstructured_to_structured(arr, dtype=None, names=None, align=False, [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]]) - >>> unstructured_to_structured(a, dt) + >>> _unstructured_to_structured(a, dt) array([( 0, ( 1., 2), [ 3., 4.]), ( 5, ( 6., 7), [ 8., 9.]), (10, (11., 12), [13., 14.]), (15, (16., 17), [18., 19.])], dtype=[('a', ' Date: Mon, 15 Jul 2019 19:36:05 -0700 Subject: Removed unnecessary decorators and dispatcher functions --- numpy/lib/recfunctions.py | 15 --------------- 1 file changed, 15 deletions(-) (limited to 'numpy/lib/recfunctions.py') diff --git a/numpy/lib/recfunctions.py b/numpy/lib/recfunctions.py index a2ee19c6b..2f82d01cc 100644 --- a/numpy/lib/recfunctions.py +++ b/numpy/lib/recfunctions.py @@ -207,11 +207,6 @@ def flatten_descr(ndtype): return tuple(descr) -def _zip_dtype_dispatcher(seqarrays, flatten=None): - return seqarrays - - -@array_function_dispatch(_zip_dtype_dispatcher) def _zip_dtype(seqarrays, flatten=False): newdtype = [] if flatten: @@ -228,7 +223,6 @@ def _zip_dtype(seqarrays, flatten=False): return np.dtype(newdtype) -@array_function_dispatch(_zip_dtype_dispatcher) def _zip_descr(seqarrays, flatten=False): """ Combine the dtype description of a series of arrays. @@ -321,11 +315,6 @@ def _izip_fields(iterable): yield element -def _izip_records_dispatcher(seqarrays, fill_value=None, flatten=None): - return seqarrays - - -@array_function_dispatch(_izip_records_dispatcher) def _izip_records(seqarrays, fill_value=None, flatten=True): """ Returns an iterator of concatenated items from a sequence of arrays. @@ -987,11 +976,7 @@ def structured_to_unstructured(arr, dtype=None, copy=False, casting='unsafe'): # finally is it safe to view the packed fields as the unstructured type return arr.view((out_dtype, (sum(counts),))) -def _unstructured_to_structured_dispatcher(arr, dtype=None, names=None, - align=None, copy=None, casting=None): - return (arr,) -@array_function_dispatch(_unstructured_to_structured_dispatcher) def _unstructured_to_structured(arr, dtype=None, names=None, align=False, copy=False, casting='unsafe'): """ -- cgit v1.2.1 From 82bc01143c74f997c0129075bb6f41490bf95bc0 Mon Sep 17 00:00:00 2001 From: Colin Snyder <8csnyder@gmail.com> Date: Sat, 20 Jul 2019 12:28:48 -0700 Subject: fixed unstructured_to_structured in recfunctions --- numpy/lib/recfunctions.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'numpy/lib/recfunctions.py') diff --git a/numpy/lib/recfunctions.py b/numpy/lib/recfunctions.py index 2f82d01cc..6e257bb3f 100644 --- a/numpy/lib/recfunctions.py +++ b/numpy/lib/recfunctions.py @@ -32,7 +32,7 @@ __all__ = [ 'join_by', 'merge_arrays', 'rec_append_fields', 'rec_drop_fields', 'rec_join', 'recursive_fill_fields', 'rename_fields', 'repack_fields', 'require_fields', - 'stack_arrays', 'structured_to_unstructured', + 'stack_arrays', 'structured_to_unstructured', 'unstructured_to_structured', ] @@ -977,7 +977,12 @@ def structured_to_unstructured(arr, dtype=None, copy=False, casting='unsafe'): return arr.view((out_dtype, (sum(counts),))) -def _unstructured_to_structured(arr, dtype=None, names=None, align=False, +def _unstructured_to_structured_dispatcher(arr, dtype=None, names=None, + align=None, copy=None, casting=None): + return (arr,) + +@array_function_dispatch(_unstructured_to_structured_dispatcher) +def unstructured_to_structured(arr, dtype=None, names=None, align=False, copy=False, casting='unsafe'): """ Converts and n-D unstructured array into an (n-1)-D structured array. @@ -1017,6 +1022,7 @@ def _unstructured_to_structured(arr, dtype=None, names=None, align=False, Examples -------- + >>> from numpy.lib import recfunctions as rfn >>> dt = np.dtype([('a', 'i4'), ('b', 'f4,u2'), ('c', 'f4', 2)]) >>> a = np.arange(20).reshape((4,5)) >>> a @@ -1024,7 +1030,7 @@ def _unstructured_to_structured(arr, dtype=None, names=None, align=False, [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]]) - >>> _unstructured_to_structured(a, dt) + >>> rfn.unstructured_to_structured(a, dt) array([( 0, ( 1., 2), [ 3., 4.]), ( 5, ( 6., 7), [ 8., 9.]), (10, (11., 12), [13., 14.]), (15, (16., 17), [18., 19.])], dtype=[('a', '