diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2014-07-30 18:06:28 -0600 |
---|---|---|
committer | Julian Taylor <jtaylor.debian@googlemail.com> | 2014-07-31 21:21:17 +0200 |
commit | 01b0d7e82211b581aaff925e3ccc36cff9ac1895 (patch) | |
tree | 8ec68353d5f09b9f0411948f1345ec79f5443b4c /numpy/lib/recfunctions.py | |
parent | dec6658cdc10a23ad0e733fb52a814306033d88c (diff) | |
download | numpy-01b0d7e82211b581aaff925e3ccc36cff9ac1895.tar.gz |
STY: Make files in numpy/lib PEP8 compliant.
The rules enforced are the same as those used for scipy.
Diffstat (limited to 'numpy/lib/recfunctions.py')
-rw-r--r-- | numpy/lib/recfunctions.py | 116 |
1 files changed, 57 insertions, 59 deletions
diff --git a/numpy/lib/recfunctions.py b/numpy/lib/recfunctions.py index 6ae8b8445..a61b1749b 100644 --- a/numpy/lib/recfunctions.py +++ b/numpy/lib/recfunctions.py @@ -1,8 +1,8 @@ """ Collection of utilities to manipulate structured arrays. -Most of these functions were initially implemented by John Hunter for matplotlib. -They have been rewritten and extended for convenience. +Most of these functions were initially implemented by John Hunter for +matplotlib. They have been rewritten and extended for convenience. """ from __future__ import division, absolute_import, print_function @@ -70,7 +70,6 @@ def recursive_fill_fields(input, output): return output - def get_names(adtype): """ Returns the field names of the input datatype as a tuple. @@ -187,7 +186,7 @@ def zip_descr(seqarrays, flatten=False): def get_fieldstructure(adtype, lastname=None, parents=None,): """ - Returns a dictionary with fields as keys and a list of parent fields as values. + Returns a dictionary with fields indexing lists of their parent fields. This function is used to simplify access to fields nested in other fields. @@ -225,8 +224,7 @@ def get_fieldstructure(adtype, lastname=None, parents=None,): else: lastparent = [_ for _ in (parents.get(lastname, []) or [])] if lastparent: -# if (lastparent[-1] != lastname): - lastparent.append(lastname) + lastparent.append(lastname) elif lastname: lastparent = [lastname, ] parents[name] = lastparent or [] @@ -237,6 +235,7 @@ def _izip_fields_flat(iterable): """ Returns an iterator of concatenated fields from a sequence of arrays, collapsing any nested structure. + """ for element in iterable: if isinstance(element, np.void): @@ -249,9 +248,11 @@ def _izip_fields_flat(iterable): def _izip_fields(iterable): """ Returns an iterator of concatenated fields from a sequence of arrays. + """ for element in iterable: - if hasattr(element, '__iter__') and not isinstance(element, basestring): + if (hasattr(element, '__iter__') and + not isinstance(element, basestring)): for f in _izip_fields(element): yield f elif isinstance(element, np.void) and len(tuple(element)) == 1: @@ -325,9 +326,8 @@ def _fix_defaults(output, defaults=None): return output - -def merge_arrays(seqarrays, - fill_value= -1, flatten=False, usemask=False, asrecarray=False): +def merge_arrays(seqarrays, fill_value=-1, flatten=False, + usemask=False, asrecarray=False): """ Merge arrays field by field. @@ -463,7 +463,6 @@ def merge_arrays(seqarrays, return output - def drop_fields(base, drop_names, usemask=True, asrecarray=False): """ Return a new array with fields in `drop_names` dropped. @@ -475,13 +474,14 @@ def drop_fields(base, drop_names, usemask=True, asrecarray=False): base : array Input array drop_names : string or sequence - String or sequence of strings corresponding to the names of the fields - to drop. + String or sequence of strings corresponding to the names of the + fields to drop. usemask : {False, True}, optional Whether to return a masked array or not. - asrecarray : string or sequence + asrecarray : string or sequence, optional Whether to return a recarray or a mrecarray (`asrecarray=True`) or - a plain ndarray or masked array with flexible dtype (`asrecarray=False`) + a plain ndarray or masked array with flexible dtype. The default + is False. Examples -------- @@ -502,7 +502,7 @@ def drop_fields(base, drop_names, usemask=True, asrecarray=False): drop_names = [drop_names, ] else: drop_names = set(drop_names) - # + def _drop_descr(ndtype, drop_names): names = ndtype.names newdtype = [] @@ -517,11 +517,11 @@ def drop_fields(base, drop_names, usemask=True, asrecarray=False): else: newdtype.append((name, current)) return newdtype - # + newdtype = _drop_descr(base.dtype, drop_names) if not newdtype: return None - # + output = np.empty(base.shape, dtype=newdtype) output = recursive_fill_fields(base, output) return _fix_output(output, usemask=usemask, asrecarray=asrecarray) @@ -534,7 +534,6 @@ def rec_drop_fields(base, drop_names): return drop_fields(base, drop_names, usemask=False, asrecarray=True) - def rename_fields(base, namemapper): """ Rename the fields from a flexible-datatype ndarray or recarray. @@ -564,8 +563,9 @@ def rename_fields(base, namemapper): newname = namemapper.get(name, name) current = ndtype[name] if current.names: - newdtype.append((newname, - _recursive_rename_fields(current, namemapper))) + newdtype.append( + (newname, _recursive_rename_fields(current, namemapper)) + ) else: newdtype.append((newname, current)) return newdtype @@ -574,7 +574,7 @@ def rename_fields(base, namemapper): def append_fields(base, names, data, dtypes=None, - fill_value= -1, usemask=True, asrecarray=False): + fill_value=-1, usemask=True, asrecarray=False): """ Add new fields to an existing array. @@ -615,7 +615,7 @@ def append_fields(base, names, data, dtypes=None, if dtypes is None: data = [np.array(a, copy=False, subok=True) for a in data] data = [a.view([(name, a.dtype)]) for (name, a) in zip(names, data)] - else : + else: if not isinstance(dtypes, (tuple, list)): dtypes = [dtypes, ] if len(data) != len(dtypes): @@ -642,7 +642,6 @@ def append_fields(base, names, data, dtypes=None, return _fix_output(output, usemask=usemask, asrecarray=asrecarray) - def rec_append_fields(base, names, data, dtypes=None): """ Add new fields to an existing array. @@ -677,7 +676,6 @@ def rec_append_fields(base, names, data, dtypes=None): asrecarray=True, usemask=False) - def stack_arrays(arrays, defaults=None, usemask=True, asrecarray=False, autoconvert=False): """ @@ -690,11 +688,11 @@ def stack_arrays(arrays, defaults=None, usemask=True, asrecarray=False, defaults : dictionary, optional Dictionary mapping field names to the corresponding default values. usemask : {True, False}, optional - Whether to return a MaskedArray (or MaskedRecords is `asrecarray==True`) - or a ndarray. + Whether to return a MaskedArray (or MaskedRecords is + `asrecarray==True`) or a ndarray. asrecarray : {False, True}, optional - Whether to return a recarray (or MaskedRecords if `usemask==True`) or - just a flexible-type ndarray. + Whether to return a recarray (or MaskedRecords if `usemask==True`) + or just a flexible-type ndarray. autoconvert : {False, True}, optional Whether automatically cast the type of the field to the maximum. @@ -744,7 +742,7 @@ def stack_arrays(arrays, defaults=None, usemask=True, asrecarray=False, current_descr[-1] = descr[1] newdescr[nameidx] = tuple(current_descr) elif descr[1] != current_descr[-1]: - raise TypeError("Incompatible type '%s' <> '%s'" % \ + raise TypeError("Incompatible type '%s' <> '%s'" % (dict(newdescr)[name], descr[1])) # Only one field: use concatenate if len(newdescr) == 1: @@ -768,7 +766,6 @@ def stack_arrays(arrays, defaults=None, usemask=True, asrecarray=False, usemask=usemask, asrecarray=asrecarray) - def find_duplicates(a, key=None, ignoremask=True, return_index=False): """ Find the duplicates in a structured array along a given key @@ -823,19 +820,17 @@ def find_duplicates(a, key=None, ignoremask=True, return_index=False): return duplicates - def join_by(key, r1, r2, jointype='inner', r1postfix='1', r2postfix='2', defaults=None, usemask=True, asrecarray=False): """ Join arrays `r1` and `r2` on key `key`. The key should be either a string or a sequence of string corresponding - to the fields used to join the array. - An exception is raised if the `key` field cannot be found in the two input - arrays. - Neither `r1` nor `r2` should have any duplicates along `key`: the presence - of duplicates will make the output quite unreliable. Note that duplicates - are not looked for by the algorithm. + to the fields used to join the array. An exception is raised if the + `key` field cannot be found in the two input arrays. Neither `r1` nor + `r2` should have any duplicates along `key`: the presence of duplicates + will make the output quite unreliable. Note that duplicates are not + looked for by the algorithm. Parameters ---------- @@ -846,39 +841,41 @@ def join_by(key, r1, r2, jointype='inner', r1postfix='1', r2postfix='2', Structured arrays. jointype : {'inner', 'outer', 'leftouter'}, optional If 'inner', returns the elements common to both r1 and r2. - If 'outer', returns the common elements as well as the elements of r1 - not in r2 and the elements of not in r2. - If 'leftouter', returns the common elements and the elements of r1 not - in r2. + If 'outer', returns the common elements as well as the elements of + r1 not in r2 and the elements of not in r2. + If 'leftouter', returns the common elements and the elements of r1 + not in r2. r1postfix : string, optional - String appended to the names of the fields of r1 that are present in r2 - but absent of the key. + String appended to the names of the fields of r1 that are present + in r2 but absent of the key. r2postfix : string, optional - String appended to the names of the fields of r2 that are present in r1 - but absent of the key. + String appended to the names of the fields of r2 that are present + in r1 but absent of the key. defaults : {dictionary}, optional Dictionary mapping field names to the corresponding default values. usemask : {True, False}, optional - Whether to return a MaskedArray (or MaskedRecords is `asrecarray==True`) - or a ndarray. + Whether to return a MaskedArray (or MaskedRecords is + `asrecarray==True`) or a ndarray. asrecarray : {False, True}, optional - Whether to return a recarray (or MaskedRecords if `usemask==True`) or - just a flexible-type ndarray. + Whether to return a recarray (or MaskedRecords if `usemask==True`) + or just a flexible-type ndarray. Notes ----- * The output is sorted along the key. - * A temporary array is formed by dropping the fields not in the key for the - two arrays and concatenating the result. This array is then sorted, and - the common entries selected. The output is constructed by filling the fields - with the selected entries. Matching is not preserved if there are some - duplicates... + * A temporary array is formed by dropping the fields not in the key for + the two arrays and concatenating the result. This array is then + sorted, and the common entries selected. The output is constructed by + filling the fields with the selected entries. Matching is not + preserved if there are some duplicates... """ # Check jointype if jointype not in ('inner', 'outer', 'leftouter'): - raise ValueError("The 'jointype' argument should be in 'inner', "\ - "'outer' or 'leftouter' (got '%s' instead)" % jointype) + raise ValueError( + "The 'jointype' argument should be in 'inner', " + "'outer' or 'leftouter' (got '%s' instead)" % jointype + ) # If we have a single key, put it in a tuple if isinstance(key, basestring): key = (key,) @@ -963,14 +960,15 @@ def join_by(key, r1, r2, jointype='inner', r1postfix='1', r2postfix='2', ndtype.append(desc) # Revert the elements to tuples ndtype = [tuple(_) for _ in ndtype] - # Find the largest nb of common fields : r1cmn and r2cmn should be equal, but... + # Find the largest nb of common fields : + # r1cmn and r2cmn should be equal, but... cmn = max(r1cmn, r2cmn) # Construct an empty array output = ma.masked_all((cmn + r1spc + r2spc,), dtype=ndtype) names = output.dtype.names for f in r1names: selected = s1[f] - if f not in names or (f in r2names and not r2postfix and not f in key): + if f not in names or (f in r2names and not r2postfix and f not in key): f += r1postfix current = output[f] current[:r1cmn] = selected[:r1cmn] |