def put_in_child(node): # placing the overwrite logic within "propagate" makes this # decision point more explicit and avoids hidden behavior where # the decision to overwrite is imposed in the library. permission = (overwrite or not is_null(node.contents.get(child_key, None))) if permission: value = get_from_parent(node.parent) if value is not None: put(child_key)(node, value)
def __call__(self, node, arr): unique = tuple(set(Functor.make_hashable(arr))) unique = tuple(u for u in unique if u is not None) unique = { 0: lambda x: None, 1: lambda x: x[0] }.get(len(unique), lambda x: x)(unique) return put(self.key)(node, unique)
def __call__(self, node, arr): result = tuple(x for x in Functor.flatten(arr) if x is not None) # check if result is a single value if len(result) == 1: return None # check if result is the same as arr try: if np.all(np.asarray(arr) == np.asarray(result)): return None except: pass return put(self.key)(node, result)
def __call__(self, node, arr): return put(self.key)(node, Functor.std(arr))
def __call__(self, node, arr): return put(self.key)(node, Functor.minimum(arr))
def put_in_child(node): if overwrite or (child_key not in node.contents): value = get_from_parent(node.parent) if value is not None: put(child_key)(node, value)
return func reader = ExcelIO(build=build, mechanical=mechanical, porosity=porosity, default=generic) nodes = reader.load(os.path.join('..', 'tests', 'data', 'example.xlsx')) lineage = generate_tree(get_nodeid=get('name'), get_parent=get('parent name'), cmp=strcmp(str.lower, str.strip))(nodes) aggregator = aggregate(get("modulus (GPa)"), reduce=lambda node, arr: put("average modulus") (node, mean(arr))) propagator = propagate("spot size (um)") for root in lineage: aggregator(root) propagator(root) to_dataframe(nodes).to_excel('output.xlsx', index=False) ##### OO-based example ##### # class Example(object): # _uid_key: str # _parent_key: str # # def __init__(self, uid: str, parent: str): # """