def _compare(a, b): """Compare two python objects.""" global last_keys skip_types = ['whitener', 'proj', 'reginv', 'noisenorm', 'nchan', 'command_line', 'working_dir', 'mri_file', 'mri_id'] try: if isinstance(a, (dict, Info)): assert_true(isinstance(b, (dict, Info))) for k, v in six.iteritems(a): if k not in b and k not in skip_types: raise ValueError('First one had one second one didn\'t:\n' '%s not in %s' % (k, b.keys())) if k not in skip_types: last_keys.pop() last_keys = [k] + last_keys _compare(v, b[k]) for k, v in six.iteritems(b): if k not in a and k not in skip_types: raise ValueError('Second one had one first one didn\'t:\n' '%s not in %s' % (k, a.keys())) elif isinstance(a, list): assert_true(len(a) == len(b)) for i, j in zip(a, b): _compare(i, j) elif isinstance(a, sparse.csr.csr_matrix): assert_array_almost_equal(a.data, b.data) assert_equal(a.indices, b.indices) assert_equal(a.indptr, b.indptr) elif isinstance(a, np.ndarray): assert_array_almost_equal(a, b) else: assert_equal(a, b) except Exception: print(last_keys) raise
def _compare(a, b): """Compare two python objects.""" global last_keys skip_types = ['whitener', 'proj', 'reginv', 'noisenorm', 'nchan', 'command_line', 'working_dir', 'mri_file', 'mri_id'] try: if isinstance(a, (dict, Info)): assert (isinstance(b, (dict, Info))) for k, v in six.iteritems(a): if k not in b and k not in skip_types: raise ValueError('First one had one second one didn\'t:\n' '%s not in %s' % (k, b.keys())) if k not in skip_types: last_keys.pop() last_keys = [k] + last_keys _compare(v, b[k]) for k, v in six.iteritems(b): if k not in a and k not in skip_types: raise ValueError('Second one had one first one didn\'t:\n' '%s not in %s' % (k, a.keys())) elif isinstance(a, list): assert (len(a) == len(b)) for i, j in zip(a, b): _compare(i, j) elif isinstance(a, sparse.csr.csr_matrix): assert_array_almost_equal(a.data, b.data) assert_equal(a.indices, b.indices) assert_equal(a.indptr, b.indptr) elif isinstance(a, np.ndarray): assert_array_almost_equal(a, b) else: assert_equal(a, b) except Exception: print(last_keys) raise
def _pprint(params, offset=0, printer=repr): """Pretty print the dictionary 'params' (copied from sklearn) Parameters ---------- params : dict The dictionary to pretty print offset : int The offset in characters to add at the begin of each line. printer : callable The function to convert entries to strings, typically the builtin str or repr Returns ------- lines : str The pretty print of the dictionary as a string. """ # Do a multi-line justified repr: options = np.get_printoptions() np.set_printoptions(precision=5, threshold=64, edgeitems=2) params_list = list() this_line_length = offset line_sep = ',\n' + (1 + offset // 2) * ' ' for i, (k, v) in enumerate(sorted(six.iteritems(params))): if type(v) is float: # use str for representing floating point numbers # this way we get consistent representation across # architectures and versions. this_repr = '%s=%s' % (k, str(v)) else: # use repr of the rest this_repr = '%s=%s' % (k, printer(v)) if len(this_repr) > 500: this_repr = this_repr[:300] + '...' + this_repr[-100:] if i > 0: if (this_line_length + len(this_repr) >= 75 or '\n' in this_repr): params_list.append(line_sep) this_line_length = len(line_sep) else: params_list.append(', ') this_line_length += 2 params_list.append(this_repr) this_line_length += len(this_repr) np.set_printoptions(**options) lines = ''.join(params_list) # Strip trailing space to avoid nightmare in doctests lines = '\n'.join(l.rstrip(' ') for l in lines.split('\n')) return lines