def update(source, data): """ Update source with data This checks a few things first 1. If the data is the same, then don't update 2. If numpy is available and the data is numeric, then convert to numpy arrays 3. If profiling then perform the update in another callback """ if (not np or not any(isinstance(v, np.ndarray) for v in source.data.values())): if source.data == data: return if np and len(data[first(data)]) > 10: d = {} for k, v in data.items(): if type(v) is not np.ndarray and isinstance(v[0], Number): d[k] = np.array(v) else: d[k] = v else: d = data if PROFILING: curdoc().add_next_tick_callback(lambda: source.data.update(d)) else: source.data.update(d)
def find_unbalanced(self): node = self while True: grouped_children = node.grouped('children') unbalanced = cc.pipe(grouped_children, cc.valfilter(lambda x: len(x) == 1), lambda x: cc.first(x.values())[0]) if unbalanced.children_are_balanced: return unbalanced node = unbalanced
x[1], lambda x: x.split(','), cc.map(str.strip), list))), list) tree_val_dict = cc.pipe( data_input, cc.map(cc.first), cc.map(lambda x: [tree_val_re.match(x).group(y) for y in (1, 2)]), dict, cc.valmap(int)) tree_mapping_dict = cc.pipe( data_input, cc.map(lambda x: (tree_val_re.match(x[0]).group(1), x[1])), dict) root = cc.pipe( tree_mapping_dict.keys(), cc.filter(lambda x: x not in cc.concat(tree_mapping_dict.values())), cc.first) tree = Tree(root, tree_mapping_dict, tree_val_dict) unbalanced = tree.find_unbalanced() unbalanced_self_weight = unbalanced.weight - sum(x.weight for x in unbalanced.children) unbalanced_grouped_siblings = unbalanced.grouped('siblings') balanced_weight = cc.first( cc.valfilter(lambda x: len(x) > 1, unbalanced_grouped_siblings).keys()) unbalanced_weight = cc.first( cc.valfilter(lambda x: len(x) == 1, unbalanced_grouped_siblings).keys()) weight_offset = balanced_weight - unbalanced_weight pp(unbalanced_self_weight + weight_offset)